mirror of
https://github.com/opennetworkinglab/onos.git
synced 2025-11-03 09:41:14 +01:00
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:
parent
1beb60c575
commit
ba6b1172d8
@ -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.
|
||||||
@ -68,13 +72,13 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
|
|||||||
|
|
||||||
if (cpuLoadJson != null) {
|
if (cpuLoadJson != null) {
|
||||||
cm = new ControlMetric(ControlMetricType.CPU_LOAD,
|
cm = new ControlMetric(ControlMetricType.CPU_LOAD,
|
||||||
new MetricValue.Builder().load(cpuLoadJson.asLong()).add());
|
new MetricValue.Builder().load(cpuLoadJson.asLong()).add());
|
||||||
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
|
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (totalCpuTimeJson != null) {
|
if (totalCpuTimeJson != null) {
|
||||||
cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
|
cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
|
||||||
new MetricValue.Builder().load(totalCpuTimeJson.asLong()).add());
|
new MetricValue.Builder().load(totalCpuTimeJson.asLong()).add());
|
||||||
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
|
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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);
|
||||||
JsonNode readBytes = jsonTree.get("readBytes");
|
ArrayNode diskRes = (ArrayNode) jsonTree.get("disks");
|
||||||
JsonNode writeBytes = jsonTree.get("writeBytes");
|
diskRes.forEach(node-> {
|
||||||
|
JsonNode resourceName = node.get("resourceName");
|
||||||
|
nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
|
||||||
|
|
||||||
if (readBytes != null) {
|
JsonNode readBytes = jsonTree.get("readBytes");
|
||||||
cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
|
JsonNode writeBytes = jsonTree.get("writeBytes");
|
||||||
new MetricValue.Builder().load(readBytes.asLong()).add());
|
|
||||||
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (writeBytes != null) {
|
if (readBytes != null) {
|
||||||
cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
|
cm[0] = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
|
||||||
new MetricValue.Builder().load(writeBytes.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) {
|
||||||
|
cm[0] = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
|
||||||
|
new MetricValue.Builder().load(writeBytes.asLong()).add());
|
||||||
|
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);
|
||||||
JsonNode inBytes = jsonTree.get("incomingBytes");
|
ArrayNode networkRes = (ArrayNode) jsonTree.get("networks");
|
||||||
JsonNode outBytes = jsonTree.get("outgoingBytes");
|
networkRes.forEach(node -> {
|
||||||
JsonNode inPackets = jsonTree.get("incomingPackets");
|
JsonNode resourceName = node.get("resourceName");
|
||||||
JsonNode outPackets = jsonTree.get("outgoingPackets");
|
nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
|
||||||
|
|
||||||
if (inBytes != null) {
|
JsonNode inBytes = jsonTree.get("incomingBytes");
|
||||||
cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
|
JsonNode outBytes = jsonTree.get("outgoingBytes");
|
||||||
new MetricValue.Builder().load(inBytes.asLong()).add());
|
JsonNode inPackets = jsonTree.get("incomingPackets");
|
||||||
service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
|
JsonNode outPackets = jsonTree.get("outgoingPackets");
|
||||||
}
|
|
||||||
|
|
||||||
if (outBytes != null) {
|
if (inBytes != null) {
|
||||||
cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
|
cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
|
||||||
new MetricValue.Builder().load(outBytes.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 (inPackets != null) {
|
if (outBytes != null) {
|
||||||
cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
|
cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
|
||||||
new MetricValue.Builder().load(inPackets.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 (outPackets != null) {
|
if (inPackets != null) {
|
||||||
cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
|
cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
|
||||||
new MetricValue.Builder().load(outPackets.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) {
|
||||||
|
cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
|
||||||
|
new MetricValue.Builder().load(outPackets.asLong()).add());
|
||||||
|
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
|
||||||
@ -283,4 +296,4 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
|
|||||||
}
|
}
|
||||||
return ok(root).build();
|
return ok(root).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,19 +1,41 @@
|
|||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
"title": "disks",
|
||||||
"required": [
|
"required": [
|
||||||
"readBytes",
|
"disks"
|
||||||
"writeBytes"
|
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"readBytes": {
|
"disks": {
|
||||||
"type": "integer",
|
"type": "array",
|
||||||
"format": "int64",
|
"xml": {
|
||||||
"example": "500"
|
"name": "disks",
|
||||||
},
|
"wrapped": true
|
||||||
"writeBytes": {
|
},
|
||||||
"type": "integer",
|
"items": {
|
||||||
"format": "int64",
|
"type": "object",
|
||||||
"example": "300"
|
"title": "disks",
|
||||||
|
"required": [
|
||||||
|
"resourceName",
|
||||||
|
"readBytes",
|
||||||
|
"writeBytes"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"resourceName": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "disk1"
|
||||||
|
},
|
||||||
|
"readBytes": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64",
|
||||||
|
"example": "500"
|
||||||
|
},
|
||||||
|
"writeBytes": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64",
|
||||||
|
"example": "300"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,31 +1,53 @@
|
|||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
"title": "networks",
|
||||||
"required": [
|
"required": [
|
||||||
"incomingBytes",
|
"networks"
|
||||||
"outgoingBytes",
|
|
||||||
"incomingPackets",
|
|
||||||
"outgoingPackets"
|
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"incomingBytes": {
|
"networks": {
|
||||||
"type": "integer",
|
"type": "array",
|
||||||
"format": "int64",
|
"xml": {
|
||||||
"example": "1024"
|
"name": "networks",
|
||||||
},
|
"wrapped": true
|
||||||
"outgoingBytes": {
|
},
|
||||||
"type": "integer",
|
"items": {
|
||||||
"format": "int64",
|
"type": "object",
|
||||||
"example": "1024"
|
"title": "networks",
|
||||||
},
|
"required": [
|
||||||
"incomingPackets": {
|
"resourceName",
|
||||||
"type": "integer",
|
"incomingBytes",
|
||||||
"format": "int64",
|
"outgoingBytes",
|
||||||
"example": "1000"
|
"incomingPackets",
|
||||||
},
|
"outgoingPackets"
|
||||||
"outgoingPackets": {
|
],
|
||||||
"type": "integer",
|
"properties": {
|
||||||
"format": "int64",
|
"resourceName": {
|
||||||
"example": "2000"
|
"type": "string",
|
||||||
|
"example": "eth0"
|
||||||
|
},
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,14 @@
|
|||||||
{
|
{
|
||||||
"readBytes": 500,
|
"disks": [
|
||||||
"writeBytes": 300
|
{
|
||||||
|
"resourceName": "disk1",
|
||||||
|
"readBytes": 500,
|
||||||
|
"writeBytes": 300
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"resourceName": "disk2",
|
||||||
|
"readBytes": 500,
|
||||||
|
"writeBytes": 300
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
@ -1,6 +1,18 @@
|
|||||||
{
|
{
|
||||||
"incomingBytes": 1024,
|
"networks": [
|
||||||
"outgoingBytes": 1024,
|
{
|
||||||
"incomingPackets": 1000,
|
"resourceName": "eth0",
|
||||||
"outgoingPackets": 2000
|
"incomingBytes": 1024,
|
||||||
|
"outgoingBytes": 1024,
|
||||||
|
"incomingPackets": 1000,
|
||||||
|
"outgoingPackets": 2000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"resourceName": "eth1",
|
||||||
|
"incomingBytes": 1024,
|
||||||
|
"outgoingBytes": 1024,
|
||||||
|
"incomingPackets": 1000,
|
||||||
|
"outgoingPackets": 2000
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user