[ONOS-7683] Initial implementation of openstack telemetry service

Change-Id: I621bed9cff108af194998b7a1e8269a9a6ffd32c
diff --git a/apps/openstacktelemetry/app/BUCK b/apps/openstacktelemetry/app/BUCK
index 4696a55..39561d6 100644
--- a/apps/openstacktelemetry/app/BUCK
+++ b/apps/openstacktelemetry/app/BUCK
@@ -3,6 +3,7 @@
 COMPILE_DEPS = [
     '//lib:CORE_DEPS',
     '//lib:JACKSON',
+    '//lib:KRYO',
     '//core/store/serializers:onos-core-serializers',
     '//lib:javax.ws.rs-api',
     '//utils/rest:onlab-rest',
@@ -12,6 +13,7 @@
     '//apps/openstacktelemetry/api:onos-apps-openstacktelemetry-api',
     '//lib:kafka-clients',
     '//lib:influxdb-java',
+    '//lib:protobuf-java-3.2.0',
     '//lib:GRPC_1.3',
     '//incubator/grpc-dependencies:grpc-core-repkg-' + GRPC_VER,
     '//lib:grpc-protobuf-lite-' + GRPC_VER,
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryConfigManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryConfigManager.java
index 308ee49..0ab2b5e 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryConfigManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryConfigManager.java
@@ -16,11 +16,13 @@
 package org.onosproject.openstacktelemetry.impl;
 
 import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Modified;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
 import org.onlab.util.Tools;
 import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService;
@@ -35,6 +37,7 @@
 
 import static org.onlab.util.Tools.get;
 import static org.onlab.util.Tools.getIntegerProperty;
+import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_IP;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_PORT;
@@ -44,10 +47,13 @@
 /**
  * gRPC server configuration manager for publishing openstack telemetry.
  */
+@Component(immediate = true)
+@Service
 public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private static final String ENABLE_SERVICE = "enableService";
     private static final String ADDRESS = "address";
     private static final String PORT = "port";
     private static final String USE_PLAINTEXT = "usePlaintext";
@@ -75,24 +81,45 @@
             label = "Maximum inbound message size used for communicating with gRPC server")
     protected Integer maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
 
+    @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
+            label = "Specify the default behavior of telemetry service")
+    protected Boolean enableService = DEFAULT_DISABLE;
+
     @Activate
     protected void activate(ComponentContext context) {
         componentConfigService.registerProperties(getClass());
-        grpcTelemetryAdminService.start(getConfig());
+
+        if (enableService) {
+            grpcTelemetryAdminService.start(getConfig());
+        }
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         componentConfigService.unregisterProperties(getClass(), false);
-        grpcTelemetryAdminService.stop();
+
+        if (enableService) {
+            grpcTelemetryAdminService.stop();
+        }
         log.info("Stopped");
     }
 
     @Modified
     private void modified(ComponentContext context) {
         readComponentConfiguration(context);
-        grpcTelemetryAdminService.restart(getConfig());
+
+        if (enableService) {
+            if (grpcTelemetryAdminService.isRunning()) {
+                grpcTelemetryAdminService.restart(getConfig());
+            } else {
+                grpcTelemetryAdminService.start(getConfig());
+            }
+        } else {
+            if (grpcTelemetryAdminService.isRunning()) {
+                grpcTelemetryAdminService.stop();
+            }
+        }
         log.info("Modified");
     }
 
@@ -148,6 +175,17 @@
             maxInboundMsgSize = maxInboundMsgSizeConfigured;
             log.info("Configured. gRPC server max inbound message size is {}", maxInboundMsgSize);
         }
