Added a set of CLIs for update and enable/disable telemetry svc cfg

Change-Id: I518585f7941c98aa0479acc3583a0f1a751e6db9
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigNameCompleter.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigNameCompleter.java
new file mode 100644
index 0000000..cc0b5c1
--- /dev/null
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigNameCompleter.java
@@ -0,0 +1,57 @@
+/*
+ * 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.openstacktelemetry.cli;
+
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.CommandLine;
+import org.apache.karaf.shell.api.console.Completer;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.karaf.shell.support.completers.StringsCompleter;
+import org.onosproject.openstacktelemetry.api.TelemetryConfigService;
+import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.stream.Collectors;
+
+import static org.onosproject.cli.AbstractShellCommand.get;
+
+/**
+ * Telemetry configuration property completer.
+ */
+@Service
+public class TelemetryConfigNameCompleter implements Completer {
+
+    private static final String MASTER = "master";
+
+    @Override
+    public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+        StringsCompleter delegate = new StringsCompleter();
+        TelemetryConfigService service = get(TelemetryConfigService.class);
+
+        Set<String> set = service.getConfigs().stream()
+                .filter(c -> !c.swVersion().equals(MASTER))
+                .sorted(Comparator.comparing(TelemetryConfig::name))
+                .map(TelemetryConfig::name)
+                .collect(Collectors.toSet());
+
+        SortedSet<String> strings = delegate.getStrings();
+        strings.addAll(set);
+        return delegate.complete(session, commandLine, candidates);
+    }
+}
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigUpdateAddressCommand.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigUpdateAddressCommand.java
new file mode 100644
index 0000000..ea36247
--- /dev/null
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigUpdateAddressCommand.java
@@ -0,0 +1,68 @@
+/*
+ * 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.openstacktelemetry.cli;
+
+import com.google.common.collect.Maps;
+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.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService;
+import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
+
+import java.util.Map;
+
+/**
+ * Update telemetry configuration.
+ */
+@Service
+@Command(scope = "onos", name = "telemetry-update-address",
+        description = "Update a telemetry address")
+public class TelemetryConfigUpdateAddressCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "config name", description = "telemetry config name",
+            required = true, multiValued = false)
+    @Completion(TelemetryConfigNameCompleter.class)
+    private String configName = null;
+
+    @Argument(index = 1, name = "address", description = "telemetry config address",
+            required = true, multiValued = false)
+    private String address = null;
+
+    private static final String ADDRESS = "address";
+    private static final String NO_ELEMENT =
+            "No telemetry config is found with the given name";
+
+    @Override
+    protected void doExecute() {
+        TelemetryConfigAdminService service = get(TelemetryConfigAdminService.class);
+        TelemetryConfig config = service.getConfig(configName);
+
+        if (config == null) {
+            print(NO_ELEMENT);
+            return;
+        }
+
+        Map<String, String> updatedProperties = Maps.newHashMap(config.properties());
+        updatedProperties.put(ADDRESS, address);
+
+        TelemetryConfig updatedConfig = config.updateProperties(updatedProperties);
+
+        service.updateTelemetryConfig(updatedConfig);
+        print("Successfully updated telemetry config address!");
+    }
+}
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigViewCommand.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigViewCommand.java
new file mode 100644
index 0000000..f1e282a
--- /dev/null
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryConfigViewCommand.java
@@ -0,0 +1,62 @@
+/*
+ * 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.openstacktelemetry.cli;
+
+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.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.openstacktelemetry.api.TelemetryConfigService;
+import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
+
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+/**
+ * Queries the detailed information of telemetry config.
+ */
+@Service
+@Command(scope = "onos", name = "telemetry-config",
+        description = "Query a specific telemetry configuration")
+public class TelemetryConfigViewCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "config name", description = "telemetry config name",
+            required = true, multiValued = false)
+    @Completion(TelemetryConfigNameCompleter.class)
+    private String configName = null;
+
+    private static final String FORMAT = "%25s : %s";
+    private static final String NO_ELEMENT =
+            "No telemetry config is found with the given name";
+
+    @Override
+    protected void doExecute() {
+        TelemetryConfigService service = get(TelemetryConfigService.class);
+        TelemetryConfig config = service.getConfig(configName);
+
+        if (config == null) {
+            print(NO_ELEMENT);
+            return;
+        }
+
+        SortedSet<String> keys = new TreeSet<>(config.properties().keySet());
+
+        keys.forEach(k -> {
+            print(FORMAT, k, config.properties().get(k));
+        });
+    }
+}
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryServiceDisableCommand.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryServiceDisableCommand.java
new file mode 100644
index 0000000..13524d6
--- /dev/null
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryServiceDisableCommand.java
@@ -0,0 +1,58 @@
+/*
+ * 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.openstacktelemetry.cli;
+
+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.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService;
+import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
+
+/**
+ * Disables a telemetry service.
+ */
+@Service
+@Command(scope = "onos", name = "telemetry-disable",
+        description = "Disable a specific telemetry service")
+public class TelemetryServiceDisableCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "config name", description = "telemetry config name",
+            required = true, multiValued = false)
+    @Completion(TelemetryConfigNameCompleter.class)
+    private String configName = null;
+
+    private static final String FORMAT = "Successfully disabled telemetry service %s!";
+    private static final String NO_ELEMENT =
+            "No telemetry config is found with the given name";
+
+    @Override
+    protected void doExecute() {
+        TelemetryConfigAdminService service = get(TelemetryConfigAdminService.class);
+        TelemetryConfig config = service.getConfig(configName);
+
+        if (config == null) {
+            print(NO_ELEMENT);
+            return;
+        }
+
+        TelemetryConfig updatedConfig = config.updateEnabled(false);
+
+        service.updateTelemetryConfig(updatedConfig);
+        print(FORMAT, config.name());
+    }
+}
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryServiceEnableCommand.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryServiceEnableCommand.java
new file mode 100644
index 0000000..e484b48
--- /dev/null
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/cli/TelemetryServiceEnableCommand.java
@@ -0,0 +1,58 @@
+/*
+ * 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.openstacktelemetry.cli;
+
+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.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService;
+import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
+
+/**
+ * Enables a telemetry service.
+ */
+@Service
+@Command(scope = "onos", name = "telemetry-enable",
+        description = "Enable a specific telemetry service")
+public class TelemetryServiceEnableCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "config name", description = "telemetry config name",
+            required = true, multiValued = false)
+    @Completion(TelemetryConfigNameCompleter.class)
+    private String configName = null;
+
+    private static final String FORMAT = "Successfully enabled telemetry service %s!";
+    private static final String NO_ELEMENT =
+            "No telemetry config is found with the given name";
+
+    @Override
+    protected void doExecute() {
+        TelemetryConfigAdminService service = get(TelemetryConfigAdminService.class);
+        TelemetryConfig config = service.getConfig(configName);
+
+        if (config == null) {
+            print(NO_ELEMENT);
+            return;
+        }
+
+        TelemetryConfig updatedConfig = config.updateEnabled(true);
+
+        service.updateTelemetryConfig(updatedConfig);
+        print(FORMAT, config.name());
+    }
+}