blob: f7a60dc5f21ce21e07ce7630abb15c2142a1de55 [file] [log] [blame]
Jian Lic132c112016-01-28 20:27:34 -08001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cpman.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
Jian Liba6b1172016-02-01 22:40:42 -080019import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Lic132c112016-01-28 20:27:34 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.cpman.ControlMetric;
22import org.onosproject.cpman.ControlMetricType;
Jian Lic132c112016-01-28 20:27:34 -080023import org.onosproject.cpman.ControlPlaneMonitorService;
24import org.onosproject.cpman.MetricValue;
Jian Liba6b1172016-02-01 22:40:42 -080025import org.onosproject.cpman.impl.ControlMetricsSystemSpec;
Jian Lic132c112016-01-28 20:27:34 -080026import org.onosproject.rest.AbstractWebResource;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.io.IOException;
35import java.io.InputStream;
36import java.util.Optional;
37
Jian Liba6b1172016-02-01 22:40:42 -080038import static org.onlab.util.Tools.nullIsIllegal;
39
Jian Lic132c112016-01-28 20:27:34 -080040/**
41 * Collect control plane metrics.
42 */
Jian Li54df73e2016-02-01 17:09:03 -080043@Path("collector")
Jian Lic132c112016-01-28 20:27:34 -080044public class ControlMetricsCollectorWebResource extends AbstractWebResource {
45
46 final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
47 public static final int UPDATE_INTERVAL = 1; // 1 minute update interval
48 public static final String INVALID_SYSTEM_SPECS = "Invalid system specifications";
Jian Liba6b1172016-02-01 22:40:42 -080049 public static final String INVALID_RESOURCE_NAME = "Invalid resource name";
Jian Lic132c112016-01-28 20:27:34 -080050
51 /**
52 * Collects CPU metrics.
53 *
54 * @param stream JSON stream
55 * @return 200 OK
56 * @onos.rsModel CpuMetricsPost
57 */
58 @POST
Jian Li54df73e2016-02-01 17:09:03 -080059 @Path("cpu_metrics")
Jian Lic132c112016-01-28 20:27:34 -080060 @Consumes(MediaType.APPLICATION_JSON)
61 @Produces(MediaType.APPLICATION_JSON)
62 public Response cpuMetrics(InputStream stream) {
63 ObjectNode root = mapper().createObjectNode();
64 ControlMetric cm;
65 try {
66 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
67 JsonNode cpuLoadJson = jsonTree.get("cpuLoad");
68 JsonNode totalCpuTimeJson = jsonTree.get("totalCpuTime");
69 JsonNode sysCpuTimeJson = jsonTree.get("sysCpuTime");
70 JsonNode userCpuTimeJson = jsonTree.get("userCpuTime");
71 JsonNode cpuIdleTimeJson = jsonTree.get("cpuIdleTime");
72
73 if (cpuLoadJson != null) {
74 cm = new ControlMetric(ControlMetricType.CPU_LOAD,
Jian Liba6b1172016-02-01 22:40:42 -080075 new MetricValue.Builder().load(cpuLoadJson.asLong()).add());
Jian Lic132c112016-01-28 20:27:34 -080076 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
77 }
78
79 if (totalCpuTimeJson != null) {
80 cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
Jian Liba6b1172016-02-01 22:40:42 -080081 new MetricValue.Builder().load(totalCpuTimeJson.asLong()).add());
Jian Lic132c112016-01-28 20:27:34 -080082 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
83 }
84
85 if (sysCpuTimeJson != null) {
86 cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME,
87 new MetricValue.Builder().load(sysCpuTimeJson.asLong()).add());
88 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
89 }
90
91 if (userCpuTimeJson != null) {
92 cm = new ControlMetric(ControlMetricType.USER_CPU_TIME,
93 new MetricValue.Builder().load(userCpuTimeJson.asLong()).add());
94 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
95 }
96
97 if (cpuIdleTimeJson != null) {
98 cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME,
99 new MetricValue.Builder().load(cpuIdleTimeJson.asLong()).add());
100 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
101 }
102
103 } catch (IOException e) {
104 throw new IllegalArgumentException(e.getMessage());
105 }
106 return ok(root).build();
107 }
108
109 /**
110 * Collects memory metrics.
111 *
112 * @param stream JSON stream
113 * @return 200 OK
114 * @onos.rsModel MemoryMetricsPost
115 */
116 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800117 @Path("memory_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800118 @Consumes(MediaType.APPLICATION_JSON)
119 @Produces(MediaType.APPLICATION_JSON)
120 public Response memoryMetrics(InputStream stream) {
121 ObjectNode root = mapper().createObjectNode();
122 ControlMetric cm;
123 try {
124 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
125 JsonNode memUsedPerc = jsonTree.get("memoryUsedPercentage");
126 JsonNode memFreePerc = jsonTree.get("memoryFreePercentage");
127 JsonNode memUsed = jsonTree.get("memoryUsed");
128 JsonNode memFree = jsonTree.get("memoryFree");
129
130 if (memUsedPerc != null) {
131 cm = new ControlMetric(ControlMetricType.MEMORY_USED_PERCENTAGE,
132 new MetricValue.Builder().load(memUsedPerc.asLong()).add());
133 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
134 }
135
136 if (memFreePerc != null) {
137 cm = new ControlMetric(ControlMetricType.MEMORY_FREE_PERCENTAGE,
138 new MetricValue.Builder().load(memFreePerc.asLong()).add());
139 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
140 }
141
142 if (memUsed != null) {
143 cm = new ControlMetric(ControlMetricType.MEMORY_USED,
144 new MetricValue.Builder().load(memUsed.asLong()).add());
145 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
146 }
147
148 if (memFree != null) {
149 cm = new ControlMetric(ControlMetricType.MEMORY_FREE,
150 new MetricValue.Builder().load(memFree.asLong()).add());
151 service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null));
152 }
153
154 } catch (IOException e) {
155 throw new IllegalArgumentException(e.getMessage());
156 }
157 return ok(root).build();
158 }
159
160 /**
161 * Collects disk metrics.
162 *
163 * @param stream JSON stream
164 * @return 200 OK
165 * @onos.rsModel DiskMetricsPost
166 */
167 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800168 @Path("disk_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800169 @Consumes(MediaType.APPLICATION_JSON)
170 @Produces(MediaType.APPLICATION_JSON)
171 public Response diskMetrics(InputStream stream) {
172 ObjectNode root = mapper().createObjectNode();
Jian Liba6b1172016-02-01 22:40:42 -0800173 final ControlMetric[] cm = new ControlMetric[1];
Jian Lic132c112016-01-28 20:27:34 -0800174 try {
175 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Liba6b1172016-02-01 22:40:42 -0800176 ArrayNode diskRes = (ArrayNode) jsonTree.get("disks");
177 diskRes.forEach(node-> {
178 JsonNode resourceName = node.get("resourceName");
179 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800180
Jian Liba6b1172016-02-01 22:40:42 -0800181 JsonNode readBytes = jsonTree.get("readBytes");
182 JsonNode writeBytes = jsonTree.get("writeBytes");
Jian Lic132c112016-01-28 20:27:34 -0800183
Jian Liba6b1172016-02-01 22:40:42 -0800184 if (readBytes != null) {
185 cm[0] = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
186 new MetricValue.Builder().load(readBytes.asLong()).add());
187 service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
188 }
Jian Lic132c112016-01-28 20:27:34 -0800189
Jian Liba6b1172016-02-01 22:40:42 -0800190 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 });
Jian Lic132c112016-01-28 20:27:34 -0800196 } catch (IOException e) {
197 throw new IllegalArgumentException(e.getMessage());
198 }
199 return ok(root).build();
200 }
201
202 /**
203 * Collects network metrics.
204 *
205 * @param stream JSON stream
206 * @return 200 OK
207 * @onos.rsModel NetworkMetricsPost
208 */
209 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800210 @Path("network_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800211 @Consumes(MediaType.APPLICATION_JSON)
212 @Produces(MediaType.APPLICATION_JSON)
213 public Response networkMetrics(InputStream stream) {
214 ObjectNode root = mapper().createObjectNode();
Jian Liba6b1172016-02-01 22:40:42 -0800215 final ControlMetric[] cm = new ControlMetric[1];
Jian Lic132c112016-01-28 20:27:34 -0800216 try {
217 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Liba6b1172016-02-01 22:40:42 -0800218 ArrayNode networkRes = (ArrayNode) jsonTree.get("networks");
219 networkRes.forEach(node -> {
220 JsonNode resourceName = node.get("resourceName");
221 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800222
Jian Liba6b1172016-02-01 22:40:42 -0800223 JsonNode inBytes = jsonTree.get("incomingBytes");
224 JsonNode outBytes = jsonTree.get("outgoingBytes");
225 JsonNode inPackets = jsonTree.get("incomingPackets");
226 JsonNode outPackets = jsonTree.get("outgoingPackets");
Jian Lic132c112016-01-28 20:27:34 -0800227
Jian Liba6b1172016-02-01 22:40:42 -0800228 if (inBytes != null) {
229 cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
230 new MetricValue.Builder().load(inBytes.asLong()).add());
231 service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
232 }
Jian Lic132c112016-01-28 20:27:34 -0800233
Jian Liba6b1172016-02-01 22:40:42 -0800234 if (outBytes != null) {
235 cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
236 new MetricValue.Builder().load(outBytes.asLong()).add());
237 service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
238 }
Jian Lic132c112016-01-28 20:27:34 -0800239
Jian Liba6b1172016-02-01 22:40:42 -0800240 if (inPackets != null) {
241 cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
242 new MetricValue.Builder().load(inPackets.asLong()).add());
243 service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText());
244 }
Jian Lic132c112016-01-28 20:27:34 -0800245
Jian Liba6b1172016-02-01 22:40:42 -0800246 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 });
Jian Lic132c112016-01-28 20:27:34 -0800252 } catch (IOException e) {
253 throw new IllegalArgumentException(e.getMessage());
254 }
255 return ok(root).build();
256 }
257
Jian Lic132c112016-01-28 20:27:34 -0800258 /**
259 * Collects system specifications.
260 * The system specs include the various control metrics
261 * which do not require aggregation.
262 *
263 * @param stream JSON stream
264 * @return 200 OK
265 * @onos.rsModel SystemSpecsPost
266 */
267 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800268 @Path("system_specs")
Jian Lic132c112016-01-28 20:27:34 -0800269 @Consumes(MediaType.APPLICATION_JSON)
270 @Produces(MediaType.APPLICATION_JSON)
271 public Response systemSpecs(InputStream stream) {
272 ObjectNode root = mapper().createObjectNode();
273
274 try {
275 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
276 JsonNode numOfCores = jsonTree.get("numOfCores");
277 JsonNode numOfCpus = jsonTree.get("numOfCpus");
278 JsonNode cpuSpeed = jsonTree.get("cpuSpeed");
279 JsonNode totalMemory = jsonTree.get("totalMemory");
280
281 if (numOfCores != null && numOfCpus != null && cpuSpeed != null && totalMemory != null) {
282 ControlMetricsSystemSpec.Builder builder = new ControlMetricsSystemSpec.Builder();
283 ControlMetricsSystemSpec cmss = builder.numOfCores(numOfCores.asInt())
284 .numOfCpus(numOfCpus.asInt())
285 .cpuSpeed(cpuSpeed.asInt())
286 .totalMemory(totalMemory.asLong())
287 .build();
288 // TODO: need to implement spec store
289
290 } else {
291 throw new IllegalArgumentException(INVALID_SYSTEM_SPECS);
292 }
293
294 } catch (IOException e) {
295 throw new IllegalArgumentException(e.getMessage());
296 }
297 return ok(root).build();
298 }
Jian Liba6b1172016-02-01 22:40:42 -0800299}