blob: e0b629cf84e20f2b563ac60d151b118eaa5895c4 [file] [log] [blame]
Thomas Vachuska598924e2014-10-23 22:26:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyb784adb2018-04-02 15:33:07 -070035import static org.onlab.util.Tools.readTreeFromStream;
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080036import static org.slf4j.LoggerFactory.getLogger;
37
Thomas Vachuska598924e2014-10-23 22:26:07 -070038/**
39 * Topology viewer resource.
40 */
Thomas Vachuska998b1412014-11-23 02:42:49 -080041@Path("topology")
Thomas Vachuska598924e2014-10-23 22:26:07 -070042public class TopologyResource extends BaseResource {
43
Simon Huntbc30e682017-02-15 18:39:23 -080044 private static final String ID = "id";
45 private static final String URI = "uri";
46
47 // length of a MAC defined as a string ... "xx:xx:xx:xx:xx:xx"
48 private static final int MAC_LEN = 17;
49 private static final char SLASH_CHAR = '/';
50
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080051 private static final Logger log = getLogger(TopologyResource.class);
52
Thomas Vachuska998b1412014-11-23 02:42:49 -080053 private final ObjectMapper mapper = new ObjectMapper();
Thomas Vachuskae972ea52014-10-30 00:14:16 -070054
Simon Huntbc30e682017-02-15 18:39:23 -080055 /**
56 * Returns the location data associated with devices and hosts, that is
57 * currently cached in the Meta-UI store.
58 *
59 * @return cached location data for devices and hosts
60 */
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070061 @Path("geoloc")
Thomas Vachuska598924e2014-10-23 22:26:07 -070062 @GET
63 @Produces("application/json")
Thomas Vachuska998b1412014-11-23 02:42:49 -080064 public Response getGeoLocations() {
Thomas Vachuska598924e2014-10-23 22:26:07 -070065 ObjectNode rootNode = mapper.createObjectNode();
Thomas Vachuska998b1412014-11-23 02:42:49 -080066 ArrayNode devices = mapper.createArrayNode();
67 ArrayNode hosts = mapper.createArrayNode();
68
Thomas Vachuska07992802015-05-14 20:45:41 -070069 Map<String, ObjectNode> metaUi = TopologyViewMessageHandler.getMetaUi();
Thomas Vachuska998b1412014-11-23 02:42:49 -080070 for (String id : metaUi.keySet()) {
71 ObjectNode memento = metaUi.get(id);
Simon Huntbc30e682017-02-15 18:39:23 -080072 if (isHostId(id)) {
73 addGeoData(hosts, ID, id, memento);
Thomas Vachuska998b1412014-11-23 02:42:49 -080074 } else {
Simon Huntbc30e682017-02-15 18:39:23 -080075 addGeoData(devices, URI, id, memento);
Thomas Vachuska998b1412014-11-23 02:42:49 -080076 }
77 }
78
79 rootNode.set("devices", devices);
80 rootNode.set("hosts", hosts);
Thomas Vachuska598924e2014-10-23 22:26:07 -070081 return Response.ok(rootNode.toString()).build();
82 }
83
Simon Huntbc30e682017-02-15 18:39:23 -080084 private boolean isHostId(String id) {
85 return id.length() > MAC_LEN && id.charAt(MAC_LEN) == SLASH_CHAR;
86 }
87
Thomas Vachuska998b1412014-11-23 02:42:49 -080088 private void addGeoData(ArrayNode array, String idField, String id,
89 ObjectNode memento) {
90 ObjectNode node = mapper.createObjectNode().put(idField, id);
91 ObjectNode annot = mapper.createObjectNode();
92 node.set("annotations", annot);
Simon Huntbc30e682017-02-15 18:39:23 -080093
94 // TODO: add handling of gridY/gridX if locType is "grid" (not "geo")
95
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080096 try {
Simon Huntf27a9292017-05-04 17:36:26 -070097 annot.put("latitude", memento.get("latOrY").asDouble())
98 .put("longitude", memento.get("longOrX").asDouble());
Thomas Vachuska9aea51b2014-11-23 14:12:23 -080099 array.add(node);
100 } catch (Exception e) {
101 log.debug("Skipping geo entry");
102 }
Thomas Vachuskae972ea52014-10-30 00:14:16 -0700103 }
104
Simon Huntbc30e682017-02-15 18:39:23 -0800105 /**
106 * Stores sprite data for retrieval by the UI Topology View.
107 *
108 * @param stream input data stream (typically from an uploaded file).
109 * @return REST response
110 * @throws IOException if there is an issue reading from the stream
111 * @deprecated since Junco (1.9), in favor of client-side defined sprite layers
112 */
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700113 @Path("sprites")
114 @POST
115 @Consumes("application/json")
Simon Huntbc30e682017-02-15 18:39:23 -0800116 @Deprecated
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700117 public Response setSprites(InputStream stream) throws IOException {
Ray Milkeyb784adb2018-04-02 15:33:07 -0700118 JsonNode root = readTreeFromStream(mapper, stream);
Simon Huntfd8c7d72015-04-14 17:53:37 -0700119 String name = root.path("defn_name").asText("sprites");
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700120 get(SpriteService.class).put(name, root);
121 return Response.ok().build();
122 }
Thomas Vachuska598924e2014-10-23 22:26:07 -0700123}