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