Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 21 | import org.onlab.util.Tools; |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 22 | import org.onosproject.cluster.ClusterService; |
| 23 | import org.onosproject.cluster.NodeId; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 24 | import org.onosproject.cpman.ControlLoadSnapshot; |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 25 | import org.onosproject.cpman.ControlMetricType; |
| 26 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.rest.AbstractWebResource; |
| 29 | |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import java.util.Optional; |
| 37 | import java.util.Set; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 38 | import java.util.concurrent.CompletableFuture; |
| 39 | import java.util.concurrent.TimeUnit; |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 40 | |
| 41 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 42 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 43 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 44 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 45 | import static org.onosproject.cpman.ControlResource.NETWORK_METRICS; |
| 46 | import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE; |
| 47 | import static org.onosproject.cpman.ControlResource.Type.DISK; |
| 48 | import static org.onosproject.cpman.ControlResource.Type.NETWORK; |
| 49 | |
| 50 | /** |
| 51 | * Query control metrics. |
| 52 | */ |
Jian Li | e277ebe | 2016-04-07 11:41:51 -0700 | [diff] [blame] | 53 | @Path("controlmetrics") |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 54 | public class ControlMetricsWebResource extends AbstractWebResource { |
| 55 | |
| 56 | private final ControlPlaneMonitorService monitorService = |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame^] | 57 | get(ControlPlaneMonitorService.class); |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 58 | private final ClusterService clusterService = get(ClusterService.class); |
| 59 | private final NodeId localNodeId = clusterService.getLocalNode().id(); |
| 60 | private final ObjectNode root = mapper().createObjectNode(); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 61 | private static final long TIMEOUT_MILLIS = 1000; |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Returns control message metrics of all devices. |
| 65 | * |
| 66 | * @return array of all control message metrics |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 67 | * @onos.rsModel ControlMessageMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 68 | */ |
| 69 | @GET |
| 70 | @Path("messages") |
| 71 | @Produces(MediaType.APPLICATION_JSON) |
| 72 | public Response controlMessageMetrics() { |
| 73 | |
| 74 | ArrayNode deviceNodes = root.putArray("devices"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame^] | 75 | monitorService.availableResourcesSync(localNodeId, CONTROL_MESSAGE).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 76 | ObjectNode deviceNode = mapper().createObjectNode(); |
| 77 | ObjectNode valueNode = mapper().createObjectNode(); |
| 78 | |
| 79 | metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS, |
| 80 | DeviceId.deviceId(name), valueNode); |
| 81 | deviceNode.put("name", name); |
| 82 | deviceNode.set("value", valueNode); |
| 83 | |
| 84 | deviceNodes.add(deviceNode); |
| 85 | }); |
| 86 | |
| 87 | return ok(root).build(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns control message metrics of a given device. |
| 92 | * |
| 93 | * @param deviceId device identification |
| 94 | * @return control message metrics of a given device |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 95 | * @onos.rsModel ControlMessageMetric |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 96 | */ |
| 97 | @GET |
| 98 | @Produces(MediaType.APPLICATION_JSON) |
| 99 | @Path("messages/{deviceId}") |
| 100 | public Response controlMessageMetrics(@PathParam("deviceId") String deviceId) { |
| 101 | |
| 102 | metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS, |
| 103 | DeviceId.deviceId(deviceId), root); |
| 104 | |
| 105 | return ok(root).build(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns cpu metrics. |
| 110 | * |
| 111 | * @return cpu metrics |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 112 | * @onos.rsModel CpuMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 113 | */ |
| 114 | @GET |
| 115 | @Path("cpu_metrics") |
| 116 | @Produces(MediaType.APPLICATION_JSON) |
| 117 | public Response cpuMetrics() { |
| 118 | |
| 119 | metricsStats(monitorService, localNodeId, CPU_METRICS, root); |
| 120 | return ok(root).build(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns memory metrics. |
| 125 | * |
| 126 | * @return memory metrics |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 127 | * @onos.rsModel MemoryMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 128 | */ |
| 129 | @GET |
| 130 | @Path("memory_metrics") |
| 131 | @Produces(MediaType.APPLICATION_JSON) |
| 132 | public Response memoryMetrics() { |
| 133 | |
| 134 | metricsStats(monitorService, localNodeId, MEMORY_METRICS, root); |
| 135 | return ok(root).build(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns disk metrics of all resources. |
| 140 | * |
| 141 | * @return disk metrics of all resources |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 142 | * @onos.rsModel DiskMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 143 | */ |
| 144 | @GET |
| 145 | @Path("disk_metrics") |
| 146 | @Produces(MediaType.APPLICATION_JSON) |
| 147 | public Response diskMetrics() { |
| 148 | |
| 149 | ArrayNode diskNodes = root.putArray("disks"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame^] | 150 | monitorService.availableResourcesSync(localNodeId, DISK).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 151 | ObjectNode diskNode = mapper().createObjectNode(); |
| 152 | ObjectNode valueNode = mapper().createObjectNode(); |
| 153 | |
| 154 | metricsStats(monitorService, localNodeId, DISK_METRICS, name, valueNode); |
| 155 | diskNode.put("name", name); |
| 156 | diskNode.set("value", valueNode); |
| 157 | |
| 158 | diskNodes.add(diskNode); |
| 159 | }); |
| 160 | |
| 161 | return ok(root).build(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns network metrics of all resources. |
| 166 | * |
| 167 | * @return network metrics of all resources |
Jian Li | d86fac3 | 2016-04-01 16:10:26 -0700 | [diff] [blame] | 168 | * @onos.rsModel NetworkMetrics |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 169 | */ |
| 170 | @GET |
| 171 | @Path("network_metrics") |
| 172 | @Produces(MediaType.APPLICATION_JSON) |
| 173 | public Response networkMetrics() { |
| 174 | |
| 175 | ArrayNode networkNodes = root.putArray("networks"); |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame^] | 176 | monitorService.availableResourcesSync(localNodeId, NETWORK).forEach(name -> { |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 177 | ObjectNode networkNode = mapper().createObjectNode(); |
| 178 | ObjectNode valueNode = mapper().createObjectNode(); |
| 179 | |
| 180 | metricsStats(monitorService, localNodeId, NETWORK_METRICS, name, valueNode); |
| 181 | networkNode.put("name", name); |
| 182 | networkNode.set("value", valueNode); |
| 183 | |
| 184 | networkNodes.add(networkNode); |
| 185 | }); |
| 186 | |
| 187 | return ok(root).build(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns a collection of control message stats. |
| 192 | * |
| 193 | * @param service control plane monitoring service |
| 194 | * @param nodeId node identification |
| 195 | * @param typeSet a set of control message types |
| 196 | * @param did device identification |
| 197 | * @param node object node |
| 198 | * @return a collection of control message stats |
| 199 | */ |
| 200 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 201 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 202 | DeviceId did, ObjectNode node) { |
| 203 | return metricsStats(service, nodeId, typeSet, null, did, node); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns a collection of system metric stats. |
| 208 | * |
| 209 | * @param service control plane monitoring service |
| 210 | * @param nodeId node identification |
| 211 | * @param typeSet a set of system metric types |
| 212 | * @param node object node |
| 213 | * @return a collection of system metric stats |
| 214 | */ |
| 215 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 216 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 217 | ObjectNode node) { |
| 218 | return metricsStats(service, nodeId, typeSet, null, null, node); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns a collection of system metric stats. |
| 223 | * |
| 224 | * @param service control plane monitoring service |
| 225 | * @param nodeId node identification |
| 226 | * @param typeSet a set of control message types |
| 227 | * @param resourceName device identification |
| 228 | * @param node object node |
| 229 | * @return a collection of system metric stats |
| 230 | */ |
| 231 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 232 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 233 | String resourceName, ObjectNode node) { |
| 234 | return metricsStats(service, nodeId, typeSet, resourceName, null, node); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns a collection of control loads of the given control metric types. |
| 239 | * |
| 240 | * @param service control plane monitoring service |
| 241 | * @param nodeId node identification |
| 242 | * @param typeSet a group of control metric types |
| 243 | * @param name resource name |
| 244 | * @param did device identification |
| 245 | * @return a collection of control loads |
| 246 | */ |
| 247 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 248 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 249 | String name, DeviceId did, ObjectNode node) { |
| 250 | ArrayNode metricsNode = node.putArray("metrics"); |
| 251 | |
| 252 | if (name == null && did == null) { |
| 253 | typeSet.forEach(type -> { |
| 254 | ObjectNode metricNode = mapper().createObjectNode(); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 255 | CompletableFuture<ControlLoadSnapshot> cf = |
| 256 | service.getLoad(nodeId, type, Optional.empty()); |
| 257 | |
| 258 | if (cf != null) { |
| 259 | ControlLoadSnapshot cmr = |
| 260 | Tools.futureGetOrElse(cf, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null); |
| 261 | |
| 262 | if (cmr != null) { |
| 263 | metricNode.set(toCamelCase(type.toString(), true), |
| 264 | codec(ControlLoadSnapshot.class).encode(cmr, this)); |
| 265 | metricsNode.add(metricNode); |
| 266 | } |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 267 | } |
| 268 | }); |
| 269 | } else if (name == null) { |
| 270 | typeSet.forEach(type -> { |
| 271 | ObjectNode metricNode = mapper().createObjectNode(); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 272 | CompletableFuture<ControlLoadSnapshot> cf = |
| 273 | service.getLoad(nodeId, type, Optional.of(did)); |
| 274 | |
| 275 | if (cf != null) { |
| 276 | ControlLoadSnapshot cmr = |
| 277 | Tools.futureGetOrElse(cf, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null); |
| 278 | if (cmr != null) { |
| 279 | metricNode.set(toCamelCase(type.toString(), true), |
| 280 | codec(ControlLoadSnapshot.class).encode(cmr, this)); |
| 281 | metricsNode.add(metricNode); |
| 282 | } |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 283 | } |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 284 | |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 285 | }); |
| 286 | } else if (did == null) { |
| 287 | typeSet.forEach(type -> { |
| 288 | ObjectNode metricNode = mapper().createObjectNode(); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 289 | CompletableFuture<ControlLoadSnapshot> cf = |
| 290 | service.getLoad(nodeId, type, name); |
| 291 | |
| 292 | if (cf != null) { |
| 293 | ControlLoadSnapshot cmr = |
| 294 | Tools.futureGetOrElse(cf, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null); |
| 295 | if (cmr != null) { |
| 296 | metricNode.set(toCamelCase(type.toString(), true), |
| 297 | codec(ControlLoadSnapshot.class).encode(cmr, this)); |
| 298 | metricsNode.add(metricNode); |
| 299 | } |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 300 | } |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | return metricsNode; |
| 305 | } |
Jian Li | 8b0ea96 | 2016-04-04 11:49:29 -0700 | [diff] [blame] | 306 | |
| 307 | private String toCamelCase(String value, boolean startWithLowerCase) { |
| 308 | String[] strings = StringUtils.split(value.toLowerCase(), "_"); |
| 309 | for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++) { |
| 310 | strings[i] = StringUtils.capitalize(strings[i]); |
| 311 | } |
| 312 | return StringUtils.join(strings); |
| 313 | } |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 314 | } |