Add hostname auto-completion feature for openstack-node-check cmd

Change-Id: Id6e9776a846bb32acbda71ec5b88099e7381b9e4
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/cli/OpenstackHostnameCompleter.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/cli/OpenstackHostnameCompleter.java
new file mode 100644
index 0000000..1d08c0a
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/cli/OpenstackHostnameCompleter.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2018-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.openstacknode.cli;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.openstacknode.api.OpenstackNode;
+import org.onosproject.openstacknode.api.OpenstackNodeService;
+
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.stream.Collectors;
+
+/**
+ * Openstack host completer.
+ */
+public class OpenstackHostnameCompleter implements Completer {
+
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        StringsCompleter delegate = new StringsCompleter();
+        OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
+
+        Set<String> hostnames = osNodeService.nodes().stream()
+                                    .map(OpenstackNode::hostname)
+                                    .collect(Collectors.toSet());
+        SortedSet<String> strings = delegate.getStrings();
+
+        strings.addAll(hostnames);
+
+        return delegate.complete(buffer, cursor, candidates);
+    }
+}
diff --git a/apps/openstacknode/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/openstacknode/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 64e2812..4629980 100644
--- a/apps/openstacknode/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/openstacknode/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -21,9 +21,15 @@
         </command>
         <command>
             <action class="org.onosproject.openstacknode.cli.OpenstackNodeCheckCommand"/>
+            <completers>
+                <ref component-id="hostnameCompleter"/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.openstacknode.cli.OpenstackNodeInitCommand"/>
         </command>
     </command-bundle>
+
+    <bean id="hostnameCompleter" class="org.onosproject.openstacknode.cli.OpenstackHostnameCompleter"/>
+
 </blueprint>