blob: ca10f86837ca951242a1ce43ce7f930d81a296f6 [file] [log] [blame]
Jian Lic132c112016-01-28 20:27:34 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
30import org.onosproject.cpman.impl.SystemInfoFactory;
Jian Lib1548ee2016-05-11 09:49:57 -070031import org.onosproject.cpman.impl.SystemMetricsAggregator;
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;
Jian Lic132c112016-01-28 20:27:34 -080039import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
41import java.io.IOException;
42import java.io.InputStream;
Jian Li9f3a8852016-04-07 13:37:39 -070043import java.util.Iterator;
Jian Lic132c112016-01-28 20:27:34 -080044import java.util.Optional;
Jian Li9f3a8852016-04-07 13:37:39 -070045import java.util.Set;
46import java.util.stream.Collectors;
Jian Lic132c112016-01-28 20:27:34 -080047
Jian Liba6b1172016-02-01 22:40:42 -080048import static org.onlab.util.Tools.nullIsIllegal;
49
Jian Lic132c112016-01-28 20:27:34 -080050/**
Jian Li80c12702016-02-20 08:58:19 +090051 * Collect system metrics.
Jian Lic132c112016-01-28 20:27:34 -080052 */
Jian Li54df73e2016-02-01 17:09:03 -080053@Path("collector")
Jian Li80c12702016-02-20 08:58:19 +090054public class SystemMetricsCollectorWebResource extends AbstractWebResource {
Jian Lic132c112016-01-28 20:27:34 -080055
Jian Li9f3a8852016-04-07 13:37:39 -070056 private final Logger log = LoggerFactory.getLogger(getClass());
Jian Li7eed4172016-04-07 22:12:03 -070057 private final ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class);
58 private final MetricsService metricsService = get(MetricsService.class);
59
Jian Li80c12702016-02-20 08:58:19 +090060 private static final int UPDATE_INTERVAL_IN_MINUTE = 1;
61 private static final String INVALID_SYSTEM_SPECS = "Invalid system specifications";
62 private static final String INVALID_RESOURCE_NAME = "Invalid resource name";
63 private static final String INVALID_REQUEST = "Invalid request";
Jian Li9f3a8852016-04-07 13:37:39 -070064 private static final int PERCENT_CONSTANT = 100;
Jian Lia73fce32016-04-11 17:08:35 -070065 private static final String SYSTEM_TYPE = "system";
66 private static final String DISK_TYPE = "disk";
67 private static final String NETWORK_TYPE = "network";
Jian Li9f3a8852016-04-07 13:37:39 -070068
69 private static final Set<String> MEMORY_FIELD_SET = ControlResource.MEMORY_METRICS
70 .stream().map(type -> toCamelCase(type.toString(), true))
71 .collect(Collectors.toSet());
72
73 private static final Set<String> CPU_FIELD_SET = ControlResource.CPU_METRICS
74 .stream().map(type -> toCamelCase(type.toString(), true))
75 .collect(Collectors.toSet());
Jian Lic132c112016-01-28 20:27:34 -080076
Jian Lia73fce32016-04-11 17:08:35 -070077 private SystemMetricsAggregator aggregator = SystemMetricsAggregator.getInstance();
Jian Li7eed4172016-04-07 22:12:03 -070078
Jian Lic132c112016-01-28 20:27:34 -080079 /**
80 * Collects CPU metrics.
81 *
82 * @param stream JSON stream
83 * @return 200 OK
84 * @onos.rsModel CpuMetricsPost
85 */
86 @POST
Jian Li54df73e2016-02-01 17:09:03 -080087 @Path("cpu_metrics")
Jian Lic132c112016-01-28 20:27:34 -080088 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic132c112016-01-28 20:27:34 -080089 public Response cpuMetrics(InputStream stream) {
90 ObjectNode root = mapper().createObjectNode();
91 ControlMetric cm;
92 try {
93 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li9f3a8852016-04-07 13:37:39 -070094
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 Li1fdd2242016-02-05 10:01:19 -0800101 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 Lic132c112016-01-28 20:27:34 -0800105
Jian Lia73fce32016-04-11 17:08:35 -0700106 aggregator.setMetricsService(metricsService);
Jian Li67e1e152016-04-18 17:52:58 -0700107 aggregator.addMetrics(Optional.empty(), SYSTEM_TYPE);
Jian Lia73fce32016-04-11 17:08:35 -0700108
Jian Li1fdd2242016-02-05 10:01:19 -0800109 cm = new ControlMetric(ControlMetricType.CPU_LOAD,
110 new MetricValue.Builder().load(cpuLoad).add());
Jian Li67e1e152016-04-18 17:52:58 -0700111 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700112 aggregator.increment(ControlMetricType.CPU_LOAD, cpuLoad);
Jian Lic132c112016-01-28 20:27:34 -0800113
Jian Li1fdd2242016-02-05 10:01:19 -0800114 cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME,
115 new MetricValue.Builder().load(totalCpuTime).add());
Jian Li67e1e152016-04-18 17:52:58 -0700116 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700117 aggregator.increment(ControlMetricType.TOTAL_CPU_TIME, totalCpuTime);
Jian Lic132c112016-01-28 20:27:34 -0800118
Jian Li1fdd2242016-02-05 10:01:19 -0800119 cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME,
120 new MetricValue.Builder().load(sysCpuTime).add());
Jian Li67e1e152016-04-18 17:52:58 -0700121 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700122 aggregator.increment(ControlMetricType.SYS_CPU_TIME, sysCpuTime);
Jian Lic132c112016-01-28 20:27:34 -0800123
Jian Li1fdd2242016-02-05 10:01:19 -0800124 cm = new ControlMetric(ControlMetricType.USER_CPU_TIME,
125 new MetricValue.Builder().load(userCpuTime).add());
Jian Li67e1e152016-04-18 17:52:58 -0700126 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700127 aggregator.increment(ControlMetricType.USER_CPU_TIME, userCpuTime);
Jian Lic132c112016-01-28 20:27:34 -0800128
Jian Li1fdd2242016-02-05 10:01:19 -0800129 cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME,
130 new MetricValue.Builder().load(cpuIdleTime).add());
Jian Li67e1e152016-04-18 17:52:58 -0700131 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700132 aggregator.increment(ControlMetricType.CPU_IDLE_TIME, cpuIdleTime);
Jian Lic132c112016-01-28 20:27:34 -0800133
134 } catch (IOException e) {
Jian Lib1548ee2016-05-11 09:49:57 -0700135 throw new IllegalArgumentException(e);
Jian Lic132c112016-01-28 20:27:34 -0800136 }
137 return ok(root).build();
138 }
139
140 /**
141 * Collects memory metrics.
142 *
143 * @param stream JSON stream
144 * @return 200 OK
145 * @onos.rsModel MemoryMetricsPost
146 */
147 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800148 @Path("memory_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800149 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic132c112016-01-28 20:27:34 -0800150 public Response memoryMetrics(InputStream stream) {
151 ObjectNode root = mapper().createObjectNode();
152 ControlMetric cm;
153 try {
154 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li9f3a8852016-04-07 13:37:39 -0700155
156 if (jsonTree == null || !checkFields(jsonTree, MEMORY_FIELD_SET)) {
157 ok(root).build();
158 }
159
Jian Li1fdd2242016-02-05 10:01:19 -0800160 long memUsed = nullIsIllegal(jsonTree.get("memoryUsed").asLong(), INVALID_REQUEST);
161 long memFree = nullIsIllegal(jsonTree.get("memoryFree").asLong(), INVALID_REQUEST);
Jian Li9f3a8852016-04-07 13:37:39 -0700162 long memTotal = memUsed + memFree;
163 long memUsedRatio = memTotal == 0L ? 0L : (memUsed * PERCENT_CONSTANT) / memTotal;
164 long memFreeRatio = memTotal == 0L ? 0L : (memFree * PERCENT_CONSTANT) / memTotal;
Jian Lic132c112016-01-28 20:27:34 -0800165
Jian Lia73fce32016-04-11 17:08:35 -0700166 aggregator.setMetricsService(metricsService);
Jian Li67e1e152016-04-18 17:52:58 -0700167 aggregator.addMetrics(Optional.empty(), SYSTEM_TYPE);
Jian Lia73fce32016-04-11 17:08:35 -0700168
Jian Li1fdd2242016-02-05 10:01:19 -0800169 cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO,
170 new MetricValue.Builder().load(memUsedRatio).add());
Jian Li67e1e152016-04-18 17:52:58 -0700171 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700172 aggregator.increment(ControlMetricType.MEMORY_USED_RATIO, memUsedRatio);
Jian Lic132c112016-01-28 20:27:34 -0800173
Jian Li1fdd2242016-02-05 10:01:19 -0800174 cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO,
175 new MetricValue.Builder().load(memFreeRatio).add());
Jian Li67e1e152016-04-18 17:52:58 -0700176 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700177 aggregator.increment(ControlMetricType.MEMORY_FREE_RATIO, memFreeRatio);
Jian Lic132c112016-01-28 20:27:34 -0800178
Jian Li1fdd2242016-02-05 10:01:19 -0800179 cm = new ControlMetric(ControlMetricType.MEMORY_USED,
180 new MetricValue.Builder().load(memUsed).add());
Jian Li67e1e152016-04-18 17:52:58 -0700181 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700182 aggregator.increment(ControlMetricType.MEMORY_USED, memUsed);
Jian Lic132c112016-01-28 20:27:34 -0800183
Jian Li1fdd2242016-02-05 10:01:19 -0800184 cm = new ControlMetric(ControlMetricType.MEMORY_FREE,
185 new MetricValue.Builder().load(memFree).add());
Jian Li67e1e152016-04-18 17:52:58 -0700186 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.empty());
Jian Lia73fce32016-04-11 17:08:35 -0700187 aggregator.increment(ControlMetricType.MEMORY_FREE, memFree);
Jian Lic132c112016-01-28 20:27:34 -0800188
189 } catch (IOException e) {
Jian Lib1548ee2016-05-11 09:49:57 -0700190 throw new IllegalArgumentException(e);
Jian Lic132c112016-01-28 20:27:34 -0800191 }
192 return ok(root).build();
193 }
194
195 /**
196 * Collects disk metrics.
197 *
198 * @param stream JSON stream
199 * @return 200 OK
200 * @onos.rsModel DiskMetricsPost
201 */
202 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800203 @Path("disk_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800204 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic132c112016-01-28 20:27:34 -0800205 public Response diskMetrics(InputStream stream) {
206 ObjectNode root = mapper().createObjectNode();
Jian Li1fdd2242016-02-05 10:01:19 -0800207 ControlMetric cm;
Jian Lic132c112016-01-28 20:27:34 -0800208 try {
209 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li9f3a8852016-04-07 13:37:39 -0700210 ArrayNode diskRes =
Jian Lia73fce32016-04-11 17:08:35 -0700211 jsonTree.get("disks") == null ?
212 mapper().createArrayNode() : (ArrayNode) jsonTree.get("disks");
Jian Li9f3a8852016-04-07 13:37:39 -0700213
Jian Li1fdd2242016-02-05 10:01:19 -0800214 for (JsonNode node : diskRes) {
Jian Liba6b1172016-02-01 22:40:42 -0800215 JsonNode resourceName = node.get("resourceName");
216 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800217
Jian Lia73fce32016-04-11 17:08:35 -0700218 aggregator.setMetricsService(metricsService);
219 aggregator.addMetrics(Optional.of(resourceName.asText()), DISK_TYPE);
Jian Li7eed4172016-04-07 22:12:03 -0700220
Jian Li1fdd2242016-02-05 10:01:19 -0800221 long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST);
222 long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST);
Jian Lic132c112016-01-28 20:27:34 -0800223
Jian Li1fdd2242016-02-05 10:01:19 -0800224 cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES,
225 new MetricValue.Builder().load(readBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700226 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700227 aggregator.increment(resourceName.asText(), DISK_TYPE,
228 ControlMetricType.DISK_READ_BYTES, readBytes);
Jian Li1fdd2242016-02-05 10:01:19 -0800229 cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES,
230 new MetricValue.Builder().load(writeBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700231 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700232 aggregator.increment(resourceName.asText(), DISK_TYPE,
233 ControlMetricType.DISK_WRITE_BYTES, writeBytes);
Jian Li1fdd2242016-02-05 10:01:19 -0800234 }
Jian Lic132c112016-01-28 20:27:34 -0800235 } catch (IOException e) {
Jian Lib1548ee2016-05-11 09:49:57 -0700236 throw new IllegalArgumentException(e);
Jian Lic132c112016-01-28 20:27:34 -0800237 }
238 return ok(root).build();
239 }
240
241 /**
242 * Collects network metrics.
243 *
244 * @param stream JSON stream
245 * @return 200 OK
246 * @onos.rsModel NetworkMetricsPost
247 */
248 @POST
Jian Li54df73e2016-02-01 17:09:03 -0800249 @Path("network_metrics")
Jian Lic132c112016-01-28 20:27:34 -0800250 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic132c112016-01-28 20:27:34 -0800251 public Response networkMetrics(InputStream stream) {
252 ObjectNode root = mapper().createObjectNode();
Jian Li1fdd2242016-02-05 10:01:19 -0800253 ControlMetric cm;
Jian Lic132c112016-01-28 20:27:34 -0800254 try {
255 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Jian Li9f3a8852016-04-07 13:37:39 -0700256
257 ArrayNode networkRes = jsonTree.get("networks") == null
258 ? mapper().createArrayNode() : (ArrayNode) jsonTree.get("networks");
259
Jian Li1fdd2242016-02-05 10:01:19 -0800260 for (JsonNode node : networkRes) {
Jian Liba6b1172016-02-01 22:40:42 -0800261 JsonNode resourceName = node.get("resourceName");
262 nullIsIllegal(resourceName, INVALID_RESOURCE_NAME);
Jian Lic132c112016-01-28 20:27:34 -0800263
Jian Lia73fce32016-04-11 17:08:35 -0700264 aggregator.setMetricsService(metricsService);
265 aggregator.addMetrics(Optional.of(resourceName.asText()), NETWORK_TYPE);
Jian Li7eed4172016-04-07 22:12:03 -0700266
Jian Li1fdd2242016-02-05 10:01:19 -0800267 long inBytes = nullIsIllegal(node.get("incomingBytes").asLong(), INVALID_REQUEST);
268 long outBytes = nullIsIllegal(node.get("outgoingBytes").asLong(), INVALID_REQUEST);
269 long inPackets = nullIsIllegal(node.get("incomingPackets").asLong(), INVALID_REQUEST);
270 long outPackets = nullIsIllegal(node.get("outgoingPackets").asLong(), INVALID_REQUEST);
Jian Lic132c112016-01-28 20:27:34 -0800271
Jian Li1fdd2242016-02-05 10:01:19 -0800272 cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES,
273 new MetricValue.Builder().load(inBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700274 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700275 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
276 ControlMetricType.NW_INCOMING_BYTES, inBytes);
Jian Lic132c112016-01-28 20:27:34 -0800277
Jian Li1fdd2242016-02-05 10:01:19 -0800278 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES,
279 new MetricValue.Builder().load(outBytes).add());
Jian Li7eed4172016-04-07 22:12:03 -0700280 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700281 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
282 ControlMetricType.NW_OUTGOING_BYTES, outBytes);
Jian Lic132c112016-01-28 20:27:34 -0800283
Jian Li1fdd2242016-02-05 10:01:19 -0800284 cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS,
285 new MetricValue.Builder().load(inPackets).add());
Jian Li7eed4172016-04-07 22:12:03 -0700286 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700287 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
288 ControlMetricType.NW_INCOMING_PACKETS, inPackets);
Jian Lic132c112016-01-28 20:27:34 -0800289
Jian Li1fdd2242016-02-05 10:01:19 -0800290 cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS,
291 new MetricValue.Builder().load(outPackets).add());
Jian Li7eed4172016-04-07 22:12:03 -0700292 monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText());
Jian Lia73fce32016-04-11 17:08:35 -0700293 aggregator.increment(resourceName.asText(), NETWORK_TYPE,
294 ControlMetricType.NW_OUTGOING_PACKETS, outPackets);
Jian Li1fdd2242016-02-05 10:01:19 -0800295 }
Jian Lic132c112016-01-28 20:27:34 -0800296 } catch (IOException e) {
Jian Lib1548ee2016-05-11 09:49:57 -0700297 throw new IllegalArgumentException(e);
Jian Lic132c112016-01-28 20:27:34 -0800298 }
299 return ok(root).build();
300 }
301
Jian Lic132c112016-01-28 20:27:34 -0800302 /**
Jian Li1a424692016-02-03 16:21:18 -0800303 * Collects system information.
304 * The system information includes the various control metrics
Jian Lic132c112016-01-28 20:27:34 -0800305 * which do not require aggregation.
306 *
307 * @param stream JSON stream
308 * @return 200 OK
Jian Li1a424692016-02-03 16:21:18 -0800309 * @onos.rsModel SystemInfoPost
Jian Lic132c112016-01-28 20:27:34 -0800310 */
311 @POST
Jian Li1a424692016-02-03 16:21:18 -0800312 @Path("system_info")
Jian Lic132c112016-01-28 20:27:34 -0800313 @Consumes(MediaType.APPLICATION_JSON)
Jian Li1a424692016-02-03 16:21:18 -0800314 public Response systemInfo(InputStream stream) {
Jian Lic132c112016-01-28 20:27:34 -0800315 ObjectNode root = mapper().createObjectNode();
316
317 try {
318 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
319 JsonNode numOfCores = jsonTree.get("numOfCores");
320 JsonNode numOfCpus = jsonTree.get("numOfCpus");
321 JsonNode cpuSpeed = jsonTree.get("cpuSpeed");
322 JsonNode totalMemory = jsonTree.get("totalMemory");
323
Jian Li1a424692016-02-03 16:21:18 -0800324 if (numOfCores != null && numOfCpus != null &&
325 cpuSpeed != null && totalMemory != null) {
326 SystemInfo systemInfo = new DefaultSystemInfo.Builder()
327 .numOfCores(numOfCores.asInt())
Jian Lic132c112016-01-28 20:27:34 -0800328 .numOfCpus(numOfCpus.asInt())
329 .cpuSpeed(cpuSpeed.asInt())
Jian Li1a424692016-02-03 16:21:18 -0800330 .totalMemory(totalMemory.asInt())
Jian Lic132c112016-01-28 20:27:34 -0800331 .build();
Jian Lic132c112016-01-28 20:27:34 -0800332
Jian Li1a424692016-02-03 16:21:18 -0800333 // try to store the system info.
334 SystemInfoFactory.getInstance().setSystemInfo(systemInfo);
Jian Lic132c112016-01-28 20:27:34 -0800335 } else {
336 throw new IllegalArgumentException(INVALID_SYSTEM_SPECS);
337 }
338
339 } catch (IOException e) {
Jian Lib1548ee2016-05-11 09:49:57 -0700340 throw new IllegalArgumentException(e);
Jian Lic132c112016-01-28 20:27:34 -0800341 }
342 return ok(root).build();
343 }
Jian Li9f3a8852016-04-07 13:37:39 -0700344
345 private boolean checkFields(ObjectNode node, Set<String> original) {
346 Iterator<String> fieldNames = node.fieldNames();
347 while (fieldNames.hasNext()) {
348 String fieldName = fieldNames.next();
349 if (!original.contains(fieldName) || node.get(fieldName) == null) {
350 log.warn("Illegal field name: {}", fieldName);
351 return false;
352 }
353 }
354 return true;
355 }
356
357 private static String toCamelCase(String value, boolean startWithLowerCase) {
358 String[] strings = StringUtils.split(value.toLowerCase(), "_");
359 for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++) {
360 strings[i] = StringUtils.capitalize(strings[i]);
361 }
362 return StringUtils.join(strings);
363 }
Jian Li9d616492016-03-09 10:52:49 -0800364}