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

Change-Id: I518585f7941c98aa0479acc3583a0f1a751e6db9
diff --git a/apps/openstacktelemetry/api/src/main/java/org/onosproject/openstacktelemetry/api/config/TelemetryConfig.java b/apps/openstacktelemetry/api/src/main/java/org/onosproject/openstacktelemetry/api/config/TelemetryConfig.java
index a5d20cb..0e0b4b4 100644
--- a/apps/openstacktelemetry/api/src/main/java/org/onosproject/openstacktelemetry/api/config/TelemetryConfig.java
+++ b/apps/openstacktelemetry/api/src/main/java/org/onosproject/openstacktelemetry/api/config/TelemetryConfig.java
@@ -143,4 +143,20 @@
      * @return merged configuration
      */
     TelemetryConfig merge(TelemetryConfig other);
+
+    /**
+     * Obtains the cloned instance with updated properties.
+     *
+     * @param properties telemetry config properties
+     * @return a cloned instance
+     */
+    TelemetryConfig updateProperties(Map<String, String> properties);
+
+    /**
+     * Obtains the cloned instance with updated enabled value.
+     *
+     * @param enabled service flag
+     * @return a cloned instance
+     */
+    TelemetryConfig updateEnabled(boolean enabled);
 }
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());
+    }
+}
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/DefaultTelemetryConfig.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/DefaultTelemetryConfig.java
index 368b36b..d130446 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/DefaultTelemetryConfig.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/DefaultTelemetryConfig.java
@@ -159,6 +159,19 @@
     }
 
     @Override
+    public TelemetryConfig updateProperties(Map<String, String> properties) {
+
+        return new DefaultTelemetryConfig(name, type, parents, manufacturer,
+                swVersion, enabled, properties);
+    }
+
+    @Override
+    public TelemetryConfig updateEnabled(boolean enabled) {
+        return new DefaultTelemetryConfig(name, type, parents, manufacturer,
+                swVersion, enabled, properties);
+    }
+
+    @Override
     public Set<String> keys() {
         return properties.keySet();
     }
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryManager.java
index 796da19..781c4e2 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryManager.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.openstacktelemetry.impl;
 
+import com.google.common.collect.Maps;
 import org.influxdb.InfluxDB;
 import org.influxdb.InfluxDBFactory;
 import org.influxdb.dto.BatchPoints;
@@ -80,7 +81,7 @@
     protected TelemetryConfigService telemetryConfigService;
 
     private static final String INFLUX_PROTOCOL = "http";
-    private Map<String, InfluxDB> producers = null;
+    private Map<String, InfluxDB> producers = Maps.newConcurrentMap();
 
     @Activate
     protected void activate() {
diff --git a/apps/openstacktelemetry/app/src/main/resources/org/onosproject/openstacktelemetry/impl/influxdb-configs.xml b/apps/openstacktelemetry/app/src/main/resources/org/onosproject/openstacktelemetry/impl/influxdb-configs.xml
index d702a44..cf3729a 100644
--- a/apps/openstacktelemetry/app/src/main/resources/org/onosproject/openstacktelemetry/impl/influxdb-configs.xml
+++ b/apps/openstacktelemetry/app/src/main/resources/org/onosproject/openstacktelemetry/impl/influxdb-configs.xml
@@ -21,10 +21,16 @@
         <property name="username">onos</property>
         <property name="password">onos</property>
     </config>
-    <config name="sona-influxdb-connector" manufacturer="SK Telecom"
+    <config name="sona-influxdb-connector-1" manufacturer="SK Telecom"
             swVersion="1.0" extends="influxdb" enabled="false">
         <property name="database">ost</property>
         <property name="measurement">sonaflow</property>
         <property name="enableBatch">true</property>
     </config>
+    <config name="sona-influxdb-connector-2" manufacturer="SK Telecom"
+            swVersion="1.0" extends="influxdb" enabled="false">
+        <property name="database">ost2</property>
+        <property name="measurement">sonaflow</property>
+        <property name="enableBatch">true</property>
+    </config>
 </configs>
\ No newline at end of file