blob: 144a13c40a96f63d16fad77ad739a4a2d7b7e73d [file] [log] [blame]
Simon Hunt1f170b82015-01-16 14:30:20 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.gui;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onlab.rest.BaseResource;
22import org.onosproject.net.Annotations;
23import org.onosproject.net.Device;
24import org.onosproject.net.device.DeviceService;
25import org.slf4j.Logger;
26
27import javax.ws.rs.GET;
28import javax.ws.rs.Path;
29import javax.ws.rs.Produces;
30import javax.ws.rs.core.Response;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * UI REST resource for interacting with the inventory of infrastructure devices.
36 */
37@Path("device")
38public class DeviceGuiResource extends BaseResource {
39
40 private static final String ICON_ID_ONLINE = "deviceOnline";
41 private static final String ICON_ID_OFFLINE = "deviceOffline";
42
43 private static final Logger log = getLogger(DeviceGuiResource.class);
44
45 private final ObjectMapper mapper = new ObjectMapper();
46
47
48 // return list of devices
49 @GET
50 @Produces("application/json")
51 public Response getDevices() {
52 ObjectNode rootNode = mapper.createObjectNode();
53 ArrayNode devices = mapper.createArrayNode();
54 DeviceService service = get(DeviceService.class);
55
56 for (Device dev: service.getDevices()) {
57 devices.add(deviceJson(service, dev));
58 }
59
60 rootNode.set("devices", devices);
61 return Response.ok(rootNode.toString()).build();
62 }
63
64 /**
65 * Returns a JSON node representing the specified device.
66 *
67 * @param device infrastructure device
68 * @return JSON node
69 */
70 private ObjectNode deviceJson(DeviceService service, Device device) {
71 boolean available = service.isAvailable(device.id());
Simon Hunt4367abc2015-01-16 15:14:11 -080072 // pick the appropriate id for the icon to appear in the table row
Simon Hunt1f170b82015-01-16 14:30:20 -080073 String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
74
75 ObjectNode result = mapper.createObjectNode();
76 result.put("id", device.id().toString())
Simon Hunt4367abc2015-01-16 15:14:11 -080077 .put("available", available)
Simon Hunt1f170b82015-01-16 14:30:20 -080078 .put("_iconid_available", iconId)
79 .put("type", device.type().toString())
80 .put("role", service.getRole(device.id()).toString())
81 .put("mfr", device.manufacturer())
82 .put("hw", device.hwVersion())
83 .put("sw", device.swVersion())
84 .put("serial", device.serialNumber())
85 .set("annotations", annotations(mapper, device.annotations()));
86 return result;
87 }
88
89 /**
90 * Produces a JSON object from the specified key/value annotations.
91 *
92 * @param mapper ObjectMapper to use while converting to JSON
93 * @param annotations key/value annotations
94 * @return JSON object
95 */
96 private static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
97 ObjectNode result = mapper.createObjectNode();
98 for (String key : annotations.keys()) {
99 result.put(key, annotations.value(key));
100 }
101 return result;
102 }
103
104}