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);
+    }
 }