[ONOS-3535] Implement control metrics aggregation logic

Change-Id: I9953146851d5f3fdf7bcee9561fa86ddb7c7b3fe
diff --git a/apps/cpman/pom.xml b/apps/cpman/pom.xml
index ed60e44..7a89ab5 100644
--- a/apps/cpman/pom.xml
+++ b/apps/cpman/pom.xml
@@ -33,7 +33,48 @@
         <dependency>
             <groupId>org.onosproject</groupId>
             <artifactId>onos-api</artifactId>
-            <version>1.5.0-SNAPSHOT</version>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <version>${project.version}</version>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-misc</artifactId>
+            <version>${project.version}</version>
+            <classifier>tests</classifier>
+            <scope>test</scope>
         </dependency>
     </dependencies>
-</project>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+            </plugin>
+
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.onosproject</groupId>
+                <artifactId>onos-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneLoad.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlLoad.java
similarity index 87%
rename from apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneLoad.java
rename to apps/cpman/src/main/java/org/onosproject/cpman/ControlLoad.java
index f55945f..b783aa6 100644
--- a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneLoad.java
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlLoad.java
@@ -22,10 +22,10 @@
 /**
  * Data repository for control plane load information.
  */
-public interface ControlPlaneLoad extends Load {
+public interface ControlLoad extends Load {
 
     /**
-     * Obtain the average of the specified time duration.
+     * Obtains the average of the specified time duration.
      *
      * @param duration time duration
      * @param unit     time unit
@@ -34,7 +34,7 @@
     long average(int duration, TimeUnit unit);
 
     /**
-     * Obtain the average of all time duration.
+     * Obtains the average of all time duration.
      *
      * @return average control plane metric value
      */
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlMessageType.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMessageType.java
deleted file mode 100644
index 82e414d..0000000
--- a/apps/cpman/src/main/java/org/onosproject/cpman/ControlMessageType.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * 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.cpman;
-
-/**
- * Abstracted Control Message Type.
- */
-public enum ControlMessageType {
-
-    /** Mapped to PACKET-IN message of OpenFlow. */
-    INBOUND_PACKET,
-
-    /** Mapped to PACKET-OUT message of OpenFlow. */
-    OUTBOUND_PACKET,
-
-    /** Mapped to FLOW-MOD message of OpenFlow. */
-    FLOW_MOD_PACKET,
-
-    /** Mapped to FLOW-REMOVED message of OpenFlow. */
-    FLOW_REMOVED_PACKET,
-
-    /** Mapped to STATS-REQUEST message of OpenFlow. */
-    REQUEST_PACKET,
-
-    /** Mapped to STATS-REPLY message of OpenFlow. */
-    REPLY_PACKET,
-
-    /** All message types. */
-    ALL
-}
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMetric.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetric.java
similarity index 78%
rename from apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMetric.java
rename to apps/cpman/src/main/java/org/onosproject/cpman/ControlMetric.java
index 19f4153..a431817 100644
--- a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMetric.java
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetric.java
@@ -18,24 +18,21 @@
 /**
  * Include various control plane metrics.
  */
-public class ControlPlaneMetric {
+public class ControlMetric {
 
-    private ControlMetricType metricType;
-    private long metricValue;
+    private final ControlMetricType metricType;
+    private final MetricValue metricValue;
+
+    ControlMetric(ControlMetricType metricType, MetricValue metricValue) {
+        this.metricType = metricType;
+        this.metricValue = metricValue;
+    }
 
     ControlMetricType metricType() {
         return metricType;
     }
 
-    void setMetricType(ControlMetricType metricType) {
-        this.metricType = metricType;
-    }
-
-    long metricValue() {
+    MetricValue metricValue() {
         return metricValue;
     }
-
-    void setMetricValue(long metricValue) {
-        this.metricValue = metricValue;
-    }
 }
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricType.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricType.java
index b5e8281..ab67918 100644
--- a/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricType.java
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricType.java
@@ -20,15 +20,27 @@
  */
 public enum ControlMetricType {
 
-    /** Racket Rate of Control Message. */
-    PacketRate,
+    /** Mapped to PACKET-IN message of OpenFlow. */
+    INBOUND_PACKET,
 
-    /** Byte Rate of Control Message. */
-    ByteRate,
+    /** Mapped to PACKET-OUT message of OpenFlow. */
+    OUTBOUND_PACKET,
+
+    /** Mapped to FLOW-MOD message of OpenFlow. */
+    FLOW_MOD_PACKET,
+
+    /** Mapped to FLOW-REMOVED message of OpenFlow. */
+    FLOW_REMOVED_PACKET,
+
+    /** Mapped to STATS-REQUEST message of OpenFlow. */
+    REQUEST_PACKET,
+
+    /** Mapped to STATS-REPLY message of OpenFlow. */
+    REPLY_PACKET,
 
     /** Cpu Utilization. */
-    CpuInfo,
+    CPU_INFO,
 
     /** Memory Utilization. */
-    MemoryInfo
+    MEMORY_INFO
 }
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricsFactory.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricsFactory.java
new file mode 100644
index 0000000..65bcb8d
--- /dev/null
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricsFactory.java
@@ -0,0 +1,237 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.cpman;
+
+import org.onlab.metrics.MetricsService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Singleton class to provide various control plane metrics to other components.
+ */
+public final class ControlMetricsFactory {
+    private static volatile ControlMetricsFactory uniqueInstance;
+
+    private MetricsService metricsService;
+    private boolean enableMonitor = false;
+
+    // define a set of MetricsAggregators
+    private MetricsAggregator cpuInfoMetric;
+    private MetricsAggregator memoryInfoMetric;
+    private Map<DeviceId, MetricsAggregator> inboundPacketMetrics;
+    private Map<DeviceId, MetricsAggregator> outboundPacketMetrics;
+    private Map<DeviceId, MetricsAggregator> flowmodPacketMetrics;
+    private Map<DeviceId, MetricsAggregator> flowrmvPacketMetrics;
+    private Map<DeviceId, MetricsAggregator> requestPacketMetrics;
+    private Map<DeviceId, MetricsAggregator> replyPacketMetrics;
+    private Set<DeviceId> deviceIds = new HashSet<>();
+
+    private ControlMetricsFactory(MetricsService metricsService, DeviceService deviceService) {
+        this.metricsService = metricsService;
+        registerMetrics();
+
+        deviceService.getDevices().forEach(d->deviceIds.add(d.id()));
+
+        addAllDeviceMetrics(deviceIds);
+    }
+
+    public static ControlMetricsFactory getInstance(MetricsService metricsService,
+                                                    DeviceService deviceService) {
+        if (uniqueInstance == null) {
+            synchronized (ControlMetricsFactory.class) {
+                if (uniqueInstance == null) {
+                    uniqueInstance = new ControlMetricsFactory(metricsService, deviceService);
+                }
+            }
+        }
+        return uniqueInstance;
+    }
+
+    /**
+     * Adds control metrics of a new device.
+     *
+     * @param deviceId {@link org.onosproject.net.DeviceId}
+     */
+    public void addMetricsByDeviceId(DeviceId deviceId) {
+        MetricsAggregator inbound = new MetricsAggregator(metricsService,
+                        ControlMetricType.INBOUND_PACKET, Optional.of(deviceId));
+        MetricsAggregator outbound = new MetricsAggregator(metricsService,
+                        ControlMetricType.OUTBOUND_PACKET, Optional.of(deviceId));
+        MetricsAggregator flowmod = new MetricsAggregator(metricsService,
+                        ControlMetricType.FLOW_MOD_PACKET, Optional.of(deviceId));
+        MetricsAggregator flowrmv = new MetricsAggregator(metricsService,
+                        ControlMetricType.FLOW_REMOVED_PACKET, Optional.of(deviceId));
+        MetricsAggregator request = new MetricsAggregator(metricsService,
+                        ControlMetricType.REQUEST_PACKET, Optional.of(deviceId));
+        MetricsAggregator reply = new MetricsAggregator(metricsService,
+                        ControlMetricType.REPLY_PACKET, Optional.of(deviceId));
+
+        inboundPacketMetrics.putIfAbsent(deviceId, inbound);
+        outboundPacketMetrics.putIfAbsent(deviceId, outbound);
+        flowmodPacketMetrics.putIfAbsent(deviceId, flowmod);
+        flowrmvPacketMetrics.putIfAbsent(deviceId, flowrmv);
+        requestPacketMetrics.putIfAbsent(deviceId, request);
+        replyPacketMetrics.putIfAbsent(deviceId, reply);
+
+        deviceIds.add(deviceId);
+    }
+
+    /**
+     * Removes control metrics of an existing device.
+     *
+     * @param deviceId {@link org.onosproject.net.DeviceId}
+     */
+    public void removeMetricsByDeviceId(DeviceId deviceId) {
+        inboundPacketMetrics.remove(deviceId);
+        outboundPacketMetrics.remove(deviceId);
+        flowmodPacketMetrics.remove(deviceId);
+        flowrmvPacketMetrics.remove(deviceId);
+        requestPacketMetrics.remove(deviceId);
+        replyPacketMetrics.remove(deviceId);
+
+        deviceIds.remove(deviceId);
+    }
+
+    public Set<DeviceId> getDeviceIds() {
+        return this.deviceIds;
+    }
+
+    /**
+     * Adds control metrics for all devices.
+     *
+     * @param deviceIds a set of deviceIds
+     */
+    public void addAllDeviceMetrics(Set<DeviceId> deviceIds) {
+        deviceIds.forEach(v -> addMetricsByDeviceId(v));
+    }
+
+    /**
+     * Returns monitoring status.
+     *
+     * @return monitoring status
+     */
+    public boolean isMonitor() {
+        return this.enableMonitor;
+    }
+
+    /**
+     * Enable control plane monitoring.
+     */
+    protected void startMonitor() {
+        this.enableMonitor = true;
+    }
+
+    /**
+     * Disable control plane monitoring.
+     */
+    protected void stopMonitor() {
+        this.enableMonitor = false;
+    }
+
+    /**
+     * Registers new control metrics.
+     */
+    protected void registerMetrics() {
+        cpuInfoMetric = new MetricsAggregator(metricsService,
+                        ControlMetricType.CPU_INFO, Optional.ofNullable(null));
+        memoryInfoMetric = new MetricsAggregator(metricsService,
+                        ControlMetricType.MEMORY_INFO, Optional.ofNullable(null));
+
+        inboundPacketMetrics = new ConcurrentHashMap<>();
+        outboundPacketMetrics = new ConcurrentHashMap<>();
+        flowmodPacketMetrics = new ConcurrentHashMap<>();
+        flowrmvPacketMetrics = new ConcurrentHashMap<>();
+        requestPacketMetrics = new ConcurrentHashMap<>();
+        replyPacketMetrics = new ConcurrentHashMap<>();
+    }
+
+    /**
+     * Unregisters all control metrics.
+     */
+    protected void unregisterMetrics() {
+        cpuInfoMetric.removeMetrics();
+        memoryInfoMetric.removeMetrics();
+
+        inboundPacketMetrics.clear();
+        outboundPacketMetrics.clear();
+        flowmodPacketMetrics.clear();
+        flowrmvPacketMetrics.clear();
+        requestPacketMetrics.clear();
+        replyPacketMetrics.clear();
+    }
+
+    public MetricsAggregator cpuInfoMetric() {
+        return cpuInfoMetric;
+    }
+
+    public MetricsAggregator memoryInfoMetric() {
+        return memoryInfoMetric;
+    }
+
+    public Map<DeviceId, MetricsAggregator> inboundPacketMetrics() {
+        return inboundPacketMetrics;
+    }
+
+    public Map<DeviceId, MetricsAggregator> outboundPacketMetrics() {
+        return outboundPacketMetrics;
+    }
+
+    public Map<DeviceId, MetricsAggregator> flowmodPacketMetrics() {
+        return flowmodPacketMetrics;
+    }
+
+    public Map<DeviceId, MetricsAggregator> flowrmvPacketMetrics() {
+        return flowrmvPacketMetrics;
+    }
+
+    public Map<DeviceId, MetricsAggregator> requestPacketMetrics() {
+        return requestPacketMetrics;
+    }
+
+    public Map<DeviceId, MetricsAggregator> replyPacketMetrics() {
+        return replyPacketMetrics;
+    }
+
+    public MetricsAggregator inboundPacketMetrics(DeviceId deviceId) {
+        return inboundPacketMetrics.get(deviceId);
+    }
+
+    public MetricsAggregator outboundPacketMetrics(DeviceId deviceId) {
+        return outboundPacketMetrics.get(deviceId);
+    }
+
+    public MetricsAggregator flowmodPacketMetrics(DeviceId deviceId) {
+        return flowmodPacketMetrics.get(deviceId);
+    }
+
+    public MetricsAggregator flowrmvPacketMetrics(DeviceId deviceId) {
+        return flowrmvPacketMetrics.get(deviceId);
+    }
+
+    public MetricsAggregator requestPacketMetrics(DeviceId deviceId) {
+        return requestPacketMetrics.get(deviceId);
+    }
+
+    public MetricsAggregator replyPacketMetrics(DeviceId deviceId) {
+        return replyPacketMetrics.get(deviceId);
+    }
+}
\ No newline at end of file
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricsObserver.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricsObserver.java
new file mode 100644
index 0000000..dae5b0f
--- /dev/null
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlMetricsObserver.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.cpman;
+
+import org.onosproject.net.DeviceId;
+
+import java.util.Optional;
+
+/**
+ * Control metrics observer interface.
+ */
+public interface ControlMetricsObserver {
+
+    /**
+     * Feeds the extracted value from MetricAggregator to back-end storage.
+     *
+     * @param metricsAggregator metric aggregator
+     * @param deviceId          device id {@link org.onosproject.net.DeviceId}
+     */
+    void feedMetrics(MetricsAggregator metricsAggregator, Optional<DeviceId> deviceId);
+}
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneManager.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneManager.java
index 5f3dee7..c01fabd 100644
--- a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneManager.java
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneManager.java
@@ -15,33 +15,118 @@
  */
 package org.onosproject.cpman;
 
-import com.sun.jndi.toolkit.ctx.ComponentContext;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.metrics.MetricsService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.device.DeviceService;
 import org.slf4j.Logger;
 
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Control plane management application.
  */
+@Component(immediate = true)
 public class ControlPlaneManager {
 
     private final Logger log = getLogger(getClass());
+    private Set<ControlMetricsObserver> controlMetricsObservers = new HashSet<>();
+    private ControlMetricsObserver cpObserver;
+
+    private ApplicationId appId;
+
+    private ControlMetricsFactory cmf;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MetricsService metricsService;
 
     @Activate
-    public void activate(ComponentContext context) {
+    public void activate() {
+        appId = coreService.registerApplication("org.onosproject.cpman");
 
+        cmf = ControlMetricsFactory.getInstance(metricsService, deviceService);
+        // currently disable monitoring by default
+        // cmf.startMonitor();
+
+        registerObserver();
+
+        log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
-
+        unregisterObserver();
+        cmf.stopMonitor();
+        log.info("Stopped");
     }
 
     @Modified
-    public void modified(ComponentContext context) {
-
+    public void modified() {
     }
-}
\ No newline at end of file
+
+    private void registerObserver() {
+        cpObserver = new DefaultControlMetricsObserver();
+        this.addControlMetricsObserver(cpObserver);
+    }
+
+    private void unregisterObserver() {
+        this.removeControlMetricsObserver(cpObserver);
+    }
+
+    private void executeMonitorTask() {
+
+        // TODO: execute monitoring task with 1 minute period
+        if (cmf.isMonitor()) {
+            controlMetricsObservers.forEach(observer -> {
+
+                // try to feed the CPU and memory stats
+                observer.feedMetrics(cmf.cpuInfoMetric(), Optional.ofNullable(null));
+                observer.feedMetrics(cmf.memoryInfoMetric(), Optional.ofNullable(null));
+
+                // try to feed the control message stats
+                cmf.getDeviceIds().forEach(v -> {
+                    observer.feedMetrics(cmf.inboundPacketMetrics(v), Optional.of(v));
+                    observer.feedMetrics(cmf.outboundPacketMetrics(v), Optional.of(v));
+                    observer.feedMetrics(cmf.flowmodPacketMetrics(v), Optional.of(v));
+                    observer.feedMetrics(cmf.flowrmvPacketMetrics(v), Optional.of(v));
+                    observer.feedMetrics(cmf.requestPacketMetrics(v), Optional.of(v));
+                    observer.feedMetrics(cmf.replyPacketMetrics(v), Optional.of(v));
+                });
+            });
+        }
+    }
+
+    /**
+     * Adds a new control metrics observer.
+     *
+     * @param cmObserver control metric observer instance
+     */
+    public void addControlMetricsObserver(ControlMetricsObserver cmObserver) {
+        controlMetricsObservers.add(cmObserver);
+    }
+
+    /**
+     * Removes an existing control metrics observer.
+     *
+     * @param cmObserver control metric observer instance
+     */
+    public void removeControlMetricsObserver(ControlMetricsObserver cmObserver) {
+        controlMetricsObservers.remove(cmObserver);
+    }
+}
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMonitor.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMonitor.java
new file mode 100644
index 0000000..2d8442b
--- /dev/null
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMonitor.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.cpman;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.net.DeviceId;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Control plane monitoring service class.
+ */
+@Component(immediate = true)
+@Service
+public class ControlPlaneMonitor implements ControlPlaneMonitorService {
+
+    private final Logger log = getLogger(getClass());
+
+    @Activate
+    public void activate() {
+    }
+
+    @Deactivate
+    public void deactivate() {
+    }
+
+    @Modified
+    public void modified(ComponentContext context) {
+    }
+
+    @Override
+    public void updateMetric(ControlMetric cpm, int updateInterval,
+                             Optional<DeviceId> deviceId) {
+    }
+
+    @Override
+    public ControlLoad getLoad(NodeId nodeId, ControlMetricType type,
+                               Optional<DeviceId> deviceId) {
+        return null;
+    }
+
+    @Override
+    public ControlLoad getLoad(NodeId nodeId, ControlMetricType type,
+                               Optional<DeviceId> deviceId, int duration, TimeUnit unit) {
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneStatsService.java b/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMonitorService.java
similarity index 69%
rename from apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneStatsService.java
rename to apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMonitorService.java
index 9fc2d7a..db00278 100644
--- a/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneStatsService.java
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/ControlPlaneMonitorService.java
@@ -24,28 +24,28 @@
 /**
  * Control Plane Statistics Service Interface.
  */
-public interface ControlPlaneStatsService {
+public interface ControlPlaneMonitorService {
 
     /**
-     * Add a new control plane metric value with a certain update interval.
+     * Adds a new control metric value with a certain update interval.
      *
-     * @param cpm            control plane metric (e.g., control message rate, cpu, memory, etc.)
+     * @param controlMetric  control plane metric (e.g., control message rate, cpu, memory, etc.)
      * @param updateInterval value update interval (time unit will be in minute)
      */
-    void updateMetric(ControlPlaneMetric cpm, int updateInterval);
+    void updateMetric(ControlMetric controlMetric, int updateInterval, Optional<DeviceId> deviceId);
 
     /**
-     * Obtain the control plane load of a specific device.
+     * Obtains the control plane load of a specific device.
      *
      * @param nodeId   node id {@link org.onosproject.cluster.NodeId}
      * @param type     control metric type
      * @param deviceId device id {@link org.onosproject.net.DeviceId}
      * @return control plane load
      */
-    ControlPlaneLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId);
+    ControlLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId);
 
     /**
-     * Obtain the control plane load of a specific device with a specific time duration.
+     * Obtains the control plane load of a specific device with a specific time duration.
      *
      * @param nodeId   node id {@link org.onosproject.cluster.NodeId}
      * @param type     control metric type
@@ -54,6 +54,6 @@
      * @param deviceId device id {@link org.onosproject.net.Device}
      * @return control plane load
      */
-    ControlPlaneLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId,
-                             int duration, TimeUnit unit);
+    ControlLoad getLoad(NodeId nodeId, ControlMetricType type, Optional<DeviceId> deviceId,
+                        int duration, TimeUnit unit);
 }
\ No newline at end of file
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/DefaultControlMetricsObserver.java b/apps/cpman/src/main/java/org/onosproject/cpman/DefaultControlMetricsObserver.java
new file mode 100644
index 0000000..79921d5
--- /dev/null
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/DefaultControlMetricsObserver.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.cpman;
+
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.net.DeviceId;
+
+import java.util.Optional;
+
+/**
+ * Default ControlMetricsObserver.
+ */
+public class DefaultControlMetricsObserver implements ControlMetricsObserver {
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ControlPlaneMonitorService controlPlaneMonitorService;
+
+    @Override
+    public void feedMetrics(MetricsAggregator ma, Optional<DeviceId> deviceId) {
+        MetricValue mv = new MetricValue((long) ma.getRate(), (long) ma.getLoad(), (long) ma.getCount());
+        ControlMetric cpm = new ControlMetric(ma.getMetricsType(), mv);
+        controlPlaneMonitorService.updateMetric(cpm, 1, deviceId);
+    }
+}
\ No newline at end of file
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/MetricValue.java b/apps/cpman/src/main/java/org/onosproject/cpman/MetricValue.java
new file mode 100644
index 0000000..3603b3a
--- /dev/null
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/MetricValue.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.cpman;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Primitive Metric Value.
+ */
+public class MetricValue {
+
+    private final long rate;
+    private final long load;
+    private final long count;
+
+    /**
+     * Constructor.
+     *
+     * @param rate rate
+     * @param load load
+     * @param count count
+     */
+    MetricValue(long rate, long load, long count) {
+        this.rate = rate;
+        this.load = load;
+        this.count = count;
+    }
+
+    public long getRate() {
+        return rate;
+    }
+
+    public long getLoad() {
+        return load;
+    }
+
+    public long getCount() {
+        return count;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof MetricValue) {
+            MetricValue other = (MetricValue) obj;
+            if (this.rate == other.rate &&
+                    this.load == other.load &&
+                    this.count == other.count) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 1004;
+        int result = super.hashCode();
+        result = prime * result + (int) this.rate;
+        result = prime * result + (int) this.load;
+        result = prime * result + (int) this.count;
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(getClass())
+                .add("rate", Long.toHexString(rate))
+                .add("load", Long.toHexString(load))
+                .add("count", Long.toHexString(count)).toString();
+    }
+}
diff --git a/apps/cpman/src/main/java/org/onosproject/cpman/MetricsAggregator.java b/apps/cpman/src/main/java/org/onosproject/cpman/MetricsAggregator.java
new file mode 100644
index 0000000..2367629
--- /dev/null
+++ b/apps/cpman/src/main/java/org/onosproject/cpman/MetricsAggregator.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.cpman;
+
+import com.codahale.metrics.Meter;
+import org.onlab.metrics.MetricsComponent;
+import org.onlab.metrics.MetricsFeature;
+import org.onlab.metrics.MetricsService;
+import org.onosproject.net.DeviceId;
+
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * An aggregator that aggregates a specific network or performance metrics via the metrics service.
+ */
+public class MetricsAggregator {
+
+    private Meter rateMeter;
+    private Meter countMeter;
+    private MetricsService metricsService;
+    private MetricsComponent metricsComponent;
+    private MetricsFeature metricsFeature;
+    private ControlMetricType metricsType;
+    private static final int EXECUTE_PERIOD_IN_SECOND = 60;
+    private static final String RATE_NAME = "rate";
+    private static final String COUNT_NAME = "count";
+
+    /**
+     * Constructs a new MetricsAggregator for aggregating a metric.
+     * Instantiates the metrics service
+     * Initializes all the general metrics for that object
+     *
+     * @param type      Control metric type
+     * @param deviceId  DeviceId
+     */
+    MetricsAggregator(MetricsService metricsService, ControlMetricType type, Optional<DeviceId> deviceId) {
+        String primitiveName = type.toString();
+        String objName = "all";
+        if (deviceId.isPresent()) {
+            objName = deviceId.toString();
+        }
+
+        checkNotNull(primitiveName, "Component name cannot be null");
+        checkNotNull(objName, "Feature name cannot be null");
+
+        this.metricsType = type;
+
+        this.metricsService = metricsService;
+        this.metricsComponent = metricsService.registerComponent(primitiveName);
+        this.metricsFeature = metricsComponent.registerFeature(objName);
+
+        this.rateMeter = metricsService.createMeter(metricsComponent, metricsFeature, RATE_NAME);
+        this.countMeter = metricsService.createMeter(metricsComponent, metricsFeature, COUNT_NAME);
+    }
+
+    public ControlMetricType getMetricsType() {
+        return metricsType;
+    }
+
+    /**
+     * Removes both rate and count metrics.
+     */
+    protected void removeMetrics() {
+        metricsService.removeMetric(metricsComponent, metricsFeature, RATE_NAME);
+        metricsService.removeMetric(metricsComponent, metricsFeature, COUNT_NAME);
+    }
+
+    /**
+     * Increments the meter rate by {@code n}, and the meter counter by 1.
+     *
+     * @param n Increment the meter rate by {@code n}.
+     */
+    public void increment(long n) {
+        rateMeter.mark(n);
+        countMeter.mark(1);
+    }
+
+    /**
+     * Obtains the average load value.
+     *
+     * @return load value
+     */
+    public double getLoad() {
+        return rateMeter.getOneMinuteRate() / countMeter.getOneMinuteRate();
+    }
+
+    /**
+     * Obtains the average meter rate within recent 1 minute.
+     *
+     * @return rate value
+     */
+    public double getRate() {
+        return rateMeter.getOneMinuteRate();
+    }
+
+    /**
+     * Obtains the average meter count within recent 1 minute.
+     *
+     * @return count value
+     */
+    public double getCount() {
+        return countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND;
+    }
+}
\ No newline at end of file
diff --git a/apps/cpman/src/test/java/org/onosproject/cpman/ControlPlaneManagerTest.java b/apps/cpman/src/test/java/org/onosproject/cpman/ControlPlaneManagerTest.java
new file mode 100644
index 0000000..ae2f3f3
--- /dev/null
+++ b/apps/cpman/src/test/java/org/onosproject/cpman/ControlPlaneManagerTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.cpman;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.metrics.MetricsServiceAdapter;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.net.device.DeviceServiceAdapter;
+
+/**
+ * Set of tests of the ONOS application component.
+ */
+public class ControlPlaneManagerTest {
+
+    private ControlPlaneManager cpMan;
+
+    /**
+     * Sets up the services required by the CPMan application.
+     */
+    @Before
+    public void setUp() {
+        cpMan = new ControlPlaneManager();
+        cpMan.coreService = new CoreServiceAdapter();
+        cpMan.deviceService = new DeviceServiceAdapter();
+        cpMan.metricsService = new MetricsServiceAdapter();
+        cpMan.activate();
+    }
+
+    /**
+     * Tears down the CPMan application.
+     */
+    @After
+    public void tearDown() {
+        cpMan.deactivate();
+    }
+
+    /**
+     * Tests the control metric aggregating function.
+     *
+     * @throws Exception if metric collection fails.
+     */
+    @Test
+    public void testMetricsAggregation() throws Exception {
+    }
+
+    /**
+     * Tests the control metric collecting function.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testMetricsCollection() throws Exception {
+    }
+}
diff --git a/utils/misc/src/test/java/org/onlab/metrics/MetricsServiceAdapter.java b/utils/misc/src/test/java/org/onlab/metrics/MetricsServiceAdapter.java
index c38b23c..9593feb 100644
--- a/utils/misc/src/test/java/org/onlab/metrics/MetricsServiceAdapter.java
+++ b/utils/misc/src/test/java/org/onlab/metrics/MetricsServiceAdapter.java
@@ -107,4 +107,4 @@
     @Override
     public void removeMatching(MetricFilter filter) {
     }
-}
+}
\ No newline at end of file