Notify all metric reporters when metric registry has been changed

With existing implementation, it is difficult to notify the
metric reports on metric registry changes. With this commit,
we can spontaneously notify all reporters about the metric
registry changes, so that newly added metrics can be automatically
reported to third party monitoring system.

Change-Id: I1273194553900f6bb03e2ef6bb1b54838af1da00
diff --git a/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/DefaultInfluxDbMetricsReporter.java b/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/DefaultInfluxDbMetricsReporter.java
index 923e3db..7ff636c 100644
--- a/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/DefaultInfluxDbMetricsReporter.java
+++ b/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/DefaultInfluxDbMetricsReporter.java
@@ -93,6 +93,9 @@
     public void activate() {
         cfgService.registerProperties(getClass());
         coreService.registerApplication("org.onosproject.influxdbmetrics");
+        metricsService.registerReporter(this);
+
+        startReport();
 
         log.info("Started");
     }
@@ -101,6 +104,9 @@
     public void deactivate() {
         cfgService.unregisterProperties(getClass(), false);
 
+        stopReport();
+        metricsService.unregisterReporter(this);
+
         log.info("Stopped");
     }
 
@@ -112,20 +118,10 @@
 
     @Override
     public void startReport() {
-        try {
-            influxDbHttpSender = new InfluxDbHttpSender(DEFAULT_PROTOCOL, address,
-                    port, database, username + SEPARATOR + password, REPORT_TIME_UNIT,
-                    DEFAULT_CONN_TIMEOUT, DEFAULT_READ_TIMEOUT);
-            MetricRegistry mr = metricsService.getMetricRegistry();
-            influxDbReporter = InfluxDbReporter.forRegistry(addHostPrefix(filter(mr)))
-                    .convertRatesTo(TimeUnit.SECONDS)
-                    .convertDurationsTo(TimeUnit.MILLISECONDS)
-                    .build(influxDbHttpSender);
-            influxDbReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT);
-            log.info("Start to report metrics to influxDB.");
-        } catch (Exception e) {
-            log.error("Fail to connect to given influxDB server!");
-        }
+        configSender();
+        influxDbReporter = buildReporter(influxDbHttpSender);
+        influxDbReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT);
+        log.info("Start to report metrics to influxDB.");
     }
 
     @Override
@@ -143,6 +139,14 @@
     }
 
     @Override
+    public void notifyMetricsChange() {
+        influxDbReporter.stop();
+        influxDbReporter = buildReporter(influxDbHttpSender);
+        influxDbReporter.start(REPORT_PERIOD, REPORT_TIME_UNIT);
+        log.info("Metric registry has been changed, apply changes.");
+    }
+
+    @Override
     public void config(String address, int port, String database,
                        String username, String password) {
         this.address = address;
@@ -226,4 +230,31 @@
                     monitorAll ? "enabled" : "disabled");
         }
     }
+
+    /**
+     * Configures parameters for sender.
+     */
+    private void configSender() {
+        try {
+            influxDbHttpSender = new InfluxDbHttpSender(DEFAULT_PROTOCOL, address,
+                    port, database, username + SEPARATOR + password, REPORT_TIME_UNIT,
+                    DEFAULT_CONN_TIMEOUT, DEFAULT_READ_TIMEOUT);
+        } catch (Exception e) {
+            log.error("Fail to connect to given influxDB server!");
+        }
+    }
+
+    /**
+     * Builds reporter with the given sender.
+     *
+     * @param sender sender
+     * @return reporter
+     */
+    private InfluxDbReporter buildReporter(InfluxDbHttpSender sender) {
+        MetricRegistry mr = metricsService.getMetricRegistry();
+        return InfluxDbReporter.forRegistry(addHostPrefix(filter(mr)))
+                .convertRatesTo(TimeUnit.SECONDS)
+                .convertDurationsTo(TimeUnit.MILLISECONDS)
+                .build(sender);
+    }
 }
diff --git a/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsConfig.java b/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsConfig.java
index 7c879b9..8b79c71 100644
--- a/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsConfig.java
+++ b/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsConfig.java
@@ -86,8 +86,6 @@
         coreService.registerApplication("org.onosproject.influxdbmetrics");
 
         configReporter(influxDbMetricsReporter);
-        influxDbMetricsReporter.startReport();
-
         configRetriever(influxDbMetricsRetriever);
 
         log.info("Started");
@@ -97,7 +95,6 @@
     public void deactivate() {
         cfgService.unregisterProperties(getClass(), false);
 
-        influxDbMetricsReporter.stopReport();
         log.info("Stopped");
     }
 
diff --git a/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsReporter.java b/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsReporter.java
index 1645ee9..e0090ee 100644
--- a/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsReporter.java
+++ b/apps/influxdbmetrics/src/main/java/org/onosproject/influxdbmetrics/InfluxDbMetricsReporter.java
@@ -15,25 +15,12 @@
  */
 package org.onosproject.influxdbmetrics;
 
+import org.onlab.metrics.MetricsReporter;
+
 /**
  * A Metric reporter interface for reporting all metrics to influxDB server.
  */
-public interface InfluxDbMetricsReporter {
-
-    /**
-     * Starts to report metrics to InfluxDB server.
-     */
-    void startReport();
-
-    /**
-     * Stops reporting metrics.
-     */
-    void stopReport();
-
-    /**
-     * Restarts metrics reporter.
-     */
-    void restartReport();
+public interface InfluxDbMetricsReporter extends MetricsReporter {
 
     /**
      * Configures default parameters for influx database metrics reporter.