[ONOS-3662] Add CLI for control plane manager

- Implement CLI for enumerating control plane metrics stats
- Implement ControlResourceTypeCompleter
- Implement DiskResourceNameCompleter
- Implement NetworkResourceNameCompleter
- Implement ControlMessageDeviceIdCompleter
- Extract the control resource type and metrics in a separated
  class for the sake of simplicity.

Change-Id: Ic505191a74bd463091b0e5c75e11f1824bafb816
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
new file mode 100644
index 0000000..ab063a0
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlMessageDeviceIdCompleter.java
@@ -0,0 +1,56 @@
+/*
+ * 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/ControlMetricsStatsListCommand.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlMetricsStatsListCommand.java
new file mode 100644
index 0000000..2b061d2
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlMetricsStatsListCommand.java
@@ -0,0 +1,117 @@
+/*
+ * 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.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.cpman.ControlLoad;
+import org.onosproject.cpman.ControlMetricType;
+import org.onosproject.cpman.ControlPlaneMonitorService;
+import org.onosproject.net.DeviceId;
+
+import java.util.Optional;
+import java.util.Set;
+
+import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
+import static org.onosproject.cpman.ControlResource.CPU_METRICS;
+import static org.onosproject.cpman.ControlResource.DISK_METRICS;
+import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
+import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
+
+/**
+ * Lists all stats information of control plane metrics.
+ */
+@Command(scope = "onos", name = "cpman-stats-list",
+        description = "Lists control metrics statistics")
+public class ControlMetricsStatsListCommand extends AbstractShellCommand {
+
+    private static final String FMT = "metricType=%s, latestValue=%d, " +
+            "averageValue=%d, latestTime=%s";
+    private static final String INVALID_TYPE = "Invalid control resource type.";
+
+    @Argument(index = 0, name = "type",
+            description = "Resource type (cpu|memory|disk|network|control_message)",
+            required = true, multiValued = false)
+    String type = null;
+
+    @Argument(index = 1, name = "name", description = "Resource name (or Device Id)",
+            required = false, multiValued = false)
+    String name = null;
+
+    @Override
+    protected void execute() {
+        ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
+        ClusterService clusterService = get(ClusterService.class);
+        NodeId nodeId = clusterService.getLocalNode().id();
+        switch (type) {
+            case "cpu":
+                printMetricsStats(service, nodeId, CPU_METRICS);
+                break;
+            case "memory":
+                printMetricsStats(service, nodeId, MEMORY_METRICS);
+                break;
+            case "disk":
+                printMetricsStats(service, nodeId, DISK_METRICS, name);
+                break;
+            case "network":
+                printMetricsStats(service, nodeId, NETWORK_METRICS, name);
+                break;
+            case "control_message":
+                if (name != null) {
+                    printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS, DeviceId.deviceId(name));
+                }
+                break;
+            default:
+                print(INVALID_TYPE);
+                break;
+        }
+    }
+
+    private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
+                                   Set<ControlMetricType> typeSet) {
+        printMetricsStats(service, nodeId, typeSet, null, null);
+    }
+
+    private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
+                                   Set<ControlMetricType> typeSet, String name) {
+        printMetricsStats(service, nodeId, typeSet, name, null);
+    }
+
+    private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
+                                   Set<ControlMetricType> typeSet, DeviceId did) {
+        printMetricsStats(service, nodeId, typeSet, null, did);
+    }
+
+    private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
+                                   Set<ControlMetricType> typeSet, String name, DeviceId did) {
+        if (name == null && did == null) {
+            typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, Optional.ofNullable(null))));
+        } else if (name == null && did != null) {
+            typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, Optional.of(did))));
+        } else if (name != null && did == null) {
+            typeSet.forEach(s -> print(s, service.getLoad(nodeId, s, name)));
+        }
+    }
+
+    private void print(ControlMetricType type, ControlLoad cl) {
+        if (cl != null) {
+            print(FMT, type.toString(), cl.latest(), cl.average(), cl.time());
+        }
+    }
+}
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlResourceTypeCompleter.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlResourceTypeCompleter.java
new file mode 100644
index 0000000..cc3b225
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/ControlResourceTypeCompleter.java
@@ -0,0 +1,40 @@
+/*
+ * 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.ImmutableList;
+import org.onosproject.cli.AbstractChoicesCompleter;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.onosproject.cpman.ControlResource.Type;
+
+/**
+ * Control resource type completer.
+ */
+public class ControlResourceTypeCompleter extends AbstractChoicesCompleter {
+
+    private static final List<Type> RESOURCE_TYPES =
+            ImmutableList.of(Type.CPU, Type.MEMORY, Type.DISK, Type.NETWORK,
+                    Type.CONTROL_MESSAGE);
+
+    @Override
+    protected List<String> choices() {
+        return RESOURCE_TYPES.stream().map(type ->
+                type.toString().toLowerCase()).collect(Collectors.toList());
+    }
+}
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
new file mode 100644
index 0000000..f67d629
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/DiskResourceNameCompleter.java
@@ -0,0 +1,55 @@
+/*
+ * 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
new file mode 100644
index 0000000..576d8ee
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/NetworkResourceNameCompleter.java
@@ -0,0 +1,57 @@
+/*
+ * 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/package-info.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/package-info.java
new file mode 100644
index 0000000..adcb6cf
--- /dev/null
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/cli/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+/**
+ * CLI implementation for control plane manager.
+ */
+package org.onosproject.cpman.cli;
\ No newline at end of file