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 | |
| 53 | private final ControlPlaneMonitorService monitorService = |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 54 | get(ControlPlaneMonitorService.class); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 63 | * @onos.rsModel ControlMessageMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 64 | */ |
| 65 | @GET |
| 66 | @Path("messages") |
| 67 | @Produces(MediaType.APPLICATION_JSON) |
| 68 | public Response controlMessageMetrics() { |
| 69 | |
| 70 | ArrayNode deviceNodes = root.putArray("devices"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 71 | monitorService.availableResourcesSync(localNodeId, CONTROL_MESSAGE).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 91 | * @onos.rsModel ControlMessageMetric |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 92 | */ |
| 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 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() { |
| 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 123 | * @onos.rsModel MemoryMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 124 | */ |
| 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 138 | * @onos.rsModel DiskMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 139 | */ |
| 140 | @GET |
| 141 | @Path("disk_metrics") |
| 142 | @Produces(MediaType.APPLICATION_JSON) |
| 143 | public Response diskMetrics() { |
| 144 | |
| 145 | ArrayNode diskNodes = root.putArray("disks"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 146 | monitorService.availableResourcesSync(localNodeId, DISK).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 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 Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 164 | * @onos.rsModel NetworkMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 165 | */ |
| 166 | @GET |
| 167 | @Path("network_metrics") |
| 168 | @Produces(MediaType.APPLICATION_JSON) |
| 169 | public Response networkMetrics() { |
| 170 | |
| 171 | ArrayNode networkNodes = root.putArray("networks"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame] | 172 | monitorService.availableResourcesSync(localNodeId, NETWORK).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 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 -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 250 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.empty()); |
| 251 | processRest(cls, type, metricsNode); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 252 | }); |
| 253 | } else if (name == null) { |
| 254 | typeSet.forEach(type -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 255 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, Optional.of(did)); |
| 256 | processRest(cls, type, metricsNode); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 257 | }); |
| 258 | } else if (did == null) { |
| 259 | typeSet.forEach(type -> { |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 260 | ControlLoadSnapshot cls = service.getLoadSync(nodeId, type, name); |
| 261 | processRest(cls, type, metricsNode); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 262 | }); |
| 263 | } |
| 264 | |
| 265 | return metricsNode; |
| 266 | } |
Jian Li | 8b0ea96 | 2016-04-04 11:49:29 -0700 | [diff] [blame] | 267 | |
Jian Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 268 | /** |
| 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 Li | 8b0ea96 | 2016-04-04 11:49:29 -0700 | [diff] [blame] | 276 | 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 Li | f248aa2 | 2016-05-09 10:33:02 -0700 | [diff] [blame] | 283 | |
| 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 Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 300 | } |