Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cpman.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.cluster.ClusterService; |
| 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.cpman.ControlLoad; |
| 23 | import org.onosproject.cpman.ControlMetricType; |
| 24 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.rest.AbstractWebResource; |
| 27 | |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.util.Optional; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 38 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 39 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 40 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 41 | import static org.onosproject.cpman.ControlResource.NETWORK_METRICS; |
| 42 | import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE; |
| 43 | import static org.onosproject.cpman.ControlResource.Type.DISK; |
| 44 | import static org.onosproject.cpman.ControlResource.Type.NETWORK; |
| 45 | |
| 46 | /** |
| 47 | * Query control metrics. |
| 48 | */ |
| 49 | @Path("metrics") |
| 50 | public 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 |
| 62 | */ |
| 63 | @GET |
| 64 | @Path("messages") |
| 65 | @Produces(MediaType.APPLICATION_JSON) |
| 66 | public Response controlMessageMetrics() { |
| 67 | |
| 68 | ArrayNode deviceNodes = root.putArray("devices"); |
| 69 | monitorService.availableResources(CONTROL_MESSAGE).forEach(name -> { |
| 70 | ObjectNode deviceNode = mapper().createObjectNode(); |
| 71 | ObjectNode valueNode = mapper().createObjectNode(); |
| 72 | |
| 73 | metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS, |
| 74 | DeviceId.deviceId(name), valueNode); |
| 75 | deviceNode.put("name", name); |
| 76 | deviceNode.set("value", valueNode); |
| 77 | |
| 78 | deviceNodes.add(deviceNode); |
| 79 | }); |
| 80 | |
| 81 | return ok(root).build(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns control message metrics of a given device. |
| 86 | * |
| 87 | * @param deviceId device identification |
| 88 | * @return control message metrics of a given device |
| 89 | */ |
| 90 | @GET |
| 91 | @Produces(MediaType.APPLICATION_JSON) |
| 92 | @Path("messages/{deviceId}") |
| 93 | public Response controlMessageMetrics(@PathParam("deviceId") String deviceId) { |
| 94 | |
| 95 | metricsStats(monitorService, localNodeId, CONTROL_MESSAGE_METRICS, |
| 96 | DeviceId.deviceId(deviceId), root); |
| 97 | |
| 98 | return ok(root).build(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns cpu metrics. |
| 103 | * |
| 104 | * @return cpu metrics |
| 105 | */ |
| 106 | @GET |
| 107 | @Path("cpu_metrics") |
| 108 | @Produces(MediaType.APPLICATION_JSON) |
| 109 | public Response cpuMetrics() { |
| 110 | |
| 111 | metricsStats(monitorService, localNodeId, CPU_METRICS, root); |
| 112 | return ok(root).build(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns memory metrics. |
| 117 | * |
| 118 | * @return memory metrics |
| 119 | */ |
| 120 | @GET |
| 121 | @Path("memory_metrics") |
| 122 | @Produces(MediaType.APPLICATION_JSON) |
| 123 | public Response memoryMetrics() { |
| 124 | |
| 125 | metricsStats(monitorService, localNodeId, MEMORY_METRICS, root); |
| 126 | return ok(root).build(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns disk metrics of all resources. |
| 131 | * |
| 132 | * @return disk metrics of all resources |
| 133 | */ |
| 134 | @GET |
| 135 | @Path("disk_metrics") |
| 136 | @Produces(MediaType.APPLICATION_JSON) |
| 137 | public Response diskMetrics() { |
| 138 | |
| 139 | ArrayNode diskNodes = root.putArray("disks"); |
| 140 | monitorService.availableResources(DISK).forEach(name -> { |
| 141 | ObjectNode diskNode = mapper().createObjectNode(); |
| 142 | ObjectNode valueNode = mapper().createObjectNode(); |
| 143 | |
| 144 | metricsStats(monitorService, localNodeId, DISK_METRICS, name, valueNode); |
| 145 | diskNode.put("name", name); |
| 146 | diskNode.set("value", valueNode); |
| 147 | |
| 148 | diskNodes.add(diskNode); |
| 149 | }); |
| 150 | |
| 151 | return ok(root).build(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns network metrics of all resources. |
| 156 | * |
| 157 | * @return network metrics of all resources |
| 158 | */ |
| 159 | @GET |
| 160 | @Path("network_metrics") |
| 161 | @Produces(MediaType.APPLICATION_JSON) |
| 162 | public Response networkMetrics() { |
| 163 | |
| 164 | ArrayNode networkNodes = root.putArray("networks"); |
| 165 | monitorService.availableResources(NETWORK).forEach(name -> { |
| 166 | ObjectNode networkNode = mapper().createObjectNode(); |
| 167 | ObjectNode valueNode = mapper().createObjectNode(); |
| 168 | |
| 169 | metricsStats(monitorService, localNodeId, NETWORK_METRICS, name, valueNode); |
| 170 | networkNode.put("name", name); |
| 171 | networkNode.set("value", valueNode); |
| 172 | |
| 173 | networkNodes.add(networkNode); |
| 174 | }); |
| 175 | |
| 176 | return ok(root).build(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Returns a collection of control message stats. |
| 181 | * |
| 182 | * @param service control plane monitoring service |
| 183 | * @param nodeId node identification |
| 184 | * @param typeSet a set of control message types |
| 185 | * @param did device identification |
| 186 | * @param node object node |
| 187 | * @return a collection of control message stats |
| 188 | */ |
| 189 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 190 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 191 | DeviceId did, ObjectNode node) { |
| 192 | return metricsStats(service, nodeId, typeSet, null, did, node); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns a collection of system metric stats. |
| 197 | * |
| 198 | * @param service control plane monitoring service |
| 199 | * @param nodeId node identification |
| 200 | * @param typeSet a set of system metric types |
| 201 | * @param node object node |
| 202 | * @return a collection of system metric stats |
| 203 | */ |
| 204 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 205 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 206 | ObjectNode node) { |
| 207 | return metricsStats(service, nodeId, typeSet, null, null, node); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns a collection of system metric stats. |
| 212 | * |
| 213 | * @param service control plane monitoring service |
| 214 | * @param nodeId node identification |
| 215 | * @param typeSet a set of control message types |
| 216 | * @param resourceName device identification |
| 217 | * @param node object node |
| 218 | * @return a collection of system metric stats |
| 219 | */ |
| 220 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 221 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 222 | String resourceName, ObjectNode node) { |
| 223 | return metricsStats(service, nodeId, typeSet, resourceName, null, node); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns a collection of control loads of the given control metric types. |
| 228 | * |
| 229 | * @param service control plane monitoring service |
| 230 | * @param nodeId node identification |
| 231 | * @param typeSet a group of control metric types |
| 232 | * @param name resource name |
| 233 | * @param did device identification |
| 234 | * @return a collection of control loads |
| 235 | */ |
| 236 | private ArrayNode metricsStats(ControlPlaneMonitorService service, |
| 237 | NodeId nodeId, Set<ControlMetricType> typeSet, |
| 238 | String name, DeviceId did, ObjectNode node) { |
| 239 | ArrayNode metricsNode = node.putArray("metrics"); |
| 240 | |
| 241 | if (name == null && did == null) { |
| 242 | typeSet.forEach(type -> { |
| 243 | ObjectNode metricNode = mapper().createObjectNode(); |
| 244 | ControlLoad load = service.getLoad(nodeId, type, Optional.ofNullable(null)); |
| 245 | if (load != null) { |
| 246 | metricNode.set(type.toString().toLowerCase(), codec(ControlLoad.class) |
| 247 | .encode(service.getLoad(nodeId, type, Optional.ofNullable(null)), this)); |
| 248 | metricsNode.add(metricNode); |
| 249 | } |
| 250 | }); |
| 251 | } else if (name == null) { |
| 252 | typeSet.forEach(type -> { |
| 253 | ObjectNode metricNode = mapper().createObjectNode(); |
| 254 | ControlLoad load = service.getLoad(nodeId, type, Optional.of(did)); |
| 255 | if (load != null) { |
| 256 | metricNode.set(type.toString().toLowerCase(), |
| 257 | codec(ControlLoad.class).encode(load, this)); |
| 258 | metricsNode.add(metricNode); |
| 259 | } |
| 260 | }); |
| 261 | } else if (did == null) { |
| 262 | typeSet.forEach(type -> { |
| 263 | ObjectNode metricNode = mapper().createObjectNode(); |
| 264 | ControlLoad load = service.getLoad(nodeId, type, name); |
| 265 | if (load != null) { |
| 266 | metricNode.set(type.toString().toLowerCase(), |
| 267 | codec(ControlLoad.class).encode(load, this)); |
| 268 | metricsNode.add(metricNode); |
| 269 | } |
| 270 | }); |
| 271 | } |
| 272 | |
| 273 | return metricsNode; |
| 274 | } |
| 275 | } |