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