blob: 4b6b4069340764245406f97a750d357e77313edb [file] [log] [blame]
Jian Li80c12702016-02-20 08:58:19 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li80c12702016-02-20 08:58:19 +09003 *
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.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li8b0ea962016-04-04 11:49:29 -070020import org.apache.commons.lang3.StringUtils;
Jian Li80c12702016-02-20 08:58:19 +090021import org.onosproject.cluster.ClusterService;
22import org.onosproject.cluster.NodeId;
Jian Li67e1e152016-04-18 17:52:58 -070023import org.onosproject.cpman.ControlLoadSnapshot;
Jian Li80c12702016-02-20 08:58:19 +090024import org.onosproject.cpman.ControlMetricType;
25import org.onosproject.cpman.ControlPlaneMonitorService;
26import org.onosproject.net.DeviceId;
27import org.onosproject.rest.AbstractWebResource;
28
29import javax.ws.rs.GET;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.util.Optional;
36import java.util.Set;
37
38import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
39import static org.onosproject.cpman.ControlResource.CPU_METRICS;
40import static org.onosproject.cpman.ControlResource.DISK_METRICS;
41import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
42import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
43import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE;
44import static org.onosproject.cpman.ControlResource.Type.DISK;
45import static org.onosproject.cpman.ControlResource.Type.NETWORK;
46
47/**
48 * Query control metrics.
49 */
Jian Lie277ebe2016-04-07 11:41:51 -070050@Path("controlmetrics")
Jian Li80c12702016-02-20 08:58:19 +090051public class ControlMetricsWebResource extends AbstractWebResource {
52
Jian Li80c12702016-02-20 08:58:19 +090053 /**
54 * Returns control message metrics of all devices.
55 *
56 * @return array of all control message metrics
Jian Lid86fac32016-04-01 16:10:26 -070057 * @onos.rsModel ControlMessageMetrics
Jian Li80c12702016-02-20 08:58:19 +090058 */
59 @GET
60 @Path("messages")
61 @Produces(MediaType.APPLICATION_JSON)
62 public Response controlMessageMetrics() {
Harshada Chaundkar4f62a1e2019-02-18 12:27:34 -050063 ObjectNode root = mapper().createObjectNode();
64 ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
65 ClusterService clusterService = get(ClusterService.class);
66 NodeId localNodeId = clusterService.getLocalNode().id();
Jian Li80c12702016-02-20 08:58:19 +090067 ArrayNode deviceNodes = root.putArray("devices");
Jian Li89eeccd2016-05-06 02:10:33 -070068 monitorService.availableResourcesSync(localNodeId, CONTROL_MESSAGE).forEach(name -> {
Jian Li80c12702016-02-20 08:58:19 +090069 ObjectNode deviceNode = mapper().createObjectNode();
70 ObjectNode valueNode = mapper().createObjectNode();
71
72 metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS,
73 DeviceId.deviceId(name), valueNode);
74 deviceNode.put("name", name);
75 deviceNode.set("value", valueNode);
76
77 deviceNodes.add(deviceNode);
78 });
79
80 return ok(root).build();
81 }
82
83 /**
84 * Returns control message metrics of a given device.
85 *
86 * @param deviceId device identification
87 * @return control message metrics of a given device
Jian Lid86fac32016-04-01 16:10:26 -070088 * @onos.rsModel ControlMessageMetric
Jian Li80c12702016-02-20 08:58:19 +090089 */
90 @GET
91 @Produces(MediaType.APPLICATION_JSON)
92 @Path("messages/{deviceId}")
93 public Response controlMessageMetrics(@PathParam("deviceId") String deviceId) {
Harshada Chaundkar4f62a1e2019-02-18 12:27:34 -050094 ObjectNode root = mapper().createObjectNode();
95 ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
96 ClusterService clusterService = get(ClusterService.class);
97 NodeId localNodeId = clusterService.getLocalNode().id();
Jian Li80c12702016-02-20 08:58:19 +090098 metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS,
99 DeviceId.deviceId(deviceId), root);
100
101 return ok(root).build();
102 }
103
104 /**
105 * Returns cpu metrics.
106 *
107 * @return cpu metrics
Jian Lid86fac32016-04-01 16:10:26 -0700108 * @onos.rsModel CpuMetrics
Jian Li80c12702016-02-20 08:58:19 +0900109 */
110 @GET
111 @Path("cpu_metrics")
112 @Produces(MediaType.APPLICATION_JSON)
113 public Response cpuMetrics() {
Harshada Chaundkar4f62a1e2019-02-18 12:27:34 -0500114 ObjectNode root = mapper().createObjectNode();
115 ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
116 ClusterService clusterService = get(ClusterService.class);
117 NodeId localNodeId = clusterService.getLocalNode().id();
Jian Li80c12702016-02-20 08:58:19 +0900118 metricsStats(monitorService, localNodeId, CPU_METRICS, root);
119 return ok(root).build();
120 }
121
122 /**
123 * Returns memory metrics.
124 *
125 * @return memory metrics
Jian Lid86fac32016-04-01 16:10:26 -0700126 * @onos.rsModel MemoryMetrics
Jian Li80c12702016-02-20 08:58:19 +0900127 */
128 @GET
129 @Path("memory_metrics")
130 @Produces(MediaType.APPLICATION_JSON)
131 public Response memoryMetrics() {
Harshada Chaundkar4f62a1e2019-02-18 12:27:34 -0500132 ObjectNode root = mapper().createObjectNode();
133 ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
134 ClusterService clusterService = get(ClusterService.class);
135 NodeId localNodeId = clusterService.getLocalNode().id();
Jian Li80c12702016-02-20 08:58:19 +0900136 metricsStats(monitorService, localNodeId, MEMORY_METRICS, root);
137 return ok(root).build();
138 }
139
140 /**
141 * Returns disk metrics of all resources.
142 *
143 * @return disk metrics of all resources
Jian Lid86fac32016-04-01 16:10:26 -0700144 * @onos.rsModel DiskMetrics
Jian Li80c12702016-02-20 08:58:19 +0900145 */
146 @GET
147 @Path("disk_metrics")
148 @Produces(MediaType.APPLICATION_JSON)
149 public Response diskMetrics() {
Harshada Chaundkar4f62a1e2019-02-18 12:27:34 -0500150 ObjectNode root = mapper().createObjectNode();
151 ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
152 ClusterService clusterService = get(ClusterService.class);
153 NodeId localNodeId = clusterService.getLocalNode().id();
Jian Li80c12702016-02-20 08:58:19 +0900154 ArrayNode diskNodes = root.putArray("disks");
Jian Li89eeccd2016-05-06 02:10:33 -0700155 monitorService.availableResourcesSync(localNodeId, DISK).forEach(name -> {
Jian Li80c12702016-02-20 08:58:19 +0900156 ObjectNode diskNode = mapper().createObjectNode();
157 ObjectNode valueNode = mapper().createObjectNode();
158
159 metricsStats(monitorService, localNodeId, DISK_METRICS, name, valueNode);
160 diskNode.put("name", name);
161 diskNode.set("value", valueNode);
162
163 diskNodes.add(diskNode);
164 });
165
166 return ok(root).build();
167 }
168
169 /**
170 * Returns network metrics of all resources.
171 *
172 * @return network metrics of all resources
Jian Lid86fac32016-04-01 16:10:26 -0700173 * @onos.rsModel NetworkMetrics
Jian Li80c12702016-02-20 08:58:19 +0900174 */
175 @GET
176 @Path("network_metrics")
177 @Produces(MediaType.APPLICATION_JSON)
178 public Response networkMetrics() {
Harshada Chaundkar4f62a1e2019-02-18 12:27:34 -0500179 ObjectNode root = mapper().createObjectNode();
180 ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
181 ClusterService clusterService = get(ClusterService.class);
182 NodeId localNodeId = clusterService.getLocalNode().id();
Jian Li80c12702016-02-20 08:58:19 +0900183 ArrayNode networkNodes = root.putArray("networks");
Jian Li89eeccd2016-05-06 02:10:33 -0700184 monitorService.availableResourcesSync(localNodeId, NETWORK).forEach(name -> {
Jian Li80c12702016-02-20 08:58:19 +0900185 ObjectNode networkNode = mapper().createObjectNode();
186 ObjectNode valueNode = mapper().createObjectNode();
187
188 metricsStats(monitorService, localNodeId, NETWORK_METRICS, name, valueNode);
189 networkNode.put("name", name);
190 networkNode.set("value", valueNode);
191
192 networkNodes.add(networkNode);
193 });
194
195 return ok(root).build();
196 }
197
198 /**
199 * Returns a collection of control message stats.
200 *
201 * @param service control plane monitoring service
202 * @param nodeId node identification
203 * @param typeSet a set of control message types
204 * @param did device identification
205 * @param node object node
206 * @return a collection of control message stats
207 */
208 private ArrayNode metricsStats(ControlPlaneMonitorService service,
209 NodeId nodeId, Set<ControlMetricType> typeSet,
210 DeviceId did, ObjectNode node) {
211 return metricsStats(service, nodeId, typeSet, null, did, node);
212 }
213
214 /**
215 * Returns a collection of system metric stats.
216 *
217 * @param service control plane monitoring service
218 * @param nodeId node identification
219 * @param typeSet a set of system metric types
220 * @param node object node
221 * @return a collection of system metric stats
222 */
223 private ArrayNode metricsStats(ControlPlaneMonitorService service,
224 NodeId nodeId, Set<ControlMetricType> typeSet,
225 ObjectNode node) {
226 return metricsStats(service, nodeId, typeSet, null, null, node);
227 }
228
229 /**
230 * Returns a collection of system metric stats.
231 *
232 * @param service control plane monitoring service
233 * @param nodeId node identification
234 * @param typeSet a set of control message types
235 * @param resourceName device identification
236 * @param node object node
237 * @return a collection of system metric stats
238 */
239 private ArrayNode metricsStats(ControlPlaneMonitorService service,
240 NodeId nodeId, Set<ControlMetricType> typeSet,
241 String resourceName, ObjectNode node) {
242 return metricsStats(service, nodeId, typeSet, resourceName, null, node);
243 }
244
245 /**
246 * Returns a collection of control loads of the given control metric types.
247 *
248 * @param service control plane monitoring service
249 * @param nodeId node identification
250 * @param typeSet a group of control metric types
251 * @param name resource name
252 * @param did device identification
253 * @return a collection of control loads
254 */
255 private ArrayNode metricsStats(ControlPlaneMonitorService service,
256 NodeId nodeId, Set<ControlMetricType> typeSet,
257 String name, DeviceId did, ObjectNode node) {
258 ArrayNode metricsNode = node.putArray("metrics");
259
260 if (name == null && did == null) {
261 typeSet.forEach(type -> {
Jian Lif248aa22016-05-09 10:33:02 -0700262 ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.empty());
263 processRest(cls, type, metricsNode);
Jian Li80c12702016-02-20 08:58:19 +0900264 });
265 } else if (name == null) {
266 typeSet.forEach(type -> {
Jian Lif248aa22016-05-09 10:33:02 -0700267 ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.of(did));
268 processRest(cls, type, metricsNode);
Jian Li80c12702016-02-20 08:58:19 +0900269 });
270 } else if (did == null) {
271 typeSet.forEach(type -> {
Jian Lif248aa22016-05-09 10:33:02 -0700272 ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, name);
273 processRest(cls, type, metricsNode);
Jian Li80c12702016-02-20 08:58:19 +0900274 });
275 }
276
277 return metricsNode;
278 }
Jian Li8b0ea962016-04-04 11:49:29 -0700279
Jian Lif248aa22016-05-09 10:33:02 -0700280 /**
281 * Camelizes the input string.
282 *
283 * @param value original string
284 * @param startWithLowerCase flag that determines whether to use lower case
285 * for the camelized string
286 * @return camelized string
287 */
Jian Li8b0ea962016-04-04 11:49:29 -0700288 private String toCamelCase(String value, boolean startWithLowerCase) {
289 String[] strings = StringUtils.split(value.toLowerCase(), "_");
290 for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++) {
291 strings[i] = StringUtils.capitalize(strings[i]);
292 }
293 return StringUtils.join(strings);
294 }
Jian Lif248aa22016-05-09 10:33:02 -0700295
296 /**
297 * Transforms control load snapshot object into JSON object.
298 *
299 * @param cls control load snapshot
300 * @param type control metric type
301 * @param metricsNode array of JSON node
302 */
303 private void processRest(ControlLoadSnapshot cls, ControlMetricType type, ArrayNode metricsNode) {
304 ObjectNode metricNode = mapper().createObjectNode();
305
306 if (cls != null) {
307 metricNode.set(toCamelCase(type.toString(), true),
308 codec(ControlLoadSnapshot.class).encode(cls, this));
309 metricsNode.add(metricNode);
310 }
311 }
Jian Li80c12702016-02-20 08:58:19 +0900312}