Jian Li
Committed by Thomas Vachuska

[ONOS-3648] Implement REST API for collecting system metrics

- Implement REST API for gathering system metrics
- Add unit test for REST API
- Add swagger doc for REST API

Change-Id: Iedd21a5f6ed14d595e4d35c1fa08270b48a5031e
Showing 24 changed files with 656 additions and 113 deletions
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2014-2016 Open Networking Laboratory
~ Copyright 2015-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.
......@@ -28,6 +28,8 @@
<artifactId>onos-app-cpman</artifactId>
<packaging>bundle</packaging>
<url>http://onosproject.org</url>
<description>Control Plane Management Application</description>
<properties>
......@@ -35,17 +37,42 @@
<onos.app.category>default</onos.app.category>
<onos.app.url>http://onosproject.org</onos.app.url>
<onos.app.readme>Control plane management application.</onos.app.readme>
<web.context>/onos/cpman</web.context>
<api.version>1.0.0</api.version>
<api.title>Control Plane Manager REST API</api.title>
<api.description>
APIs for interacting with the Control Plane Management application.
</api.description>
<api.package>org.onosproject.cpman.rest</api.package>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
<artifactId>onos-cli</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<groupId>org.onosproject</groupId>
<artifactId>onos-core-serializers</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
......@@ -54,6 +81,16 @@
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
......@@ -62,6 +99,49 @@
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-misc</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-osgi</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
......@@ -73,15 +153,40 @@
<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>
<extensions>true</extensions>
<configuration>
<instructions>
<_wab>src/main/webapp/</_wab>
<Include-Resource>
WEB-INF/classes/apidoc/swagger.json=target/swagger.json,
{maven-resources}
</Include-Resource>
<Bundle-SymbolicName>
${project.groupId}.${project.artifactId}
</Bundle-SymbolicName>
<Import-Package>
org.slf4j,
org.osgi.framework,
javax.ws.rs,
javax.ws.rs.core,
com.sun.jersey.api.core,
com.sun.jersey.spi.container.servlet,
com.sun.jersey.server.impl.container.servlet,
com.fasterxml.jackson.databind,
com.fasterxml.jackson.databind.node,
com.fasterxml.jackson.core,
org.apache.karaf.shell.commands,
org.apache.karaf.shell.console,
com.google.common.*,
org.onlab.packet.*,
org.onlab.rest.*,
org.onosproject.*,
org.onlab.util.*,
org.jboss.netty.util.*
</Import-Package>
<Web-ContextPath>${web.context}</Web-ContextPath>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
......
......@@ -23,7 +23,7 @@ public class ControlMetric {
private final ControlMetricType metricType;
private final MetricValue metricValue;
ControlMetric(ControlMetricType metricType, MetricValue metricValue) {
public ControlMetric(ControlMetricType metricType, MetricValue metricValue) {
this.metricType = metricType;
this.metricValue = metricValue;
}
......
/*
* Copyright 2015-2016 Open Networking Laboratory
* 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.
......@@ -16,117 +16,47 @@
package org.onosproject.cpman;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
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;
import org.slf4j.LoggerFactory;
/**
* Control plane management application.
* Skeletal ONOS application component.
*/
@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;
private final Logger log = LoggerFactory.getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MetricsService metricsService;
protected DeviceService deviceService;
private ApplicationId appId;
@Activate
public void activate() {
protected void activate() {
appId = coreService.registerApplication("org.onosproject.cpman");
cmf = ControlMetricsFactory.getInstance(metricsService, deviceService);
// currently disable monitoring by default
// cmf.startMonitor();
registerObserver();
deviceService.getAvailableDevices();
log.info("Started");
}
@Deactivate
public void deactivate() {
unregisterObserver();
cmf.stopMonitor();
protected void deactivate() {
log.info("Stopped");
}
@Modified
public void modified() {
}
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 -> {
// only OpenFlow messages are spontaneously monitored with
// 1 minute period. Other system metrics will be pushed from
// external monitoring agent through REST API
// feed the control message stats
cmf.getDeviceIds().forEach(v -> {
observer.feedMetrics(cmf.inboundPacket(v), Optional.of(v));
observer.feedMetrics(cmf.outboundPacket(v), Optional.of(v));
observer.feedMetrics(cmf.flowmodPacket(v), Optional.of(v));
observer.feedMetrics(cmf.flowrmvPacket(v), Optional.of(v));
observer.feedMetrics(cmf.requestPacket(v), Optional.of(v));
observer.feedMetrics(cmf.replyPacket(v), Optional.of(v));
});
});
}
protected void modified() {
}
/**
* 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);
}
}
......
......@@ -16,9 +16,9 @@
package org.onosproject.cpman;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
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;
......@@ -41,6 +41,7 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
@Activate
public void activate() {
}
@Deactivate
......@@ -52,12 +53,12 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
}
@Override
public void updateMetric(ControlMetric cpm, int updateInterval,
public void updateMetric(ControlMetric cpm, Integer updateInterval,
Optional<DeviceId> deviceId) {
}
@Override
public void updateMetric(ControlMetric controlMetric, int updateInterval,
public void updateMetric(ControlMetric controlMetric, Integer updateInterval,
String resourceName) {
}
......
......@@ -33,7 +33,7 @@ public interface ControlPlaneMonitorService {
* @param updateInterval value update interval (time unit will be in minute)
* @param deviceId {@link org.onosproject.net.DeviceId}
*/
void updateMetric(ControlMetric controlMetric, int updateInterval, Optional<DeviceId> deviceId);
void updateMetric(ControlMetric controlMetric, Integer updateInterval, Optional<DeviceId> deviceId);
/**
* Adds a new control metric value with a certain update interval.
......@@ -42,7 +42,7 @@ public interface ControlPlaneMonitorService {
* @param updateInterval value update interval (time unit will be in minute)
* @param resourceName resource name
*/
void updateMetric(ControlMetric controlMetric, int updateInterval, String resourceName);
void updateMetric(ControlMetric controlMetric, Integer updateInterval, String resourceName);
/**
* Obtains the control plane load of a specific device.
......
......@@ -31,14 +31,22 @@ public class DefaultControlMetricsObserver implements ControlMetricsObserver {
@Override
public void feedMetrics(MetricsAggregator ma, Optional<DeviceId> deviceId) {
MetricValue mv = new MetricValue((long) ma.getRate(), (long) ma.getLoad(), (long) ma.getCount());
MetricValue mv = new MetricValue.Builder()
.rate(ma.getRate())
.count(ma.getCount())
.load(ma.getLoad())
.add();
ControlMetric cm = new ControlMetric(ma.getMetricsType(), mv);
controlPlaneMonitorService.updateMetric(cm, 1, deviceId);
}
@Override
public void feedMetrics(MetricsAggregator ma, String resourceName) {
MetricValue mv = new MetricValue((long) ma.getRate(), (long) ma.getLoad(), (long) ma.getCount());
MetricValue mv = new MetricValue.Builder()
.rate(ma.getRate())
.count(ma.getCount())
.load(ma.getLoad())
.add();
ControlMetric cm = new ControlMetric(ma.getMetricsType(), mv);
controlPlaneMonitorService.updateMetric(cm, 1, resourceName);
}
......
......@@ -20,7 +20,7 @@ import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Primitive Metric Value.
*/
public class MetricValue {
public final class MetricValue {
private final long rate;
private final long load;
......@@ -33,24 +33,90 @@ public class MetricValue {
* @param load load
* @param count count
*/
MetricValue(long rate, long load, long count) {
private MetricValue(long rate, long load, long count) {
this.rate = rate;
this.load = load;
this.count = count;
}
/**
* Returns rate value.
*
* @return rate
*/
public long getRate() {
return rate;
}
/**
* Returns load value.
*
* @return load
*/
public long getLoad() {
return load;
}
/**
* Returns count value.
*
* @return cound
*/
public long getCount() {
return count;
}
/**
* MetricValue builder class.
*/
public static final class Builder {
private long rate;
private long load;
private long count;
/**
* Sets rate value.
*
* @param rate rate value
* @return Builder object
*/
public Builder rate(long rate) {
this.rate = rate;
return this;
}
/**
* Sets load value.
*
* @param load load value
* @return Builder object
*/
public Builder load(long load) {
this.load = load;
return this;
}
/**
* Sets count value.
*
* @param count count value
* @return Builder object
*/
public Builder count(long count) {
this.count = count;
return this;
}
/**
* Builds a MetricValue object.
*
* @return MetricValue object
*/
public MetricValue add() {
return new MetricValue(rate, load, count);
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
......
......@@ -134,8 +134,8 @@ public class MetricsAggregator {
*
* @return load value
*/
public double getLoad() {
return rateMeter.getOneMinuteRate() / countMeter.getOneMinuteRate();
public long getLoad() {
return (long) rateMeter.getOneMinuteRate() / (long) countMeter.getOneMinuteRate();
}
/**
......@@ -143,8 +143,8 @@ public class MetricsAggregator {
*
* @return rate value
*/
public double getRate() {
return rateMeter.getOneMinuteRate();
public long getRate() {
return (long) rateMeter.getOneMinuteRate();
}
/**
......@@ -152,7 +152,7 @@ public class MetricsAggregator {
*
* @return count value
*/
public double getCount() {
return countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND;
public long getCount() {
return (long) countMeter.getOneMinuteRate() * EXECUTE_PERIOD_IN_SECOND;
}
}
\ No newline at end of file
......
/*
* 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 org.onlab.rest.AbstractWebApplication;
import java.util.Set;
/**
* CPMan REST APIs web application.
*/
public class CPManWebApplication extends AbstractWebApplication {
@Override
public Set<Class<?>> getClasses() {
return getClasses(ControlMetricsCollectorWebResource.class);
}
}
/*
* 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.
*/
/**
* REST APIs for the control plane monitor.
*/
package org.onosproject.cpman.rest;
\ No newline at end of file
{
"type": "object",
"required": [
"cpuLoad",
"totalCpuTime",
"sysCpuTime",
"userCpuTime",
"cpuIdleTime"
],
"properties": {
"cpuLoad": {
"type": "integer",
"format": "int64",
"example": "50"
},
"totalCpuTime": {
"type": "integer",
"format": "int64",
"example": "2000"
},
"sysCpuTime": {
"type": "integer",
"format": "int64",
"example": "2000"
},
"userCpuTime": {
"type": "integer",
"format": "int64",
"example": "2000"
},
"cpuIdleTime": {
"type": "integer",
"format": "int64",
"example": "2000"
}
}
}
\ No newline at end of file
{
"type": "object",
"required": [
"readBytes",
"writeBytes"
],
"properties": {
"readBytes": {
"type": "integer",
"format": "int64",
"example": "500"
},
"writeBytes": {
"type": "integer",
"format": "int64",
"example": "300"
}
}
}
\ No newline at end of file
{
"type": "object",
"required": [
"memoryUsedPercentage",
"memoryFreePercentage",
"memoryUsed",
"memoryFree"
],
"properties": {
"memoryUsedPercentage": {
"type": "integer",
"format": "int64",
"example": "30"
},
"memoryFreePercentage": {
"type": "integer",
"format": "int64",
"example": "70"
},
"memoryUsed": {
"type": "integer",
"format": "int64",
"example": "1024"
},
"memoryFree": {
"type": "integer",
"format": "int64",
"example": "2048"
}
}
}
\ No newline at end of file
{
"type": "object",
"required": [
"incomingBytes",
"outgoingBytes",
"incomingPackets",
"outgoingPackets"
],
"properties": {
"incomingBytes": {
"type": "integer",
"format": "int64",
"example": "1024"
},
"outgoingBytes": {
"type": "integer",
"format": "int64",
"example": "1024"
},
"incomingPackets": {
"type": "integer",
"format": "int64",
"example": "1000"
},
"outgoingPackets": {
"type": "integer",
"format": "int64",
"example": "2000"
}
}
}
\ No newline at end of file
{
"type": "object",
"required": [
"numOfCores",
"numOfCpus",
"cpuSpeed",
"totalMemory"
],
"properties": {
"numOfCores": {
"type": "integer",
"format": "int64",
"example": "6"
},
"numOfCpus": {
"type": "integer",
"format": "int64",
"example": "2"
},
"cpuSpeed": {
"type": "integer",
"format": "int64",
"example": "2048"
},
"totalMemory": {
"type": "integer",
"format": "int64",
"example": "4096"
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="ONOS" version="2.5">
<display-name>Control Plane REST API v1.0</display-name>
<security-constraint>
<web-resource-collection>
<web-resource-name>Secured</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>admin</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>karaf</realm-name>
</login-config>
<servlet>
<servlet-name>JAX-RS Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.onosproject.cpman.rest.CPManWebApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
......@@ -18,7 +18,6 @@ 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;
......@@ -37,7 +36,6 @@ public class ControlPlaneManagerTest {
cpMan = new ControlPlaneManager();
cpMan.coreService = new CoreServiceAdapter();
cpMan.deviceService = new DeviceServiceAdapter();
cpMan.metricsService = new MetricsServiceAdapter();
cpMan.activate();
}
......
/*
* 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();
}
}
{
"cpuLoad": 50,
"totalCpuTime": 2000,
"sysCpuTime": 2000,
"userCpuTime": 2000,
"cpuIdleTime": 2000
}
\ No newline at end of file
{
"readBytes": 500,
"writeBytes": 300
}
\ No newline at end of file
{
"memoryUsedPercentage": 30,
"memoryFreePercentage": 70,
"memoryUsed": 1024,
"memoryFree": 2048
}
\ No newline at end of file
{
"incomingBytes": 1024,
"outgoingBytes": 1024,
"incomingPackets": 1000,
"outgoingPackets": 2000
}
\ No newline at end of file
{
"numOfCores": 6,
"numOfCpus": 2,
"cpuSpeed": 2048,
"totalMemory": 4096
}
\ No newline at end of file