blob: c96d1d95fd5c2767829fade01946a764c25b4513 [file] [log] [blame]
Jian Lic132c112016-01-28 20:27:34 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Lic132c112016-01-28 20:27:34 -08003 *
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 */
16package org.onosproject.cpman.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
Jian Liba6b1172016-02-01 22:40:42 -080019import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Lic132c112016-01-28 20:27:34 -080020import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li9f3a8852016-04-07 13:37:39 -070021import org.apache.commons.lang3.StringUtils;
Jian Li7eed4172016-04-07 22:12:03 -070022import org.onlab.metrics.MetricsService;
Jian Lic132c112016-01-28 20:27:34 -080023import org.onosproject.cpman.ControlMetric;
24import org.onosproject.cpman.ControlMetricType;
Jian Lic132c112016-01-28 20:27:34 -080025import org.onosproject.cpman.ControlPlaneMonitorService;
Jian Li9f3a8852016-04-07 13:37:39 -070026import org.onosproject.cpman.ControlResource;
Jian Lic132c112016-01-28 20:27:34 -080027import org.onosproject.cpman.MetricValue;
Jian Li1a424692016-02-03 16:21:18 -080028import org.onosproject.cpman.SystemInfo;
29import org.onosproject.cpman.impl.DefaultSystemInfo;
Jian Li7eed4172016-04-07 22:12:03 -070030import org.onosproject.cpman.impl.SystemMetricsAggregator;
Jian Li1a424692016-02-03 16:21:18 -080031import org.onosproject.cpman.impl.SystemInfoFactory;
Jian Lic132c112016-01-28 20:27:34 -080032import org.onosproject.rest.AbstractWebResource;
Jian Li9f3a8852016-04-07 13:37:39 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Jian Lic132c112016-01-28 20:27:34 -080035
36import javax.ws.rs.Consumes;
37import javax.ws.rs.POST;
38import javax.ws.rs.Path;
39import javax.ws.rs.Produces;
40import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
42import java.io.IOException;
43import java.io.InputStream;
Jian Li9f3a8852016-04-07 13:37:39 -070044import java.util.Iterator;
Jian Lic132c112016-01-28 20:27:34 -080045import java.util.Optional;
Jian Li9f3a8852016-04-07 13:37:39 -070046import java.util.Set;
47import java.util.stream.Collectors;
Jian Lic132c112016-01-28 20:27:34 -080048
Jian Liba6b1172016-02-01 22:40:42 -080049import static org.onlab.util.Tools.nullIsIllegal;
50
Jian Lic132c112016-01-28 20:27:34 -080051/**
Jian Li80c12702016-02-20 08:58:19 +090052 * Collect system metrics.
Jian Lic132c112016-01-28 20:27:34 -080053 */
Jian Li54df73e2016-02-01 17:09:03 -080054@Path("collector")
Jian Li80c12702016-02-20 08:58:19 +090055public class SystemMetricsCollectorWebResource extends AbstractWebResource {
Jian Lic132c112016-01-28 20:27:34 -080056
Jian Li9f3a8852016-04-07 13:37:39 -070057 private final Logger log = LoggerFactory.getLogger(getClass());
Jian Li7eed4172016-04-07 22:12:03 -070058 private final ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
59 private final MetricsService metricsService = get(MetricsService.class);
60
Jian Li80c12702016-02-20 08:58:19 +090061 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 Li9f3a8852016-04-07 13:37:39 -070065 private static final int PERCENT_CONSTANT = 100;
Jian Lia73fce32016-04-11 17:08:35 -070066 private static final String SYSTEM_TYPE = "system";
67 private static final String DISK_TYPE = "disk";
68 private static final String NETWORK_TYPE = "network";
Jian Li9f3a8852016-04-07 13:37:39 -070069
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 Lic132c112016-01-28 20:27:34 -080077
Jian Lia73fce32016-04-11 17:08:35 -070078 private SystemMetricsAggregator aggregator = SystemMetricsAggregator.getInstance();
Jian Li7eed4172016-04-07 22:12:03 -070079
Jian Lic132c112016-01-28 20:27:34 -080080 /**
81 * Collects CPU metrics.
82 *
83 * @param stream JSON stream
84 * @return 200 OK
85 * @onos.rsModel CpuMetricsPost
86 */
87 @POST
Jian Li54df73e2016-02-01 17:09:03 -080088 @Path("cpu_metrics")
Jian Lic132c112016-01-28 20:27:34 -080089 @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 Li9f3a8852016-04-07 13:37:39 -070096
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 Li1fdd2242016-02-05 10:01:19 -0800103 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 Lic132c112016-01-28 20:27:34 -0800107
Jian Lia73fce32016-04-11 17:08:35 -0700108 aggregator.setMetricsService(metricsService);
109 aggregator.addMetrics(Optional.ofNullable(null), SYSTEM_TYPE);
110
Jian Li1fdd2242016-02-05 10:01:19 -0800111 cm = new ControlMetric(ControlMetricType.CPU_LOAD,
112 new MetricValue.Builder().load(cpuLoad).add());
Jian Li7eed4172016-04-07 22:12:03 -0700113 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700114 aggregator.increment(ControlMetricType.CPU_LOAD, cpuLoad);
Jian Lic132c112016-01-28 20:27:34 -0800115
Jian Li1fdd2242016-02-05 10:01:19 -0800116 cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
117 new MetricValue.Builder().load(totalCpuTime).add());
Jian Li7eed4172016-04-07 22:12:03 -0700118 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700119 aggregator.increment(ControlMetricType.TOTAL_CPU_TIME, totalCpuTime);
Jian Lic132c112016-01-28 20:27:34 -0800120
Jian Li1fdd2242016-02-05 10:01:19 -0800121 cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME,
122 new MetricValue.Builder().load(sysCpuTime).add());
Jian Li7eed4172016-04-07 22:12:03 -0700123 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700124 aggregator.increment(ControlMetricType.SYS_CPU_TIME, sysCpuTime);
Jian Lic132c112016-01-28 20:27:34 -0800125
Jian Li1fdd2242016-02-05 10:01:19 -0800126 cm = new ControlMetric(ControlMetricType.USER_CPU_TIME,
127 new MetricValue.Builder().load(userCpuTime).add());
Jian Li7eed4172016-04-07 22:12:03 -0700128 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700129 aggregator.increment(ControlMetricType.USER_CPU_TIME, userCpuTime);
Jian Lic132c112016-01-28 20:27:34 -0800130
Jian Li1fdd2242016-02-05 10:01:19 -0800131 cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME,
132 new MetricValue.Builder().load(cpuIdleTime).add());
Jian Li7eed4172016-04-07 22:12:03 -0700133 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700134 aggregator.increment(ControlMetricType.CPU_IDLE_TIME, cpuIdleTime);
Jian Lic132c112016-01-28 20:27:34 -0800135
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 Li54df73e2016-02-01 17:09:03 -0800150 @Path("memory_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800151 @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 Li9f3a8852016-04-07 13:37:39 -0700158
159 if (jsonTree == null || !checkFields(jsonTree, MEMORY_FIELD_SET)) {
160 ok(root).build();
161 }
162
Jian Li1fdd2242016-02-05 10:01:19 -0800163 long memUsed = nullIsIllegal(jsonTree.get("memoryUsed").asLong(), INVALID_REQUEST);
164 long memFree = nullIsIllegal(jsonTree.get("memoryFree").asLong(), INVALID_REQUEST);
Jian Li9f3a8852016-04-07 13:37:39 -0700165 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 Lic132c112016-01-28 20:27:34 -0800168
Jian Lia73fce32016-04-11 17:08:35 -0700169 aggregator.setMetricsService(metricsService);
170 aggregator.addMetrics(Optional.ofNullable(null), SYSTEM_TYPE);
171
Jian Li1fdd2242016-02-05 10:01:19 -0800172 cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO,
173 new MetricValue.Builder().load(memUsedRatio).add());
Jian Li7eed4172016-04-07 22:12:03 -0700174 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700175 aggregator.increment(ControlMetricType.MEMORY_USED_RATIO, memUsedRatio);
Jian Lic132c112016-01-28 20:27:34 -0800176
Jian Li1fdd2242016-02-05 10:01:19 -0800177 cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO,
178 new MetricValue.Builder().load(memFreeRatio).add());
Jian Li7eed4172016-04-07 22:12:03 -0700179 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700180 aggregator.increment(ControlMetricType.MEMORY_FREE_RATIO, memFreeRatio);
Jian Lic132c112016-01-28 20:27:34 -0800181
Jian Li1fdd2242016-02-05 10:01:19 -0800182 cm = new ControlMetric(ControlMetricType.MEMORY_USED,
183 new MetricValue.Builder().load(memUsed).add());
Jian Li7eed4172016-04-07 22:12:03 -0700184 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700185 aggregator.increment(ControlMetricType.MEMORY_USED, memUsed);
Jian Lic132c112016-01-28 20:27:34 -0800186
Jian Li1fdd2242016-02-05 10:01:19 -0800187 cm = new ControlMetric(ControlMetricType.MEMORY_FREE,
188 new MetricValue.Builder().load(memFree).add());
Jian Li7eed4172016-04-07 22:12:03 -0700189 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null));
Jian Lia73fce32016-04-11 17:08:35 -0700190 aggregator.increment(ControlMetricType.MEMORY_FREE, memFree);
Jian Lic132c112016-01-28 20:27:34 -0800191
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 Li54df73e2016-02-01 17:09:03 -0800206 @Path("disk_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800207 @Consumes(MediaType.APPLICATION_JSON)
208 @Produces(MediaType.APPLICATION_JSON)
209 public Response diskMetrics(InputStream stream) {
210 ObjectNode root = mapper().createObjectNode();
Jian Li1fdd2242016-02-05 10:01:19 -0800211 ControlMetric cm;
Jian Lic132c112016-01-28 20:27:34 -0800212 try {
213 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li9f3a8852016-04-07 13:37:39 -0700214 ArrayNode diskRes =
Jian Lia73fce32016-04-11 17:08:35 -0700215 jsonTree.get("disks") == null ?
216 mapper().createArrayNode() : (ArrayNode) jsonTree.get("disks");
Jian Li9f3a8852016-04-07 13:37:39 -0700217
Jian Li1fdd2242016-02-05 10:01:19 -0800218 for (JsonNode node : diskRes) {
Jian Liba6b1172016-02-01 22:40:42 -0800219 JsonNode resourceName = node.get("resourceName");
220 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800221
Jian Lia73fce32016-04-11 17:08:35 -0700222 aggregator.setMetricsService(metricsService);
223 aggregator.addMetrics(Optional.of(resourceName.asText()), DISK_TYPE);
Jian Li7eed4172016-04-07 22:12:03 -0700224
Jian Li1fdd2242016-02-05 10:01:19 -0800225 long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST);
226 long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST);
Jian Lic132c112016-01-28 20:27:34 -0800227
Jian Li1fdd2242016-02-05 10:01:19 -0800228 cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
229 new MetricValue.Builder().load(readBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700230 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700231 aggregator.increment(resourceName.asText(), DISK_TYPE,
232 ControlMetricType.DISK_READ_BYTES, readBytes);
Jian Li1fdd2242016-02-05 10:01:19 -0800233 cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
234 new MetricValue.Builder().load(writeBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700235 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700236 aggregator.increment(resourceName.asText(), DISK_TYPE,
237 ControlMetricType.DISK_WRITE_BYTES, writeBytes);
Jian Li1fdd2242016-02-05 10:01:19 -0800238 }
Jian Lic132c112016-01-28 20:27:34 -0800239 } 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 Li54df73e2016-02-01 17:09:03 -0800253 @Path("network_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800254 @Consumes(MediaType.APPLICATION_JSON)
255 @Produces(MediaType.APPLICATION_JSON)
256 public Response networkMetrics(InputStream stream) {
257 ObjectNode root = mapper().createObjectNode();
Jian Li1fdd2242016-02-05 10:01:19 -0800258 ControlMetric cm;
Jian Lic132c112016-01-28 20:27:34 -0800259 try {
260 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li9f3a8852016-04-07 13:37:39 -0700261
262 ArrayNode networkRes = jsonTree.get("networks") == null
263 ? mapper().createArrayNode() : (ArrayNode) jsonTree.get("networks");
264
Jian Li1fdd2242016-02-05 10:01:19 -0800265 for (JsonNode node : networkRes) {
Jian Liba6b1172016-02-01 22:40:42 -0800266 JsonNode resourceName = node.get("resourceName");
267 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800268
Jian Lia73fce32016-04-11 17:08:35 -0700269 aggregator.setMetricsService(metricsService);
270 aggregator.addMetrics(Optional.of(resourceName.asText()), NETWORK_TYPE);
Jian Li7eed4172016-04-07 22:12:03 -0700271
Jian Li1fdd2242016-02-05 10:01:19 -0800272 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 Lic132c112016-01-28 20:27:34 -0800276
Jian Li1fdd2242016-02-05 10:01:19 -0800277 cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
278 new MetricValue.Builder().load(inBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700279 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700280 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
281 ControlMetricType.NW_INCOMING_BYTES, inBytes);
Jian Lic132c112016-01-28 20:27:34 -0800282
Jian Li1fdd2242016-02-05 10:01:19 -0800283 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
284 new MetricValue.Builder().load(outBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700285 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700286 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
287 ControlMetricType.NW_OUTGOING_BYTES, outBytes);
Jian Lic132c112016-01-28 20:27:34 -0800288
Jian Li1fdd2242016-02-05 10:01:19 -0800289 cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
290 new MetricValue.Builder().load(inPackets).add());
Jian Li7eed4172016-04-07 22:12:03 -0700291 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700292 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
293 ControlMetricType.NW_INCOMING_PACKETS, inPackets);
Jian Lic132c112016-01-28 20:27:34 -0800294
Jian Li1fdd2242016-02-05 10:01:19 -0800295 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
296 new MetricValue.Builder().load(outPackets).add());
Jian Li7eed4172016-04-07 22:12:03 -0700297 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700298 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
299 ControlMetricType.NW_OUTGOING_PACKETS, outPackets);
Jian Li1fdd2242016-02-05 10:01:19 -0800300 }
Jian Lic132c112016-01-28 20:27:34 -0800301 } catch (IOException e) {
302 throw new IllegalArgumentException(e.getMessage());
303 }
304 return ok(root).build();
305 }
306
Jian Lic132c112016-01-28 20:27:34 -0800307 /**
Jian Li1a424692016-02-03 16:21:18 -0800308 * Collects system information.
309 * The system information includes the various control metrics
Jian Lic132c112016-01-28 20:27:34 -0800310 * which do not require aggregation.
311 *
312 * @param stream JSON stream
313 * @return 200 OK
Jian Li1a424692016-02-03 16:21:18 -0800314 * @onos.rsModel SystemInfoPost
Jian Lic132c112016-01-28 20:27:34 -0800315 */
316 @POST
Jian Li1a424692016-02-03 16:21:18 -0800317 @Path("system_info")
Jian Lic132c112016-01-28 20:27:34 -0800318 @Consumes(MediaType.APPLICATION_JSON)
319 @Produces(MediaType.APPLICATION_JSON)
Jian Li1a424692016-02-03 16:21:18 -0800320 public Response systemInfo(InputStream stream) {
Jian Lic132c112016-01-28 20:27:34 -0800321 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 Li1a424692016-02-03 16:21:18 -0800330 if (numOfCores != null && numOfCpus != null &&
331 cpuSpeed != null && totalMemory != null) {
332 SystemInfo systemInfo = new DefaultSystemInfo.Builder()
333 .numOfCores(numOfCores.asInt())
Jian Lic132c112016-01-28 20:27:34 -0800334 .numOfCpus(numOfCpus.asInt())
335 .cpuSpeed(cpuSpeed.asInt())
Jian Li1a424692016-02-03 16:21:18 -0800336 .totalMemory(totalMemory.asInt())
Jian Lic132c112016-01-28 20:27:34 -0800337 .build();
Jian Lic132c112016-01-28 20:27:34 -0800338
Jian Li1a424692016-02-03 16:21:18 -0800339 // try to store the system info.
340 SystemInfoFactory.getInstance().setSystemInfo(systemInfo);
Jian Lic132c112016-01-28 20:27:34 -0800341 } 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 Li9f3a8852016-04-07 13:37:39 -0700350
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 Li9d616492016-03-09 10:52:49 -0800370}