blob: c87a7f6bdb4ff2ef69ae258fb764fa7014d97179 [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;
47import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080048import org.onlab.onos.net.link.LinkEvent;
49import org.onlab.onos.net.link.LinkService;
50import org.onlab.onos.net.provider.ProviderId;
51import org.onlab.osgi.ServiceDirectory;
52import org.onlab.packet.IpAddress;
Thomas Vachuska0f6baee2014-11-11 15:02:32 -080053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080055
56import java.util.Iterator;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080057import java.util.List;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080058import java.util.Map;
59import java.util.Set;
60import java.util.concurrent.ConcurrentHashMap;
61
62import static com.google.common.base.Preconditions.checkNotNull;
63import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED;
64import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_REMOVED;
65import static org.onlab.onos.cluster.ControllerNode.State.ACTIVE;
66import static org.onlab.onos.net.PortNumber.portNumber;
67import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
68import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED;
69import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
70import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED;
71import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
72import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED;
73
74/**
75 * Facility for creating messages bound for the topology viewer.
76 */
77public abstract class TopologyMessages {
78
Thomas Vachuska0f6baee2014-11-11 15:02:32 -080079 protected static final Logger log = LoggerFactory.getLogger(TopologyMessages.class);
80
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080081 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
82 private static final String COMPACT = "%s/%s-%s/%s";
83
84 protected final ServiceDirectory directory;
85 protected final ClusterService clusterService;
86 protected final DeviceService deviceService;
87 protected final LinkService linkService;
88 protected final HostService hostService;
89 protected final MastershipService mastershipService;
90 protected final IntentService intentService;
91
92 protected final ObjectMapper mapper = new ObjectMapper();
93
94 // TODO: extract into an external & durable state; good enough for now and demo
95 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
96
97 /**
98 * Creates a messaging facility for creating messages for topology viewer.
99 *
100 * @param directory service directory
101 */
102 protected TopologyMessages(ServiceDirectory directory) {
103 this.directory = checkNotNull(directory, "Directory cannot be null");
104 clusterService = directory.get(ClusterService.class);
105 deviceService = directory.get(DeviceService.class);
106 linkService = directory.get(LinkService.class);
107 hostService = directory.get(HostService.class);
108 mastershipService = directory.get(MastershipService.class);
109 intentService = directory.get(IntentService.class);
110 }
111
112 // Retrieves the payload from the specified event.
113 protected ObjectNode payload(ObjectNode event) {
114 return (ObjectNode) event.path("payload");
115 }
116
117 // Returns the specified node property as a number
118 protected long number(ObjectNode node, String name) {
119 return node.path(name).asLong();
120 }
121
122 // Returns the specified node property as a string.
123 protected String string(ObjectNode node, String name) {
124 return node.path(name).asText();
125 }
126
127 // Returns the specified node property as a string.
128 protected String string(ObjectNode node, String name, String defaultValue) {
129 return node.path(name).asText(defaultValue);
130 }
131
132 // Returns the specified set of IP addresses as a string.
133 private String ip(Set<IpAddress> ipAddresses) {
134 Iterator<IpAddress> it = ipAddresses.iterator();
135 return it.hasNext() ? it.next().toString() : "unknown";
136 }
137
138 // Produces JSON structure from annotations.
139 private JsonNode props(Annotations annotations) {
140 ObjectNode props = mapper.createObjectNode();
141 for (String key : annotations.keys()) {
142 props.put(key, annotations.value(key));
143 }
144 return props;
145 }
146
147 // Produces an informational log message event bound to the client.
148 protected ObjectNode info(long id, String message) {
149 return message("info", id, message);
150 }
151
152 // Produces a warning log message event bound to the client.
153 protected ObjectNode warning(long id, String message) {
154 return message("warning", id, message);
155 }
156
157 // Produces an error log message event bound to the client.
158 protected ObjectNode error(long id, String message) {
159 return message("error", id, message);
160 }
161
162 // Produces a log message event bound to the client.
163 private ObjectNode message(String severity, long id, String message) {
164 return envelope("message", id,
165 mapper.createObjectNode()
166 .put("severity", severity)
167 .put("message", message));
168 }
169
170 // Puts the payload into an envelope and returns it.
171 protected ObjectNode envelope(String type, long sid, ObjectNode payload) {
172 ObjectNode event = mapper.createObjectNode();
173 event.put("event", type);
174 if (sid > 0) {
175 event.put("sid", sid);
176 }
177 event.set("payload", payload);
178 return event;
179 }
180
181 // Produces a cluster instance message to the client.
182 protected ObjectNode instanceMessage(ClusterEvent event) {
183 ControllerNode node = event.subject();
184 ObjectNode payload = mapper.createObjectNode()
185 .put("id", node.id().toString())
186 .put("online", clusterService.getState(node.id()) == ACTIVE);
187
188 ArrayNode labels = mapper.createArrayNode();
189 labels.add(node.id().toString());
190 labels.add(node.ip().toString());
191
192 // Add labels, props and stuff the payload into envelope.
193 payload.set("labels", labels);
194 addMetaUi(node.id().toString(), payload);
195
196 String type = (event.type() == INSTANCE_ADDED) ? "addInstance" :
197 ((event.type() == INSTANCE_REMOVED) ? "removeInstance" : "updateInstance");
198 return envelope(type, 0, payload);
199 }
200
201 // Produces a device event message to the client.
202 protected ObjectNode deviceMessage(DeviceEvent event) {
203 Device device = event.subject();
204 ObjectNode payload = mapper.createObjectNode()
205 .put("id", device.id().toString())
206 .put("type", device.type().toString().toLowerCase())
207 .put("online", deviceService.isAvailable(device.id()))
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800208 .put("master", master(device.id()));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800209
210 // Generate labels: id, chassis id, no-label, optional-name
211 ArrayNode labels = mapper.createArrayNode();
212 labels.add(device.id().toString());
213 labels.add(device.chassisId().toString());
214 labels.add(""); // compact no-label view
215 labels.add(device.annotations().value("name"));
216
217 // Add labels, props and stuff the payload into envelope.
218 payload.set("labels", labels);
219 payload.set("props", props(device.annotations()));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800220 addGeoLocation(device, payload);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800221 addMetaUi(device.id().toString(), payload);
222
223 String type = (event.type() == DEVICE_ADDED) ? "addDevice" :
224 ((event.type() == DEVICE_REMOVED) ? "removeDevice" : "updateDevice");
225 return envelope(type, 0, payload);
226 }
227
228 // Produces a link event message to the client.
229 protected ObjectNode linkMessage(LinkEvent event) {
230 Link link = event.subject();
231 ObjectNode payload = mapper.createObjectNode()
232 .put("id", compactLinkString(link))
233 .put("type", link.type().toString().toLowerCase())
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800234 .put("online", true) // link.state()) TODO: add link state field
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800235 .put("linkWidth", 2)
236 .put("src", link.src().deviceId().toString())
237 .put("srcPort", link.src().port().toString())
238 .put("dst", link.dst().deviceId().toString())
239 .put("dstPort", link.dst().port().toString());
240 String type = (event.type() == LINK_ADDED) ? "addLink" :
241 ((event.type() == LINK_REMOVED) ? "removeLink" : "updateLink");
242 return envelope(type, 0, payload);
243 }
244
245 // Produces a host event message to the client.
246 protected ObjectNode hostMessage(HostEvent event) {
247 Host host = event.subject();
248 ObjectNode payload = mapper.createObjectNode()
249 .put("id", host.id().toString())
250 .put("ingress", compactLinkString(edgeLink(host, true)))
251 .put("egress", compactLinkString(edgeLink(host, false)));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800252 payload.set("cp", hostConnect(mapper, host.location()));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800253 payload.set("labels", labels(mapper, ip(host.ipAddresses()),
254 host.mac().toString()));
255 payload.set("props", props(host.annotations()));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800256 addGeoLocation(host, payload);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800257 addMetaUi(host.id().toString(), payload);
258
259 String type = (event.type() == HOST_ADDED) ? "addHost" :
260 ((event.type() == HOST_REMOVED) ? "removeHost" : "updateHost");
261 return envelope(type, 0, payload);
262 }
263
264 // Encodes the specified host location into a JSON object.
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800265 private ObjectNode hostConnect(ObjectMapper mapper, HostLocation location) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800266 return mapper.createObjectNode()
267 .put("device", location.deviceId().toString())
268 .put("port", location.port().toLong());
269 }
270
271 // Encodes the specified list of labels a JSON array.
272 private ArrayNode labels(ObjectMapper mapper, String... labels) {
273 ArrayNode json = mapper.createArrayNode();
274 for (String label : labels) {
275 json.add(label);
276 }
277 return json;
278 }
279
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800280 // Returns the name of the master node for the specified device id.
281 private String master(DeviceId deviceId) {
282 NodeId master = mastershipService.getMasterFor(deviceId);
283 return master != null ? master.toString() : "";
284 }
285
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800286 // Generates an edge link from the specified host location.
287 private EdgeLink edgeLink(Host host, boolean ingress) {
288 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
289 host.location(), ingress);
290 }
291
292 // Adds meta UI information for the specified object.
293 private void addMetaUi(String id, ObjectNode payload) {
294 ObjectNode meta = metaUi.get(id);
295 if (meta != null) {
296 payload.set("metaUi", meta);
297 }
298 }
299
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800300 // Adds a geo location JSON to the specified payload object.
301 private void addGeoLocation(Annotated annotated, ObjectNode payload) {
302 Annotations annotations = annotated.annotations();
303 String slat = annotations.value("latitude");
304 String slng = annotations.value("longitude");
305 try {
306 if (slat != null && slng != null && !slat.isEmpty() && !slng.isEmpty()) {
307 double lat = Double.parseDouble(slat);
308 double lng = Double.parseDouble(slng);
309 ObjectNode loc = mapper.createObjectNode()
310 .put("type", "latlng").put("lat", lat).put("lng", lng);
311 payload.set("location", loc);
312 }
313 } catch (NumberFormatException e) {
314 log.warn("Invalid geo data latitude={}; longiture={}", slat, slng);
315 }
316 }
317
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800318 // Updates meta UI information for the specified object.
319 protected void updateMetaUi(ObjectNode event) {
320 ObjectNode payload = payload(event);
Simon Hunt3b9cddb2014-11-11 20:50:04 -0800321 metaUi.put(string(payload, "id"), (ObjectNode) payload.path("memento"));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800322 }
323
324 // Returns device details response.
325 protected ObjectNode deviceDetails(DeviceId deviceId, long sid) {
326 Device device = deviceService.getDevice(deviceId);
327 Annotations annot = device.annotations();
328 int portCount = deviceService.getPorts(deviceId).size();
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800329 return envelope("showDetails", sid,
330 json(deviceId.toString(),
331 device.type().toString().toLowerCase(),
332 new Prop("Name", annot.value("name")),
333 new Prop("Vendor", device.manufacturer()),
334 new Prop("H/W Version", device.hwVersion()),
335 new Prop("S/W Version", device.swVersion()),
336 new Prop("Serial Number", device.serialNumber()),
337 new Separator(),
338 new Prop("Latitude", annot.value("latitude")),
339 new Prop("Longitude", annot.value("longitude")),
340 new Prop("Ports", Integer.toString(portCount)),
341 new Separator(),
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800342 new Prop("Master", master(deviceId))));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800343 }
344
345 // Returns host details response.
346 protected ObjectNode hostDetails(HostId hostId, long sid) {
347 Host host = hostService.getHost(hostId);
348 Annotations annot = host.annotations();
349 return envelope("showDetails", sid,
350 json(hostId.toString(), "host",
351 new Prop("MAC", host.mac().toString()),
352 new Prop("IP", host.ipAddresses().toString()),
353 new Separator(),
354 new Prop("Latitude", annot.value("latitude")),
355 new Prop("Longitude", annot.value("longitude"))));
356 }
357
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800358 // Produces a path payload to the client.
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800359 protected ObjectNode pathMessage(Path path, String type) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800360 ObjectNode payload = mapper.createObjectNode();
361 ArrayNode links = mapper.createArrayNode();
362 for (Link link : path.links()) {
363 links.add(compactLinkString(link));
364 }
365
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800366 payload.put("type", type).set("links", links);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800367 return payload;
368 }
369
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800370
371 // Produces JSON message to trigger traffic visualization
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800372 protected ObjectNode trafficMessage(long sid, TrafficClass... trafficClasses) {
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800373 ObjectNode payload = mapper.createObjectNode();
374 ArrayNode paths = mapper.createArrayNode();
375 payload.set("paths", paths);
376
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800377 for (TrafficClass trafficClass : trafficClasses) {
378 for (Intent intent : trafficClass.intents) {
379 List<Intent> installables = intentService.getInstallableIntents(intent.id());
380 for (Intent installable : installables) {
381 if (installable instanceof ConnectivityIntent) {
382 addPathTraffic(paths, trafficClass.type, (ConnectivityIntent) installable);
383 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800384 }
385 }
386 }
387
388 return envelope("showTraffic", sid, payload);
389 }
390
391 // Adds the link segments (path or tree) associated with the specified
392 // connectivity intent
393 protected void addPathTraffic(ArrayNode paths, String type,
394 ConnectivityIntent installable) {
395 ObjectNode pathNode = mapper.createObjectNode();
396 ArrayNode linksNode = mapper.createArrayNode();
397
398 Iterable<Link> links;
399 if (installable instanceof PathIntent) {
400 links = ((PathIntent) installable).path().links();
401 } else if (installable instanceof LinkCollectionIntent) {
402 links = ((LinkCollectionIntent) installable).links();
403 } else {
404 return;
405 }
406
407 for (Link link : links) {
408 linksNode.add(compactLinkString(link));
409 }
410 pathNode.put("type", type).set("links", linksNode);
411 paths.add(pathNode);
412 }
413
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800414 // Produces compact string representation of a link.
415 private static String compactLinkString(Link link) {
416 return String.format(COMPACT, link.src().elementId(), link.src().port(),
417 link.dst().elementId(), link.dst().port());
418 }
419
420 // Produces JSON property details.
421 private ObjectNode json(String id, String type, Prop... props) {
422 ObjectMapper mapper = new ObjectMapper();
423 ObjectNode result = mapper.createObjectNode()
424 .put("id", id).put("type", type);
425 ObjectNode pnode = mapper.createObjectNode();
426 ArrayNode porder = mapper.createArrayNode();
427 for (Prop p : props) {
428 porder.add(p.key);
429 pnode.put(p.key, p.value);
430 }
431 result.set("propOrder", porder);
432 result.set("props", pnode);
433 return result;
434 }
435
436 // Auxiliary key/value carrier.
437 private class Prop {
438 public final String key;
439 public final String value;
440
441 protected Prop(String key, String value) {
442 this.key = key;
443 this.value = value;
444 }
445 }
446
447 // Auxiliary properties separator
448 private class Separator extends Prop {
449 protected Separator() {
450 super("-", "");
451 }
452 }
453
Thomas Vachuskae7591e52014-11-13 21:31:15 -0800454 // Auxiliary carrier of data for requesting traffic message.
455 protected class TrafficClass {
456 public final String type;
457 public final Set<Intent> intents;
458
459 TrafficClass(String type, Set<Intent> intents) {
460 this.type = type;
461 this.intents = intents;
462 }
463 }
464
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800465}