+
+        Boolean enableServiceConfigured =
+                getBooleanProperty(properties, ENABLE_SERVICE);
+        if (enableServiceConfigured == null) {
+            enableService = DEFAULT_DISABLE;
+            log.info("gRPC service enable flag is NOT " +
+                    "configured, default value is {}", enableService);
+        } else {
+            enableService = enableServiceConfigured;
+            log.info("Configured. gRPC service enable flag is {}", enableService);
+        }
     }
 
 }
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryManager.java
index 20ded3e..8812cca 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/GrpcTelemetryManager.java
@@ -16,13 +16,14 @@
 package org.onosproject.openstacktelemetry.impl;
 
 import io.grpc.ManagedChannel;
-import io.grpc.ManagedChannelBuilder;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService;
-import org.onosproject.openstacktelemetry.api.config.GrpcTelemetryConfig;
+import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
 import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,16 +37,25 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected OpenstackTelemetryService openstackTelemetryService;
+
     private ManagedChannel channel = null;
 
     @Activate
     protected void activate() {
+
+        openstackTelemetryService.addTelemetryService(this);
+
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         stop();
+
+        openstackTelemetryService.removeTelemetryService(this);
+
         log.info("Stopped");
     }
 
@@ -56,22 +66,24 @@
             return;
         }
 
-        GrpcTelemetryConfig grpcConfig = (GrpcTelemetryConfig) config;
-        channel = ManagedChannelBuilder
-                .forAddress(grpcConfig.address(), grpcConfig.port())
-                .maxInboundMessageSize(grpcConfig.maxInboundMsgSize())
-                .usePlaintext(grpcConfig.usePlaintext())
-                .build();
+        // FIXME do not activate grpc service for now due to deps conflict
+//        GrpcTelemetryConfig grpcConfig = (GrpcTelemetryConfig) config;
+//        channel = ManagedChannelBuilder
+//                .forAddress(grpcConfig.address(), grpcConfig.port())
+//                .maxInboundMessageSize(grpcConfig.maxInboundMsgSize())
+//                .usePlaintext(grpcConfig.usePlaintext())
+//                .build();
 
         log.info("gRPC producer has Started");
     }
 
     @Override
     public void stop() {
-        if (channel != null) {
-            channel.shutdown();
-            channel = null;
-        }
+        // FIXME do not activate grpc service for now due to deps conflict
+//        if (channel != null) {
+//            channel.shutdown();
+//            channel = null;
+//        }
 
         log.info("gRPC producer has Stopped");
     }
@@ -85,6 +97,16 @@
     @Override
     public Object publish(Object record) {
         // TODO: need to find a way to invoke gRPC endpoint using channel
+
+        if (channel == null) {
+            log.warn("gRPC telemetry service has not been enabled!");
+        }
+
         return null;
     }
+
+    @Override
+    public boolean isRunning() {
+        return channel != null;
+    }
 }
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryConfigManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryConfigManager.java
index 77c3d63..069c374 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryConfigManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/InfluxDbTelemetryConfigManager.java
@@ -16,11 +16,13 @@
 package org.onosproject.openstacktelemetry.impl;
 
 import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Modified;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
 import org.onlab.util.Tools;
 import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService;
@@ -33,6 +35,7 @@
 
 import java.util.Dictionary;
 
+import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_DATABASE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_ENABLE_BATCH;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_INFLUXDB_PASSWORD;
@@ -44,10 +47,13 @@
 /**
  * InfluxDB server configuration manager for publishing openstack telemetry.
  */
+@Component(immediate = true)
+@Service
 public class InfluxDbTelemetryConfigManager implements InfluxDbTelemetryConfigService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private static final String ENABLE_SERVICE = "enableService";
     private static final String ADDRESS = "address";
     private static final String PORT = "port";
     private static final String USERNAME = "username";
@@ -85,24 +91,45 @@
             label = "Flag value of enabling batch mode of InfluxDB server")
     protected Boolean enableBatch = DEFAULT_INFLUXDB_ENABLE_BATCH;
 
