Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [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.JsonNode; |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.cpman.ControlMetric; |
| 22 | import org.onosproject.cpman.ControlMetricType; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 23 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 24 | import org.onosproject.cpman.MetricValue; |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 25 | import org.onosproject.cpman.impl.ControlMetricsSystemSpec; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 26 | import org.onosproject.rest.AbstractWebResource; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.Optional; |
| 37 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 38 | import static org.onlab.util.Tools.nullIsIllegal; |
| 39 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Collect control plane metrics. |
| 42 | */ |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 43 | @Path("collector") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 44 | public class ControlMetricsCollectorWebResource extends AbstractWebResource { |
| 45 | |
| 46 | final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class); |
| 47 | public static final int UPDATE_INTERVAL = 1; // 1 minute update interval |
| 48 | public static final String INVALID_SYSTEM_SPECS = "Invalid system specifications"; |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 49 | public static final String INVALID_RESOURCE_NAME = "Invalid resource name"; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Collects CPU metrics. |
| 53 | * |
| 54 | * @param stream JSON stream |
| 55 | * @return 200 OK |
| 56 | * @onos.rsModel CpuMetricsPost |
| 57 | */ |
| 58 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 59 | @Path("cpu_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 60 | @Consumes(MediaType.APPLICATION_JSON) |
| 61 | @Produces(MediaType.APPLICATION_JSON) |
| 62 | public Response cpuMetrics(InputStream stream) { |
| 63 | ObjectNode root = mapper().createObjectNode(); |
| 64 | ControlMetric cm; |
| 65 | try { |
| 66 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 67 | JsonNode cpuLoadJson = jsonTree.get("cpuLoad"); |
| 68 | JsonNode totalCpuTimeJson = jsonTree.get("totalCpuTime"); |
| 69 | JsonNode sysCpuTimeJson = jsonTree.get("sysCpuTime"); |
| 70 | JsonNode userCpuTimeJson = jsonTree.get("userCpuTime"); |
| 71 | JsonNode cpuIdleTimeJson = jsonTree.get("cpuIdleTime"); |
| 72 | |
| 73 | if (cpuLoadJson != null) { |
| 74 | cm = new ControlMetric(ControlMetricType.CPU_LOAD, |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 75 | new MetricValue.Builder().load(cpuLoadJson.asLong()).add()); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 76 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 77 | } |
| 78 | |
| 79 | if (totalCpuTimeJson != null) { |
| 80 | cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME, |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 81 | new MetricValue.Builder().load(totalCpuTimeJson.asLong()).add()); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 82 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 83 | } |
| 84 | |
| 85 | if (sysCpuTimeJson != null) { |
| 86 | cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME, |
| 87 | new MetricValue.Builder().load(sysCpuTimeJson.asLong()).add()); |
| 88 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 89 | } |
| 90 | |
| 91 | if (userCpuTimeJson != null) { |
| 92 | cm = new ControlMetric(ControlMetricType.USER_CPU_TIME, |
| 93 | new MetricValue.Builder().load(userCpuTimeJson.asLong()).add()); |
| 94 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 95 | } |
| 96 | |
| 97 | if (cpuIdleTimeJson != null) { |
| 98 | cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME, |
| 99 | new MetricValue.Builder().load(cpuIdleTimeJson.asLong()).add()); |
| 100 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 101 | } |
| 102 | |
| 103 | } catch (IOException e) { |
| 104 | throw new IllegalArgumentException(e.getMessage()); |
| 105 | } |
| 106 | return ok(root).build(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Collects memory metrics. |
| 111 | * |
| 112 | * @param stream JSON stream |
| 113 | * @return 200 OK |
| 114 | * @onos.rsModel MemoryMetricsPost |
| 115 | */ |
| 116 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 117 | @Path("memory_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 118 | @Consumes(MediaType.APPLICATION_JSON) |
| 119 | @Produces(MediaType.APPLICATION_JSON) |
| 120 | public Response memoryMetrics(InputStream stream) { |
| 121 | ObjectNode root = mapper().createObjectNode(); |
| 122 | ControlMetric cm; |
| 123 | try { |
| 124 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 125 | JsonNode memUsedPerc = jsonTree.get("memoryUsedPercentage"); |
| 126 | JsonNode memFreePerc = jsonTree.get("memoryFreePercentage"); |
| 127 | JsonNode memUsed = jsonTree.get("memoryUsed"); |
| 128 | JsonNode memFree = jsonTree.get("memoryFree"); |
| 129 | |
| 130 | if (memUsedPerc != null) { |
| 131 | cm = new ControlMetric(ControlMetricType.MEMORY_USED_PERCENTAGE, |
| 132 | new MetricValue.Builder().load(memUsedPerc.asLong()).add()); |
| 133 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 134 | } |
| 135 | |
| 136 | if (memFreePerc != null) { |
| 137 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE_PERCENTAGE, |
| 138 | new MetricValue.Builder().load(memFreePerc.asLong()).add()); |
| 139 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 140 | } |
| 141 | |
| 142 | if (memUsed != null) { |
| 143 | cm = new ControlMetric(ControlMetricType.MEMORY_USED, |
| 144 | new MetricValue.Builder().load(memUsed.asLong()).add()); |
| 145 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 146 | } |
| 147 | |
| 148 | if (memFree != null) { |
| 149 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE, |
| 150 | new MetricValue.Builder().load(memFree.asLong()).add()); |
| 151 | service.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 152 | } |
| 153 | |
| 154 | } catch (IOException e) { |
| 155 | throw new IllegalArgumentException(e.getMessage()); |
| 156 | } |
| 157 | return ok(root).build(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Collects disk metrics. |
| 162 | * |
| 163 | * @param stream JSON stream |
| 164 | * @return 200 OK |
| 165 | * @onos.rsModel DiskMetricsPost |
| 166 | */ |
| 167 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 168 | @Path("disk_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 169 | @Consumes(MediaType.APPLICATION_JSON) |
| 170 | @Produces(MediaType.APPLICATION_JSON) |
| 171 | public Response diskMetrics(InputStream stream) { |
| 172 | ObjectNode root = mapper().createObjectNode(); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 173 | final ControlMetric[] cm = new ControlMetric[1]; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 174 | try { |
| 175 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 176 | ArrayNode diskRes = (ArrayNode) jsonTree.get("disks"); |
| 177 | diskRes.forEach(node-> { |
| 178 | JsonNode resourceName = node.get("resourceName"); |
| 179 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 180 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 181 | JsonNode readBytes = jsonTree.get("readBytes"); |
| 182 | JsonNode writeBytes = jsonTree.get("writeBytes"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 183 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 184 | if (readBytes != null) { |
| 185 | cm[0] = new ControlMetric(ControlMetricType.DISK_READ_BYTES, |
| 186 | new MetricValue.Builder().load(readBytes.asLong()).add()); |
| 187 | service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText()); |
| 188 | } |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 189 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 190 | if (writeBytes != null) { |
| 191 | cm[0] = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, |
| 192 | new MetricValue.Builder().load(writeBytes.asLong()).add()); |
| 193 | service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText()); |
| 194 | } |
| 195 | }); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 196 | } catch (IOException e) { |
| 197 | throw new IllegalArgumentException(e.getMessage()); |
| 198 | } |
| 199 | return ok(root).build(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Collects network metrics. |
| 204 | * |
| 205 | * @param stream JSON stream |
| 206 | * @return 200 OK |
| 207 | * @onos.rsModel NetworkMetricsPost |
| 208 | */ |
| 209 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 210 | @Path("network_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 211 | @Consumes(MediaType.APPLICATION_JSON) |
| 212 | @Produces(MediaType.APPLICATION_JSON) |
| 213 | public Response networkMetrics(InputStream stream) { |
| 214 | ObjectNode root = mapper().createObjectNode(); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 215 | final ControlMetric[] cm = new ControlMetric[1]; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 216 | try { |
| 217 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 218 | ArrayNode networkRes = (ArrayNode) jsonTree.get("networks"); |
| 219 | networkRes.forEach(node -> { |
| 220 | JsonNode resourceName = node.get("resourceName"); |
| 221 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 222 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 223 | JsonNode inBytes = jsonTree.get("incomingBytes"); |
| 224 | JsonNode outBytes = jsonTree.get("outgoingBytes"); |
| 225 | JsonNode inPackets = jsonTree.get("incomingPackets"); |
| 226 | JsonNode outPackets = jsonTree.get("outgoingPackets"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 227 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 228 | if (inBytes != null) { |
| 229 | cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, |
| 230 | new MetricValue.Builder().load(inBytes.asLong()).add()); |
| 231 | service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText()); |
| 232 | } |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 233 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 234 | if (outBytes != null) { |
| 235 | cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, |
| 236 | new MetricValue.Builder().load(outBytes.asLong()).add()); |
| 237 | service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText()); |
| 238 | } |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 239 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 240 | if (inPackets != null) { |
| 241 | cm[0] = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, |
| 242 | new MetricValue.Builder().load(inPackets.asLong()).add()); |
| 243 | service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText()); |
| 244 | } |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 245 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 246 | if (outPackets != null) { |
| 247 | cm[0] = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, |
| 248 | new MetricValue.Builder().load(outPackets.asLong()).add()); |
| 249 | service.updateMetric(cm[0], UPDATE_INTERVAL, resourceName.asText()); |
| 250 | } |
| 251 | }); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 252 | } catch (IOException e) { |
| 253 | throw new IllegalArgumentException(e.getMessage()); |
| 254 | } |
| 255 | return ok(root).build(); |
| 256 | } |
| 257 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 258 | /** |
| 259 | * Collects system specifications. |
| 260 | * The system specs include the various control metrics |
| 261 | * which do not require aggregation. |
| 262 | * |
| 263 | * @param stream JSON stream |
| 264 | * @return 200 OK |
| 265 | * @onos.rsModel SystemSpecsPost |
| 266 | */ |
| 267 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 268 | @Path("system_specs") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 269 | @Consumes(MediaType.APPLICATION_JSON) |
| 270 | @Produces(MediaType.APPLICATION_JSON) |
| 271 | public Response systemSpecs(InputStream stream) { |
| 272 | ObjectNode root = mapper().createObjectNode(); |
| 273 | |
| 274 | try { |
| 275 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 276 | JsonNode numOfCores = jsonTree.get("numOfCores"); |
| 277 | JsonNode numOfCpus = jsonTree.get("numOfCpus"); |
| 278 | JsonNode cpuSpeed = jsonTree.get("cpuSpeed"); |
| 279 | JsonNode totalMemory = jsonTree.get("totalMemory"); |
| 280 | |
| 281 | if (numOfCores != null && numOfCpus != null && cpuSpeed != null && totalMemory != null) { |
| 282 | ControlMetricsSystemSpec.Builder builder = new ControlMetricsSystemSpec.Builder(); |
| 283 | ControlMetricsSystemSpec cmss = builder.numOfCores(numOfCores.asInt()) |
| 284 | .numOfCpus(numOfCpus.asInt()) |
| 285 | .cpuSpeed(cpuSpeed.asInt()) |
| 286 | .totalMemory(totalMemory.asLong()) |
| 287 | .build(); |
| 288 | // TODO: need to implement spec store |
| 289 | |
| 290 | } else { |
| 291 | throw new IllegalArgumentException(INVALID_SYSTEM_SPECS); |
| 292 | } |
| 293 | |
| 294 | } catch (IOException e) { |
| 295 | throw new IllegalArgumentException(e.getMessage()); |
| 296 | } |
| 297 | return ok(root).build(); |
| 298 | } |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame^] | 299 | } |