ONOS-785 Adding distributed store for apps & app admin CLIs

Change-Id: Ia7639f3258fca2a18ba513f0c95de0ab8ea7ceee
diff --git a/cli/src/main/java/org/onosproject/cli/ApplicationInstallCommand.java b/cli/src/main/java/org/onosproject/cli/ApplicationInstallCommand.java
deleted file mode 100644
index f520804..0000000
--- a/cli/src/main/java/org/onosproject/cli/ApplicationInstallCommand.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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.karaf.shell.commands.Command;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-
-/**
- * Lists application ID information.
- */
-@Command(scope = "onos", name = "app-install",
-        description = "Lists application ID information")
-public class ApplicationInstallCommand extends AbstractShellCommand {
-
-    @Override
-    protected void execute() {
-        // FIXME: merely an experiment for now
-        try (InputStreamReader isr = new InputStreamReader(System.in);
-             BufferedReader br = new BufferedReader(isr)) {
-            String line;
-            while ((line = br.readLine()) != null) {
-                print("%s", line.toUpperCase());
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-    }
-
-}
diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationActivateCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationActivateCommand.java
new file mode 100644
index 0000000..672b6fc
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationActivateCommand.java
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.app.ApplicationAdminService;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+
+/**
+ * Activates an installed application.
+ */
+@Command(scope = "onos", name = "app-activate",
+        description = "Activates an installed application")
+public class ApplicationActivateCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "name", description = "Application name",
+            required = true, multiValued = false)
+    String name = null;
+
+    @Override
+    protected void execute() {
+        ApplicationAdminService service = get(ApplicationAdminService.class);
+        ApplicationId appId = service.getId(name);
+        if (appId != null) {
+            service.activate(appId);
+        } else {
+            print("No such application: %s", name);
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationDeactivateCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationDeactivateCommand.java
new file mode 100644
index 0000000..317e93f
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationDeactivateCommand.java
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.app.ApplicationAdminService;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+
+/**
+ * Deactivates an installed application.
+ */
+@Command(scope = "onos", name = "app-deactivate",
+        description = "Deactivates an installed application")
+public class ApplicationDeactivateCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "name", description = "Application name",
+            required = true, multiValued = false)
+    String name = null;
+
+    @Override
+    protected void execute() {
+        ApplicationAdminService service = get(ApplicationAdminService.class);
+        ApplicationId appId = service.getId(name);
+        if (appId != null) {
+            service.deactivate(appId);
+        } else {
+            print("No such application: %s", name);
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/ApplicationIdListCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationIdListCommand.java
similarity index 92%
rename from cli/src/main/java/org/onosproject/cli/ApplicationIdListCommand.java
rename to cli/src/main/java/org/onosproject/cli/app/ApplicationIdListCommand.java
index 400ada4..3ea8090 100644
--- a/cli/src/main/java/org/onosproject/cli/ApplicationIdListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationIdListCommand.java
@@ -13,12 +13,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.onosproject.cli;
+package org.onosproject.cli.app;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cli.Comparators;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 
@@ -30,7 +32,7 @@
 /**
  * Lists application ID information.
  */
-@Command(scope = "onos", name = "apps",
+@Command(scope = "onos", name = "app-ids",
          description = "Lists application ID information")
 public class ApplicationIdListCommand extends AbstractShellCommand {
 
diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationNameCompleter.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationNameCompleter.java
new file mode 100644
index 0000000..daabdb0
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationNameCompleter.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 org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onosproject.app.ApplicationService;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.Application;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.SortedSet;
+
+/**
+ * Application name completer.
+ */
+public class ApplicationNameCompleter 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
+        ApplicationService service = AbstractShellCommand.get(ApplicationService.class);
+        Iterator<Application> it = service.getApplications().iterator();
+        SortedSet<String> strings = delegate.getStrings();
+        while (it.hasNext()) {
+            strings.add(it.next().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/app/ApplicationUninstallCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationUninstallCommand.java
new file mode 100644
index 0000000..fd86c74
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationUninstallCommand.java
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.app.ApplicationAdminService;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+
+/**
+ * Uninstalls an application.
+ */
+@Command(scope = "onos", name = "app-uninstall",
+        description = "Uninstalls an application")
+public class ApplicationUninstallCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "name", description = "Application name",
+            required = true, multiValued = false)
+    String name = null;
+
+    @Override
+    protected void execute() {
+        ApplicationAdminService service = get(ApplicationAdminService.class);
+        ApplicationId appId = service.getId(name);
+        if (appId != null) {
+            service.uninstall(appId);
+        } else {
+            print("No such application: %s", name);
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java
new file mode 100644
index 0000000..2b90f66
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.karaf.shell.commands.Command;
+import org.onosproject.app.ApplicationService;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.Application;
+
+import static org.onosproject.app.ApplicationState.ACTIVE;
+
+/**
+ * Lists application information.
+ */
+@Command(scope = "onos", name = "apps",
+        description = "Lists application information")
+public class ApplicationsListCommand extends AbstractShellCommand {
+
+    private static final String FMT =
+            "%s id=%d, name=%s, version=%s, origin=%s, description=%s, " +
+                    "features=%s, featuresRepo=%s, permissions=%s";
+
+    @Override
+    protected void execute() {
+        ApplicationService service = get(ApplicationService.class);
+        for (Application app : service.getApplications()) {
+            print(FMT, service.getState(app.id()) == ACTIVE ? "*" : " ",
+                  app.id().id(), app.id().name(), app.version(), app.origin(),
+                  app.description(), app.features(), app.featuresRepo(), app.permissions());
+        }
+    }
+
+}
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 5e4e98c..478eaa9 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -21,7 +21,31 @@
         </command>
 
         <command>
-            <action class="org.onosproject.cli.ApplicationInstallCommand"/>
+            <action class="org.onosproject.cli.app.ApplicationsListCommand"/>
+        </command>
+
+        <command>
+            <action class="org.onosproject.cli.app.ApplicationActivateCommand"/>
+            <completers>
+                <ref component-id="appNameCompleter"/>
+                <null/>
+            </completers>
+        </command>
+
+        <command>
+            <action class="org.onosproject.cli.app.ApplicationDeactivateCommand"/>
+            <completers>
+                <ref component-id="appNameCompleter"/>
+                <null/>
+            </completers>
+        </command>
+
+        <command>
+            <action class="org.onosproject.cli.app.ApplicationUninstallCommand"/>
+            <completers>
+                <ref component-id="appNameCompleter"/>
+                <null/>
+            </completers>
         </command>
 
         <command>
@@ -77,7 +101,7 @@
             <action class="org.onosproject.cli.BalanceMastersCommand"/>
         </command>
         <command>
-            <action class="org.onosproject.cli.ApplicationIdListCommand"/>
+            <action class="org.onosproject.cli.app.ApplicationIdListCommand"/>
         </command>
 
         <command>
@@ -275,6 +299,7 @@
         </command>
     </command-bundle>
 
+    <bean id="appNameCompleter" class="org.onosproject.cli.app.ApplicationNameCompleter"/>
     <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"/>