Brian O'Connor
Committed by Gerrit Code Review

adding purge option to IntentRemoveCommand

Change-Id: Ic97acd9a8308355cd3135ea5df0a470b3579106e
......@@ -221,7 +221,7 @@ public class IntentPushTestCommand extends AbstractShellCommand
log.warn("install event latch is null");
}
} else if (IGNORE_EVENT.contains(event.type())) {
log.info("Unexpected intent event: {}", event);
log.debug("Unexpected intent event: {}", event);
}
}
}
......
......@@ -17,14 +17,24 @@ package org.onosproject.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentEvent;
import org.onosproject.net.intent.IntentListener;
import org.onosproject.net.intent.IntentService;
import org.onosproject.net.intent.IntentState;
import org.onosproject.net.intent.Key;
import java.math.BigInteger;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.onosproject.net.intent.IntentState.FAILED;
import static org.onosproject.net.intent.IntentState.WITHDRAWN;
/**
* Removes an intent.
......@@ -43,6 +53,16 @@ public class IntentRemoveCommand extends AbstractShellCommand {
required = true, multiValued = false)
String id = null;
@Option(name = "-p", aliases = "--purge",
description = "Purge the intent from the store after removal",
required = false, multiValued = false)
private boolean purgeAfterRemove = false;
@Option(name = "-s", aliases = "--sync",
description = "Waits for the removal before returning",
required = false, multiValued = false)
private boolean sync = false;
@Override
protected void execute() {
IntentService intentService = get(IntentService.class);
......@@ -63,8 +83,38 @@ public class IntentRemoveCommand extends AbstractShellCommand {
Key key = Key.of(new BigInteger(id, 16).longValue(), appId);
Intent intent = intentService.getIntent(key);
if (intent != null) {
// set up latch and listener to track uninstall progress
CountDownLatch latch = new CountDownLatch(1);
IntentListener listener = (IntentEvent event) -> {
if (Objects.equals(event.subject().key(), key) &&
(event.type() == IntentEvent.Type.WITHDRAWN
|| event.type() == IntentEvent.Type.WITHDRAWN)) {
latch.countDown();
}
};
intentService.addListener(listener);
// request the withdraw
intentService.withdraw(intent);
if (purgeAfterRemove || sync) {
try {
latch.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
print("Timed out waiting for intent {}", key);
}
// double check the state
IntentState state = intentService.getIntentState(key);
if (purgeAfterRemove && (state == WITHDRAWN || state == FAILED)) {
intentService.purge(key);
}
}
// clean up the listener
intentService.removeListener(listener);
}
}
}
......
......@@ -112,4 +112,11 @@ public interface IntentService {
* @param listener listener to be removed
*/
void removeListener(IntentListener listener);
/**
* Purges a specific intent from the system if it is FAILED or WITHDRAWN.
*
* @param key key of the intent to purge
*/
void purge(Key key);
}
......
......@@ -109,4 +109,10 @@ public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> {
* @return pending intents
*/
Iterable<Intent> getPending();
/** Purges a specific intent from the system if it is FAILED or WITHDRAWN.
*
* @param key key of the intent to purge
*/
void purge(Key key);
}
......
......@@ -230,6 +230,11 @@ public class FakeIntentManager implements TestableIntentService {
listeners.remove(listener);
}
@Override
public void purge(Key key) {
// FIXME implement this
}
private void dispatch(IntentEvent event) {
for (IntentListener listener : listeners) {
listener.event(event);
......
......@@ -76,4 +76,9 @@ public class IntentServiceAdapter implements IntentService {
public void removeListener(IntentListener listener) {
}
@Override
public void purge(Key key) {
}
}
......
......@@ -193,6 +193,11 @@ public class IntentManager
}
@Override
public void purge(Key key) {
store.purge(key);
}
@Override
public <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler) {
compilerRegistry.registerCompiler(cls, compiler);
}
......
......@@ -278,6 +278,14 @@ public class GossipIntentStore
.collect(Collectors.toList());
}
@Override
public void purge(Key key) {
IntentData data = currentMap.get(key);
if (data.state() == WITHDRAWN || data.state() == FAILED) {
currentMap.remove(key, data);
}
}
private void notifyDelegateIfNotNull(IntentEvent event) {
if (event != null) {
notifyDelegate(event);
......
......@@ -253,4 +253,12 @@ public class SimpleIntentStore
.map(IntentData::intent)
.collect(Collectors.toList());
}
@Override
public void purge(Key key) {
IntentData data = current.get(key);
if (data.state() == IntentState.WITHDRAWN || data.state() == IntentState.FAILED) {
current.remove(key, data);
}
}
}
......