Add resource name param to diskMetrics and networkMetrics method

- Enable to add metrics of multiple disks
- Enable to add metrics of multiple network interfaces

Change-Id: I6e91d63b7a02f0d2f63fe445712a23e72d208789
This commit is contained in:
Jian Li 2016-02-01 22:40:42 -08:00 committed by Gerrit Code Review
parent 1beb60c575
commit ba6b1172d8
6 changed files with 175 additions and 93 deletions

View File

@ -16,12 +16,13 @@
package org.onosproject.cpman.rest; package org.onosproject.cpman.rest;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.cpman.ControlMetric; import org.onosproject.cpman.ControlMetric;
import org.onosproject.cpman.ControlMetricType; import org.onosproject.cpman.ControlMetricType;
import org.onosproject.cpman.impl.ControlMetricsSystemSpec;
import org.onosproject.cpman.ControlPlaneMonitorService; import org.onosproject.cpman.ControlPlaneMonitorService;
import org.onosproject.cpman.MetricValue; import org.onosproject.cpman.MetricValue;
import org.onosproject.cpman.impl.ControlMetricsSystemSpec;
import org.onosproject.rest.AbstractWebResource; import org.onosproject.rest.AbstractWebResource;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
@ -34,6 +35,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Optional; import java.util.Optional;
import static org.onlab.util.Tools.nullIsIllegal;
/** /**
* Collect control plane metrics. * Collect control plane metrics.
*/ */
@ -43,6 +46,7 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class); final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
public static final int UPDATE_INTERVAL = 1; // 1 minute update interval public static final int UPDATE_INTERVAL = 1; // 1 minute update interval
public static final String INVALID_SYSTEM_SPECS = "Invalid system specifications"; public static final String INVALID_SYSTEM_SPECS = "Invalid system specifications";
public static final String INVALID_RESOURCE_NAME = "Invalid resource name";
/** /**
* Collects CPU metrics. * Collects CPU metrics.
@ -166,24 +170,29 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response diskMetrics(InputStream stream) { public Response diskMetrics(InputStream stream) {
ObjectNode root = mapper().createObjectNode(); ObjectNode root = mapper().createObjectNode();
ControlMetric cm; final ControlMetric[] cm = new ControlMetric[1];
try { try {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
ArrayNode diskRes = (ArrayNode) jsonTree.get("disks");
diskRes.forEach(node-> {
JsonNode resourceName = node.get("resourceName");
nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
JsonNode readBytes = jsonTree.get("readBytes"); JsonNode readBytes = jsonTree.get("readBytes");
JsonNode writeBytes = jsonTree.get("writeBytes"); JsonNode writeBytes = jsonTree.get("writeBytes");
if (readBytes != null) { if (readBytes != null) {
cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES, cm[0] = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
new MetricValue.Builder().load(readBytes.asLong()).add()); new MetricValue.Builder().load(readBytes.asLong()).add());
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
} }
if (writeBytes != null) { if (writeBytes != null) {
cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, cm[0] = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
new MetricValue.Builder().load(writeBytes.asLong()).add()); new MetricValue.Builder().load(writeBytes.asLong()).add());
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
} }
});
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException(e.getMessage()); throw new IllegalArgumentException(e.getMessage());
} }
@ -203,45 +212,49 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response networkMetrics(InputStream stream) { public Response networkMetrics(InputStream stream) {
ObjectNode root = mapper().createObjectNode(); ObjectNode root = mapper().createObjectNode();
ControlMetric cm; final ControlMetric[] cm = new ControlMetric[1];
try { try {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
ArrayNode networkRes = (ArrayNode) jsonTree.get("networks");
networkRes.forEach(node -> {
JsonNode resourceName = node.get("resourceName");
nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
JsonNode inBytes = jsonTree.get("incomingBytes"); JsonNode inBytes = jsonTree.get("incomingBytes");
JsonNode outBytes = jsonTree.get("outgoingBytes"); JsonNode outBytes = jsonTree.get("outgoingBytes");
JsonNode inPackets = jsonTree.get("incomingPackets"); JsonNode inPackets = jsonTree.get("incomingPackets");
JsonNode outPackets = jsonTree.get("outgoingPackets"); JsonNode outPackets = jsonTree.get("outgoingPackets");
if (inBytes != null) { if (inBytes != null) {
cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
new MetricValue.Builder().load(inBytes.asLong()).add()); new MetricValue.Builder().load(inBytes.asLong()).add());
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
} }
if (outBytes != null) { if (outBytes != null) {
cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
new MetricValue.Builder().load(outBytes.asLong()).add()); new MetricValue.Builder().load(outBytes.asLong()).add());
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
} }
if (inPackets != null) { if (inPackets != null) {
cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
new MetricValue.Builder().load(inPackets.asLong()).add()); new MetricValue.Builder().load(inPackets.asLong()).add());
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
} }
if (outPackets != null) { if (outPackets != null) {
cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
new MetricValue.Builder().load(outPackets.asLong()).add()); new MetricValue.Builder().load(outPackets.asLong()).add());
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
} }
});
} catch (IOException e) { } catch (IOException e) {
throw new IllegalArgumentException(e.getMessage()); throw new IllegalArgumentException(e.getMessage());
} }
return ok(root).build(); return ok(root).build();
} }
/** /**
* Collects system specifications. * Collects system specifications.
* The system specs include the various control metrics * The system specs include the various control metrics

View File

@ -1,10 +1,29 @@
{ {
"type": "object", "type": "object",
"title": "disks",
"required": [ "required": [
"disks"
],
"properties": {
"disks": {
"type": "array",
"xml": {
"name": "disks",
"wrapped": true
},
"items": {
"type": "object",
"title": "disks",
"required": [
"resourceName",
"readBytes", "readBytes",
"writeBytes" "writeBytes"
], ],
"properties": { "properties": {
"resourceName": {
"type": "string",
"example": "disk1"
},
"readBytes": { "readBytes": {
"type": "integer", "type": "integer",
"format": "int64", "format": "int64",
@ -16,4 +35,7 @@
"example": "300" "example": "300"
} }
} }
}
}
}
} }

View File

@ -1,12 +1,31 @@
{ {
"type": "object", "type": "object",
"title": "networks",
"required": [ "required": [
"networks"
],
"properties": {
"networks": {
"type": "array",
"xml": {
"name": "networks",
"wrapped": true
},
"items": {
"type": "object",
"title": "networks",
"required": [
"resourceName",
"incomingBytes", "incomingBytes",
"outgoingBytes", "outgoingBytes",
"incomingPackets", "incomingPackets",
"outgoingPackets" "outgoingPackets"
], ],
"properties": { "properties": {
"resourceName": {
"type": "string",
"example": "eth0"
},
"incomingBytes": { "incomingBytes": {
"type": "integer", "type": "integer",
"format": "int64", "format": "int64",
@ -28,4 +47,7 @@
"example": "2000" "example": "2000"
} }
} }
}
}
}
} }

View File

@ -37,6 +37,7 @@ import java.net.ServerSocket;
import java.util.Optional; import java.util.Optional;
import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyString;
import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.replay;
@ -84,19 +85,17 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest {
} }
@Test @Test
public void testDiskMetricsPost() { public void testDiskMetricsWithNullName() {
mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), anyString());
(Optional<DeviceId>) anyObject()); expectLastCall().times(4);
expectLastCall().times(2);
replay(mockControlPlaneMonitorService); replay(mockControlPlaneMonitorService);
basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics"); basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics");
} }
@Test @Test
public void testNetworkMetricsPost() { public void testNetworkMetricsWithNullName() {
mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), anyString());
(Optional<DeviceId>) anyObject()); expectLastCall().times(8);
expectLastCall().times(4);
replay(mockControlPlaneMonitorService); replay(mockControlPlaneMonitorService);
basePostTest("network-metrics-post.json", PREFIX + "/network_metrics"); basePostTest("network-metrics-post.json", PREFIX + "/network_metrics");
} }
@ -106,16 +105,20 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest {
basePostTest("system-spec-post.json", PREFIX + "/system_specs"); basePostTest("system-spec-post.json", PREFIX + "/system_specs");
} }
private void basePostTest(String jsonFile, String path) { private ClientResponse baseTest(String jsonFile, String path) {
final WebResource rs = resource(); final WebResource rs = resource();
InputStream jsonStream = ControlMetricsCollectorResourceTest.class InputStream jsonStream = ControlMetricsCollectorResourceTest.class
.getResourceAsStream(jsonFile); .getResourceAsStream(jsonFile);
assertThat(jsonStream, notNullValue()); assertThat(jsonStream, notNullValue());
ClientResponse response = rs.path(path) return rs.path(path)
.type(MediaType.APPLICATION_JSON_TYPE) .type(MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class, jsonStream); .post(ClientResponse.class, jsonStream);
}
private void basePostTest(String jsonFile, String path) {
ClientResponse response = baseTest(jsonFile, path);
assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
} }

View File

@ -1,4 +1,14 @@
{ {
"disks": [
{
"resourceName": "disk1",
"readBytes": 500, "readBytes": 500,
"writeBytes": 300 "writeBytes": 300
},
{
"resourceName": "disk2",
"readBytes": 500,
"writeBytes": 300
}
]
} }

View File

@ -1,6 +1,18 @@
{ {
"networks": [
{
"resourceName": "eth0",
"incomingBytes": 1024, "incomingBytes": 1024,
"outgoingBytes": 1024, "outgoingBytes": 1024,
"incomingPackets": 1000, "incomingPackets": 1000,
"outgoingPackets": 2000 "outgoingPackets": 2000
},
{
"resourceName": "eth1",
"incomingBytes": 1024,
"outgoingBytes": 1024,
"incomingPackets": 1000,
"outgoingPackets": 2000
}
]
} }