[ONOS-7732] Automating switch workflow: workplace cli command completeter

Change-Id: Ibcabd8e861b2506d21929ab0c0625eab132dc3d1
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceNameCompleter.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceNameCompleter.java
new file mode 100644
index 0000000..22e4c5c
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceNameCompleter.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * 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.workflow.cli;
+
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.workflow.api.WorkplaceStore;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.onlab.osgi.DefaultServiceDirectory.getService;
+
+/**
+ * Workplace name completer.
+ */
+@Service
+public class WorkplaceNameCompleter extends AbstractChoicesCompleter {
+    @Override
+    protected List<String> choices() {
+        WorkplaceStore workplaceStore = getService(WorkplaceStore.class);
+        return workplaceStore.getWorkplaces().stream()
+                .map(workplace -> workplace.name())
+                .collect(Collectors.toList());
+    }
+}
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCommand.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCommand.java
index 1b4ba8a..3c2e8be 100644
--- a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCommand.java
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCommand.java
@@ -17,6 +17,7 @@
 
 import org.apache.karaf.shell.api.action.Argument;
 import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
 import org.apache.karaf.shell.api.action.Option;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.onosproject.cli.AbstractShellCommand;
@@ -35,10 +36,21 @@
         description = "workplace cli")
 public class WorkplaceStoreCommand extends AbstractShellCommand {
 
-    @Argument(index = 0, name = "cmd", description = "command(add/rm/clear/print)", required = false)
+    static final String ADD   = "add";
+    static final String RM    = "rm";
+    static final String CLEAR = "clear";
+    static final String PRINT = "print";
+
+    @Argument(index = 0, name = "cmd",
+            description = "command(" + ADD + "/" + RM + "/" + CLEAR + "/" + PRINT + ")",
+            required = false)
+    @Completion(WorkplaceStoreCompleter.class)
     private String cmd = null;
 
-    @Argument(index = 1, name = "name", description = "workspace name", required = false)
+    @Argument(index = 1, name = "name",
+            description = "workplace name",
+            required = false)
+    @Completion(WorkplaceNameCompleter.class)
     private String name = null;
 
     @Option(name = "-f", aliases = "--filter", description = "including filter",
@@ -58,7 +70,7 @@
         }
 
         switch (cmd) {
-            case "add":
+            case ADD:
                 if (Objects.isNull(name)) {
                     error("invalid name");
                     return;
@@ -66,7 +78,7 @@
                 addEmptyWorkplace(name);
                 return;
 
-            case "rm":
+            case RM:
                 if (Objects.isNull(name)) {
                     print("invalid name");
                     return;
@@ -74,11 +86,11 @@
                 rmWorkplace(name);
                 break;
 
-            case "clear":
+            case CLEAR:
                 clearWorkplace();
                 break;
 
-            case "print":
+            case PRINT:
                 if (Objects.isNull(name)) {
                     print("invalid name");
                     return;
@@ -142,6 +154,10 @@
     private void printWorkplace(String name) {
         WorkplaceStore workplaceStore = get(WorkplaceStore.class);
         Workplace workplace = workplaceStore.getWorkplace(name);
+        if (Objects.isNull(workplace)) {
+            print("Not existing workplace " + name);
+            return;
+        }
         print(getWorkplaceString(workplace));
     }
 
diff --git a/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCompleter.java b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCompleter.java
new file mode 100644
index 0000000..e0c8404
--- /dev/null
+++ b/apps/workflow/app/src/main/java/org/onosproject/workflow/cli/WorkplaceStoreCompleter.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * 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.workflow.cli;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractChoicesCompleter;
+
+import java.util.List;
+
+import static org.onosproject.workflow.cli.WorkplaceStoreCommand.ADD;
+import static org.onosproject.workflow.cli.WorkplaceStoreCommand.CLEAR;
+import static org.onosproject.workflow.cli.WorkplaceStoreCommand.PRINT;
+import static org.onosproject.workflow.cli.WorkplaceStoreCommand.RM;
+
+/**
+ * Workplace store command completer.
+ */
+@Service
+public class WorkplaceStoreCompleter extends AbstractChoicesCompleter {
+    @Override
+    protected List<String> choices() {
+        return ImmutableList.of(ADD, RM, CLEAR, PRINT);
+    }
+}