+    @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
+            label = "Specify the default behavior of telemetry service")
+    protected Boolean enableService = DEFAULT_DISABLE;
+
     @Activate
     protected void activate(ComponentContext context) {
         componentConfigService.registerProperties(getClass());
-        influxDbTelemetryAdminService.start(getConfig());
+
+        if (enableService) {
+            influxDbTelemetryAdminService.start(getConfig());
+        }
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         componentConfigService.unregisterProperties(getClass(), false);
-        influxDbTelemetryAdminService.stop();
+
+        if (enableService) {
+            influxDbTelemetryAdminService.stop();
+        }
         log.info("Stopped");
     }
 
     @Modified
     private void modified(ComponentContext context) {
         readComponentConfiguration(context);
-        influxDbTelemetryAdminService.restart(getConfig());
+
+        if (enableService) {
+            if (influxDbTelemetryAdminService.isRunning()) {
+                influxDbTelemetryAdminService.restart(getConfig());
+            } else {
+                influxDbTelemetryAdminService.start(getConfig());
+            }
+        } else {
+            if (influxDbTelemetryAdminService.isRunning()) {
+                influxDbTelemetryAdminService.stop();
+            }
+        }
         log.info("Modified");
     }
 
@@ -160,5 +187,16 @@
             enableBatch = enableBatchConfigured;
             log.info("Configured. InfluxDB server enable batch is {}", enableBatch);
         }
+
+        Boolean enableServiceConfigured =
+                getBooleanProperty(properties, ENABLE_SERVICE);
+        if (enableServiceConfigured == null) {
+            enableService = DEFAULT_DISABLE;
+            log.info("InfluxDB service enable flag is NOT " +
+                    "configured, default value is {}", enableService);
+        } else {
+            enableService = enableServiceConfigured;
+            log.info("Configured. InfluxDB service enable flag is {}", enableService);
+        }
     }
 }
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 b79d703..fa8d9fe 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
@@ -18,11 +18,14 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.influxdb.InfluxDB;
 import org.influxdb.InfluxDBFactory;
 import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryAdminService;
 import org.onosproject.openstacktelemetry.api.InfluxRecord;
+import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
 import org.onosproject.openstacktelemetry.api.config.InfluxDbTelemetryConfig;
 import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
 import org.slf4j.Logger;
@@ -37,17 +40,26 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected OpenstackTelemetryService openstackTelemetryService;
+
     private static final String PROTOCOL = "http";
     private InfluxDB producer = null;
 
     @Activate
     protected void activate() {
+
+        openstackTelemetryService.addTelemetryService(this);
+
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         stop();
+
+        openstackTelemetryService.removeTelemetryService(this);
+
         log.info("Stopped");
     }
 
@@ -91,5 +103,14 @@
     @Override
     public void publish(InfluxRecord<String, Object> record) {
         // TODO: need to find a way to invoke InfluxDB endpoint using producer
+
+        if (producer == null) {
+            log.warn("InfluxDB telemetry service has not been enabled!");
+        }
+    }
+
+    @Override
+    public boolean isRunning() {
+        return producer != null;
     }
 }
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryConfigManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryConfigManager.java
index 242709d..9309c75 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryConfigManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryConfigManager.java
@@ -16,11 +16,13 @@
 package org.onosproject.openstacktelemetry.impl;
 
 import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Modified;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
 import org.onlab.util.Tools;
 import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.openstacktelemetry.api.KafkaTelemetryAdminService;
@@ -33,6 +35,7 @@
 
 import java.util.Dictionary;
 
+import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_KAFKA_BATCH_SIZE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_KAFKA_KEY_SERIALIZER;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_KAFKA_LINGER_MS;
@@ -42,14 +45,18 @@
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_KAFKA_SERVER_IP;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_KAFKA_SERVER_PORT;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_KAFKA_VALUE_SERIALIZER;
+import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
 
 /**
  * Kafka server configuration manager for publishing openstack telemetry.
  */
