ONOS-1058 - Add application Id to intent withdraw command

Change-Id: I6f660f2e115e4acc9d3c7d1ab2e2115caaf1822f
diff --git a/cli/pom.xml b/cli/pom.xml
index cbcdb47..c5c3cd9 100644
--- a/cli/pom.xml
+++ b/cli/pom.xml
@@ -59,6 +59,10 @@
             <groupId>org.apache.karaf.shell</groupId>
             <artifactId>org.apache.karaf.shell.console</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
@@ -67,6 +71,11 @@
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
             </plugin>
+
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
         </plugins>
     </build>
 
diff --git a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
index b404db6..6e6eb03 100644
--- a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
@@ -52,7 +52,8 @@
      * @return command-line application identifier
      */
     protected ApplicationId appId() {
-        return get(CoreService.class).registerApplication("org.onosproject.cli");
+        return get(CoreService.class)
+               .registerApplication("org.onosproject.cli");
     }
 
     /**
diff --git a/cli/src/main/java/org/onosproject/cli/CliComponent.java b/cli/src/main/java/org/onosproject/cli/CliComponent.java
new file mode 100644
index 0000000..6ae816b
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/CliComponent.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.cli;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.core.CoreService;
+
+/**
+ * OSGI Component for the ONOS CLI.
+ */
+
+@Component(immediate = true)
+public class CliComponent {
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Activate
+    public void activate() {
+        coreService
+            .registerApplication("org.onosproject.cli");
+    }
+}
diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationIdNameCompleter.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationIdNameCompleter.java
new file mode 100644
index 0000000..479b727
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationIdNameCompleter.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.cli.app;
+
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+
+/**
+ * Application name completer.
+ */
+public class ApplicationIdNameCompleter implements Completer {
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        // Delegate string completer
+        StringsCompleter delegate = new StringsCompleter();
+
+        // Fetch our service and feed it's offerings to the string completer
+        CoreService service = AbstractShellCommand.get(CoreService.class);
+        Set<ApplicationId> ids = service.getAppIds();
+        SortedSet<String> strings = delegate.getStrings();
+        for (ApplicationId id : ids) {
+            strings.add(id.name());
+        }
+
+        // Now let the completer do the work for figuring out what to offer.
+        return delegate.complete(buffer, cursor, candidates);
+    }
+
+}
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 3b05b7e..de6984f 100644
--- a/cli/src/main/java/org/onosproject/cli/net/IntentRemoveCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/IntentRemoveCommand.java
@@ -18,6 +18,8 @@
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 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.IntentService;
 import org.onosproject.net.intent.Key;
@@ -25,28 +27,44 @@
 import java.math.BigInteger;
 
 /**
- * Removes host-to-host connectivity intent.
+ * Removes an intent.
  */
 @Command(scope = "onos", name = "remove-intent",
          description = "Removes the specified intent")
 public class IntentRemoveCommand extends AbstractShellCommand {
 
-    @Argument(index = 0, name = "id", description = "Intent ID",
+    @Argument(index = 0, name = "app",
+              description = "Application ID",
+              required = true, multiValued = false)
+    String applicationIdString = null;
+
+    @Argument(index = 1, name = "id",
+              description = "Intent ID",
               required = true, multiValued = false)
     String id = null;
 
     @Override
     protected void execute() {
-        IntentService service = get(IntentService.class);
+        IntentService intentService = get(IntentService.class);
+        CoreService coreService = get(CoreService.class);
+
+        ApplicationId appId = appId();
+        if (applicationIdString != null) {
+            appId = coreService.getAppId(applicationIdString);
+            if (appId == null) {
+                print("Cannot find application Id %s", applicationIdString);
+                return;
+            }
+        }
 
         if (id.startsWith("0x")) {
             id = id.replaceFirst("0x", "");
         }
 
-        Key key = Key.of(new BigInteger(id, 16).longValue(), appId());
-        Intent intent = service.getIntent(key);
+        Key key = Key.of(new BigInteger(id, 16).longValue(), appId);
+        Intent intent = intentService.getIntent(key);
         if (intent != null) {
-            service.withdraw(intent);
+            intentService.withdraw(intent);
         }
     }
 }
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 8206d5a..fdfa54c 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -130,6 +130,7 @@
         <command>
             <action class="org.onosproject.cli.net.IntentRemoveCommand"/>
             <completers>
+                <ref component-id="appIdNameCompleter" />
                 <ref component-id="intentIdCompleter"/>
                 <null/>
             </completers>
@@ -279,6 +280,7 @@
     </command-bundle>
 
     <bean id="appNameCompleter" class="org.onosproject.cli.app.ApplicationNameCompleter"/>
+    <bean id="appIdNameCompleter" class="org.onosproject.cli.app.ApplicationIdNameCompleter"/>
     <bean id="nodeIdCompleter" class="org.onosproject.cli.NodeIdCompleter"/>
     <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
     <bean id="clusterIdCompleter" class="org.onosproject.cli.net.ClusterIdCompleter"/>