Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 15 | */ |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 16 | package org.onosproject.ui.impl; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 22 | import org.onlab.rest.BaseResource; |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 23 | import org.slf4j.Logger; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 24 | |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 25 | import javax.ws.rs.Consumes; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 26 | import javax.ws.rs.GET; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 27 | import javax.ws.rs.POST; |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 28 | import javax.ws.rs.Path; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 29 | import javax.ws.rs.Produces; |
| 30 | import javax.ws.rs.core.Response; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 33 | import java.util.Map; |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 34 | |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Topology viewer resource. |
| 39 | */ |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 40 | @Path("topology") |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 41 | public class TopologyResource extends BaseResource { |
| 42 | |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 43 | private static final Logger log = getLogger(TopologyResource.class); |
| 44 | |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 45 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 46 | |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 47 | @Path("geoloc") |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 48 | @GET |
| 49 | @Produces("application/json") |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 50 | public Response getGeoLocations() { |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 51 | ObjectNode rootNode = mapper.createObjectNode(); |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 52 | ArrayNode devices = mapper.createArrayNode(); |
| 53 | ArrayNode hosts = mapper.createArrayNode(); |
| 54 | |
Thomas Vachuska | 0799280 | 2015-05-14 20:45:41 -0700 | [diff] [blame] | 55 | Map<String, ObjectNode> metaUi = TopologyViewMessageHandler.getMetaUi(); |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 56 | for (String id : metaUi.keySet()) { |
| 57 | ObjectNode memento = metaUi.get(id); |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 58 | if (id.length() > 17 && id.charAt(17) == '/') { |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 59 | addGeoData(hosts, "id", id, memento); |
| 60 | } else { |
| 61 | addGeoData(devices, "uri", id, memento); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | rootNode.set("devices", devices); |
| 66 | rootNode.set("hosts", hosts); |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 67 | return Response.ok(rootNode.toString()).build(); |
| 68 | } |
| 69 | |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 70 | private void addGeoData(ArrayNode array, String idField, String id, |
| 71 | ObjectNode memento) { |
| 72 | ObjectNode node = mapper.createObjectNode().put(idField, id); |
| 73 | ObjectNode annot = mapper.createObjectNode(); |
| 74 | node.set("annotations", annot); |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 75 | try { |
| 76 | annot.put("latitude", memento.get("lat").asDouble()) |
| 77 | .put("longitude", memento.get("lng").asDouble()); |
| 78 | array.add(node); |
| 79 | } catch (Exception e) { |
| 80 | log.debug("Skipping geo entry"); |
| 81 | } |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 84 | @Path("sprites") |
| 85 | @POST |
| 86 | @Consumes("application/json") |
| 87 | public Response setSprites(InputStream stream) throws IOException { |
| 88 | JsonNode root = mapper.readTree(stream); |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 89 | String name = root.path("defn_name").asText("sprites"); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 90 | get(SpriteService.class).put(name, root); |
| 91 | return Response.ok().build(); |
| 92 | } |
| 93 | |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 94 | } |