blob: 2f813d0496ded544bcde674d671610840c057a38 [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
Simon Huntbc30e682017-02-15 18:39:23 -080043 private static final String ID = "id";
44 private static final String URI = "uri";
45
46 // length of a MAC defined as a string ... "xx:xx:xx:xx:xx:xx"
47 private static final int MAC_LEN = 17;
48 private static final char SLASH_CHAR = '/';
49
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080050 private static final Logger log = getLogger(TopologyResource.class);
51
Thomas Vachuska998b1412014-11-23 02:42:49 -080052 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskae972ea52014-10-30 00:14:16 -070053
Simon Huntbc30e682017-02-15 18:39:23 -080054 /**
55 * Returns the location data associated with devices and hosts, that is
56 * currently cached in the Meta-UI store.
57 *
58 * @return cached location data for devices and hosts
59 */
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070060 @Path("geoloc")
Thomas Vachuska598924e2014-10-23 22:26:07 -070061 @GET
62 @Produces("application/json")
Thomas Vachuska998b1412014-11-23 02:42:49 -080063 public Response getGeoLocations() {
Thomas Vachuska598924e2014-10-23 22:26:07 -070064 ObjectNode rootNode = mapper.createObjectNode();
Thomas Vachuska998b1412014-11-23 02:42:49 -080065 ArrayNode devices = mapper.createArrayNode();
66 ArrayNode hosts = mapper.createArrayNode();
67
Thomas Vachuska07992802015-05-14 20:45:41 -070068 Map<String, ObjectNode> metaUi = TopologyViewMessageHandler.getMetaUi();
Thomas Vachuska998b1412014-11-23 02:42:49 -080069 for (String id : metaUi.keySet()) {
70 ObjectNode memento = metaUi.get(id);
Simon Huntbc30e682017-02-15 18:39:23 -080071 if (isHostId(id)) {
72 addGeoData(hosts, ID, id, memento);
Thomas Vachuska998b1412014-11-23 02:42:49 -080073 } else {
Simon Huntbc30e682017-02-15 18:39:23 -080074 addGeoData(devices, URI, id, memento);
Thomas Vachuska998b1412014-11-23 02:42:49 -080075 }
76 }
77
78 rootNode.set("devices", devices);
79 rootNode.set("hosts", hosts);
Thomas Vachuska598924e2014-10-23 22:26:07 -070080 return Response.ok(rootNode.toString()).build();
81 }
82
Simon Huntbc30e682017-02-15 18:39:23 -080083 private boolean isHostId(String id) {
84 return id.length() > MAC_LEN && id.charAt(MAC_LEN) == SLASH_CHAR;
85 }
86
Thomas Vachuska998b1412014-11-23 02:42:49 -080087 private void addGeoData(ArrayNode array, String idField, String id,
88 ObjectNode memento) {
89 ObjectNode node = mapper.createObjectNode().put(idField, id);
90 ObjectNode annot = mapper.createObjectNode();
91 node.set("annotations", annot);
Simon Huntbc30e682017-02-15 18:39:23 -080092
93 // TODO: add handling of gridY/gridX if locType is "grid" (not "geo")
94
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080095 try {
96 annot.put("latitude", memento.get("lat").asDouble())
97 .put("longitude", memento.get("lng").asDouble());
98 array.add(node);
99 } catch (Exception e) {
100 log.debug("Skipping geo entry");
101 }
Thomas Vachuskae972ea52014-10-30 00:14:16 -0700102 }
103
Simon Huntbc30e682017-02-15 18:39:23 -0800104 /**
105 * Stores sprite data for retrieval by the UI Topology View.
106 *
107 * @param stream input data stream (typically from an uploaded file).
108 * @return REST response
109 * @throws IOException if there is an issue reading from the stream
110 * @deprecated since Junco (1.9), in favor of client-side defined sprite layers
111 */
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700112 @Path("sprites")
113 @POST
114 @Consumes("application/json")
Simon Huntbc30e682017-02-15 18:39:23 -0800115 @Deprecated
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700116 public Response setSprites(InputStream stream) throws IOException {
117 JsonNode root = mapper.readTree(stream);
Simon Huntfd8c7d72015-04-14 17:53:37 -0700118 String name = root.path("defn_name").asText("sprites");
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700119 get(SpriteService.class).put(name, root);
120 return Response.ok().build();
121 }
Thomas Vachuska598924e2014-10-23 22:26:07 -0700122}