blob: 07412861a38681bc3858d512661e9159e43205f5 [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 Li1a424692016-02-03 16:21:18 -080025import org.onosproject.cpman.SystemInfo;
26import org.onosproject.cpman.impl.DefaultSystemInfo;
27import org.onosproject.cpman.impl.SystemInfoFactory;
Jian Lic132c112016-01-28 20:27:34 -080028import org.onosproject.rest.AbstractWebResource;
29
30import javax.ws.rs.Consumes;
31import javax.ws.rs.POST;
32import javax.ws.rs.Path;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import java.io.IOException;
37import java.io.InputStream;
38import java.util.Optional;
39
Jian Liba6b1172016-02-01 22:40:42 -080040import static org.onlab.util.Tools.nullIsIllegal;
41
Jian Lic132c112016-01-28 20:27:34 -080042/**
43 * Collect control plane metrics.
44 */
Jian Li54df73e2016-02-01 17:09:03 -080045@Path("collector")
Jian Lic132c112016-01-28 20:27:34 -080046public class ControlMetricsCollectorWebResource extends AbstractWebResource {
47
48 final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
Jian Li1fdd2242016-02-05 10:01:19 -080049 public static final int UPDATE_INTERVAL_IN_MINUTE = 1;
Jian Lic132c112016-01-28 20:27:34 -080050 public static final String INVALID_SYSTEM_SPECS = "Invalid system specifications";
Jian Liba6b1172016-02-01 22:40:42 -080051 public static final String INVALID_RESOURCE_NAME = "Invalid resource name";
Jian Li1fdd2242016-02-05 10:01:19 -080052 public static final String INVALID_REQUEST = "Invalid request";
Jian Lic132c112016-01-28 20:27:34 -080053
54 /**
55 * Collects CPU metrics.
56 *
57 * @param stream JSON stream
58 * @return 200 OK
59 * @onos.rsModel CpuMetricsPost
60 */
61 @POST
Jian Li54df73e2016-02-01 17:09:03 -080062 @Path("cpu_metrics")
Jian Lic132c112016-01-28 20:27:34 -080063 @Consumes(MediaType.APPLICATION_JSON)
64 @Produces(MediaType.APPLICATION_JSON)
65 public Response cpuMetrics(InputStream stream) {
66 ObjectNode root = mapper().createObjectNode();
67 ControlMetric cm;
68 try {
69 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li1fdd2242016-02-05 10:01:19 -080070 long cpuLoad = nullIsIllegal(jsonTree.get("cpuLoad").asLong(), INVALID_REQUEST);
71 long totalCpuTime = nullIsIllegal(jsonTree.get("totalCpuTime").asLong(), INVALID_REQUEST);
72 long sysCpuTime = nullIsIllegal(jsonTree.get("sysCpuTime").asLong(), INVALID_REQUEST);
73 long userCpuTime = nullIsIllegal(jsonTree.get("userCpuTime").asLong(), INVALID_REQUEST);
74 long cpuIdleTime = nullIsIllegal(jsonTree.get("cpuIdleTime").asLong(), INVALID_REQUEST);
Jian Lic132c112016-01-28 20:27:34 -080075
Jian Li1fdd2242016-02-05 10:01:19 -080076 cm = new ControlMetric(ControlMetricType.CPU_LOAD,
77 new MetricValue.Builder().load(cpuLoad).add());
78 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -080079
Jian Li1fdd2242016-02-05 10:01:19 -080080 cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
81 new MetricValue.Builder().load(totalCpuTime).add());
82 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -080083
Jian Li1fdd2242016-02-05 10:01:19 -080084 cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME,
85 new MetricValue.Builder().load(sysCpuTime).add());
86 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -080087
Jian Li1fdd2242016-02-05 10:01:19 -080088 cm = new ControlMetric(ControlMetricType.USER_CPU_TIME,
89 new MetricValue.Builder().load(userCpuTime).add());
90 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -080091
Jian Li1fdd2242016-02-05 10:01:19 -080092 cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME,
93 new MetricValue.Builder().load(cpuIdleTime).add());
94 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -080095
96 } catch (IOException e) {
97 throw new IllegalArgumentException(e.getMessage());
98 }
99 return ok(root).build();
100 }
101
102 /**
103 * Collects memory metrics.
104 *
105 * @param stream JSON stream
106 * @return 200 OK
107 * @onos.rsModel MemoryMetricsPost
108 */
109 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800110 @Path("memory_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800111 @Consumes(MediaType.APPLICATION_JSON)
112 @Produces(MediaType.APPLICATION_JSON)
113 public Response memoryMetrics(InputStream stream) {
114 ObjectNode root = mapper().createObjectNode();
115 ControlMetric cm;
116 try {
117 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li1fdd2242016-02-05 10:01:19 -0800118 long memUsedRatio = nullIsIllegal(jsonTree.get("memoryUsedRatio").asLong(), INVALID_REQUEST);
119 long memFreeRatio = nullIsIllegal(jsonTree.get("memoryFreeRatio").asLong(), INVALID_REQUEST);
120 long memUsed = nullIsIllegal(jsonTree.get("memoryUsed").asLong(), INVALID_REQUEST);
121 long memFree = nullIsIllegal(jsonTree.get("memoryFree").asLong(), INVALID_REQUEST);
Jian Lic132c112016-01-28 20:27:34 -0800122
Jian Li1fdd2242016-02-05 10:01:19 -0800123 cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO,
124 new MetricValue.Builder().load(memUsedRatio).add());
125 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -0800126
Jian Li1fdd2242016-02-05 10:01:19 -0800127 cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO,
128 new MetricValue.Builder().load(memFreeRatio).add());
129 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -0800130
Jian Li1fdd2242016-02-05 10:01:19 -0800131 cm = new ControlMetric(ControlMetricType.MEMORY_USED,
132 new MetricValue.Builder().load(memUsed).add());
133 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -0800134
Jian Li1fdd2242016-02-05 10:01:19 -0800135 cm = new ControlMetric(ControlMetricType.MEMORY_FREE,
136 new MetricValue.Builder().load(memFree).add());
137 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lic132c112016-01-28 20:27:34 -0800138
139 } catch (IOException e) {
140 throw new IllegalArgumentException(e.getMessage());
141 }
142 return ok(root).build();
143 }
144
145 /**
146 * Collects disk metrics.
147 *
148 * @param stream JSON stream
149 * @return 200 OK
150 * @onos.rsModel DiskMetricsPost
151 */
152 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800153 @Path("disk_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800154 @Consumes(MediaType.APPLICATION_JSON)
155 @Produces(MediaType.APPLICATION_JSON)
156 public Response diskMetrics(InputStream stream) {
157 ObjectNode root = mapper().createObjectNode();
Jian Li1fdd2242016-02-05 10:01:19 -0800158 ControlMetric cm;
Jian Lic132c112016-01-28 20:27:34 -0800159 try {
160 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Liba6b1172016-02-01 22:40:42 -0800161 ArrayNode diskRes = (ArrayNode) jsonTree.get("disks");
Jian Li1fdd2242016-02-05 10:01:19 -0800162 for (JsonNode node : diskRes) {
Jian Liba6b1172016-02-01 22:40:42 -0800163 JsonNode resourceName = node.get("resourceName");
164 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800165
Jian Li1fdd2242016-02-05 10:01:19 -0800166 long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST);
167 long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST);
Jian Lic132c112016-01-28 20:27:34 -0800168
Jian Li1fdd2242016-02-05 10:01:19 -0800169 cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
170 new MetricValue.Builder().load(readBytes).add());
171 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lic132c112016-01-28 20:27:34 -0800172
Jian Li1fdd2242016-02-05 10:01:19 -0800173 cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
174 new MetricValue.Builder().load(writeBytes).add());
175 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
176 }
Jian Lic132c112016-01-28 20:27:34 -0800177 } catch (IOException e) {
178 throw new IllegalArgumentException(e.getMessage());
179 }
180 return ok(root).build();
181 }
182
183 /**
184 * Collects network metrics.
185 *
186 * @param stream JSON stream
187 * @return 200 OK
188 * @onos.rsModel NetworkMetricsPost
189 */
190 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800191 @Path("network_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800192 @Consumes(MediaType.APPLICATION_JSON)
193 @Produces(MediaType.APPLICATION_JSON)
194 public Response networkMetrics(InputStream stream) {
195 ObjectNode root = mapper().createObjectNode();
Jian Li1fdd2242016-02-05 10:01:19 -0800196 ControlMetric cm;
Jian Lic132c112016-01-28 20:27:34 -0800197 try {
198 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Liba6b1172016-02-01 22:40:42 -0800199 ArrayNode networkRes = (ArrayNode) jsonTree.get("networks");
Jian Li1fdd2242016-02-05 10:01:19 -0800200 for (JsonNode node : networkRes) {
Jian Liba6b1172016-02-01 22:40:42 -0800201 JsonNode resourceName = node.get("resourceName");
202 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800203
Jian Li1fdd2242016-02-05 10:01:19 -0800204 long inBytes = nullIsIllegal(node.get("incomingBytes").asLong(), INVALID_REQUEST);
205 long outBytes = nullIsIllegal(node.get("outgoingBytes").asLong(), INVALID_REQUEST);
206 long inPackets = nullIsIllegal(node.get("incomingPackets").asLong(), INVALID_REQUEST);
207 long outPackets = nullIsIllegal(node.get("outgoingPackets").asLong(), INVALID_REQUEST);
Jian Lic132c112016-01-28 20:27:34 -0800208
Jian Li1fdd2242016-02-05 10:01:19 -0800209 cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
210 new MetricValue.Builder().load(inBytes).add());
211 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lic132c112016-01-28 20:27:34 -0800212
Jian Li1fdd2242016-02-05 10:01:19 -0800213 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
214 new MetricValue.Builder().load(outBytes).add());
215 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lic132c112016-01-28 20:27:34 -0800216
Jian Li1fdd2242016-02-05 10:01:19 -0800217 cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
218 new MetricValue.Builder().load(inPackets).add());
219 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lic132c112016-01-28 20:27:34 -0800220
Jian Li1fdd2242016-02-05 10:01:19 -0800221 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
222 new MetricValue.Builder().load(outPackets).add());
223 service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
224 }
Jian Lic132c112016-01-28 20:27:34 -0800225 } catch (IOException e) {
226 throw new IllegalArgumentException(e.getMessage());
227 }
228 return ok(root).build();
229 }
230
Jian Lic132c112016-01-28 20:27:34 -0800231 /**
Jian Li1a424692016-02-03 16:21:18 -0800232 * Collects system information.
233 * The system information includes the various control metrics
Jian Lic132c112016-01-28 20:27:34 -0800234 * which do not require aggregation.
235 *
236 * @param stream JSON stream
237 * @return 200 OK
Jian Li1a424692016-02-03 16:21:18 -0800238 * @onos.rsModel SystemInfoPost
Jian Lic132c112016-01-28 20:27:34 -0800239 */
240 @POST
Jian Li1a424692016-02-03 16:21:18 -0800241 @Path("system_info")
Jian Lic132c112016-01-28 20:27:34 -0800242 @Consumes(MediaType.APPLICATION_JSON)
243 @Produces(MediaType.APPLICATION_JSON)
Jian Li1a424692016-02-03 16:21:18 -0800244 public Response systemInfo(InputStream stream) {
Jian Lic132c112016-01-28 20:27:34 -0800245 ObjectNode root = mapper().createObjectNode();
246
247 try {
248 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
249 JsonNode numOfCores = jsonTree.get("numOfCores");
250 JsonNode numOfCpus = jsonTree.get("numOfCpus");
251 JsonNode cpuSpeed = jsonTree.get("cpuSpeed");
252 JsonNode totalMemory = jsonTree.get("totalMemory");
253
Jian Li1a424692016-02-03 16:21:18 -0800254 if (numOfCores != null && numOfCpus != null &&
255 cpuSpeed != null && totalMemory != null) {
256 SystemInfo systemInfo = new DefaultSystemInfo.Builder()
257 .numOfCores(numOfCores.asInt())
Jian Lic132c112016-01-28 20:27:34 -0800258 .numOfCpus(numOfCpus.asInt())
259 .cpuSpeed(cpuSpeed.asInt())
Jian Li1a424692016-02-03 16:21:18 -0800260 .totalMemory(totalMemory.asInt())
Jian Lic132c112016-01-28 20:27:34 -0800261 .build();
Jian Lic132c112016-01-28 20:27:34 -0800262
Jian Li1a424692016-02-03 16:21:18 -0800263 // try to store the system info.
264 SystemInfoFactory.getInstance().setSystemInfo(systemInfo);
Jian Lic132c112016-01-28 20:27:34 -0800265 } else {
266 throw new IllegalArgumentException(INVALID_SYSTEM_SPECS);
267 }
268
269 } catch (IOException e) {
270 throw new IllegalArgumentException(e.getMessage());
271 }
272 return ok(root).build();
273 }
Jian Li9d616492016-03-09 10:52:49 -0800274}