Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 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()); |
Simon Hunt | 4367abc | 2015-01-16 15:14:11 -0800 | [diff] [blame] | 72 | // pick the appropriate id for the icon to appear in the table row |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 73 | String iconId = available ? ICON_ID_ONLINE : ICON_ID_OFFLINE; |
| 74 | |
| 75 | ObjectNode result = mapper.createObjectNode(); |
| 76 | result.put("id", device.id().toString()) |
Simon Hunt | 4367abc | 2015-01-16 15:14:11 -0800 | [diff] [blame] | 77 | .put("available", available) |
Simon Hunt | 1f170b8 | 2015-01-16 14:30:20 -0800 | [diff] [blame] | 78 | .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 | } |