blob: aef9e956fbb6a369ae494d16758940ee6770eddd [file] [log] [blame]
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
7 *
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.
15 */
16package org.onlab.onos.gui;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onlab.onos.cluster.ClusterEvent;
23import org.onlab.onos.cluster.ClusterService;
24import org.onlab.onos.cluster.ControllerNode;
25import org.onlab.onos.cluster.NodeId;
26import org.onlab.onos.mastership.MastershipService;
Thomas Vachuska0f6baee2014-11-11 15:02:32 -080027import org.onlab.onos.net.Annotated;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080028import org.onlab.onos.net.Annotations;
29import org.onlab.onos.net.ConnectPoint;
30import org.onlab.onos.net.DefaultEdgeLink;
31import org.onlab.onos.net.Device;
32import org.onlab.onos.net.DeviceId;
33import org.onlab.onos.net.EdgeLink;
34import org.onlab.onos.net.Host;
35import org.onlab.onos.net.HostId;
36import org.onlab.onos.net.HostLocation;
37import org.onlab.onos.net.Link;
38import org.onlab.onos.net.Path;
39import org.onlab.onos.net.device.DeviceEvent;
40import org.onlab.onos.net.device.DeviceService;
41import org.onlab.onos.net.host.HostEvent;
42import org.onlab.onos.net.host.HostService;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080043import org.onlab.onos.net.intent.ConnectivityIntent;
44import org.onlab.onos.net.intent.Intent;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080045import org.onlab.onos.net.intent.IntentService;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080046import org.onlab.onos.net.intent.LinkCollectionIntent;
Thomas Vachuska22e34922014-11-14 00:40:55 -080047import org.onlab.onos.net.intent.OpticalConnectivityIntent;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080048import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080049import org.onlab.onos.net.link.LinkEvent;
50import org.onlab.onos.net.link.LinkService;
51import org.onlab.onos.net.provider.ProviderId;
52import org.onlab.osgi.ServiceDirectory;
53import org.onlab.packet.IpAddress;
Thomas Vachuska0f6baee2014-11-11 15:02:32 -080054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080056
57import java.util.Iterator;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080058import java.util.List;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080059import java.util.Map;
60import java.util.Set;
61import java.util.concurrent.ConcurrentHashMap;
62
63import static com.google.common.base.Preconditions.checkNotNull;
64import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED;
65import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_REMOVED;
66import static org.onlab.onos.cluster.ControllerNode.State.ACTIVE;
67import static org.onlab.onos.net.PortNumber.portNumber;
68import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
69import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED;
70import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
71import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED;
72import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
73import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED;
74
75/**
76 * Facility for creating messages bound for the topology viewer.
77 */
78public abstract class TopologyMessages {
79
Thomas Vachuska0f6baee2014-11-11 15:02:32 -080080 protected static final Logger log = LoggerFactory.getLogger(TopologyMessages.class);
81
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080082 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
83 private static final String COMPACT = "%s/%s-%s/%s";
84
85 protected final ServiceDirectory directory;
86 protected final ClusterService clusterService;
87 protected final DeviceService deviceService;
88 protected final LinkService linkService;
89 protected final HostService hostService;
90 protected final MastershipService mastershipService;
91 protected final IntentService intentService;
Thomas Vachuska22e34922014-11-14 00:40:55 -080092// protected final StatisticService statService;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080093
94 protected final ObjectMapper mapper = new ObjectMapper();
95
96 // TODO: extract into an external & durable state; good enough for now and demo
97 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
98
99 /**
100 * Creates a messaging facility for creating messages for topology viewer.
101 *
102 * @param directory service directory
103 */
104 protected TopologyMessages(ServiceDirectory directory) {
105 this.directory = checkNotNull(directory, "Directory cannot be null");
106 clusterService = directory.get(ClusterService.class);
107 deviceService = directory.get(DeviceService.class);
108 linkService = directory.get(LinkService.class);
109 hostService = directory.get(HostService.class);
110 mastershipService = directory.get(MastershipService.class);
111 intentService = directory.get(IntentService.class);
Thomas Vachuska22e34922014-11-14 00:40:55 -0800112// statService = directory.get(StatisticService.class);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800113 }
114
115 // Retrieves the payload from the specified event.
116 protected ObjectNode payload(ObjectNode event) {
117 return (ObjectNode) event.path("payload");
118 }
119
120 // Returns the specified node property as a number
121 protected long number(ObjectNode node, String name) {
122 return node.path(name).asLong();
123 }
124
125 // Returns the specified node property as a string.
126 protected String string(ObjectNode node, String name) {
127 return node.path(name).asText();
128 }
129
130 // Returns the specified node property as a string.
131 protected String string(ObjectNode node, String name, String defaultValue) {
132 return node.path(name).asText(defaultValue);
133 }
134
135 // Returns the specified set of IP addresses as a string.
136 private String ip(Set<IpAddress> ipAddresses) {
137 Iterator<IpAddress> it = ipAddresses.iterator();
138 return it.hasNext() ? it.next().toString() : "unknown";
139 }
140
141 // Produces JSON structure from annotations.
142 private JsonNode props(Annotations annotations) {
143 ObjectNode props = mapper.createObjectNode();
144 for (String key : annotations.keys()) {
145 props.put(key, annotations.value(key));
146 }
147 return props;
148 }
149
150 // Produces an informational log message event bound to the client.
151 protected ObjectNode info(long id, String message) {
152 return message("info", id, message);
153 }
154
155 // Produces a warning log message event bound to the client.
156 protected ObjectNode warning(long id, String message) {
157 return message("warning", id, message);
158 }
159
160 // Produces an error log message event bound to the client.
161 protected ObjectNode error(long id, String message) {
162 return message("error", id, message);
163 }
164
165 // Produces a log message event bound to the client.
166 private ObjectNode message(String severity, long id, String message) {
167 return envelope("message", id,
168 mapper.createObjectNode()
169 .put("severity", severity)
170 .put("message", message));
171 }
172
173 // Puts the payload into an envelope and returns it.
174 protected ObjectNode envelope(String type, long sid, ObjectNode payload) {
175 ObjectNode event = mapper.createObjectNode();
176 event.put("event", type);
177 if (sid > 0) {
178 event.put("sid", sid);
179 }
180 event.set("payload", payload);
181 return event;
182 }
183
184 // Produces a cluster instance message to the client.
185 protected ObjectNode instanceMessage(ClusterEvent event) {
186 ControllerNode node = event.subject();
187 ObjectNode payload = mapper.createObjectNode()
188 .put("id", node.id().toString())
189 .put("online", clusterService.getState(node.id()) == ACTIVE);
190
191 ArrayNode labels = mapper.createArrayNode();
192 labels.add(node.id().toString());
193 labels.add(node.ip().toString());
194
195 // Add labels, props and stuff the payload into envelope.
196 payload.set("labels", labels);
197 addMetaUi(node.id().toString(), payload);
198
199 String type = (event.type() == INSTANCE_ADDED) ? "addInstance" :
200 ((event.type() == INSTANCE_REMOVED) ? "removeInstance" : "updateInstance");
201 return envelope(type, 0, payload);
202 }
203
204 // Produces a device event message to the client.
205 protected ObjectNode deviceMessage(DeviceEvent event) {
206 Device device = event.subject();
207 ObjectNode payload = mapper.createObjectNode()
208 .put("id", device.id().toString())
209 .put("type", device.type().toString().toLowerCase())
210 .put("online", deviceService.isAvailable(device.id()))
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800211 .put("master", master(device.id()));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800212
213 // Generate labels: id, chassis id, no-label, optional-name
214 ArrayNode labels = mapper.createArrayNode();
215 labels.add(device.id().toString());
216 labels.add(device.chassisId().toString());
217 labels.add(""); // compact no-label view
218 labels.add(device.annotations().value("name"));
219
220 // Add labels, props and stuff the payload into envelope.
221 payload.set("labels", labels);
222 payload.set("props", props(device.annotations()));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800223 addGeoLocation(device, payload);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800224 addMetaUi(device.id().toString(), payload);
225
226 String type = (event.type() == DEVICE_ADDED) ? "addDevice" :
227 ((event.type() == DEVICE_REMOVED) ? "removeDevice" : "updateDevice");
228 return envelope(type, 0, payload);
229 }
230
231 // Produces a link event message to the client.
232 protected ObjectNode linkMessage(LinkEvent event) {
233 Link link = event.subject();
234 ObjectNode payload = mapper.createObjectNode()
235 .put("id", compactLinkString(link))
236 .put("type", link.type().toString().toLowerCase())
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800237 .put("online", true) // link.state()) TODO: add link state field
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800238 .put("linkWidth", 2)
239 .put("src", link.src().deviceId().toString())
240 .put("srcPort", link.src().port().toString())
241 .put("dst", link.dst().deviceId().toString())
242 .put("dstPort", link.dst().port().toString());
243 String type = (event.type() == LINK_ADDED) ? "addLink" :
244 ((event.type() == LINK_REMOVED) ? "removeLink" : "updateLink");
245 return envelope(type, 0, payload);
246 }
247
248 // Produces a host event message to the client.
249 protected ObjectNode hostMessage(HostEvent event) {
250 Host host = event.subject();
251 ObjectNode payload = mapper.createObjectNode()
252 .put("id", host.id().toString())
253 .put("ingress", compactLinkString(edgeLink(host, true)))
254 .put("egress", compactLinkString(edgeLink(host, false)));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800255 payload.set("cp", hostConnect(mapper, host.location()));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800256 payload.set("labels", labels(mapper, ip(host.ipAddresses()),
257 host.mac().toString()));
258 payload.set("props", props(host.annotations()));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800259 addGeoLocation(host, payload);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800260 addMetaUi(host.id().toString(), payload);
261
262 String type = (event.type() == HOST_ADDED) ? "addHost" :
263 ((event.type() == HOST_REMOVED) ? "removeHost" : "updateHost");
264 return envelope(type, 0, payload);
265 }
266
267 // Encodes the specified host location into a JSON object.
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800268 private ObjectNode hostConnect(ObjectMapper mapper, HostLocation location) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800269 return mapper.createObjectNode()
270 .put("device", location.deviceId().toString())
271 .put("port", location.port().toLong());
272 }
273
274 // Encodes the specified list of labels a JSON array.
275 private ArrayNode labels(ObjectMapper mapper, String... labels) {
276 ArrayNode json = mapper.createArrayNode();
277 for (String label : labels) {
278 json.add(label);
279 }
280 return json;
281 }
282
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800283 // Returns the name of the master node for the specified device id.
284 private String master(DeviceId deviceId) {
285 NodeId master = mastershipService.getMasterFor(deviceId);
286 return master != null ? master.toString() : "";
287 }
288
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800289 // Generates an edge link from the specified host location.
290 private EdgeLink edgeLink(Host host, boolean ingress) {
291 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
292 host.location(), ingress);
293 }
294
295 // Adds meta UI information for the specified object.
296 private void addMetaUi(String id, ObjectNode payload) {
297 ObjectNode meta = metaUi.get(id);
298 if (meta != null) {
299 payload.set("metaUi", meta);
300 }
301 }
302
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800303 // Adds a geo location JSON to the specified payload object.
304 private void addGeoLocation(Annotated annotated, ObjectNode payload) {
305 Annotations annotations = annotated.annotations();
306 String slat = annotations.value("latitude");
307 String slng = annotations.value("longitude");
308 try {
309 if (slat != null && slng != null && !slat.isEmpty() && !slng.isEmpty()) {
310 double lat = Double.parseDouble(slat);
311 double lng = Double.parseDouble(slng);
312 ObjectNode loc = mapper.createObjectNode()
313 .put("type", "latlng").put("lat", lat).put("lng", lng);
314 payload.set("location", loc);
315 }
316 } catch (NumberFormatException e) {
317 log.warn("Invalid geo data latitude={}; longiture={}", slat, slng);
318 }
319 }
320
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800321 // Updates meta UI information for the specified object.
322 protected void updateMetaUi(ObjectNode event) {
323 ObjectNode payload = payload(event);
Simon Hunt3b9cddb2014-11-11 20:50:04 -0800324 metaUi.put(string(payload, "id"), (ObjectNode) payload.path("memento"));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800325 }
326
327 // Returns device details response.
328 protected ObjectNode deviceDetails(DeviceId deviceId, long sid) {
329 Device device = deviceService.getDevice(deviceId);
330 Annotations annot = device.annotations();
331 int portCount = deviceService.getPorts(deviceId).size();
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800332 return envelope("showDetails", sid,
333 json(deviceId.toString(),
334 device.type().toString().toLowerCase(),
335 new Prop("Name", annot.value("name")),
336 new Prop("Vendor", device.manufacturer()),
337 new Prop("H/W Version", device.hwVersion()),
338 new Prop("S/W Version", device.swVersion()),
339 new Prop("Serial Number", device.serialNumber()),
340 new Separator(),
341 new Prop("Latitude", annot.value("latitude")),
342 new Prop("Longitude", annot.value("longitude")),
343 new Prop("Ports", Integer.toString(portCount)),
344 new Separator(),
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800345 new Prop("Master", master(deviceId))));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800346 }
347
348 // Returns host details response.
349 protected ObjectNode hostDetails(HostId hostId, long sid) {
350 Host host = hostService.getHost(hostId);
351 Annotations annot = host.annotations();
352 return envelope("showDetails", sid,
353 json(hostId.toString(), "host",
354 new Prop("MAC", host.mac().toString()),
355 new Prop("IP", host.ipAddresses().toString()),
356 new Separator(),
357 new Prop("Latitude", annot.value("latitude")),
358 new Prop("Longitude", annot.value("longitude"))));
359 }
360
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800361 // Produces a path payload to the client.
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800362 protected ObjectNode pathMessage(Path path, String type) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800363 ObjectNode payload = mapper.createObjectNode();
364 ArrayNode links = mapper.createArrayNode();
365 for (Link link : path.links()) {
366 links.add(compactLinkString(link));
367 }
368
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800369 payload.put("type", type).set("links", links);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800370 return payload;
371 }
372
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800373
374 // Produces JSON message to trigger traffic visualization
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800375 protected ObjectNode trafficMessage(long sid, TrafficClass... trafficClasses) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800376 ObjectNode payload = mapper.createObjectNode();
377 ArrayNode paths = mapper.createArrayNode();
378 payload.set("paths", paths);
379
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800380 for (TrafficClass trafficClass : trafficClasses) {
381 for (Intent intent : trafficClass.intents) {
Thomas Vachuska22e34922014-11-14 00:40:55 -0800382 boolean isOptical = intent instanceof OpticalConnectivityIntent;
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800383 List<Intent> installables = intentService.getInstallableIntents(intent.id());
Thomas Vachuska22e34922014-11-14 00:40:55 -0800384 if (installables != null) {
385 for (Intent installable : installables) {
386 String cls = isOptical ? trafficClass.type + " optical" : trafficClass.type;
387 if (installable instanceof ConnectivityIntent) {
388 addPathTraffic(paths, cls, (ConnectivityIntent) installable);
389 }
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800390 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800391 }
392 }
393 }
394
395 return envelope("showTraffic", sid, payload);
396 }
397
398 // Adds the link segments (path or tree) associated with the specified
399 // connectivity intent
400 protected void addPathTraffic(ArrayNode paths, String type,
401 ConnectivityIntent installable) {
402 ObjectNode pathNode = mapper.createObjectNode();
403 ArrayNode linksNode = mapper.createArrayNode();
404
Thomas Vachuska22e34922014-11-14 00:40:55 -0800405 Iterable<Link> links = pathLinks(installable);
406 if (links != null) {
407 ArrayNode labels = mapper.createArrayNode();
408 boolean hasTraffic = true; // FIXME
409 for (Link link : links) {
410 linksNode.add(compactLinkString(link));
411// Load load = statService.load(link);
412 String label = "";
413// if (load.rate() > 0) {
414// label = load.toString();
415// }
416 labels.add(label);
417 }
418 pathNode.put("class", hasTraffic ? type + " animated" : type);
419 pathNode.put("traffic", hasTraffic);
420 pathNode.set("links", linksNode);
421 pathNode.set("labels", labels);
422 paths.add(pathNode);
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800423 }
Thomas Vachuska22e34922014-11-14 00:40:55 -0800424 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800425
Thomas Vachuska22e34922014-11-14 00:40:55 -0800426 private Iterable<Link> pathLinks(ConnectivityIntent intent) {
427 if (intent instanceof PathIntent) {
428 return ((PathIntent) intent).path().links();
429 } else if (intent instanceof LinkCollectionIntent) {
430 return ((LinkCollectionIntent) intent).links();
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800431 }
Thomas Vachuska22e34922014-11-14 00:40:55 -0800432 return null;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800433 }
434
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800435 // Produces compact string representation of a link.
436 private static String compactLinkString(Link link) {
437 return String.format(COMPACT, link.src().elementId(), link.src().port(),
438 link.dst().elementId(), link.dst().port());
439 }
440
441 // Produces JSON property details.
442 private ObjectNode json(String id, String type, Prop... props) {
443 ObjectMapper mapper = new ObjectMapper();
444 ObjectNode result = mapper.createObjectNode()
445 .put("id", id).put("type", type);
446 ObjectNode pnode = mapper.createObjectNode();
447 ArrayNode porder = mapper.createArrayNode();
448 for (Prop p : props) {
449 porder.add(p.key);
450 pnode.put(p.key, p.value);
451 }
452 result.set("propOrder", porder);
453 result.set("props", pnode);
454 return result;
455 }
456
457 // Auxiliary key/value carrier.
458 private class Prop {
459 public final String key;
460 public final String value;
461
462 protected Prop(String key, String value) {
463 this.key = key;
464 this.value = value;
465 }
466 }
467
468 // Auxiliary properties separator
469 private class Separator extends Prop {
470 protected Separator() {
471 super("-", "");
472 }
473 }
474
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800475 // Auxiliary carrier of data for requesting traffic message.
476 protected class TrafficClass {
477 public final String type;
478 public final Set<Intent> intents;
479
480 TrafficClass(String type, Set<Intent> intents) {
481 this.type = type;
482 this.intents = intents;
483 }
484 }
485
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800486}