+@Component(immediate = true)
+@Service
 public class KafkaTelemetryConfigManager implements KafkaTelemetryConfigService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private static final String ENABLE_SERVICE = "enableService";
     private static final String ADDRESS = "address";
     private static final String PORT = "port";
     private static final String RETRIES = "retries";
@@ -103,24 +110,45 @@
             label = "Serializer class for value that implements the Serializer interface")
     protected String valueSerializer = DEFAULT_KAFKA_VALUE_SERIALIZER;
 
+    @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
+            label = "Specify the default behavior of telemetry service")
+    protected Boolean enableService = DEFAULT_DISABLE;
+
     @Activate
     protected void activate(ComponentContext context) {
         componentConfigService.registerProperties(getClass());
-        kafkaTelemetryAdminService.start(getConfig());
+
+        if (enableService) {
+            kafkaTelemetryAdminService.start(getConfig());
+        }
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         componentConfigService.unregisterProperties(getClass(), false);
-        kafkaTelemetryAdminService.stop();
+
+        if (enableService) {
+            kafkaTelemetryAdminService.stop();
+        }
         log.info("Stopped");
     }
 
     @Modified
     private void modified(ComponentContext context) {
         readComponentConfiguration(context);
-        kafkaTelemetryAdminService.restart(getConfig());
+
+        if (enableService) {
+            if (kafkaTelemetryAdminService.isRunning()) {
+                kafkaTelemetryAdminService.restart(getConfig());
+            } else {
+                kafkaTelemetryAdminService.start(getConfig());
+            }
+        } else {
+            if (kafkaTelemetryAdminService.isRunning()) {
+                kafkaTelemetryAdminService.stop();
+            }
+        }
         log.info("Modified");
     }
 
@@ -207,5 +235,16 @@
         String valueSerializerStr = Tools.get(properties, VALUE_SERIALIZER);
         valueSerializer = valueSerializerStr != null ? valueSerializerStr : DEFAULT_KAFKA_VALUE_SERIALIZER;
         log.info("Configured, Kafka value serializer is {}", valueSerializer);
+
+        Boolean enableServiceConfigured =
+                getBooleanProperty(properties, ENABLE_SERVICE);
+        if (enableServiceConfigured == null) {
+            enableService = DEFAULT_DISABLE;
+            log.info("Kafka service enable flag is NOT " +
+                    "configured, default value is {}", enableService);
+        } else {
+            enableService = enableServiceConfigured;
+            log.info("Configured. Kafka service enable flag is {}", enableService);
+        }
     }
 }
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryManager.java
index e935111..8d6f62f 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/KafkaTelemetryManager.java
@@ -18,12 +18,15 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.kafka.clients.producer.KafkaProducer;
 import org.apache.kafka.clients.producer.Producer;
 import org.apache.kafka.clients.producer.ProducerRecord;
 import org.apache.kafka.clients.producer.RecordMetadata;
 import org.onosproject.openstacktelemetry.api.KafkaTelemetryAdminService;
+import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
 import org.onosproject.openstacktelemetry.api.config.KafkaTelemetryConfig;
 import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
 import org.slf4j.Logger;
@@ -50,16 +53,25 @@
     private static final String KEY_SERIALIZER = "key.serializer";
     private static final String VALUE_SERIALIZER = "value.serializer";
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected OpenstackTelemetryService openstackTelemetryService;
+
     private Producer<String, byte[]> producer = null;
 
     @Activate
     protected void activate() {
+
+        openstackTelemetryService.addTelemetryService(this);
+
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         stop();
+
+        openstackTelemetryService.removeTelemetryService(this);
+
         log.info("Stopped");
     }
 
@@ -110,6 +122,17 @@
 
     @Override
     public Future<RecordMetadata> publish(ProducerRecord<String, byte[]> record) {
+
+        if (producer == null) {
+            log.warn("Kafka telemetry service has not been enabled!");
+            return null;
+        }
+
         return producer.send(record);
     }
