blob: e8f147db874a08fa4f209537a314d4f0101c634f [file] [log] [blame]
Thomas Vachuska598924e2014-10-23 22:26:07 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
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 */
16package org.onlab.onos.gui;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuskae972ea52014-10-30 00:14:16 -070021import org.onlab.onos.net.Annotations;
Thomas Vachuska598924e2014-10-23 22:26:07 -070022import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.Device;
Thomas Vachuskae972ea52014-10-30 00:14:16 -070024import org.onlab.onos.net.DeviceId;
Thomas Vachuska598924e2014-10-23 22:26:07 -070025import org.onlab.onos.net.Host;
Thomas Vachuskae972ea52014-10-30 00:14:16 -070026import org.onlab.onos.net.HostId;
Thomas Vachuska598924e2014-10-23 22:26:07 -070027import org.onlab.onos.net.HostLocation;
28import org.onlab.onos.net.Link;
29import org.onlab.onos.net.device.DeviceService;
30import org.onlab.onos.net.host.HostService;
31import org.onlab.onos.net.link.LinkService;
32import org.onlab.onos.net.topology.Topology;
33import org.onlab.onos.net.topology.TopologyGraph;
34import org.onlab.onos.net.topology.TopologyService;
35import org.onlab.onos.net.topology.TopologyVertex;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070036import org.onlab.packet.IpAddress;
Thomas Vachuska598924e2014-10-23 22:26:07 -070037import org.onlab.packet.MacAddress;
38import org.onlab.rest.BaseResource;
39
40import javax.ws.rs.GET;
Thomas Vachuskae972ea52014-10-30 00:14:16 -070041import javax.ws.rs.PathParam;
Thomas Vachuska598924e2014-10-23 22:26:07 -070042import javax.ws.rs.Produces;
43import javax.ws.rs.core.Response;
44import java.util.HashMap;
45import java.util.HashSet;
46import java.util.Iterator;
47import java.util.Map;
48import java.util.Set;
49
Thomas Vachuskae972ea52014-10-30 00:14:16 -070050import static org.onlab.onos.net.DeviceId.deviceId;
51import static org.onlab.onos.net.HostId.hostId;
52
Thomas Vachuska598924e2014-10-23 22:26:07 -070053/**
54 * Topology viewer resource.
55 */
56@javax.ws.rs.Path("topology")
57public class TopologyResource extends BaseResource {
58
Thomas Vachuskae972ea52014-10-30 00:14:16 -070059 private static final String HOST_SEP = "/";
60
Thomas Vachuska598924e2014-10-23 22:26:07 -070061 @javax.ws.rs.Path("/graph")
62 @GET
63 @Produces("application/json")
64 public Response graph() {
65 // Fetch the services we'll be using.
66 DeviceService deviceService = get(DeviceService.class);
67 HostService hostService = get(HostService.class);
68 TopologyService topologyService = get(TopologyService.class);
69
70 // Fetch the current topology and its graph that we'll use to render.
71 Topology topo = topologyService.currentTopology();
72 TopologyGraph graph = topologyService.getGraph(topo);
73
74 ObjectMapper mapper = new ObjectMapper();
75 ObjectNode rootNode = mapper.createObjectNode();
76 rootNode.set("devices", getDevices(mapper, deviceService, graph));
77 rootNode.set("links", getLinks(mapper, topo, graph));
78 rootNode.set("hosts", getHosts(mapper, hostService));
79 return Response.ok(rootNode.toString()).build();
80 }
81
Thomas Vachuskae972ea52014-10-30 00:14:16 -070082 @javax.ws.rs.Path("/graph/{id}")
83 @GET
84 @Produces("application/json")
85 public Response details(@PathParam("id") String id) {
86 if (id.contains(HOST_SEP)) {
87 return hostDetails(hostId(id));
88 }
89 return deviceDetails(deviceId(id));
90 }
91
92 // Returns device details response.
93 private Response deviceDetails(DeviceId deviceId) {
94 DeviceService deviceService = get(DeviceService.class);
95 Device device = deviceService.getDevice(deviceId);
96 Annotations annot = device.annotations();
97 ObjectNode r = json(deviceId.toString(),
98 new Prop("Name", annot.value("name")),
99 new Prop("Vendor", device.manufacturer()),
100 new Prop("H/W Version", device.hwVersion()),
101 new Prop("S/W Version", device.swVersion()),
102 new Prop("S/W Version", device.serialNumber()),
103 new Prop("Latitude", annot.value("latitude")),
104 new Prop("Longitude", annot.value("longitude")));
105 return Response.ok(r.toString()).build();
106 }
107
108 // Returns host details response.
109 private Response hostDetails(HostId hostId) {
110 HostService hostService = get(HostService.class);
111 Host host = hostService.getHost(hostId);
112 Annotations annot = host.annotations();
113 ObjectNode r = json(hostId.toString(),
114 new Prop("MAC", host.mac().toString()),
115 new Prop("IP", host.ipAddresses().toString()),
116 new Prop("Latitude", annot.value("latitude")),
117 new Prop("Longitude", annot.value("longitude")));
118 return Response.ok(r.toString()).build();
119 }
120
121 // Produces JSON property details.
122 private ObjectNode json(String id, Prop... props) {
123 ObjectMapper mapper = new ObjectMapper();
124 ObjectNode result = mapper.createObjectNode().put("id", id);
125 ObjectNode pnode = mapper.createObjectNode();
126 ArrayNode porder = mapper.createArrayNode();
127 for (Prop p : props) {
128 porder.add(p.key);
129 pnode.put(p.key, p.value);
130 }
131 result.set("propOrder", porder);
132 result.set("props", pnode);
133 return result;
134 }
135
Thomas Vachuska598924e2014-10-23 22:26:07 -0700136 // Encodes all infrastructure devices.
137 private ArrayNode getDevices(ObjectMapper mapper, DeviceService deviceService,
138 TopologyGraph graph) {
139 ArrayNode devices = mapper.createArrayNode();
140 for (TopologyVertex vertex : graph.getVertexes()) {
141 devices.add(json(mapper, deviceService.getDevice(vertex.deviceId()),
142 deviceService.isAvailable(vertex.deviceId())));
143 }
144 return devices;
145 }
146
147 // Encodes all infrastructure links.
148 private ArrayNode getLinks(ObjectMapper mapper, Topology topo, TopologyGraph graph) {
149 // Now scan all links and count number of them between the same devices
150 // using a normalized link key.
151 Map<String, AggLink> linkRecords = aggregateLinks();
152
153 // Now build all interior edges using the aggregated links.
154 ArrayNode links = mapper.createArrayNode();
155 for (AggLink lr : linkRecords.values()) {
156 links.add(json(mapper, lr));
157 }
158 return links;
159 }
160
161 // Encodes all end-station hosts.
162 private ArrayNode getHosts(ObjectMapper mapper, HostService hostService) {
163 ArrayNode hosts = mapper.createArrayNode();
164 for (Host host : hostService.getHosts()) {
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700165 Set<IpAddress> ipAddresses = host.ipAddresses();
166 IpAddress ipAddress = ipAddresses.isEmpty() ? null : ipAddresses.iterator().next();
Thomas Vachuska598924e2014-10-23 22:26:07 -0700167 String label = ipAddress != null ? ipAddress.toString() : host.mac().toString();
168 hosts.add(json(mapper, host));
169 }
170 return hosts;
171 }
172
173 // Scan all links and counts number of them between the same devices
174 // using a normalized link key.
175 private Map<String, AggLink> aggregateLinks() {
176 Map<String, AggLink> aggLinks = new HashMap<>();
177 LinkService linkService = get(LinkService.class);
178 for (Link link : linkService.getLinks()) {
179 String key = key(link);
180 AggLink lr = aggLinks.get(key);
181 if (lr == null) {
182 lr = new AggLink(key);
183 aggLinks.put(key, lr);
184 }
185 lr.addLink(link);
186 }
187 return aggLinks;
188 }
189
190 // Produces JSON for a device.
191 private ObjectNode json(ObjectMapper mapper, Device device, boolean isOnline) {
192 ObjectNode node = mapper.createObjectNode()
193 .put("id", device.id().toString())
194 .put("type", device.type().toString().toLowerCase())
195 .put("online", isOnline);
196 node.set("labels", labels(mapper,
197 device.id().uri().getSchemeSpecificPart(),
198 MacAddress.valueOf(device.chassisId().value()).toString(),
199 device.serialNumber()));
200 return node;
201 }
202
203 // Produces JSON for a link.
204 private ObjectNode json(ObjectMapper mapper, AggLink aggLink) {
205 Link link = aggLink.link;
206 return mapper.createObjectNode()
207 .put("src", link.src().deviceId().toString())
208 .put("dst", link.dst().deviceId().toString())
209 .put("type", link.type().toString().toLowerCase())
210 .put("linkWidth", aggLink.links.size());
211 }
212
213 // Produces JSON for a device.
214 private ObjectNode json(ObjectMapper mapper, Host host) {
215 ObjectNode json = mapper.createObjectNode()
216 .put("id", host.id().toString());
217 json.set("cp", location(mapper, host.location()));
218 json.set("labels", labels(mapper, ip(host.ipAddresses()),
219 host.mac().toString()));
220 return json;
221 }
222
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700223 private String ip(Set<IpAddress> ipAddresses) {
224 Iterator<IpAddress> it = ipAddresses.iterator();
Thomas Vachuska598924e2014-10-23 22:26:07 -0700225 return it.hasNext() ? it.next().toString() : "unknown";
226 }
227
228 private ObjectNode location(ObjectMapper mapper, HostLocation location) {
229 return mapper.createObjectNode()
230 .put("device", location.deviceId().toString())
231 .put("port", location.port().toLong());
232 }
233
234 private ArrayNode labels(ObjectMapper mapper, String... labels) {
235 ArrayNode json = mapper.createArrayNode();
236 for (String label : labels) {
237 json.add(label);
238 }
239 return json;
240 }
241
242 // Aggregate link of all links between the same devices regardless of
243 // their direction.
244 private class AggLink {
245 Link link; // representative links
246
247 final String key;
248 final Set<Link> links = new HashSet<>();
249
250 AggLink(String key) {
251 this.key = key;
252 }
253
254 void addLink(Link link) {
255 links.add(link);
256 if (this.link == null) {
257 this.link = link;
258 }
259 }
260 }
261
262 // Returns a canonical key for the specified link.
263 static String key(Link link) {
264 String s = id(link.src());
265 String d = id(link.dst());
266 return s.compareTo(d) > 0 ? d + s : s + d;
267 }
268
269 // Returns a formatted string for the element associated with the given
270 // connection point.
271 private static String id(ConnectPoint cp) {
272 return cp.elementId().toString();
273 }
274
Thomas Vachuskae972ea52014-10-30 00:14:16 -0700275 // Auxiliary key/value carrier.
276 private final class Prop {
277 private final String key;
278 private final String value;
279
280 private Prop(String key, String value) {
281 this.key = key;
282 this.value = value;
283 }
284 }
Thomas Vachuska598924e2014-10-23 22:26:07 -0700285}