Merge various resource type completers as one completer for CPMan

Change-Id: If4a9f6fa6705f49847139cd95b28ec0482d47415
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlMessageDeviceIdCompleter.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlMessageDeviceIdCompleter.java
deleted file mode 100644
index ab063a0..0000000
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlMessageDeviceIdCompleter.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright 2016 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.cpman.cli;
-
-import org.apache.karaf.shell.console.completer.ArgumentCompleter;
-import org.apache.karaf.shell.console.completer.StringsCompleter;
-import org.onosproject.cli.AbstractCompleter;
-import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.cpman.ControlPlaneMonitorService;
-
-import java.util.List;
-import java.util.Set;
-import java.util.SortedSet;
-
-import static org.onosproject.cpman.ControlResource.Type;
-
-/**
- * Device identification completer for control plane manager.
- */
-public class ControlMessageDeviceIdCompleter extends AbstractCompleter {
-    @Override
-    public int complete(String buffer, int cursor, List<String> candidates) {
-        // delegate string completer
-        StringsCompleter delegate = new StringsCompleter();
-
-        // Resource type is the second argument.
-        ArgumentCompleter.ArgumentList list = getArgumentList();
-        String type = list.getArguments()[1];
-
-        if (Type.CONTROL_MESSAGE.toString().toLowerCase().equals(type)) {
-            ControlPlaneMonitorService monitorService =
-                    AbstractShellCommand.get(ControlPlaneMonitorService.class);
-
-            Set<String> set = monitorService.availableResources(Type.CONTROL_MESSAGE);
-
-            SortedSet<String> strings = delegate.getStrings();
-            if (set != null) {
-                set.forEach(s -> strings.add(s));
-            }
-        }
-        return delegate.complete(buffer, cursor, candidates);
-    }
-}
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/DiskResourceNameCompleter.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/DiskResourceNameCompleter.java
deleted file mode 100644
index f67d629..0000000
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/DiskResourceNameCompleter.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright 2016 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.cpman.cli;
-
-import org.apache.karaf.shell.console.completer.ArgumentCompleter;
-import org.apache.karaf.shell.console.completer.StringsCompleter;
-import org.onosproject.cli.AbstractCompleter;
-import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.cpman.ControlPlaneMonitorService;
-
-import java.util.List;
-import java.util.Set;
-import java.util.SortedSet;
-
-import static org.onosproject.cpman.ControlResource.Type;
-/**
- * Disk resource name completer.
- */
-public class DiskResourceNameCompleter extends AbstractCompleter {
-    @Override
-    public int complete(String buffer, int cursor, List<String> candidates) {
-        // delegate string completer
-        StringsCompleter delegate = new StringsCompleter();
-
-        // Resource type is the second argument.
-        ArgumentCompleter.ArgumentList list = getArgumentList();
-        String type = list.getArguments()[1];
-
-        if (Type.DISK.toString().toLowerCase().equals(type)) {
-            ControlPlaneMonitorService monitorService =
-                    AbstractShellCommand.get(ControlPlaneMonitorService.class);
-
-            Set<String> set = monitorService.availableResources(Type.DISK);
-            SortedSet<String> strings = delegate.getStrings();
-
-            if (set != null) {
-                set.forEach(s -> strings.add(s));
-            }
-        }
-        return delegate.complete(buffer, cursor, candidates);
-    }
-}
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/NetworkResourceNameCompleter.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/NetworkResourceNameCompleter.java
deleted file mode 100644
index 576d8ee..0000000
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/NetworkResourceNameCompleter.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright 2016 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.cpman.cli;
-
-import org.apache.karaf.shell.console.completer.ArgumentCompleter;
-import org.apache.karaf.shell.console.completer.StringsCompleter;
-import org.onosproject.cli.AbstractCompleter;
-import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.cpman.ControlPlaneMonitorService;
-
-import java.util.List;
-import java.util.Set;
-import java.util.SortedSet;
-
-import static org.onosproject.cpman.ControlResource.Type;
-
-/**
- * Network resource name completer.
- */
-public class NetworkResourceNameCompleter extends AbstractCompleter {
-    @Override
-    public int complete(String buffer, int cursor, List<String> candidates) {
-        // delegate string completer
-        StringsCompleter delegate = new StringsCompleter();
-
-        // Resource type is the second argument.
-        ArgumentCompleter.ArgumentList list = getArgumentList();
-        String type = list.getArguments()[1];
-
-        if (Type.NETWORK.toString().toLowerCase().equals(type)) {
-            ControlPlaneMonitorService monitorService =
-                    AbstractShellCommand.get(ControlPlaneMonitorService.class);
-
-            Set<String> set = monitorService.availableResources(Type.NETWORK);
-            SortedSet<String> strings = delegate.getStrings();
-
-            if (set != null) {
-                set.forEach(s -> strings.add(s));
-            }
-        }
-
-        return delegate.complete(buffer, cursor, candidates);
-    }
-}
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ResourceNameCompleter.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ResourceNameCompleter.java
new file mode 100644
index 0000000..08edcf0
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ResourceNameCompleter.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2016 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.cpman.cli;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+import org.apache.karaf.shell.console.completer.ArgumentCompleter;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onosproject.cli.AbstractCompleter;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cpman.ControlPlaneMonitorService;
+import org.onosproject.cpman.ControlResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+
+/**
+ * Resource name completer.
+ */
+public class ResourceNameCompleter extends AbstractCompleter {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private static final String NETWORK = "network";
+    private static final String DISK = "disk";
+    private static final String CONTROL_MESSAGE = "control_message";
+    Set<String> resourceTypes = ImmutableSet.of(NETWORK, DISK, CONTROL_MESSAGE);
+    private static final String INVALID_MSG = "Invalid type name";
+
+
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        // delegate string completer
+        StringsCompleter delegate = new StringsCompleter();
+
+        // Resource type is the second argument.
+        ArgumentCompleter.ArgumentList list = getArgumentList();
+        String type = list.getArguments()[1];
+
+        if (resourceTypes.contains(type)) {
+            ControlPlaneMonitorService monitorService =
+                    AbstractShellCommand.get(ControlPlaneMonitorService.class);
+
+            Set<String> set = Sets.newHashSet();
+            switch (type) {
+                case NETWORK:
+                    set = monitorService.availableResources(ControlResource.Type.NETWORK);
+                    break;
+                case DISK:
+                    set = monitorService.availableResources(ControlResource.Type.DISK);
+                    break;
+                case CONTROL_MESSAGE:
+                    set = monitorService.availableResources(ControlResource.Type.CONTROL_MESSAGE);
+                    break;
+                default:
+                    log.warn(INVALID_MSG);
+                    break;
+            }
+
+            SortedSet<String> strings = delegate.getStrings();
+
+            if (set.size() != 0) {
+                set.forEach(s -> strings.add(s));
+            }
+        }
+
+        return delegate.complete(buffer, cursor, candidates);
+    }
+}
diff --git a/apps/cpman/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/cpman/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 095211f..8f6ffb7 100644
--- a/apps/cpman/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/cpman/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -20,15 +20,11 @@
             <action class="org.onosproject.cpman.cli.ControlMetricsStatsListCommand"/>
             <completers>
                 <ref component-id="controlResourceTypeCompleter"/>
-                <ref component-id="networkResourceNameCompleter"/>
-                <ref component-id="diskResourceNameCompleter"/>
-                <ref component-id="controlMessageDeviceIdCompleter"/>
+                <ref component-id="resourceNameCompleter"/>
             </completers>
         </command>
     </command-bundle>
 
     <bean id="controlResourceTypeCompleter" class="org.onosproject.cpman.cli.ControlResourceTypeCompleter"/>
-    <bean id="networkResourceNameCompleter" class="org.onosproject.cpman.cli.NetworkResourceNameCompleter"/>
-    <bean id="diskResourceNameCompleter" class="org.onosproject.cpman.cli.DiskResourceNameCompleter"/>
-    <bean id="controlMessageDeviceIdCompleter" class="org.onosproject.cpman.cli.ControlMessageDeviceIdCompleter"/>
+    <bean id="resourceNameCompleter" class="org.onosproject.cpman.cli.ResourceNameCompleter"/>
 </blueprint>
\ No newline at end of file