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