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.
...@@ -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);
176 + ArrayNode diskRes = (ArrayNode) jsonTree.get("disks");
177 + diskRes.forEach(node-> {
178 + JsonNode resourceName = node.get("resourceName");
179 + nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
180 +
172 JsonNode readBytes = jsonTree.get("readBytes"); 181 JsonNode readBytes = jsonTree.get("readBytes");
173 JsonNode writeBytes = jsonTree.get("writeBytes"); 182 JsonNode writeBytes = jsonTree.get("writeBytes");
174 183
175 if (readBytes != null) { 184 if (readBytes != null) {
176 - cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES, 185 + cm[0] = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
177 new MetricValue.Builder().load(readBytes.asLong()).add()); 186 new MetricValue.Builder().load(readBytes.asLong()).add());
178 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 187 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
179 } 188 }
180 189
181 if (writeBytes != null) { 190 if (writeBytes != null) {
182 - cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, 191 + cm[0] = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
183 new MetricValue.Builder().load(writeBytes.asLong()).add()); 192 new MetricValue.Builder().load(writeBytes.asLong()).add());
184 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 193 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
185 } 194 }
186 - 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);
218 + ArrayNode networkRes = (ArrayNode) jsonTree.get("networks");
219 + networkRes.forEach(node -> {
220 + JsonNode resourceName = node.get("resourceName");
221 + nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
222 +
209 JsonNode inBytes = jsonTree.get("incomingBytes"); 223 JsonNode inBytes = jsonTree.get("incomingBytes");
210 JsonNode outBytes = jsonTree.get("outgoingBytes"); 224 JsonNode outBytes = jsonTree.get("outgoingBytes");
211 JsonNode inPackets = jsonTree.get("incomingPackets"); 225 JsonNode inPackets = jsonTree.get("incomingPackets");
212 JsonNode outPackets = jsonTree.get("outgoingPackets"); 226 JsonNode outPackets = jsonTree.get("outgoingPackets");
213 227
214 if (inBytes != null) { 228 if (inBytes != null) {
215 - cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, 229 + cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
216 new MetricValue.Builder().load(inBytes.asLong()).add()); 230 new MetricValue.Builder().load(inBytes.asLong()).add());
217 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 231 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
218 } 232 }
219 233
220 if (outBytes != null) { 234 if (outBytes != null) {
221 - cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, 235 + cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
222 new MetricValue.Builder().load(outBytes.asLong()).add()); 236 new MetricValue.Builder().load(outBytes.asLong()).add());
223 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 237 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
224 } 238 }
225 239
226 if (inPackets != null) { 240 if (inPackets != null) {
227 - cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, 241 + cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
228 new MetricValue.Builder().load(inPackets.asLong()).add()); 242 new MetricValue.Builder().load(inPackets.asLong()).add());
229 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 243 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
230 } 244 }
231 245
232 if (outPackets != null) { 246 if (outPackets != null) {
233 - cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, 247 + cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
234 new MetricValue.Builder().load(outPackets.asLong()).add()); 248 new MetricValue.Builder().load(outPackets.asLong()).add());
235 - service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); 249 + service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
236 } 250 }
237 - 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
......
1 { 1 {
2 "type": "object", 2 "type": "object",
3 + "title": "disks",
3 "required": [ 4 "required": [
5 + "disks"
6 + ],
7 + "properties": {
8 + "disks": {
9 + "type": "array",
10 + "xml": {
11 + "name": "disks",
12 + "wrapped": true
13 + },
14 + "items": {
15 + "type": "object",
16 + "title": "disks",
17 + "required": [
18 + "resourceName",
4 "readBytes", 19 "readBytes",
5 "writeBytes" 20 "writeBytes"
6 ], 21 ],
7 "properties": { 22 "properties": {
23 + "resourceName": {
24 + "type": "string",
25 + "example": "disk1"
26 + },
8 "readBytes": { 27 "readBytes": {
9 "type": "integer", 28 "type": "integer",
10 "format": "int64", 29 "format": "int64",
...@@ -16,4 +35,7 @@ ...@@ -16,4 +35,7 @@
16 "example": "300" 35 "example": "300"
17 } 36 }
18 } 37 }
38 + }
39 + }
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": [
5 + "networks"
6 + ],
7 + "properties": {
8 + "networks": {
9 + "type": "array",
10 + "xml": {
11 + "name": "networks",
12 + "wrapped": true
13 + },
14 + "items": {
15 + "type": "object",
16 + "title": "networks",
17 + "required": [
18 + "resourceName",
4 "incomingBytes", 19 "incomingBytes",
5 "outgoingBytes", 20 "outgoingBytes",
6 "incomingPackets", 21 "incomingPackets",
7 "outgoingPackets" 22 "outgoingPackets"
8 ], 23 ],
9 "properties": { 24 "properties": {
25 + "resourceName": {
26 + "type": "string",
27 + "example": "eth0"
28 + },
10 "incomingBytes": { 29 "incomingBytes": {
11 "type": "integer", 30 "type": "integer",
12 "format": "int64", 31 "format": "int64",
...@@ -28,4 +47,7 @@ ...@@ -28,4 +47,7 @@
28 "example": "2000" 47 "example": "2000"
29 } 48 }
30 } 49 }
50 + }
51 + }
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 + "disks": [
3 + {
4 + "resourceName": "disk1",
2 "readBytes": 500, 5 "readBytes": 500,
3 "writeBytes": 300 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 + "networks": [
3 + {
4 + "resourceName": "eth0",
2 "incomingBytes": 1024, 5 "incomingBytes": 1024,
3 "outgoingBytes": 1024, 6 "outgoingBytes": 1024,
4 "incomingPackets": 1000, 7 "incomingPackets": 1000,
5 "outgoingPackets": 2000 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
......