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