Split CPMan into api and app submodules

Change-Id: Iacddea67ea0f7189ab918cf9e2a7a414100fc503
diff --git a/apps/cpman/app/src/test/java/org/onosproject/cpman/impl/ControlPlaneManagerTest.java b/apps/cpman/app/src/test/java/org/onosproject/cpman/impl/ControlPlaneManagerTest.java
new file mode 100644
index 0000000..3fc6675
--- /dev/null
+++ b/apps/cpman/app/src/test/java/org/onosproject/cpman/impl/ControlPlaneManagerTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2016 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.impl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+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.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 {
+    }
+}
\ No newline at end of file
diff --git a/apps/cpman/app/src/test/java/org/onosproject/cpman/rest/ControlMetricsCollectorResourceTest.java b/apps/cpman/app/src/test/java/org/onosproject/cpman/rest/ControlMetricsCollectorResourceTest.java
new file mode 100644
index 0000000..e61873e
--- /dev/null
+++ b/apps/cpman/app/src/test/java/org/onosproject/cpman/rest/ControlMetricsCollectorResourceTest.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2016 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.rest;
+
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.spi.container.servlet.ServletContainer;
+import com.sun.jersey.test.framework.AppDescriptor;
+import com.sun.jersey.test.framework.JerseyTest;
+import com.sun.jersey.test.framework.WebAppDescriptor;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.rest.BaseResource;
+import org.onosproject.cpman.ControlPlaneMonitorService;
+import org.onosproject.net.DeviceId;
+
+import javax.ws.rs.core.MediaType;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.ServerSocket;
+import java.util.Optional;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit test for ControlMetricsCollector.
+ */
+public class ControlMetricsCollectorResourceTest extends JerseyTest {
+
+    final ControlPlaneMonitorService mockControlPlaneMonitorService =
+                                     createMock(ControlPlaneMonitorService.class);
+
+    /**
+     * Sets up the global values for all the tests.
+     */
+    @Before
+    public void setUpTest() {
+        ServiceDirectory testDirectory =
+                new TestServiceDirectory()
+                        .add(ControlPlaneMonitorService.class, mockControlPlaneMonitorService);
+        BaseResource.setServiceDirectory(testDirectory);
+    }
+
+    @Test
+    public void testCpuMetricsPost() {
+        mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(),
+                (Optional<DeviceId>) anyObject());
+        expectLastCall().times(5);
+        replay(mockControlPlaneMonitorService);
+        basePostTest("cpu-metrics-post.json", "cpman/cpumetrics");
+    }
+
+    @Test
+    public void testMemoryMetricsPost() {
+        mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(),
+                (Optional<DeviceId>) anyObject());
+        expectLastCall().times(4);
+        replay(mockControlPlaneMonitorService);
+        basePostTest("memory-metrics-post.json", "cpman/memorymetrics");
+    }
+
+    @Test
+    public void testDiskMetricsPost() {
+        mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(),
+                (Optional<DeviceId>) anyObject());
+        expectLastCall().times(2);
+        replay(mockControlPlaneMonitorService);
+        basePostTest("disk-metrics-post.json", "cpman/diskmetrics");
+    }
+
+    @Test
+    public void testNetworkMetricsPost() {
+        mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(),
+                (Optional<DeviceId>) anyObject());
+        expectLastCall().times(4);
+        replay(mockControlPlaneMonitorService);
+        basePostTest("network-metrics-post.json", "cpman/networkmetrics");
+    }
+
+    @Test
+    public void testSystemSpecsPost() {
+        basePostTest("system-spec-post.json", "cpman/systemspecs");
+    }
+
+    private void basePostTest(String jsonFile, String path) {
+        final WebResource rs = resource();
+        InputStream jsonStream = ControlMetricsCollectorResourceTest.class
+                .getResourceAsStream(jsonFile);
+
+        assertThat(jsonStream, notNullValue());
+
+        ClientResponse response = rs.path(path)
+                .type(MediaType.APPLICATION_JSON_TYPE)
+                .post(ClientResponse.class, jsonStream);
+        assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
+    }
+
+    public ControlMetricsCollectorResourceTest() {
+        super(new WebAppDescriptor.Builder("javax.ws.rs.Application",
+                CPManWebApplication.class.getCanonicalName())
+                .servletClass(ServletContainer.class).build());
+    }
+
+    /**
+     * Assigns an available port for the test.
+     *
+     * @param defaultPort If a port cannot be determined, this one is used.
+     * @return free port
+     */
+    @Override
+    public int getPort(int defaultPort) {
+        try {
+            ServerSocket socket = new ServerSocket(0);
+            socket.setReuseAddress(true);
+            int port = socket.getLocalPort();
+            socket.close();
+            return port;
+        } catch (IOException ioe) {
+            return defaultPort;
+        }
+    }
+
+    @Override
+    public AppDescriptor configure() {
+        return new WebAppDescriptor.Builder("org.onosproject.cpman.rest").build();
+    }
+}
diff --git a/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/cpu-metrics-post.json b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/cpu-metrics-post.json
new file mode 100644
index 0000000..0f6c7bc
--- /dev/null
+++ b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/cpu-metrics-post.json
@@ -0,0 +1,7 @@
+{
+  "cpuLoad": 50,
+  "totalCpuTime": 2000,
+  "sysCpuTime": 2000,
+  "userCpuTime": 2000,
+  "cpuIdleTime": 2000
+}
\ No newline at end of file
diff --git a/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/disk-metrics-post.json b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/disk-metrics-post.json
new file mode 100644
index 0000000..ebdf00a
--- /dev/null
+++ b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/disk-metrics-post.json
@@ -0,0 +1,4 @@
+{
+  "readBytes": 500,
+  "writeBytes": 300
+}
\ No newline at end of file
diff --git a/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/memory-metrics-post.json b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/memory-metrics-post.json
new file mode 100644
index 0000000..8bce870
--- /dev/null
+++ b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/memory-metrics-post.json
@@ -0,0 +1,6 @@
+{
+  "memoryUsedPercentage": 30,
+  "memoryFreePercentage": 70,
+  "memoryUsed": 1024,
+  "memoryFree": 2048
+}
\ No newline at end of file
diff --git a/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/network-metrics-post.json b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/network-metrics-post.json
new file mode 100644
index 0000000..bdf7c8d
--- /dev/null
+++ b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/network-metrics-post.json
@@ -0,0 +1,6 @@
+{
+  "incomingBytes": 1024,
+  "outgoingBytes": 1024,
+  "incomingPackets": 1000,
+  "outgoingPackets": 2000
+}
\ No newline at end of file
diff --git a/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/system-spec-post.json b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/system-spec-post.json
new file mode 100644
index 0000000..5b6f0cc
--- /dev/null
+++ b/apps/cpman/app/src/test/resources/org/onosproject/cpman/rest/system-spec-post.json
@@ -0,0 +1,6 @@
+{
+  "numOfCores": 6,
+  "numOfCpus": 2,
+  "cpuSpeed": 2048,
+  "totalMemory": 4096
+}
\ No newline at end of file