Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -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 | 781d18b | 2014-10-27 10:31:25 -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 | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 16 | package org.onlab.onos.tvue; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.onos.net.ConnectPoint; |
| 22 | import org.onlab.onos.net.ElementId; |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.Host; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.Link; |
tom | a33dd34 | 2014-09-12 19:07:40 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.Path; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.device.DeviceService; |
| 27 | import org.onlab.onos.net.host.HostService; |
| 28 | import org.onlab.onos.net.link.LinkService; |
tom | 210f3f3 | 2014-09-16 17:33:40 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.topology.PathService; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.topology.Topology; |
| 31 | import org.onlab.onos.net.topology.TopologyGraph; |
| 32 | import org.onlab.onos.net.topology.TopologyService; |
| 33 | import org.onlab.onos.net.topology.TopologyVertex; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 34 | import org.onlab.packet.IpAddress; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 35 | import org.onlab.rest.BaseResource; |
| 36 | |
| 37 | import javax.ws.rs.GET; |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 38 | import javax.ws.rs.PathParam; |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 39 | import javax.ws.rs.Produces; |
| 40 | import javax.ws.rs.core.Response; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.Map; |
| 44 | import java.util.Set; |
| 45 | |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 46 | import static org.onlab.onos.net.DeviceId.deviceId; |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 47 | import static org.onlab.onos.net.HostId.hostId; |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 48 | import static org.onlab.onos.net.PortNumber.portNumber; |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 49 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Topology viewer resource. |
| 52 | */ |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 53 | @javax.ws.rs.Path("topology") |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 54 | public class TopologyResource extends BaseResource { |
| 55 | |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 56 | @javax.ws.rs.Path("/graph") |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 57 | @GET |
| 58 | @Produces("application/json") |
| 59 | public Response graph() { |
| 60 | ObjectMapper mapper = new ObjectMapper(); |
| 61 | |
| 62 | // Fetch the services we'll be using. |
| 63 | DeviceService deviceService = get(DeviceService.class); |
| 64 | HostService hostService = get(HostService.class); |
| 65 | TopologyService topologyService = get(TopologyService.class); |
| 66 | |
| 67 | // Fetch the current topology and its graph that we'll use to render. |
| 68 | Topology topo = topologyService.currentTopology(); |
| 69 | TopologyGraph graph = topologyService.getGraph(topo); |
| 70 | |
| 71 | // Build all interior vertexes, i.e. no end-station hosts yet |
| 72 | ArrayNode vertexesNode = mapper.createArrayNode(); |
| 73 | for (TopologyVertex vertex : graph.getVertexes()) { |
| 74 | vertexesNode.add(json(mapper, vertex.deviceId(), 2, |
tom | 4d0c663 | 2014-09-15 23:27:01 -0700 | [diff] [blame] | 75 | vertex.deviceId().uri().getSchemeSpecificPart(), |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 76 | deviceService.isAvailable(vertex.deviceId()))); |
| 77 | } |
| 78 | |
| 79 | // Now scan all links and count number of them between the same devices |
| 80 | // using a normalized link key. |
| 81 | Map<String, AggLink> linkRecords = aggregateLinks(); |
| 82 | |
| 83 | // Now build all interior edges using the aggregated links. |
| 84 | ArrayNode edgesNode = mapper.createArrayNode(); |
| 85 | for (AggLink lr : linkRecords.values()) { |
| 86 | edgesNode.add(json(mapper, lr.links.size(), lr.link.src(), lr.link.dst())); |
| 87 | } |
| 88 | |
| 89 | // Merge the exterior and interior vertexes and inject host links as |
| 90 | // the exterior edges. |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 91 | for (Host host : hostService.getHosts()) { |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 92 | Set<IpAddress> ipAddresses = host.ipAddresses(); |
| 93 | IpAddress ipAddress = ipAddresses.isEmpty() ? null : ipAddresses.iterator().next(); |
tom | 4d0c663 | 2014-09-15 23:27:01 -0700 | [diff] [blame] | 94 | String label = ipAddress != null ? ipAddress.toString() : host.mac().toString(); |
| 95 | vertexesNode.add(json(mapper, host.id(), 3, label, true)); |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 96 | edgesNode.add(json(mapper, 1, host.location(), new ConnectPoint(host.id(), portNumber(-1)))); |
| 97 | } |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 98 | |
| 99 | // Now put the vertexes and edges into a root node and ship them off |
| 100 | ObjectNode rootNode = mapper.createObjectNode(); |
tom | 4d0c663 | 2014-09-15 23:27:01 -0700 | [diff] [blame] | 101 | rootNode.set("vertexes", vertexesNode); |
| 102 | rootNode.set("edges", edgesNode); |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 103 | return Response.ok(rootNode.toString()).build(); |
| 104 | } |
| 105 | |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 106 | |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Returns a JSON array of all paths between the specified hosts. |
| 109 | * |
| 110 | * @param src source host id |
| 111 | * @param dst target host id |
| 112 | * @return JSON array of paths |
| 113 | */ |
| 114 | @javax.ws.rs.Path("/paths/{src}/{dst}") |
| 115 | @GET |
| 116 | @Produces("application/json") |
| 117 | public Response paths(@PathParam("src") String src, @PathParam("dst") String dst) { |
| 118 | ObjectMapper mapper = new ObjectMapper(); |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 119 | PathService pathService = get(PathService.class); |
| 120 | Set<Path> paths = pathService.getPaths(elementId(src), elementId(dst)); |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 121 | |
| 122 | ArrayNode pathsNode = mapper.createArrayNode(); |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 123 | for (Path path : paths) { |
tom | a33dd34 | 2014-09-12 19:07:40 -0700 | [diff] [blame] | 124 | pathsNode.add(json(mapper, path)); |
| 125 | } |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 126 | |
| 127 | // Now put the vertexes and edges into a root node and ship them off |
| 128 | ObjectNode rootNode = mapper.createObjectNode(); |
tom | a33dd34 | 2014-09-12 19:07:40 -0700 | [diff] [blame] | 129 | rootNode.set("paths", pathsNode); |
tom | 6a9986b | 2014-09-12 18:44:52 -0700 | [diff] [blame] | 130 | return Response.ok(rootNode.toString()).build(); |
| 131 | } |
| 132 | |
tom | cbefa23 | 2014-09-16 14:17:20 -0700 | [diff] [blame] | 133 | // Creates either device ID or host ID as appropriate. |
| 134 | private ElementId elementId(String id) { |
| 135 | return id.startsWith("nic:") ? hostId(id) : deviceId(id); |
| 136 | } |
| 137 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 138 | // Scan all links and counts number of them between the same devices |
| 139 | // using a normalized link key. |
| 140 | private Map<String, AggLink> aggregateLinks() { |
| 141 | Map<String, AggLink> aggLinks = new HashMap<>(); |
| 142 | LinkService linkService = get(LinkService.class); |
| 143 | for (Link link : linkService.getLinks()) { |
| 144 | String key = key(link); |
| 145 | AggLink lr = aggLinks.get(key); |
| 146 | if (lr == null) { |
| 147 | lr = new AggLink(key); |
| 148 | aggLinks.put(key, lr); |
| 149 | } |
| 150 | lr.addLink(link); |
| 151 | } |
| 152 | return aggLinks; |
| 153 | } |
| 154 | |
| 155 | // Produces JSON for a graph vertex. |
| 156 | private ObjectNode json(ObjectMapper mapper, ElementId id, int group, |
tom | 4d0c663 | 2014-09-15 23:27:01 -0700 | [diff] [blame] | 157 | String label, boolean isOnline) { |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 158 | return mapper.createObjectNode() |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 159 | .put("name", id.toString()) |
tom | 4d0c663 | 2014-09-15 23:27:01 -0700 | [diff] [blame] | 160 | .put("label", label) |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 161 | .put("group", group) |
| 162 | .put("online", isOnline); |
| 163 | } |
| 164 | |
| 165 | // Produces JSON for a graph edge. |
| 166 | private ObjectNode json(ObjectMapper mapper, int count, |
| 167 | ConnectPoint src, ConnectPoint dst) { |
| 168 | return json(mapper, count, id(src), id(dst)); |
| 169 | } |
| 170 | |
| 171 | // Produces JSON for a graph edge. |
| 172 | private ObjectNode json(ObjectMapper mapper, int count, String src, String dst) { |
| 173 | return mapper.createObjectNode() |
| 174 | .put("source", src).put("target", dst).put("value", count); |
| 175 | } |
| 176 | |
tom | a33dd34 | 2014-09-12 19:07:40 -0700 | [diff] [blame] | 177 | // Produces JSON representation of a network path. |
| 178 | private ArrayNode json(ObjectMapper mapper, Path path) { |
| 179 | ArrayNode pathNode = mapper.createArrayNode(); |
| 180 | for (Link link : path.links()) { |
| 181 | ObjectNode linkNode = mapper.createObjectNode() |
| 182 | .put("src", id(link.src())) |
| 183 | .put("dst", id(link.dst())); |
| 184 | pathNode.add(linkNode); |
| 185 | } |
| 186 | return pathNode; |
| 187 | } |
| 188 | |
| 189 | |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 190 | // Aggregate link of all links between the same devices regardless of |
| 191 | // their direction. |
| 192 | private class AggLink { |
| 193 | Link link; // representative links |
| 194 | |
| 195 | final String key; |
| 196 | final Set<Link> links = new HashSet<>(); |
| 197 | |
| 198 | AggLink(String key) { |
| 199 | this.key = key; |
| 200 | } |
| 201 | |
| 202 | void addLink(Link link) { |
| 203 | links.add(link); |
| 204 | if (this.link == null) { |
| 205 | this.link = link; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Returns a canonical key for the specified link. |
| 211 | static String key(Link link) { |
| 212 | String s = id(link.src()); |
| 213 | String d = id(link.dst()); |
| 214 | return s.compareTo(d) > 0 ? d + s : s + d; |
| 215 | } |
| 216 | |
| 217 | // Returns a formatted string for the element associated with the given |
| 218 | // connection point. |
| 219 | private static String id(ConnectPoint cp) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 220 | return cp.elementId().toString(); |
tom | 8bb1606 | 2014-09-12 14:47:46 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | } |