Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 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 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 21 | import org.onlab.rest.BaseResource; |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 22 | import org.slf4j.Logger; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 23 | |
| 24 | import javax.ws.rs.GET; |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 25 | import javax.ws.rs.Path; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 26 | import javax.ws.rs.Produces; |
| 27 | import javax.ws.rs.core.Response; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 28 | import java.util.Map; |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 29 | |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Topology viewer resource. |
| 34 | */ |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 35 | @Path("topology") |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 36 | public class TopologyResource extends BaseResource { |
| 37 | |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 38 | private static final Logger log = getLogger(TopologyResource.class); |
| 39 | |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 40 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 41 | |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 42 | |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 43 | @Path("/geoloc") |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 44 | @GET |
| 45 | @Produces("application/json") |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 46 | public Response getGeoLocations() { |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 47 | ObjectNode rootNode = mapper.createObjectNode(); |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 48 | ArrayNode devices = mapper.createArrayNode(); |
| 49 | ArrayNode hosts = mapper.createArrayNode(); |
| 50 | |
| 51 | Map<String, ObjectNode> metaUi = TopologyViewMessages.getMetaUi(); |
| 52 | for (String id : metaUi.keySet()) { |
| 53 | ObjectNode memento = metaUi.get(id); |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 54 | if (id.charAt(17) == '/') { |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 55 | addGeoData(hosts, "id", id, memento); |
| 56 | } else { |
| 57 | addGeoData(devices, "uri", id, memento); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | rootNode.set("devices", devices); |
| 62 | rootNode.set("hosts", hosts); |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 63 | return Response.ok(rootNode.toString()).build(); |
| 64 | } |
| 65 | |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 66 | private void addGeoData(ArrayNode array, String idField, String id, |
| 67 | ObjectNode memento) { |
| 68 | ObjectNode node = mapper.createObjectNode().put(idField, id); |
| 69 | ObjectNode annot = mapper.createObjectNode(); |
| 70 | node.set("annotations", annot); |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 71 | try { |
| 72 | annot.put("latitude", memento.get("lat").asDouble()) |
| 73 | .put("longitude", memento.get("lng").asDouble()); |
| 74 | array.add(node); |
| 75 | } catch (Exception e) { |
| 76 | log.debug("Skipping geo entry"); |
| 77 | } |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 80 | } |