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