+
+    @Override
+    public boolean isRunning() {
+        return producer != null;
+    }
 }
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/OpenstackTelemetryManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/OpenstackTelemetryManager.java
new file mode 100644
index 0000000..4629a4e
--- /dev/null
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/OpenstackTelemetryManager.java
@@ -0,0 +1,112 @@
+/*
+ * 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.impl;
+
+import com.google.common.collect.Lists;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.onosproject.openstacktelemetry.api.ByteBufferCodec;
+import org.onosproject.openstacktelemetry.api.FlowInfo;
+import org.onosproject.openstacktelemetry.api.GrpcTelemetryService;
+import org.onosproject.openstacktelemetry.api.InfluxDbTelemetryService;
+import org.onosproject.openstacktelemetry.api.KafkaTelemetryService;
+import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
+import org.onosproject.openstacktelemetry.api.RestTelemetryService;
+import org.onosproject.openstacktelemetry.api.TelemetryService;
+import org.onosproject.openstacktelemetry.codec.TinaFlowInfoByteBufferCodec;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+/**
+ * Openstack telemetry manager.
+ */
+@Component(immediate = true)
+@Service
+public class OpenstackTelemetryManager implements OpenstackTelemetryService {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private List<TelemetryService> telemetryServices = Lists.newArrayList();
+
+    @Activate
+    protected void activate() {
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        log.info("Stopped");
+    }
+
+    @Override
+    public void addTelemetryService(TelemetryService telemetryService) {
+        telemetryServices.add(telemetryService);
+    }
+
+    @Override
+    public void removeTelemetryService(TelemetryService telemetryService) {
+        telemetryServices.remove(telemetryService);
+    }
+
+    @Override
+    public void publish(FlowInfo flowInfo) {
+        telemetryServices.forEach(service -> {
+
+
+            if (service instanceof GrpcTelemetryManager) {
+                invokeGrpcPublisher((GrpcTelemetryService) service, flowInfo);
+            }
+
+            if (service instanceof InfluxDbTelemetryManager) {
+                invokeInfluxDbPublisher((InfluxDbTelemetryService) service, flowInfo);
+            }
+
+            if (service instanceof KafkaTelemetryManager) {
+                invokeKafkaPublisher((KafkaTelemetryService) service, flowInfo);
+            }
+
+            if (service instanceof RestTelemetryManager) {
+                invokeRestPublisher((RestTelemetryService) service, flowInfo);
+            }
+
+        });
+    }
+
+    private void invokeGrpcPublisher(GrpcTelemetryService service, FlowInfo flowInfo) {
+        // TODO: need provide implementation
+    }
+
+    private void invokeInfluxDbPublisher(InfluxDbTelemetryService service, FlowInfo flowInfo) {
+        // TODO: need provide implementation
+    }
+
+    private void invokeKafkaPublisher(KafkaTelemetryService service, FlowInfo flowInfo) {
+        ByteBufferCodec codec = new TinaFlowInfoByteBufferCodec();
+        ByteBuffer buffer = codec.encode(flowInfo);
+        service.publish(new ProducerRecord<>("sona.flow", "flowdata", buffer.array()));
+    }
+
+    private void invokeRestPublisher(RestTelemetryService service, FlowInfo flowInfo) {
+        // TODO: need provide implementation
+    }
+
+}
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryConfigManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryConfigManager.java
index 832c60b..455de34 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryConfigManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryConfigManager.java
@@ -16,11 +16,13 @@
 package org.onosproject.openstacktelemetry.impl;
 
 import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Modified;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
 import org.onlab.util.Tools;
 import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService;
@@ -33,20 +35,25 @@
 
 import java.util.Dictionary;
 
+import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_ENDPOINT;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_METHOD;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_REQUEST_MEDIA_TYPE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_RESPONSE_MEDIA_TYPE;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_IP;
 import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_PORT;
