blob: 95f9a07b51d7df465566f4e66cbfd5088e8e50ce [file] [log] [blame]
Thomas Vachuska598924e2014-10-23 22:26:07 -07001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Thomas Vachuska598924e2014-10-23 22:26:07 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska598924e2014-10-23 22:26:07 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska598924e2014-10-23 22:26:07 -070015 */
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080016package org.onosproject.ui.impl;
Thomas Vachuska598924e2014-10-23 22:26:07 -070017
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska598924e2014-10-23 22:26:07 -070021import org.onlab.rest.BaseResource;
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080022import org.slf4j.Logger;
Thomas Vachuska598924e2014-10-23 22:26:07 -070023
24import javax.ws.rs.GET;
Thomas Vachuska998b1412014-11-23 02:42:49 -080025import javax.ws.rs.Path;
Thomas Vachuska598924e2014-10-23 22:26:07 -070026import javax.ws.rs.Produces;
27import javax.ws.rs.core.Response;
Thomas Vachuska598924e2014-10-23 22:26:07 -070028import java.util.Map;
Thomas Vachuskae972ea52014-10-30 00:14:16 -070029
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080030import static org.slf4j.LoggerFactory.getLogger;
31
Thomas Vachuska598924e2014-10-23 22:26:07 -070032/**
33 * Topology viewer resource.
34 */
Thomas Vachuska998b1412014-11-23 02:42:49 -080035@Path("topology")
Thomas Vachuska598924e2014-10-23 22:26:07 -070036public class TopologyResource extends BaseResource {
37
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080038 private static final Logger log = getLogger(TopologyResource.class);
39
Thomas Vachuska998b1412014-11-23 02:42:49 -080040 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskae972ea52014-10-30 00:14:16 -070041
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080042
Thomas Vachuska998b1412014-11-23 02:42:49 -080043 @Path("/geoloc")
Thomas Vachuska598924e2014-10-23 22:26:07 -070044 @GET
45 @Produces("application/json")
Thomas Vachuska998b1412014-11-23 02:42:49 -080046 public Response getGeoLocations() {
Thomas Vachuska598924e2014-10-23 22:26:07 -070047 ObjectNode rootNode = mapper.createObjectNode();
Thomas Vachuska998b1412014-11-23 02:42:49 -080048 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 Vachuska9aea51b2014-11-23 14:12:23 -080054 if (id.charAt(17) == '/') {
Thomas Vachuska998b1412014-11-23 02:42:49 -080055 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 Vachuska598924e2014-10-23 22:26:07 -070063 return Response.ok(rootNode.toString()).build();
64 }
65
Thomas Vachuska998b1412014-11-23 02:42:49 -080066 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 Vachuska9aea51b2014-11-23 14:12:23 -080071 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 Vachuskae972ea52014-10-30 00:14:16 -070078 }
79
Thomas Vachuska598924e2014-10-23 22:26:07 -070080}