Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 22 | import org.onlab.rest.BaseResource; |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 23 | import org.slf4j.Logger; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 24 | |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 25 | import javax.ws.rs.Consumes; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 26 | import javax.ws.rs.GET; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 27 | import javax.ws.rs.POST; |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 28 | import javax.ws.rs.Path; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 29 | import javax.ws.rs.Produces; |
| 30 | import javax.ws.rs.core.Response; |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 33 | import java.util.Map; |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 34 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 35 | import static org.onlab.util.Tools.readTreeFromStream; |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Topology viewer resource. |
| 40 | */ |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 41 | @Path("topology") |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 42 | public class TopologyResource extends BaseResource { |
| 43 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 44 | 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 Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 51 | private static final Logger log = getLogger(TopologyResource.class); |
| 52 | |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 53 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 54 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 55 | /** |
| 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 Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 61 | @Path("geoloc") |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 62 | @GET |
| 63 | @Produces("application/json") |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 64 | public Response getGeoLocations() { |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 65 | ObjectNode rootNode = mapper.createObjectNode(); |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 66 | ArrayNode devices = mapper.createArrayNode(); |
| 67 | ArrayNode hosts = mapper.createArrayNode(); |
| 68 | |
Thomas Vachuska | 0799280 | 2015-05-14 20:45:41 -0700 | [diff] [blame] | 69 | Map<String, ObjectNode> metaUi = TopologyViewMessageHandler.getMetaUi(); |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 70 | for (String id : metaUi.keySet()) { |
| 71 | ObjectNode memento = metaUi.get(id); |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 72 | if (isHostId(id)) { |
| 73 | addGeoData(hosts, ID, id, memento); |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 74 | } else { |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 75 | addGeoData(devices, URI, id, memento); |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | rootNode.set("devices", devices); |
| 80 | rootNode.set("hosts", hosts); |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 81 | return Response.ok(rootNode.toString()).build(); |
| 82 | } |
| 83 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 84 | private boolean isHostId(String id) { |
| 85 | return id.length() > MAC_LEN && id.charAt(MAC_LEN) == SLASH_CHAR; |
| 86 | } |
| 87 | |
Thomas Vachuska | 998b141 | 2014-11-23 02:42:49 -0800 | [diff] [blame] | 88 | 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 Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 93 | |
| 94 | // TODO: add handling of gridY/gridX if locType is "grid" (not "geo") |
| 95 | |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 96 | try { |
Simon Hunt | f27a929 | 2017-05-04 17:36:26 -0700 | [diff] [blame] | 97 | annot.put("latitude", memento.get("latOrY").asDouble()) |
| 98 | .put("longitude", memento.get("longOrX").asDouble()); |
Thomas Vachuska | 9aea51b | 2014-11-23 14:12:23 -0800 | [diff] [blame] | 99 | array.add(node); |
| 100 | } catch (Exception e) { |
| 101 | log.debug("Skipping geo entry"); |
| 102 | } |
Thomas Vachuska | e972ea5 | 2014-10-30 00:14:16 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 105 | /** |
| 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 Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 113 | @Path("sprites") |
| 114 | @POST |
| 115 | @Consumes("application/json") |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 116 | @Deprecated |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 117 | public Response setSprites(InputStream stream) throws IOException { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 118 | JsonNode root = readTreeFromStream(mapper, stream); |
Simon Hunt | fd8c7d7 | 2015-04-14 17:53:37 -0700 | [diff] [blame] | 119 | String name = root.path("defn_name").asText("sprites"); |
Thomas Vachuska | 9ed335b | 2015-04-14 12:07:47 -0700 | [diff] [blame] | 120 | get(SpriteService.class).put(name, root); |
| 121 | return Response.ok().build(); |
| 122 | } |
Thomas Vachuska | 598924e | 2014-10-23 22:26:07 -0700 | [diff] [blame] | 123 | } |