Adding STC scenario for testing host intent-based connectivity.

Change-Id: I7375a9fdb121a6a288df5d54a23cfbd9d54258f3
diff --git a/cli/src/main/java/org/onosproject/cli/net/IntentRemoveCommand.java b/cli/src/main/java/org/onosproject/cli/net/IntentRemoveCommand.java
index dceed7b..4181dd4 100644
--- a/cli/src/main/java/org/onosproject/cli/net/IntentRemoveCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/IntentRemoveCommand.java
@@ -33,6 +33,7 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
+import static com.google.common.base.Strings.isNullOrEmpty;
 import static org.onosproject.net.intent.IntentState.FAILED;
 import static org.onosproject.net.intent.IntentState.WITHDRAWN;
 
@@ -40,17 +41,17 @@
  * Removes an intent.
  */
 @Command(scope = "onos", name = "remove-intent",
-         description = "Removes the specified intent")
+        description = "Removes the specified intent")
 public class IntentRemoveCommand extends AbstractShellCommand {
 
     @Argument(index = 0, name = "app",
-              description = "Application ID",
-              required = true, multiValued = false)
+            description = "Application ID",
+            required = false, multiValued = false)
     String applicationIdString = null;
 
     @Argument(index = 1, name = "key",
-              description = "Intent Key",
-              required = true, multiValued = false)
+            description = "Intent Key",
+            required = false, multiValued = false)
     String keyString = null;
 
     @Option(name = "-p", aliases = "--purge",
@@ -69,7 +70,7 @@
         CoreService coreService = get(CoreService.class);
 
         ApplicationId appId = appId();
-        if (applicationIdString != null) {
+        if (!isNullOrEmpty(applicationIdString)) {
             appId = coreService.getAppId(applicationIdString);
             if (appId == null) {
                 print("Cannot find application Id %s", applicationIdString);
@@ -77,73 +78,87 @@
             }
         }
 
-        final Key key;
-        if (keyString.startsWith("0x")) {
-            // The intent uses a LongKey
-            keyString = keyString.replaceFirst("0x", "");
-            key = Key.of(new BigInteger(keyString, 16).longValue(), appId);
-        } else {
-            // The intent uses a StringKey
-            key = Key.of(keyString, appId);
-        }
-
-        Intent intent = intentService.getIntent(key);
-        if (intent != null) {
-            IntentListener listener = null;
-            final CountDownLatch withdrawLatch, purgeLatch;
-            if (purgeAfterRemove || sync) {
-                // set up latch and listener to track uninstall progress
-                withdrawLatch = new CountDownLatch(1);
-                purgeLatch = purgeAfterRemove ? new CountDownLatch(1) : null;
-                listener = (IntentEvent event) -> {
-                    if (Objects.equals(event.subject().key(), key)) {
-                        if (event.type() == IntentEvent.Type.WITHDRAWN ||
-                                event.type() == IntentEvent.Type.FAILED) {
-                            withdrawLatch.countDown();
-                        } else if (purgeAfterRemove &&
-                                event.type() == IntentEvent.Type.PURGED) {
-                            purgeLatch.countDown();
-                        }
-                    }
-                };
-                intentService.addListener(listener);
-            } else {
-                purgeLatch = null;
-                withdrawLatch = null;
+        if (isNullOrEmpty(keyString)) {
+            for (Intent intent : intentService.getIntents()) {
+                if (intent.appId().equals(appId)) {
+                    removeIntent(intentService, intent);
+                }
             }
 
-            // request the withdraw
-            intentService.withdraw(intent);
+        } else {
+            final Key key;
+            if (keyString.startsWith("0x")) {
+                // The intent uses a LongKey
+                keyString = keyString.replaceFirst("0x", "");
+                key = Key.of(new BigInteger(keyString, 16).longValue(), appId);
+            } else {
+                // The intent uses a StringKey
+                key = Key.of(keyString, appId);
+            }
 
-            if (purgeAfterRemove || sync) {
-                try { // wait for withdraw event
-                    withdrawLatch.await(5, TimeUnit.SECONDS);
-                } catch (InterruptedException e) {
-                    print("Timed out waiting for intent {} withdraw", key);
+            Intent intent = intentService.getIntent(key);
+            if (intent != null) {
+                removeIntent(intentService, intent);
+            }
+        }
+    }
+
+    private void removeIntent(IntentService intentService, Intent intent) {
+        IntentListener listener = null;
+        Key key = intent.key();
+        final CountDownLatch withdrawLatch, purgeLatch;
+        if (purgeAfterRemove || sync) {
+            // set up latch and listener to track uninstall progress
+            withdrawLatch = new CountDownLatch(1);
+            purgeLatch = purgeAfterRemove ? new CountDownLatch(1) : null;
+            listener = (IntentEvent event) -> {
+                if (Objects.equals(event.subject().key(), key)) {
+                    if (event.type() == IntentEvent.Type.WITHDRAWN ||
+                            event.type() == IntentEvent.Type.FAILED) {
+                        withdrawLatch.countDown();
+                    } else if (purgeAfterRemove &&
+                            event.type() == IntentEvent.Type.PURGED) {
+                        purgeLatch.countDown();
+                    }
                 }
-                // double check the state
-                IntentState state = intentService.getIntentState(key);
-                if (purgeAfterRemove && (state == WITHDRAWN || state == FAILED)) {
-                    intentService.purge(intent);
-                }
-                if (sync) { // wait for purge event
+            };
+            intentService.addListener(listener);
+        } else {
+            purgeLatch = null;
+            withdrawLatch = null;
+        }
+
+        // request the withdraw
+        intentService.withdraw(intent);
+
+        if (purgeAfterRemove || sync) {
+            try { // wait for withdraw event
+                withdrawLatch.await(5, TimeUnit.SECONDS);
+            } catch (InterruptedException e) {
+                print("Timed out waiting for intent {} withdraw", key);
+            }
+            // double check the state
+            IntentState state = intentService.getIntentState(key);
+            if (purgeAfterRemove && (state == WITHDRAWN || state == FAILED)) {
+                intentService.purge(intent);
+            }
+            if (sync) { // wait for purge event
                     /* TODO
                        Technically, the event comes before map.remove() is called.
                        If we depend on sync and purge working together, we will
                        need to address this.
                     */
-                    try {
-                        purgeLatch.await(5, TimeUnit.SECONDS);
-                    } catch (InterruptedException e) {
-                        print("Timed out waiting for intent {} purge", key);
-                    }
+                try {
+                    purgeLatch.await(5, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    print("Timed out waiting for intent {} purge", key);
                 }
             }
+        }
 
-            if (listener != null) {
-                // clean up the listener
-                intentService.removeListener(listener);
-            }
+        if (listener != null) {
+            // clean up the listener
+            intentService.removeListener(listener);
         }
     }
 }