blob: 59dfee6f9675193232c990abc490a6ecf100114b [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
53 private final ControlPlaneMonitorService monitorService =
Jian Li89eeccd2016-05-06 02:10:33 -070054 get(ControlPlaneMonitorService.class);
Jian Li80c12702016-02-20 08:58:19 +090055 private final ClusterService clusterService = get(ClusterService.class);
56 private final NodeId localNodeId = clusterService.getLocalNode().id();
57 private final ObjectNode root = mapper().createObjectNode();
58
59 /**
60 * Returns control message metrics of all devices.
61 *
62 * @return array of all control message metrics
Jian Lid86fac32016-04-01 16:10:26 -070063 * @onos.rsModel ControlMessageMetrics
Jian Li80c12702016-02-20 08:58:19 +090064 */
65 @GET
66 @Path("messages")
67 @Produces(MediaType.APPLICATION_JSON)
68 public Response controlMessageMetrics() {
69
70 ArrayNode deviceNodes = root.putArray("devices");
Jian Li89eeccd2016-05-06 02:10:33 -070071 monitorService.availableResourcesSync(localNodeId, CONTROL_MESSAGE).forEach(name -> {
Jian Li80c12702016-02-20 08:58:19 +090072 ObjectNode deviceNode = mapper().createObjectNode();
73 ObjectNode valueNode = mapper().createObjectNode();
74
75 metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS,
76 DeviceId.deviceId(name), valueNode);
77 deviceNode.put("name", name);
78 deviceNode.set("value", valueNode);
79
80 deviceNodes.add(deviceNode);
81 });
82
83 return ok(root).build();
84 }
85
86 /**
87 * Returns control message metrics of a given device.
88 *
89 * @param deviceId device identification
90 * @return control message metrics of a given device
Jian Lid86fac32016-04-01 16:10:26 -070091 * @onos.rsModel ControlMessageMetric
Jian Li80c12702016-02-20 08:58:19 +090092 */
93 @GET
94 @Produces(MediaType.APPLICATION_JSON)
95 @Path("messages/{deviceId}")
96 public Response controlMessageMetrics(@PathParam("deviceId") String deviceId) {
97
98 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() {
114
115 metricsStats(monitorService, localNodeId, CPU_METRICS, root);
116 return ok(root).build();
117 }
118
119 /**
120 * Returns memory metrics.
121 *
122 * @return memory metrics
Jian Lid86fac32016-04-01 16:10:26 -0700123 * @onos.rsModel MemoryMetrics
Jian Li80c12702016-02-20 08:58:19 +0900124 */
125 @GET
126 @Path("memory_metrics")
127 @Produces(MediaType.APPLICATION_JSON)
128 public Response memoryMetrics() {
129
130 metricsStats(monitorService, localNodeId, MEMORY_METRICS, root);
131 return ok(root).build();
132 }
133
134 /**
135 * Returns disk metrics of all resources.
136 *
137 * @return disk metrics of all resources
Jian Lid86fac32016-04-01 16:10:26 -0700138 * @onos.rsModel DiskMetrics
Jian Li80c12702016-02-20 08:58:19 +0900139 */
140 @GET
141 @Path("disk_metrics")
142 @Produces(MediaType.APPLICATION_JSON)
143 public Response diskMetrics() {
144
145 ArrayNode diskNodes = root.putArray("disks");
Jian Li89eeccd2016-05-06 02:10:33 -0700146 monitorService.availableResourcesSync(localNodeId, DISK).forEach(name -> {
Jian Li80c12702016-02-20 08:58:19 +0900147 ObjectNode diskNode = mapper().createObjectNode();
148 ObjectNode valueNode = mapper().createObjectNode();
149
150 metricsStats(monitorService, localNodeId, DISK_METRICS, name, valueNode);
151 diskNode.put("name", name);
152 diskNode.set("value", valueNode);
153
154 diskNodes.add(diskNode);
155 });
156
157 return ok(root).build();
158 }
159
160 /**
161 * Returns network metrics of all resources.
162 *
163 * @return network metrics of all resources
Jian Lid86fac32016-04-01 16:10:26 -0700164 * @onos.rsModel NetworkMetrics
Jian Li80c12702016-02-20 08:58:19 +0900165 */
166 @GET
167 @Path("network_metrics")
168 @Produces(MediaType.APPLICATION_JSON)
169 public Response networkMetrics() {
170
171 ArrayNode networkNodes = root.putArray("networks");
Jian Li89eeccd2016-05-06 02:10:33 -0700172 monitorService.availableResourcesSync(localNodeId, NETWORK).forEach(name -> {
Jian Li80c12702016-02-20 08:58:19 +0900173 ObjectNode networkNode = mapper().createObjectNode();
174 ObjectNode valueNode = mapper().createObjectNode();
175
176 metricsStats(monitorService, localNodeId, NETWORK_METRICS, name, valueNode);
177 networkNode.put("name", name);
178 networkNode.set("value", valueNode);
179
180 networkNodes.add(networkNode);
181 });
182
183 return ok(root).build();
184 }
185
186 /**
187 * Returns a collection of control message stats.
188 *
189 * @param service control plane monitoring service
190 * @param nodeId node identification
191 * @param typeSet a set of control message types
192 * @param did device identification
193 * @param node object node
194 * @return a collection of control message stats
195 */
196 private ArrayNode metricsStats(ControlPlaneMonitorService service,
197 NodeId nodeId, Set<ControlMetricType> typeSet,
198 DeviceId did, ObjectNode node) {
199 return metricsStats(service, nodeId, typeSet, null, did, node);
200 }
201
202 /**
203 * Returns a collection of system metric stats.
204 *
205 * @param service control plane monitoring service
206 * @param nodeId node identification
207 * @param typeSet a set of system metric types
208 * @param node object node
209 * @return a collection of system metric stats
210 */
211 private ArrayNode metricsStats(ControlPlaneMonitorService service,
212 NodeId nodeId, Set<ControlMetricType> typeSet,
213 ObjectNode node) {
214 return metricsStats(service, nodeId, typeSet, null, null, node);
215 }
216
217 /**
218 * Returns a collection of system metric stats.
219 *
220 * @param service control plane monitoring service
221 * @param nodeId node identification
222 * @param typeSet a set of control message types
223 * @param resourceName device identification
224 * @param node object node
225 * @return a collection of system metric stats
226 */
227 private ArrayNode metricsStats(ControlPlaneMonitorService service,
228 NodeId nodeId, Set<ControlMetricType> typeSet,
229 String resourceName, ObjectNode node) {
230 return metricsStats(service, nodeId, typeSet, resourceName, null, node);
231 }
232
233 /**
234 * Returns a collection of control loads of the given control metric types.
235 *
236 * @param service control plane monitoring service
237 * @param nodeId node identification
238 * @param typeSet a group of control metric types
239 * @param name resource name
240 * @param did device identification
241 * @return a collection of control loads
242 */
243 private ArrayNode metricsStats(ControlPlaneMonitorService service,
244 NodeId nodeId, Set<ControlMetricType> typeSet,
245 String name, DeviceId did, ObjectNode node) {
246 ArrayNode metricsNode = node.putArray("metrics");
247
248 if (name == null && did == null) {
249 typeSet.forEach(type -> {
Jian Lif248aa22016-05-09 10:33:02 -0700250 ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.empty());
251 processRest(cls, type, metricsNode);
Jian Li80c12702016-02-20 08:58:19 +0900252 });
253 } else if (name == null) {
254 typeSet.forEach(type -> {
Jian Lif248aa22016-05-09 10:33:02 -0700255 ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.of(did));
256 processRest(cls, type, metricsNode);
Jian Li80c12702016-02-20 08:58:19 +0900257 });
258 } else if (did == null) {
259 typeSet.forEach(type -> {
Jian Lif248aa22016-05-09 10:33:02 -0700260 ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, name);
261 processRest(cls, type, metricsNode);
Jian Li80c12702016-02-20 08:58:19 +0900262 });
263 }
264
265 return metricsNode;
266 }
Jian Li8b0ea962016-04-04 11:49:29 -0700267
Jian Lif248aa22016-05-09 10:33:02 -0700268 /**
269 * Camelizes the input string.
270 *
271 * @param value original string
272 * @param startWithLowerCase flag that determines whether to use lower case
273 * for the camelized string
274 * @return camelized string
275 */
Jian Li8b0ea962016-04-04 11:49:29 -0700276 private String toCamelCase(String value, boolean startWithLowerCase) {
277 String[] strings = StringUtils.split(value.toLowerCase(), "_");
278 for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++) {
279 strings[i] = StringUtils.capitalize(strings[i]);
280 }
281 return StringUtils.join(strings);
282 }
Jian Lif248aa22016-05-09 10:33:02 -0700283
284 /**
285 * Transforms control load snapshot object into JSON object.
286 *
287 * @param cls control load snapshot
288 * @param type control metric type
289 * @param metricsNode array of JSON node
290 */
291 private void processRest(ControlLoadSnapshot cls, ControlMetricType type, ArrayNode metricsNode) {
292 ObjectNode metricNode = mapper().createObjectNode();
293
294 if (cls != null) {
295 metricNode.set(toCamelCase(type.toString(), true),
296 codec(ControlLoadSnapshot.class).encode(cls, this));
297 metricsNode.add(metricNode);
298 }
299 }
Jian Li80c12702016-02-20 08:58:19 +0900300}