blob: 12e1083ad7c4b18ead6230e7ab0988ee40d2d96d [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;
20import org.onosproject.cluster.ClusterService;
21import org.onosproject.cluster.NodeId;
22import org.onosproject.cpman.ControlLoad;
23import org.onosproject.cpman.ControlMetricType;
24import org.onosproject.cpman.ControlPlaneMonitorService;
25import org.onosproject.net.DeviceId;
26import org.onosproject.rest.AbstractWebResource;
27
28import javax.ws.rs.GET;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.util.Optional;
35import java.util.Set;
36
37import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
38import static org.onosproject.cpman.ControlResource.CPU_METRICS;
39import static org.onosproject.cpman.ControlResource.DISK_METRICS;
40import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
41import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
42import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE;
43import static org.onosproject.cpman.ControlResource.Type.DISK;
44import static org.onosproject.cpman.ControlResource.Type.NETWORK;
45
46/**
47 * Query control metrics.
48 */
49@Path("metrics")
50public class ControlMetricsWebResource extends AbstractWebResource {
51
52 private final ControlPlaneMonitorService monitorService =
53 get(ControlPlaneMonitorService.class);
54 private final ClusterService clusterService = get(ClusterService.class);
55 private final NodeId localNodeId = clusterService.getLocalNode().id();
56 private final ObjectNode root = mapper().createObjectNode();
57
58 /**
59 * Returns control message metrics of all devices.
60 *
61 * @return array of all control message metrics
Jian Lid86fac32016-04-01 16:10:26 -070062 * @onos.rsModel ControlMessageMetrics
Jian Li80c12702016-02-20 08:58:19 +090063 */
64 @GET
65 @Path("messages")
66 @Produces(MediaType.APPLICATION_JSON)
67 public Response controlMessageMetrics() {
68
69 ArrayNode deviceNodes = root.putArray("devices");
70 monitorService.availableResources(CONTROL_MESSAGE).forEach(name -> {
71 ObjectNode deviceNode = mapper().createObjectNode();
72 ObjectNode valueNode = mapper().createObjectNode();
73
74 metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS,
75 DeviceId.deviceId(name), valueNode);
76 deviceNode.put("name", name);
77 deviceNode.set("value", valueNode);
78
79 deviceNodes.add(deviceNode);
80 });
81
82 return ok(root).build();
83 }
84
85 /**
86 * Returns control message metrics of a given device.
87 *
88 * @param deviceId device identification
89 * @return control message metrics of a given device
Jian Lid86fac32016-04-01 16:10:26 -070090 * @onos.rsModel ControlMessageMetric
Jian Li80c12702016-02-20 08:58:19 +090091 */
92 @GET
93 @Produces(MediaType.APPLICATION_JSON)
94 @Path("messages/{deviceId}")
95 public Response controlMessageMetrics(@PathParam("deviceId") String deviceId) {
96
97 metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS,
98 DeviceId.deviceId(deviceId), root);
99
100 return ok(root).build();
101 }
102
103 /**
104 * Returns cpu metrics.
105 *
106 * @return cpu metrics
Jian Lid86fac32016-04-01 16:10:26 -0700107 * @onos.rsModel CpuMetrics
Jian Li80c12702016-02-20 08:58:19 +0900108 */
109 @GET
110 @Path("cpu_metrics")
111 @Produces(MediaType.APPLICATION_JSON)
112 public Response cpuMetrics() {
113
114 metricsStats(monitorService, localNodeId, CPU_METRICS, root);
115 return ok(root).build();
116 }
117
118 /**
119 * Returns memory metrics.
120 *
121 * @return memory metrics
Jian Lid86fac32016-04-01 16:10:26 -0700122 * @onos.rsModel MemoryMetrics
Jian Li80c12702016-02-20 08:58:19 +0900123 */
124 @GET
125 @Path("memory_metrics")
126 @Produces(MediaType.APPLICATION_JSON)
127 public Response memoryMetrics() {
128
129 metricsStats(monitorService, localNodeId, MEMORY_METRICS, root);
130 return ok(root).build();
131 }
132
133 /**
134 * Returns disk metrics of all resources.
135 *
136 * @return disk metrics of all resources
Jian Lid86fac32016-04-01 16:10:26 -0700137 * @onos.rsModel DiskMetrics
Jian Li80c12702016-02-20 08:58:19 +0900138 */
139 @GET
140 @Path("disk_metrics")
141 @Produces(MediaType.APPLICATION_JSON)
142 public Response diskMetrics() {
143
144 ArrayNode diskNodes = root.putArray("disks");
145 monitorService.availableResources(DISK).forEach(name -> {
146 ObjectNode diskNode = mapper().createObjectNode();
147 ObjectNode valueNode = mapper().createObjectNode();
148
149 metricsStats(monitorService, localNodeId, DISK_METRICS, name, valueNode);
150 diskNode.put("name", name);
151 diskNode.set("value", valueNode);
152
153 diskNodes.add(diskNode);
154 });
155
156 return ok(root).build();
157 }
158
159 /**
160 * Returns network metrics of all resources.
161 *
162 * @return network metrics of all resources
Jian Lid86fac32016-04-01 16:10:26 -0700163 * @onos.rsModel NetworkMetrics
Jian Li80c12702016-02-20 08:58:19 +0900164 */
165 @GET
166 @Path("network_metrics")
167 @Produces(MediaType.APPLICATION_JSON)
168 public Response networkMetrics() {
169
170 ArrayNode networkNodes = root.putArray("networks");
171 monitorService.availableResources(NETWORK).forEach(name -> {
172 ObjectNode networkNode = mapper().createObjectNode();
173 ObjectNode valueNode = mapper().createObjectNode();
174
175 metricsStats(monitorService, localNodeId, NETWORK_METRICS, name, valueNode);
176 networkNode.put("name", name);
177 networkNode.set("value", valueNode);
178
179 networkNodes.add(networkNode);
180 });
181
182 return ok(root).build();
183 }
184
185 /**
186 * Returns a collection of control message stats.
187 *
188 * @param service control plane monitoring service
189 * @param nodeId node identification
190 * @param typeSet a set of control message types
191 * @param did device identification
192 * @param node object node
193 * @return a collection of control message stats
194 */
195 private ArrayNode metricsStats(ControlPlaneMonitorService service,
196 NodeId nodeId, Set<ControlMetricType> typeSet,
197 DeviceId did, ObjectNode node) {
198 return metricsStats(service, nodeId, typeSet, null, did, node);
199 }
200
201 /**
202 * Returns a collection of system metric stats.
203 *
204 * @param service control plane monitoring service
205 * @param nodeId node identification
206 * @param typeSet a set of system metric types
207 * @param node object node
208 * @return a collection of system metric stats
209 */
210 private ArrayNode metricsStats(ControlPlaneMonitorService service,
211 NodeId nodeId, Set<ControlMetricType> typeSet,
212 ObjectNode node) {
213 return metricsStats(service, nodeId, typeSet, null, null, node);
214 }
215
216 /**
217 * Returns a collection of system metric stats.
218 *
219 * @param service control plane monitoring service
220 * @param nodeId node identification
221 * @param typeSet a set of control message types
222 * @param resourceName device identification
223 * @param node object node
224 * @return a collection of system metric stats
225 */
226 private ArrayNode metricsStats(ControlPlaneMonitorService service,
227 NodeId nodeId, Set<ControlMetricType> typeSet,
228 String resourceName, ObjectNode node) {
229 return metricsStats(service, nodeId, typeSet, resourceName, null, node);
230 }
231
232 /**
233 * Returns a collection of control loads of the given control metric types.
234 *
235 * @param service control plane monitoring service
236 * @param nodeId node identification
237 * @param typeSet a group of control metric types
238 * @param name resource name
239 * @param did device identification
240 * @return a collection of control loads
241 */
242 private ArrayNode metricsStats(ControlPlaneMonitorService service,
243 NodeId nodeId, Set<ControlMetricType> typeSet,
244 String name, DeviceId did, ObjectNode node) {
245 ArrayNode metricsNode = node.putArray("metrics");
246
247 if (name == null && did == null) {
248 typeSet.forEach(type -> {
249 ObjectNode metricNode = mapper().createObjectNode();
Jian Li23906cc2016-03-31 11:16:44 -0700250 ControlLoad load = service.getLocalLoad(type, Optional.ofNullable(null));
Jian Li80c12702016-02-20 08:58:19 +0900251 if (load != null) {
252 metricNode.set(type.toString().toLowerCase(), codec(ControlLoad.class)
Jian Li23906cc2016-03-31 11:16:44 -0700253 .encode(service.getLocalLoad(type, Optional.ofNullable(null)), this));
Jian Li80c12702016-02-20 08:58:19 +0900254 metricsNode.add(metricNode);
255 }
256 });
257 } else if (name == null) {
258 typeSet.forEach(type -> {
259 ObjectNode metricNode = mapper().createObjectNode();
Jian Li23906cc2016-03-31 11:16:44 -0700260 ControlLoad load = service.getLocalLoad(type, Optional.of(did));
Jian Li80c12702016-02-20 08:58:19 +0900261 if (load != null) {
262 metricNode.set(type.toString().toLowerCase(),
263 codec(ControlLoad.class).encode(load, this));
264 metricsNode.add(metricNode);
265 }
266 });
267 } else if (did == null) {
268 typeSet.forEach(type -> {
269 ObjectNode metricNode = mapper().createObjectNode();
Jian Li23906cc2016-03-31 11:16:44 -0700270 ControlLoad load = service.getLocalLoad(type, name);
Jian Li80c12702016-02-20 08:58:19 +0900271 if (load != null) {
272 metricNode.set(type.toString().toLowerCase(),
273 codec(ControlLoad.class).encode(load, this));
274 metricsNode.add(metricNode);
275 }
276 });
277 }
278
279 return metricsNode;
280 }
281}