blob: 8fb899d211768fa0fbc9f2e2fe1107987083b257 [file] [log] [blame]
Jian Li80c12702016-02-20 08:58:19 +09001/*
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.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;
23import org.onosproject.cpman.ControlLoad;
24import 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 */
50@Path("metrics")
51public class ControlMetricsWebResource extends AbstractWebResource {
52
53 private final ControlPlaneMonitorService monitorService =
54 get(ControlPlaneMonitorService.class);
55 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");
71 monitorService.availableResources(CONTROL_MESSAGE).forEach(name -> {
72 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");
146 monitorService.availableResources(DISK).forEach(name -> {
147 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");
172 monitorService.availableResources(NETWORK).forEach(name -> {
173 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 -> {
250 ObjectNode metricNode = mapper().createObjectNode();
Jian Li23906cc2016-03-31 11:16:44 -0700251 ControlLoad load = service.getLocalLoad(type, Optional.ofNullable(null));
Jian Li80c12702016-02-20 08:58:19 +0900252 if (load != null) {
Jian Li8b0ea962016-04-04 11:49:29 -0700253 metricNode.set(toCamelCase(type.toString(), true), codec(ControlLoad.class)
Jian Li23906cc2016-03-31 11:16:44 -0700254 .encode(service.getLocalLoad(type, Optional.ofNullable(null)), this));
Jian Li80c12702016-02-20 08:58:19 +0900255 metricsNode.add(metricNode);
256 }
257 });
258 } else if (name == null) {
259 typeSet.forEach(type -> {
260 ObjectNode metricNode = mapper().createObjectNode();
Jian Li23906cc2016-03-31 11:16:44 -0700261 ControlLoad load = service.getLocalLoad(type, Optional.of(did));
Jian Li80c12702016-02-20 08:58:19 +0900262 if (load != null) {
Jian Li8b0ea962016-04-04 11:49:29 -0700263 metricNode.set(toCamelCase(type.toString(), true),
Jian Li80c12702016-02-20 08:58:19 +0900264 codec(ControlLoad.class).encode(load, this));
265 metricsNode.add(metricNode);
266 }
267 });
268 } else if (did == null) {
269 typeSet.forEach(type -> {
270 ObjectNode metricNode = mapper().createObjectNode();
Jian Li23906cc2016-03-31 11:16:44 -0700271 ControlLoad load = service.getLocalLoad(type, name);
Jian Li80c12702016-02-20 08:58:19 +0900272 if (load != null) {
Jian Li8b0ea962016-04-04 11:49:29 -0700273 metricNode.set(toCamelCase(type.toString(), true),
Jian Li80c12702016-02-20 08:58:19 +0900274 codec(ControlLoad.class).encode(load, this));
275 metricsNode.add(metricNode);
276 }
277 });
278 }
279
280 return metricsNode;
281 }
Jian Li8b0ea962016-04-04 11:49:29 -0700282
283 private String toCamelCase(String value, boolean startWithLowerCase) {
284 String[] strings = StringUtils.split(value.toLowerCase(), "_");
285 for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++) {
286 strings[i] = StringUtils.capitalize(strings[i]);
287 }
288 return StringUtils.join(strings);
289 }
Jian Li80c12702016-02-20 08:58:19 +0900290}