Simon Hunt
Committed by Gerrit Code Review

GUI -- Simpler version of DeviceGuiResource for the UI.

 D'Oh -- has to have public-no-args constructor!

Change-Id: Ib586f74bdd109385fd1772e53122e096bdd7e8d5
1 +/*
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 + */
16 +package org.onosproject.gui;
17 +
18 +import com.fasterxml.jackson.databind.ObjectMapper;
19 +import com.fasterxml.jackson.databind.node.ArrayNode;
20 +import com.fasterxml.jackson.databind.node.ObjectNode;
21 +import org.onlab.rest.BaseResource;
22 +import org.onosproject.net.Annotations;
23 +import org.onosproject.net.Device;
24 +import org.onosproject.net.device.DeviceService;
25 +import org.slf4j.Logger;
26 +
27 +import javax.ws.rs.GET;
28 +import javax.ws.rs.Path;
29 +import javax.ws.rs.Produces;
30 +import javax.ws.rs.core.Response;
31 +
32 +import static org.slf4j.LoggerFactory.getLogger;
33 +
34 +/**
35 + * UI REST resource for interacting with the inventory of infrastructure devices.
36 + */
37 +@Path("device")
38 +public 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());
72 + String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE;
73 +
74 + ObjectNode result = mapper.createObjectNode();
75 + result.put("id", device.id().toString())
76 + .put("available", service.isAvailable(device.id()))
77 + .put("_iconid_available", iconId)
78 + .put("type", device.type().toString())
79 + .put("role", service.getRole(device.id()).toString())
80 + .put("mfr", device.manufacturer())
81 + .put("hw", device.hwVersion())
82 + .put("sw", device.swVersion())
83 + .put("serial", device.serialNumber())
84 + .set("annotations", annotations(mapper, device.annotations()));
85 + return result;
86 + }
87 +
88 + /**
89 + * Produces a JSON object from the specified key/value annotations.
90 + *
91 + * @param mapper ObjectMapper to use while converting to JSON
92 + * @param annotations key/value annotations
93 + * @return JSON object
94 + */
95 + private static ObjectNode annotations(ObjectMapper mapper, Annotations annotations) {
96 + ObjectNode result = mapper.createObjectNode();
97 + for (String key : annotations.keys()) {
98 + result.put(key, annotations.value(key));
99 + }
100 + return result;
101 + }
102 +
103 +}