blob: fa2613bb056a4c5f16dbdb0aadada84c488eca42 [file] [log] [blame]
Thomas Vachuska598924e2014-10-23 22:26:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuska598924e2014-10-23 22:26:07 -070019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska598924e2014-10-23 22:26:07 -070022import org.onlab.rest.BaseResource;
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080023import org.slf4j.Logger;
Thomas Vachuska598924e2014-10-23 22:26:07 -070024
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070025import javax.ws.rs.Consumes;
Thomas Vachuska598924e2014-10-23 22:26:07 -070026import javax.ws.rs.GET;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070027import javax.ws.rs.POST;
Thomas Vachuska998b1412014-11-23 02:42:49 -080028import javax.ws.rs.Path;
Thomas Vachuska598924e2014-10-23 22:26:07 -070029import javax.ws.rs.Produces;
30import javax.ws.rs.core.Response;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070031import java.io.IOException;
32import java.io.InputStream;
Thomas Vachuska598924e2014-10-23 22:26:07 -070033import java.util.Map;
Thomas Vachuskae972ea52014-10-30 00:14:16 -070034
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080035import static org.slf4j.LoggerFactory.getLogger;
36
Thomas Vachuska598924e2014-10-23 22:26:07 -070037/**
38 * Topology viewer resource.
39 */
Thomas Vachuska998b1412014-11-23 02:42:49 -080040@Path("topology")
Thomas Vachuska598924e2014-10-23 22:26:07 -070041public class TopologyResource extends BaseResource {
42
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080043 private static final Logger log = getLogger(TopologyResource.class);
44
Thomas Vachuska998b1412014-11-23 02:42:49 -080045 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskae972ea52014-10-30 00:14:16 -070046
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070047 @Path("geoloc")
Thomas Vachuska598924e2014-10-23 22:26:07 -070048 @GET
49 @Produces("application/json")
Thomas Vachuska998b1412014-11-23 02:42:49 -080050 public Response getGeoLocations() {
Thomas Vachuska598924e2014-10-23 22:26:07 -070051 ObjectNode rootNode = mapper.createObjectNode();
Thomas Vachuska998b1412014-11-23 02:42:49 -080052 ArrayNode devices = mapper.createArrayNode();
53 ArrayNode hosts = mapper.createArrayNode();
54
Thomas Vachuska07992802015-05-14 20:45:41 -070055 Map<String, ObjectNode> metaUi = TopologyViewMessageHandler.getMetaUi();
Thomas Vachuska998b1412014-11-23 02:42:49 -080056 for (String id : metaUi.keySet()) {
57 ObjectNode memento = metaUi.get(id);
Thomas Vachuska1a989c12015-06-09 18:29:22 -070058 if (id.length() > 17 && id.charAt(17) == '/') {
Thomas Vachuska998b1412014-11-23 02:42:49 -080059 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 Vachuska598924e2014-10-23 22:26:07 -070067 return Response.ok(rootNode.toString()).build();
68 }
69
Thomas Vachuska998b1412014-11-23 02:42:49 -080070 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 Vachuska9aea51b2014-11-23 14:12:23 -080075 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 Vachuskae972ea52014-10-30 00:14:16 -070082 }
83
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070084 @Path("sprites")
85 @POST
86 @Consumes("application/json")
87 public Response setSprites(InputStream stream) throws IOException {
88 JsonNode root = mapper.readTree(stream);
Simon Huntfd8c7d72015-04-14 17:53:37 -070089 String name = root.path("defn_name").asText("sprites");
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070090 get(SpriteService.class).put(name, root);
91 return Response.ok().build();
92 }
93
Thomas Vachuska598924e2014-10-23 22:26:07 -070094}