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; |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 21 | import org.apache.commons.lang3.StringUtils; |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 22 | import org.onlab.metrics.MetricsService; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 23 | import org.onosproject.cpman.ControlMetric; |
| 24 | import org.onosproject.cpman.ControlMetricType; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 25 | import org.onosproject.cpman.ControlPlaneMonitorService; |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 26 | import org.onosproject.cpman.ControlResource; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 27 | import org.onosproject.cpman.MetricValue; |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 28 | import org.onosproject.cpman.SystemInfo; |
| 29 | import org.onosproject.cpman.impl.DefaultSystemInfo; |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 30 | import org.onosproject.cpman.impl.SystemMetricsAggregator; |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 31 | import org.onosproject.cpman.impl.SystemInfoFactory; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 32 | import org.onosproject.rest.AbstractWebResource; |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 35 | |
| 36 | import javax.ws.rs.Consumes; |
| 37 | import javax.ws.rs.POST; |
| 38 | import javax.ws.rs.Path; |
| 39 | import javax.ws.rs.Produces; |
| 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
| 42 | import java.io.IOException; |
| 43 | import java.io.InputStream; |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 44 | import java.util.Iterator; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 45 | import java.util.Optional; |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 46 | import java.util.Set; |
| 47 | import java.util.stream.Collectors; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 48 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 49 | import static org.onlab.util.Tools.nullIsIllegal; |
| 50 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 51 | /** |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 52 | * Collect system metrics. |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 53 | */ |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 54 | @Path("collector") |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 55 | public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 56 | |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 58 | private final ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
| 59 | private final MetricsService metricsService = get(MetricsService.class); |
| 60 | |
Jian Li | 80c1270 | 2016-02-20 08:58:19 +0900 | [diff] [blame] | 61 | private static final int UPDATE_INTERVAL_IN_MINUTE = 1; |
| 62 | private static final String INVALID_SYSTEM_SPECS = "Invalid system specifications"; |
| 63 | private static final String INVALID_RESOURCE_NAME = "Invalid resource name"; |
| 64 | private static final String INVALID_REQUEST = "Invalid request"; |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 65 | private static final int PERCENT_CONSTANT = 100; |
| 66 | |
| 67 | private static final Set<String> MEMORY_FIELD_SET = ControlResource.MEMORY_METRICS |
| 68 | .stream().map(type -> toCamelCase(type.toString(), true)) |
| 69 | .collect(Collectors.toSet()); |
| 70 | |
| 71 | private static final Set<String> CPU_FIELD_SET = ControlResource.CPU_METRICS |
| 72 | .stream().map(type -> toCamelCase(type.toString(), true)) |
| 73 | .collect(Collectors.toSet()); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 74 | |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 75 | private SystemMetricsAggregator systemAggr = |
| 76 | new SystemMetricsAggregator(metricsService, Optional.ofNullable(null), "system"); |
| 77 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Collects CPU metrics. |
| 80 | * |
| 81 | * @param stream JSON stream |
| 82 | * @return 200 OK |
| 83 | * @onos.rsModel CpuMetricsPost |
| 84 | */ |
| 85 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 86 | @Path("cpu_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 87 | @Consumes(MediaType.APPLICATION_JSON) |
| 88 | @Produces(MediaType.APPLICATION_JSON) |
| 89 | public Response cpuMetrics(InputStream stream) { |
| 90 | ObjectNode root = mapper().createObjectNode(); |
| 91 | ControlMetric cm; |
| 92 | try { |
| 93 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 94 | |
| 95 | if (jsonTree == null || !checkFields(jsonTree, CPU_FIELD_SET)) { |
| 96 | ok(root).build(); |
| 97 | } |
| 98 | |
| 99 | long cpuLoad = nullIsIllegal((long) (jsonTree.get("cpuLoad").asDouble() |
| 100 | * PERCENT_CONSTANT), INVALID_REQUEST); |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 101 | long totalCpuTime = nullIsIllegal(jsonTree.get("totalCpuTime").asLong(), INVALID_REQUEST); |
| 102 | long sysCpuTime = nullIsIllegal(jsonTree.get("sysCpuTime").asLong(), INVALID_REQUEST); |
| 103 | long userCpuTime = nullIsIllegal(jsonTree.get("userCpuTime").asLong(), INVALID_REQUEST); |
| 104 | long cpuIdleTime = nullIsIllegal(jsonTree.get("cpuIdleTime").asLong(), INVALID_REQUEST); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 105 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 106 | cm = new ControlMetric(ControlMetricType.CPU_LOAD, |
| 107 | new MetricValue.Builder().load(cpuLoad).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 108 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 109 | systemAggr.increment(ControlMetricType.CPU_LOAD, cpuLoad); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 110 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 111 | cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME, |
| 112 | new MetricValue.Builder().load(totalCpuTime).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 113 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 114 | systemAggr.increment(ControlMetricType.TOTAL_CPU_TIME, totalCpuTime); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 115 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 116 | cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME, |
| 117 | new MetricValue.Builder().load(sysCpuTime).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 118 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 119 | systemAggr.increment(ControlMetricType.SYS_CPU_TIME, sysCpuTime); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 120 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 121 | cm = new ControlMetric(ControlMetricType.USER_CPU_TIME, |
| 122 | new MetricValue.Builder().load(userCpuTime).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 123 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 124 | systemAggr.increment(ControlMetricType.USER_CPU_TIME, userCpuTime); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 125 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 126 | cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME, |
| 127 | new MetricValue.Builder().load(cpuIdleTime).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 128 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 129 | systemAggr.increment(ControlMetricType.CPU_IDLE_TIME, cpuIdleTime); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 130 | |
| 131 | } catch (IOException e) { |
| 132 | throw new IllegalArgumentException(e.getMessage()); |
| 133 | } |
| 134 | return ok(root).build(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Collects memory metrics. |
| 139 | * |
| 140 | * @param stream JSON stream |
| 141 | * @return 200 OK |
| 142 | * @onos.rsModel MemoryMetricsPost |
| 143 | */ |
| 144 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 145 | @Path("memory_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 146 | @Consumes(MediaType.APPLICATION_JSON) |
| 147 | @Produces(MediaType.APPLICATION_JSON) |
| 148 | public Response memoryMetrics(InputStream stream) { |
| 149 | ObjectNode root = mapper().createObjectNode(); |
| 150 | ControlMetric cm; |
| 151 | try { |
| 152 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 153 | |
| 154 | if (jsonTree == null || !checkFields(jsonTree, MEMORY_FIELD_SET)) { |
| 155 | ok(root).build(); |
| 156 | } |
| 157 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 158 | long memUsed = nullIsIllegal(jsonTree.get("memoryUsed").asLong(), INVALID_REQUEST); |
| 159 | long memFree = nullIsIllegal(jsonTree.get("memoryFree").asLong(), INVALID_REQUEST); |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 160 | long memTotal = memUsed + memFree; |
| 161 | long memUsedRatio = memTotal == 0L ? 0L : (memUsed * PERCENT_CONSTANT) / memTotal; |
| 162 | long memFreeRatio = memTotal == 0L ? 0L : (memFree * PERCENT_CONSTANT) / memTotal; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 163 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 164 | cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO, |
| 165 | new MetricValue.Builder().load(memUsedRatio).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 166 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 167 | systemAggr.increment(ControlMetricType.MEMORY_USED_RATIO, memUsedRatio); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 168 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 169 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO, |
| 170 | new MetricValue.Builder().load(memFreeRatio).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 171 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 172 | systemAggr.increment(ControlMetricType.MEMORY_FREE_RATIO, memFreeRatio); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 173 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 174 | cm = new ControlMetric(ControlMetricType.MEMORY_USED, |
| 175 | new MetricValue.Builder().load(memUsed).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 176 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 177 | systemAggr.increment(ControlMetricType.MEMORY_USED, memUsed); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 178 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 179 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE, |
| 180 | new MetricValue.Builder().load(memFree).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 181 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
| 182 | systemAggr.increment(ControlMetricType.MEMORY_FREE, memFree); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 183 | |
| 184 | } catch (IOException e) { |
| 185 | throw new IllegalArgumentException(e.getMessage()); |
| 186 | } |
| 187 | return ok(root).build(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Collects disk metrics. |
| 192 | * |
| 193 | * @param stream JSON stream |
| 194 | * @return 200 OK |
| 195 | * @onos.rsModel DiskMetricsPost |
| 196 | */ |
| 197 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 198 | @Path("disk_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 199 | @Consumes(MediaType.APPLICATION_JSON) |
| 200 | @Produces(MediaType.APPLICATION_JSON) |
| 201 | public Response diskMetrics(InputStream stream) { |
| 202 | ObjectNode root = mapper().createObjectNode(); |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 203 | ControlMetric cm; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 204 | try { |
| 205 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 206 | ArrayNode diskRes = |
| 207 | jsonTree.get("disks") == null ? mapper().createArrayNode() : (ArrayNode) jsonTree.get("disks"); |
| 208 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 209 | for (JsonNode node : diskRes) { |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 210 | JsonNode resourceName = node.get("resourceName"); |
| 211 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 212 | |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 213 | SystemMetricsAggregator diskAggr = new SystemMetricsAggregator(metricsService, |
| 214 | Optional.of(resourceName.asText()), "disk"); |
| 215 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 216 | long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST); |
| 217 | long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 218 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 219 | cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES, |
| 220 | new MetricValue.Builder().load(readBytes).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 221 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
| 222 | diskAggr.increment(ControlMetricType.DISK_READ_BYTES, readBytes); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 223 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 224 | cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, |
| 225 | new MetricValue.Builder().load(writeBytes).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 226 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
| 227 | diskAggr.increment(ControlMetricType.DISK_WRITE_BYTES, writeBytes); |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 228 | } |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 229 | } catch (IOException e) { |
| 230 | throw new IllegalArgumentException(e.getMessage()); |
| 231 | } |
| 232 | return ok(root).build(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Collects network metrics. |
| 237 | * |
| 238 | * @param stream JSON stream |
| 239 | * @return 200 OK |
| 240 | * @onos.rsModel NetworkMetricsPost |
| 241 | */ |
| 242 | @POST |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 243 | @Path("network_metrics") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 244 | @Consumes(MediaType.APPLICATION_JSON) |
| 245 | @Produces(MediaType.APPLICATION_JSON) |
| 246 | public Response networkMetrics(InputStream stream) { |
| 247 | ObjectNode root = mapper().createObjectNode(); |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 248 | ControlMetric cm; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 249 | try { |
| 250 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 251 | |
| 252 | ArrayNode networkRes = jsonTree.get("networks") == null |
| 253 | ? mapper().createArrayNode() : (ArrayNode) jsonTree.get("networks"); |
| 254 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 255 | for (JsonNode node : networkRes) { |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 256 | JsonNode resourceName = node.get("resourceName"); |
| 257 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 258 | |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 259 | SystemMetricsAggregator networkAggr = new SystemMetricsAggregator(metricsService, |
| 260 | Optional.of(resourceName.asText()), "network"); |
| 261 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 262 | long inBytes = nullIsIllegal(node.get("incomingBytes").asLong(), INVALID_REQUEST); |
| 263 | long outBytes = nullIsIllegal(node.get("outgoingBytes").asLong(), INVALID_REQUEST); |
| 264 | long inPackets = nullIsIllegal(node.get("incomingPackets").asLong(), INVALID_REQUEST); |
| 265 | long outPackets = nullIsIllegal(node.get("outgoingPackets").asLong(), INVALID_REQUEST); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 266 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 267 | cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, |
| 268 | new MetricValue.Builder().load(inBytes).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 269 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
| 270 | networkAggr.increment(ControlMetricType.NW_INCOMING_BYTES, inBytes); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 271 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 272 | cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, |
| 273 | new MetricValue.Builder().load(outBytes).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 274 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
| 275 | networkAggr.increment(ControlMetricType.NW_OUTGOING_BYTES, outBytes); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 276 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 277 | cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, |
| 278 | new MetricValue.Builder().load(inPackets).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 279 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
| 280 | networkAggr.increment(ControlMetricType.NW_INCOMING_PACKETS, inPackets); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 281 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 282 | cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, |
| 283 | new MetricValue.Builder().load(outPackets).add()); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame^] | 284 | monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
| 285 | networkAggr.increment(ControlMetricType.NW_OUTGOING_PACKETS, outPackets); |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 286 | } |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 287 | } catch (IOException e) { |
| 288 | throw new IllegalArgumentException(e.getMessage()); |
| 289 | } |
| 290 | return ok(root).build(); |
| 291 | } |
| 292 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 293 | /** |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 294 | * Collects system information. |
| 295 | * The system information includes the various control metrics |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 296 | * which do not require aggregation. |
| 297 | * |
| 298 | * @param stream JSON stream |
| 299 | * @return 200 OK |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 300 | * @onos.rsModel SystemInfoPost |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 301 | */ |
| 302 | @POST |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 303 | @Path("system_info") |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 304 | @Consumes(MediaType.APPLICATION_JSON) |
| 305 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 306 | public Response systemInfo(InputStream stream) { |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 307 | ObjectNode root = mapper().createObjectNode(); |
| 308 | |
| 309 | try { |
| 310 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 311 | JsonNode numOfCores = jsonTree.get("numOfCores"); |
| 312 | JsonNode numOfCpus = jsonTree.get("numOfCpus"); |
| 313 | JsonNode cpuSpeed = jsonTree.get("cpuSpeed"); |
| 314 | JsonNode totalMemory = jsonTree.get("totalMemory"); |
| 315 | |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 316 | if (numOfCores != null && numOfCpus != null && |
| 317 | cpuSpeed != null && totalMemory != null) { |
| 318 | SystemInfo systemInfo = new DefaultSystemInfo.Builder() |
| 319 | .numOfCores(numOfCores.asInt()) |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 320 | .numOfCpus(numOfCpus.asInt()) |
| 321 | .cpuSpeed(cpuSpeed.asInt()) |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 322 | .totalMemory(totalMemory.asInt()) |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 323 | .build(); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 324 | |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 325 | // try to store the system info. |
| 326 | SystemInfoFactory.getInstance().setSystemInfo(systemInfo); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 327 | } else { |
| 328 | throw new IllegalArgumentException(INVALID_SYSTEM_SPECS); |
| 329 | } |
| 330 | |
| 331 | } catch (IOException e) { |
| 332 | throw new IllegalArgumentException(e.getMessage()); |
| 333 | } |
| 334 | return ok(root).build(); |
| 335 | } |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 336 | |
| 337 | private boolean checkFields(ObjectNode node, Set<String> original) { |
| 338 | Iterator<String> fieldNames = node.fieldNames(); |
| 339 | while (fieldNames.hasNext()) { |
| 340 | String fieldName = fieldNames.next(); |
| 341 | if (!original.contains(fieldName) || node.get(fieldName) == null) { |
| 342 | log.warn("Illegal field name: {}", fieldName); |
| 343 | return false; |
| 344 | } |
| 345 | } |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | private static String toCamelCase(String value, boolean startWithLowerCase) { |
| 350 | String[] strings = StringUtils.split(value.toLowerCase(), "_"); |
| 351 | for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++) { |
| 352 | strings[i] = StringUtils.capitalize(strings[i]); |
| 353 | } |
| 354 | return StringUtils.join(strings); |
| 355 | } |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 356 | } |