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