Notify all metric reporters when new types of system metric added

Change-Id: I307be0cb68bdc7fc3c75212c3fac4390fb9391a9
diff --git a/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/SystemMetricsCollectorWebResource.java b/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/SystemMetricsCollectorWebResource.java
index 6c565ff..c96d1d9 100644
--- a/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/SystemMetricsCollectorWebResource.java
+++ b/apps/cpman/app/src/main/java/org/onosproject/cpman/rest/SystemMetricsCollectorWebResource.java
@@ -63,6 +63,9 @@
     private static final String INVALID_RESOURCE_NAME = "Invalid resource name";
     private static final String INVALID_REQUEST = "Invalid request";
     private static final int PERCENT_CONSTANT = 100;
+    private static final String SYSTEM_TYPE = "system";
+    private static final String DISK_TYPE = "disk";
+    private static final String NETWORK_TYPE = "network";
 
     private static final Set<String> MEMORY_FIELD_SET = ControlResource.MEMORY_METRICS
             .stream().map(type -> toCamelCase(type.toString(), true))
@@ -72,8 +75,7 @@
             .stream().map(type -> toCamelCase(type.toString(), true))
             .collect(Collectors.toSet());
 
-    private SystemMetricsAggregator systemAggr =
-            new SystemMetricsAggregator(metricsService, Optional.ofNullable(null), "system");
+    private SystemMetricsAggregator aggregator = SystemMetricsAggregator.getInstance();
 
     /**
      * Collects CPU metrics.
@@ -103,30 +105,33 @@
             long userCpuTime = nullIsIllegal(jsonTree.get("userCpuTime").asLong(), INVALID_REQUEST);
             long cpuIdleTime = nullIsIllegal(jsonTree.get("cpuIdleTime").asLong(), INVALID_REQUEST);
 
+            aggregator.setMetricsService(metricsService);
+            aggregator.addMetrics(Optional.ofNullable(null), SYSTEM_TYPE);
+
             cm = new ControlMetric(ControlMetricType.CPU_LOAD,
                     new MetricValue.Builder().load(cpuLoad).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.CPU_LOAD, cpuLoad);
+            aggregator.increment(ControlMetricType.CPU_LOAD, cpuLoad);
 
             cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
                     new MetricValue.Builder().load(totalCpuTime).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.TOTAL_CPU_TIME, totalCpuTime);
+            aggregator.increment(ControlMetricType.TOTAL_CPU_TIME, totalCpuTime);
 
             cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME,
                     new MetricValue.Builder().load(sysCpuTime).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.SYS_CPU_TIME, sysCpuTime);
+            aggregator.increment(ControlMetricType.SYS_CPU_TIME, sysCpuTime);
 
             cm = new ControlMetric(ControlMetricType.USER_CPU_TIME,
                     new MetricValue.Builder().load(userCpuTime).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.USER_CPU_TIME, userCpuTime);
+            aggregator.increment(ControlMetricType.USER_CPU_TIME, userCpuTime);
 
             cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME,
                     new MetricValue.Builder().load(cpuIdleTime).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.CPU_IDLE_TIME, cpuIdleTime);
+            aggregator.increment(ControlMetricType.CPU_IDLE_TIME, cpuIdleTime);
 
         } catch (IOException e) {
             throw new IllegalArgumentException(e.getMessage());
@@ -161,25 +166,28 @@
             long memUsedRatio = memTotal == 0L ? 0L : (memUsed * PERCENT_CONSTANT) / memTotal;
             long memFreeRatio = memTotal == 0L ? 0L : (memFree * PERCENT_CONSTANT) / memTotal;
 
+            aggregator.setMetricsService(metricsService);
+            aggregator.addMetrics(Optional.ofNullable(null), SYSTEM_TYPE);
+
             cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO,
                     new MetricValue.Builder().load(memUsedRatio).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.MEMORY_USED_RATIO, memUsedRatio);
+            aggregator.increment(ControlMetricType.MEMORY_USED_RATIO, memUsedRatio);
 
             cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO,
                     new MetricValue.Builder().load(memFreeRatio).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.MEMORY_FREE_RATIO, memFreeRatio);
+            aggregator.increment(ControlMetricType.MEMORY_FREE_RATIO, memFreeRatio);
 
             cm = new ControlMetric(ControlMetricType.MEMORY_USED,
                     new MetricValue.Builder().load(memUsed).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.MEMORY_USED, memUsed);
+            aggregator.increment(ControlMetricType.MEMORY_USED, memUsed);
 
             cm = new ControlMetric(ControlMetricType.MEMORY_FREE,
                     new MetricValue.Builder().load(memFree).add());
             monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
-            systemAggr.increment(ControlMetricType.MEMORY_FREE, memFree);
+            aggregator.increment(ControlMetricType.MEMORY_FREE, memFree);
 
         } catch (IOException e) {
             throw new IllegalArgumentException(e.getMessage());
@@ -204,14 +212,15 @@
         try {
             ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
             ArrayNode diskRes =
-                    jsonTree.get("disks") == null ? mapper().createArrayNode() : (ArrayNode) jsonTree.get("disks");
+                    jsonTree.get("disks") == null ?
+                            mapper().createArrayNode() : (ArrayNode) jsonTree.get("disks");
 
             for (JsonNode node : diskRes) {
                 JsonNode resourceName = node.get("resourceName");
                 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
 
-                SystemMetricsAggregator diskAggr = new SystemMetricsAggregator(metricsService,
-                        Optional.of(resourceName.asText()), "disk");
+                aggregator.setMetricsService(metricsService);
+                aggregator.addMetrics(Optional.of(resourceName.asText()), DISK_TYPE);
 
                 long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST);
                 long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST);
@@ -219,12 +228,13 @@
                 cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
                         new MetricValue.Builder().load(readBytes).add());
                 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
-                diskAggr.increment(ControlMetricType.DISK_READ_BYTES, readBytes);
-
+                aggregator.increment(resourceName.asText(), DISK_TYPE,
+                        ControlMetricType.DISK_READ_BYTES, readBytes);
                 cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
                         new MetricValue.Builder().load(writeBytes).add());
                 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
-                diskAggr.increment(ControlMetricType.DISK_WRITE_BYTES, writeBytes);
+                aggregator.increment(resourceName.asText(), DISK_TYPE,
+                        ControlMetricType.DISK_WRITE_BYTES, writeBytes);
             }
         } catch (IOException e) {
             throw new IllegalArgumentException(e.getMessage());
@@ -256,8 +266,8 @@
                 JsonNode resourceName = node.get("resourceName");
                 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
 
-                SystemMetricsAggregator networkAggr = new SystemMetricsAggregator(metricsService,
-                        Optional.of(resourceName.asText()), "network");
+                aggregator.setMetricsService(metricsService);
+                aggregator.addMetrics(Optional.of(resourceName.asText()), NETWORK_TYPE);
 
                 long inBytes = nullIsIllegal(node.get("incomingBytes").asLong(), INVALID_REQUEST);
                 long outBytes = nullIsIllegal(node.get("outgoingBytes").asLong(), INVALID_REQUEST);
@@ -267,22 +277,26 @@
                 cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
                         new MetricValue.Builder().load(inBytes).add());
                 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
-                networkAggr.increment(ControlMetricType.NW_INCOMING_BYTES, inBytes);
+                aggregator.increment(resourceName.asText(), NETWORK_TYPE,
+                        ControlMetricType.NW_INCOMING_BYTES, inBytes);
 
                 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
                         new MetricValue.Builder().load(outBytes).add());
                 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
-                networkAggr.increment(ControlMetricType.NW_OUTGOING_BYTES, outBytes);
+                aggregator.increment(resourceName.asText(), NETWORK_TYPE,
+                        ControlMetricType.NW_OUTGOING_BYTES, outBytes);
 
                 cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
                         new MetricValue.Builder().load(inPackets).add());
                 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
-                networkAggr.increment(ControlMetricType.NW_INCOMING_PACKETS, inPackets);
+                aggregator.increment(resourceName.asText(), NETWORK_TYPE,
+                        ControlMetricType.NW_INCOMING_PACKETS, inPackets);
 
                 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
                         new MetricValue.Builder().load(outPackets).add());
                 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
-                networkAggr.increment(ControlMetricType.NW_OUTGOING_PACKETS, outPackets);
+                aggregator.increment(resourceName.asText(), NETWORK_TYPE,
+                        ControlMetricType.NW_OUTGOING_PACKETS, outPackets);
             }
         } catch (IOException e) {
             throw new IllegalArgumentException(e.getMessage());