Jian Li
Committed by Gerrit Code Review

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
...@@ -16,12 +16,13 @@ ...@@ -16,12 +16,13 @@
16 package org.onosproject.cpman.rest; 16 package org.onosproject.cpman.rest;
17 17
18 import com.fasterxml.jackson.databind.JsonNode; 18 import com.fasterxml.jackson.databind.JsonNode;
19 +import com.fasterxml.jackson.databind.node.ArrayNode;
19 import com.fasterxml.jackson.databind.node.ObjectNode; 20 import com.fasterxml.jackson.databind.node.ObjectNode;
20 import org.onosproject.cpman.ControlMetric; 21 import org.onosproject.cpman.ControlMetric;
21 import org.onosproject.cpman.ControlMetricType; 22 import org.onosproject.cpman.ControlMetricType;
22 -import org.onosproject.cpman.impl.ControlMetricsSystemSpec;
23 import org.onosproject.cpman.ControlPlaneMonitorService; 23 import org.onosproject.cpman.ControlPlaneMonitorService;
24 import org.onosproject.cpman.MetricValue; 24 import org.onosproject.cpman.MetricValue;
25 +import org.onosproject.cpman.impl.ControlMetricsSystemSpec;
25 import org.onosproject.rest.AbstractWebResource; 26 import org.onosproject.rest.AbstractWebResource;
26 27
27 import javax.ws.rs.Consumes; 28 import javax.ws.rs.Consumes;
...@@ -34,6 +35,8 @@ import java.io.IOException; ...@@ -34,6 +35,8 @@ import java.io.IOException;
34 import java.io.InputStream; 35 import java.io.InputStream;
35 import java.util.Optional; 36 import java.util.Optional;
36 37
38 +import static org.onlab.util.Tools.nullIsIllegal;
39 +
37 /** 40 /**
38 * Collect control plane metrics. 41 * Collect control plane metrics.
39 */ 42 */
...@@ -43,6 +46,7 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource { ...@@ -43,6 +46,7 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
43 final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class); 46 final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
44 public static final int UPDATE_INTERVAL = 1; // 1 minute update interval 47 public static final int UPDATE_INTERVAL = 1; // 1 minute update interval
45 public static final String INVALID_SYSTEM_SPECS = "Invalid system specifications"; 48 public static final String INVALID_SYSTEM_SPECS = "Invalid system specifications";
49 + public static final String INVALID_RESOURCE_NAME = "Invalid resource name";
46 50
47 /** 51 /**
48 * Collects CPU metrics. 52 * Collects CPU metrics.
...@@ -68,13 +72,13 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource { ...@@ -68,13 +72,13 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
68 72
69 if (cpuLoadJson != null) { 73 if (cpuLoadJson != null) {
70 cm = new ControlMetric(ControlMetricType.CPU_LOAD, 74 cm = new ControlMetric(ControlMetricType.CPU_LOAD,
71 - new MetricValue.Builder().load(cpuLoadJson.asLong()).add()); 75 + new MetricValue.Builder().load(cpuLoadJson.asLong()).add());
72 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 76 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
73 } 77 }
74 78
75 if (totalCpuTimeJson != null) { 79 if (totalCpuTimeJson != null) {
76 cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME, 80 cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
77 - new MetricValue.Builder().load(totalCpuTimeJson.asLong()).add()); 81 + new MetricValue.Builder().load(totalCpuTimeJson.asLong()).add());
78 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 82 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
79 } 83 }
80 84
...@@ -166,24 +170,29 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource { ...@@ -166,24 +170,29 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
166 @Produces(MediaType.APPLICATION_JSON) 170 @Produces(MediaType.APPLICATION_JSON)
167 public Response diskMetrics(InputStream stream) { 171 public Response diskMetrics(InputStream stream) {
168 ObjectNode root = mapper().createObjectNode(); 172 ObjectNode root = mapper().createObjectNode();
169 - ControlMetric cm; 173 + final ControlMetric[] cm = new ControlMetric[1];
170 try { 174 try {
171 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); 175 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
172 - JsonNode readBytes = jsonTree.get("readBytes"); 176 + ArrayNode diskRes = (ArrayNode) jsonTree.get("disks");
173 - JsonNode writeBytes = jsonTree.get("writeBytes"); 177 + diskRes.forEach(node-> {
174 - 178 + JsonNode resourceName = node.get("resourceName");
175 - if (readBytes != null) { 179 + nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
176 - cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES, 180 +
177 - new MetricValue.Builder().load(readBytes.asLong()).add()); 181 + JsonNode readBytes = jsonTree.get("readBytes");
178 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 182 + JsonNode writeBytes = jsonTree.get("writeBytes");
179 - } 183 +
180 - 184 + if (readBytes != null) {
181 - if (writeBytes != null) { 185 + cm[0] = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
182 - cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, 186 + new MetricValue.Builder().load(readBytes.asLong()).add());
183 - new MetricValue.Builder().load(writeBytes.asLong()).add()); 187 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
184 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 188 + }
185 - } 189 +
186 - 190 + if (writeBytes != null) {
191 + cm[0] = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
192 + new MetricValue.Builder().load(writeBytes.asLong()).add());
193 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
194 + }
195 + });
187 } catch (IOException e) { 196 } catch (IOException e) {
188 throw new IllegalArgumentException(e.getMessage()); 197 throw new IllegalArgumentException(e.getMessage());
189 } 198 }
...@@ -203,45 +212,49 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource { ...@@ -203,45 +212,49 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
203 @Produces(MediaType.APPLICATION_JSON) 212 @Produces(MediaType.APPLICATION_JSON)
204 public Response networkMetrics(InputStream stream) { 213 public Response networkMetrics(InputStream stream) {
205 ObjectNode root = mapper().createObjectNode(); 214 ObjectNode root = mapper().createObjectNode();
206 - ControlMetric cm; 215 + final ControlMetric[] cm = new ControlMetric[1];
207 try { 216 try {
208 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); 217 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
209 - JsonNode inBytes = jsonTree.get("incomingBytes"); 218 + ArrayNode networkRes = (ArrayNode) jsonTree.get("networks");
210 - JsonNode outBytes = jsonTree.get("outgoingBytes"); 219 + networkRes.forEach(node -> {
211 - JsonNode inPackets = jsonTree.get("incomingPackets"); 220 + JsonNode resourceName = node.get("resourceName");
212 - JsonNode outPackets = jsonTree.get("outgoingPackets"); 221 + nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
213 - 222 +
214 - if (inBytes != null) { 223 + JsonNode inBytes = jsonTree.get("incomingBytes");
215 - cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, 224 + JsonNode outBytes = jsonTree.get("outgoingBytes");
216 - new MetricValue.Builder().load(inBytes.asLong()).add()); 225 + JsonNode inPackets = jsonTree.get("incomingPackets");
217 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 226 + JsonNode outPackets = jsonTree.get("outgoingPackets");
218 - } 227 +
219 - 228 + if (inBytes != null) {
220 - if (outBytes != null) { 229 + cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
221 - cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, 230 + new MetricValue.Builder().load(inBytes.asLong()).add());
222 - new MetricValue.Builder().load(outBytes.asLong()).add()); 231 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
223 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 232 + }
224 - } 233 +
225 - 234 + if (outBytes != null) {
226 - if (inPackets != null) { 235 + cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
227 - cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, 236 + new MetricValue.Builder().load(outBytes.asLong()).add());
228 - new MetricValue.Builder().load(inPackets.asLong()).add()); 237 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
229 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 238 + }
230 - } 239 +
231 - 240 + if (inPackets != null) {
232 - if (outPackets != null) { 241 + cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
233 - cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, 242 + new MetricValue.Builder().load(inPackets.asLong()).add());
234 - new MetricValue.Builder().load(outPackets.asLong()).add()); 243 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
235 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 244 + }
236 - } 245 +
237 - 246 + if (outPackets != null) {
247 + cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
248 + new MetricValue.Builder().load(outPackets.asLong()).add());
249 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
250 + }
251 + });
238 } catch (IOException e) { 252 } catch (IOException e) {
239 throw new IllegalArgumentException(e.getMessage()); 253 throw new IllegalArgumentException(e.getMessage());
240 } 254 }
241 return ok(root).build(); 255 return ok(root).build();
242 } 256 }
243 257
244 -
245 /** 258 /**
246 * Collects system specifications. 259 * Collects system specifications.
247 * The system specs include the various control metrics 260 * The system specs include the various control metrics
...@@ -283,4 +296,4 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource { ...@@ -283,4 +296,4 @@ public class ControlMetricsCollectorWebResource extends AbstractWebResource {
283 } 296 }
284 return ok(root).build(); 297 return ok(root).build();
285 } 298 }
286 -} 299 +}
...\ No newline at end of file ...\ No newline at end of file
......
1 { 1 {
2 "type": "object", 2 "type": "object",
3 + "title": "disks",
3 "required": [ 4 "required": [
4 - "readBytes", 5 + "disks"
5 - "writeBytes"
6 ], 6 ],
7 "properties": { 7 "properties": {
8 - "readBytes": { 8 + "disks": {
9 - "type": "integer", 9 + "type": "array",
10 - "format": "int64", 10 + "xml": {
11 - "example": "500" 11 + "name": "disks",
12 - }, 12 + "wrapped": true
13 - "writeBytes": { 13 + },
14 - "type": "integer", 14 + "items": {
15 - "format": "int64", 15 + "type": "object",
16 - "example": "300" 16 + "title": "disks",
17 + "required": [
18 + "resourceName",
19 + "readBytes",
20 + "writeBytes"
21 + ],
22 + "properties": {
23 + "resourceName": {
24 + "type": "string",
25 + "example": "disk1"
26 + },
27 + "readBytes": {
28 + "type": "integer",
29 + "format": "int64",
30 + "example": "500"
31 + },
32 + "writeBytes": {
33 + "type": "integer",
34 + "format": "int64",
35 + "example": "300"
36 + }
37 + }
38 + }
17 } 39 }
18 } 40 }
19 } 41 }
...\ No newline at end of file ...\ No newline at end of file
......
1 { 1 {
2 "type": "object", 2 "type": "object",
3 + "title": "networks",
3 "required": [ 4 "required": [
4 - "incomingBytes", 5 + "networks"
5 - "outgoingBytes",
6 - "incomingPackets",
7 - "outgoingPackets"
8 ], 6 ],
9 "properties": { 7 "properties": {
10 - "incomingBytes": { 8 + "networks": {
11 - "type": "integer", 9 + "type": "array",
12 - "format": "int64", 10 + "xml": {
13 - "example": "1024" 11 + "name": "networks",
14 - }, 12 + "wrapped": true
15 - "outgoingBytes": { 13 + },
16 - "type": "integer", 14 + "items": {
17 - "format": "int64", 15 + "type": "object",
18 - "example": "1024" 16 + "title": "networks",
19 - }, 17 + "required": [
20 - "incomingPackets": { 18 + "resourceName",
21 - "type": "integer", 19 + "incomingBytes",
22 - "format": "int64", 20 + "outgoingBytes",
23 - "example": "1000" 21 + "incomingPackets",
24 - }, 22 + "outgoingPackets"
25 - "outgoingPackets": { 23 + ],
26 - "type": "integer", 24 + "properties": {
27 - "format": "int64", 25 + "resourceName": {
28 - "example": "2000" 26 + "type": "string",
27 + "example": "eth0"
28 + },
29 + "incomingBytes": {
30 + "type": "integer",
31 + "format": "int64",
32 + "example": "1024"
33 + },
34 + "outgoingBytes": {
35 + "type": "integer",
36 + "format": "int64",
37 + "example": "1024"
38 + },
39 + "incomingPackets": {
40 + "type": "integer",
41 + "format": "int64",
42 + "example": "1000"
43 + },
44 + "outgoingPackets": {
45 + "type": "integer",
46 + "format": "int64",
47 + "example": "2000"
48 + }
49 + }
50 + }
29 } 51 }
30 } 52 }
31 } 53 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -37,6 +37,7 @@ import java.net.ServerSocket; ...@@ -37,6 +37,7 @@ import java.net.ServerSocket;
37 import java.util.Optional; 37 import java.util.Optional;
38 38
39 import static org.easymock.EasyMock.anyObject; 39 import static org.easymock.EasyMock.anyObject;
40 +import static org.easymock.EasyMock.anyString;
40 import static org.easymock.EasyMock.createMock; 41 import static org.easymock.EasyMock.createMock;
41 import static org.easymock.EasyMock.expectLastCall; 42 import static org.easymock.EasyMock.expectLastCall;
42 import static org.easymock.EasyMock.replay; 43 import static org.easymock.EasyMock.replay;
...@@ -84,19 +85,17 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest { ...@@ -84,19 +85,17 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest {
84 } 85 }
85 86
86 @Test 87 @Test
87 - public void testDiskMetricsPost() { 88 + public void testDiskMetricsWithNullName() {
88 - mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), 89 + mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), anyString());
89 - (Optional<DeviceId>) anyObject()); 90 + expectLastCall().times(4);
90 - expectLastCall().times(2);
91 replay(mockControlPlaneMonitorService); 91 replay(mockControlPlaneMonitorService);
92 basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics"); 92 basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics");
93 } 93 }
94 94
95 @Test 95 @Test
96 - public void testNetworkMetricsPost() { 96 + public void testNetworkMetricsWithNullName() {
97 - mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), 97 + mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), anyString());
98 - (Optional<DeviceId>) anyObject()); 98 + expectLastCall().times(8);
99 - expectLastCall().times(4);
100 replay(mockControlPlaneMonitorService); 99 replay(mockControlPlaneMonitorService);
101 basePostTest("network-metrics-post.json", PREFIX + "/network_metrics"); 100 basePostTest("network-metrics-post.json", PREFIX + "/network_metrics");
102 } 101 }
...@@ -106,16 +105,20 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest { ...@@ -106,16 +105,20 @@ public class ControlMetricsCollectorResourceTest extends JerseyTest {
106 basePostTest("system-spec-post.json", PREFIX + "/system_specs"); 105 basePostTest("system-spec-post.json", PREFIX + "/system_specs");
107 } 106 }
108 107
109 - private void basePostTest(String jsonFile, String path) { 108 + private ClientResponse baseTest(String jsonFile, String path) {
110 final WebResource rs = resource(); 109 final WebResource rs = resource();
111 InputStream jsonStream = ControlMetricsCollectorResourceTest.class 110 InputStream jsonStream = ControlMetricsCollectorResourceTest.class
112 .getResourceAsStream(jsonFile); 111 .getResourceAsStream(jsonFile);
113 112
114 assertThat(jsonStream, notNullValue()); 113 assertThat(jsonStream, notNullValue());
115 114
116 - ClientResponse response = rs.path(path) 115 + return rs.path(path)
117 .type(MediaType.APPLICATION_JSON_TYPE) 116 .type(MediaType.APPLICATION_JSON_TYPE)
118 .post(ClientResponse.class, jsonStream); 117 .post(ClientResponse.class, jsonStream);
118 + }
119 +
120 + private void basePostTest(String jsonFile, String path) {
121 + ClientResponse response = baseTest(jsonFile, path);
119 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); 122 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
120 } 123 }
121 124
......
1 { 1 {
2 - "readBytes": 500, 2 + "disks": [
3 - "writeBytes": 300 3 + {
4 + "resourceName": "disk1",
5 + "readBytes": 500,
6 + "writeBytes": 300
7 + },
8 + {
9 + "resourceName": "disk2",
10 + "readBytes": 500,
11 + "writeBytes": 300
12 + }
13 + ]
4 } 14 }
...\ No newline at end of file ...\ No newline at end of file
......
1 { 1 {
2 - "incomingBytes": 1024, 2 + "networks": [
3 - "outgoingBytes": 1024, 3 + {
4 - "incomingPackets": 1000, 4 + "resourceName": "eth0",
5 - "outgoingPackets": 2000 5 + "incomingBytes": 1024,
6 + "outgoingBytes": 1024,
7 + "incomingPackets": 1000,
8 + "outgoingPackets": 2000
9 + },
10 + {
11 + "resourceName": "eth1",
12 + "incomingBytes": 1024,
13 + "outgoingBytes": 1024,
14 + "incomingPackets": 1000,
15 + "outgoingPackets": 2000
16 + }
17 + ]
6 } 18 }
...\ No newline at end of file ...\ No newline at end of file
......