+import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
 
 /**
  * REST server configuration manager for publishing openstack telemetry.
  */
+@Component(immediate = true)
+@Service
 public class RestTelemetryConfigManager implements RestTelemetryConfigService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private static final String ENABLE_SERVICE = "enableService";
     private static final String ADDRESS = "address";
     private static final String PORT = "port";
     private static final String ENDPOINT = "endpoint";
@@ -84,24 +91,45 @@
             label = "Response media type of REST server")
     protected String responseMediaType = DEFAULT_REST_RESPONSE_MEDIA_TYPE;
 
+    @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
+            label = "Specify the default behavior of telemetry service")
+    protected Boolean enableService = DEFAULT_DISABLE;
+
     @Activate
     protected void activate(ComponentContext context) {
         componentConfigService.registerProperties(getClass());
-        restTelemetryAdminService.start(getConfig());
+
+        if (enableService) {
+            restTelemetryAdminService.start(getConfig());
+        }
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         componentConfigService.unregisterProperties(getClass(), false);
-        restTelemetryAdminService.stop();
+
+        if (enableService) {
+            restTelemetryAdminService.stop();
+        }
         log.info("Stopped");
     }
 
     @Modified
     private void modified(ComponentContext context) {
         readComponentConfiguration(context);
-        restTelemetryAdminService.restart(getConfig());
+
+        if (enableService) {
+            if (restTelemetryAdminService.isRunning()) {
+                restTelemetryAdminService.restart(getConfig());
+            } else {
+                restTelemetryAdminService.start(getConfig());
+            }
+        } else {
+            if (restTelemetryAdminService.isRunning()) {
+                restTelemetryAdminService.stop();
+            }
+        }
         log.info("Modified");
     }
 
@@ -155,5 +183,16 @@
         responseMediaType = responseMediaTypeStr != null ?
                 responseMediaTypeStr : DEFAULT_REST_RESPONSE_MEDIA_TYPE;
         log.info("Configured. REST server response media type is {}", responseMediaType);
+
+        Boolean enableServiceConfigured =
+                getBooleanProperty(properties, ENABLE_SERVICE);
+        if (enableServiceConfigured == null) {
+            enableService = DEFAULT_DISABLE;
+            log.info("REST service enable flag is NOT " +
+                    "configured, default value is {}", enableService);
+        } else {
+            enableService = enableServiceConfigured;
+            log.info("Configured. REST service enable flag is {}", enableService);
+        }
     }
 }
diff --git a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryManager.java b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryManager.java
index 6f5710b..78dd101 100644
--- a/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryManager.java
+++ b/apps/openstacktelemetry/app/src/main/java/org/onosproject/openstacktelemetry/impl/RestTelemetryManager.java
@@ -18,7 +18,10 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService;
 import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService;
 import org.onosproject.openstacktelemetry.api.config.RestTelemetryConfig;
 import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
@@ -44,17 +47,26 @@
     private static final String POST_METHOD = "POST";
     private static final String GET_METHOD  = "GET";
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected OpenstackTelemetryService openstackTelemetryService;
+
     private WebTarget target = null;
     private RestTelemetryConfig restConfig = null;
 
     @Activate
     protected void activate() {
+
+        openstackTelemetryService.addTelemetryService(this);
+
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
         stop();
+
+        openstackTelemetryService.removeTelemetryService(this);
+
         log.info("Stopped");
     }
 
@@ -119,6 +131,12 @@
 
     @Override
     public Response publish(String record) {
+
+        if (target == null) {
+            log.warn("REST telemetry service has not been enabled!");
+            return null;
+        }
+
         switch (restConfig.method()) {
             case POST_METHOD:
                 return target.request(restConfig.requestMediaType())
@@ -129,4 +147,9 @@
                 return null;
         }
     }
+
+    @Override
+    public boolean isRunning() {
+        return target != null;
+    }
 }