Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cpman.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 8b0ea96 | 2016-04-04 11:49:29 -0700 | [diff] [blame] | 20 | import org.apache.commons.lang3.StringUtils; |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 21 | import org.onosproject.cluster.ClusterService; |
| 22 | import org.onosproject.cluster.NodeId; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 23 | import org.onosproject.cpman.ControlLoadSnapshot; |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 24 | import org.onosproject.cpman.ControlMetricType; |
| 25 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.rest.AbstractWebResource; |
| 28 | |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | import java.util.Optional; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 39 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 40 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 41 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 42 | import static org.onosproject.cpman.ControlResource.NETWORK_METRICS; |
| 43 | import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE; |
| 44 | import static org.onosproject.cpman.ControlResource.Type.DISK; |
| 45 | import static org.onosproject.cpman.ControlResource.Type.NETWORK; |
| 46 | |
| 47 | /** |
| 48 | * Query control metrics. |
| 49 | */ |
Jian Li | e277ebe | 2016-04-07 11:41:51 -0700 | [diff] [blame] | 50 | @Path("controlmetrics") |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 51 | public class ControlMetricsWebResource extends AbstractWebResource { |
| 52 | |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 53 | /** |
| 54 | * Returns control message metrics of all devices. |
| 55 | * |
| 56 | * @return array of all control message metrics |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 57 | * @onos.rsModel ControlMessageMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 58 | */ |
| 59 | @GET |
| 60 | @Path("messages") |
| 61 | @Produces(MediaType.APPLICATION_JSON) |
| 62 | public Response controlMessageMetrics() { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame^] | 63 | ObjectNode root = mapper().createObjectNode(); |
| 64 | ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
| 65 | ClusterService clusterService = get(ClusterService.class); |
| 66 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 67 | ArrayNode deviceNodes = root.putArray("devices"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 68 | monitorService.availableResourcesSync(localNodeId, CONTROL_MESSAGE).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 69 | 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 88 | * @onos.rsModel ControlMessageMetric |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 89 | */ |
| 90 | @GET |
| 91 | @Produces(MediaType.APPLICATION_JSON) |
| 92 | @Path("messages/{deviceId}") |
| 93 | public Response controlMessageMetrics(@PathParam("deviceId") String deviceId) { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame^] | 94 | ObjectNode root = mapper().createObjectNode(); |
| 95 | ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
| 96 | ClusterService clusterService = get(ClusterService.class); |
| 97 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 108 | * @onos.rsModel CpuMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 109 | */ |
| 110 | @GET |
| 111 | @Path("cpu_metrics") |
| 112 | @Produces(MediaType.APPLICATION_JSON) |
| 113 | public Response cpuMetrics() { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame^] | 114 | ObjectNode root = mapper().createObjectNode(); |
| 115 | ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
| 116 | ClusterService clusterService = get(ClusterService.class); |
| 117 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 118 | metricsStats(monitorService, localNodeId, CPU_METRICS, root); |
| 119 | return ok(root).build(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns memory metrics. |
| 124 | * |
| 125 | * @return memory metrics |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 126 | * @onos.rsModel MemoryMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 127 | */ |
| 128 | @GET |
| 129 | @Path("memory_metrics") |
| 130 | @Produces(MediaType.APPLICATION_JSON) |
| 131 | public Response memoryMetrics() { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame^] | 132 | ObjectNode root = mapper().createObjectNode(); |
| 133 | ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
| 134 | ClusterService clusterService = get(ClusterService.class); |
| 135 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 136 | 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 144 | * @onos.rsModel DiskMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 145 | */ |
| 146 | @GET |
| 147 | @Path("disk_metrics") |
| 148 | @Produces(MediaType.APPLICATION_JSON) |
| 149 | public Response diskMetrics() { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame^] | 150 | ObjectNode root = mapper().createObjectNode(); |
| 151 | ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
| 152 | ClusterService clusterService = get(ClusterService.class); |
| 153 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 154 | ArrayNode diskNodes = root.putArray("disks"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 155 | monitorService.availableResourcesSync(localNodeId, DISK).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 156 | 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 173 | * @onos.rsModel NetworkMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 174 | */ |
| 175 | @GET |
| 176 | @Path("network_metrics") |
| 177 | @Produces(MediaType.APPLICATION_JSON) |
| 178 | public Response networkMetrics() { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame^] | 179 | ObjectNode root = mapper().createObjectNode(); |
| 180 | ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
| 181 | ClusterService clusterService = get(ClusterService.class); |
| 182 | NodeId localNodeId = clusterService.getLocalNode().id(); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 183 | ArrayNode networkNodes = root.putArray("networks"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 184 | monitorService.availableResourcesSync(localNodeId, NETWORK).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 185 | 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 Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 262 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.empty()); |
| 263 | processRest(cls, type, metricsNode); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 264 | }); |
| 265 | } else if (name == null) { |
| 266 | typeSet.forEach(type -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 267 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.of(did)); |
| 268 | processRest(cls, type, metricsNode); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 269 | }); |
| 270 | } else if (did == null) { |
| 271 | typeSet.forEach(type -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 272 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, name); |
| 273 | processRest(cls, type, metricsNode); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 274 | }); |
| 275 | } |
| 276 | |
| 277 | return metricsNode; |
| 278 | } |
Jian Li | 8b0ea96 | 2016-04-04 11:49:29 -0700 | [diff] [blame] | 279 | |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 280 | /** |
| 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 Li | 8b0ea96 | 2016-04-04 11:49:29 -0700 | [diff] [blame] | 288 | 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 Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 295 | |
| 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 Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 312 | } |