blob: d68e8fa8b2063efc74688bd2180db91ce5047f26 [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;
43import org.onlab.onos.net.intent.IntentService;
44import org.onlab.onos.net.link.LinkEvent;
45import org.onlab.onos.net.link.LinkService;
46import org.onlab.onos.net.provider.ProviderId;
47import org.onlab.osgi.ServiceDirectory;
48import org.onlab.packet.IpAddress;
Thomas Vachuska0f6baee2014-11-11 15:02:32 -080049import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080051
52import java.util.Iterator;
53import java.util.Map;
54import java.util.Set;
55import java.util.concurrent.ConcurrentHashMap;
56
57import static com.google.common.base.Preconditions.checkNotNull;
58import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED;
59import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_REMOVED;
60import static org.onlab.onos.cluster.ControllerNode.State.ACTIVE;
61import static org.onlab.onos.net.PortNumber.portNumber;
62import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
63import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED;
64import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
65import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED;
66import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
67import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED;
68
69/**
70 * Facility for creating messages bound for the topology viewer.
71 */
72public abstract class TopologyMessages {
73
Thomas Vachuska0f6baee2014-11-11 15:02:32 -080074 protected static final Logger log = LoggerFactory.getLogger(TopologyMessages.class);
75
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -080076 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
77 private static final String COMPACT = "%s/%s-%s/%s";
78
79 protected final ServiceDirectory directory;
80 protected final ClusterService clusterService;
81 protected final DeviceService deviceService;
82 protected final LinkService linkService;
83 protected final HostService hostService;
84 protected final MastershipService mastershipService;
85 protected final IntentService intentService;
86
87 protected final ObjectMapper mapper = new ObjectMapper();
88
89 // TODO: extract into an external & durable state; good enough for now and demo
90 private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>();
91
92 /**
93 * Creates a messaging facility for creating messages for topology viewer.
94 *
95 * @param directory service directory
96 */
97 protected TopologyMessages(ServiceDirectory directory) {
98 this.directory = checkNotNull(directory, "Directory cannot be null");
99 clusterService = directory.get(ClusterService.class);
100 deviceService = directory.get(DeviceService.class);
101 linkService = directory.get(LinkService.class);
102 hostService = directory.get(HostService.class);
103 mastershipService = directory.get(MastershipService.class);
104 intentService = directory.get(IntentService.class);
105 }
106
107 // Retrieves the payload from the specified event.
108 protected ObjectNode payload(ObjectNode event) {
109 return (ObjectNode) event.path("payload");
110 }
111
112 // Returns the specified node property as a number
113 protected long number(ObjectNode node, String name) {
114 return node.path(name).asLong();
115 }
116
117 // Returns the specified node property as a string.
118 protected String string(ObjectNode node, String name) {
119 return node.path(name).asText();
120 }
121
122 // Returns the specified node property as a string.
123 protected String string(ObjectNode node, String name, String defaultValue) {
124 return node.path(name).asText(defaultValue);
125 }
126
127 // Returns the specified set of IP addresses as a string.
128 private String ip(Set<IpAddress> ipAddresses) {
129 Iterator<IpAddress> it = ipAddresses.iterator();
130 return it.hasNext() ? it.next().toString() : "unknown";
131 }
132
133 // Produces JSON structure from annotations.
134 private JsonNode props(Annotations annotations) {
135 ObjectNode props = mapper.createObjectNode();
136 for (String key : annotations.keys()) {
137 props.put(key, annotations.value(key));
138 }
139 return props;
140 }
141
142 // Produces an informational log message event bound to the client.
143 protected ObjectNode info(long id, String message) {
144 return message("info", id, message);
145 }
146
147 // Produces a warning log message event bound to the client.
148 protected ObjectNode warning(long id, String message) {
149 return message("warning", id, message);
150 }
151
152 // Produces an error log message event bound to the client.
153 protected ObjectNode error(long id, String message) {
154 return message("error", id, message);
155 }
156
157 // Produces a log message event bound to the client.
158 private ObjectNode message(String severity, long id, String message) {
159 return envelope("message", id,
160 mapper.createObjectNode()
161 .put("severity", severity)
162 .put("message", message));
163 }
164
165 // Puts the payload into an envelope and returns it.
166 protected ObjectNode envelope(String type, long sid, ObjectNode payload) {
167 ObjectNode event = mapper.createObjectNode();
168 event.put("event", type);
169 if (sid > 0) {
170 event.put("sid", sid);
171 }
172 event.set("payload", payload);
173 return event;
174 }
175
176 // Produces a cluster instance message to the client.
177 protected ObjectNode instanceMessage(ClusterEvent event) {
178 ControllerNode node = event.subject();
179 ObjectNode payload = mapper.createObjectNode()
180 .put("id", node.id().toString())
181 .put("online", clusterService.getState(node.id()) == ACTIVE);
182
183 ArrayNode labels = mapper.createArrayNode();
184 labels.add(node.id().toString());
185 labels.add(node.ip().toString());
186
187 // Add labels, props and stuff the payload into envelope.
188 payload.set("labels", labels);
189 addMetaUi(node.id().toString(), payload);
190
191 String type = (event.type() == INSTANCE_ADDED) ? "addInstance" :
192 ((event.type() == INSTANCE_REMOVED) ? "removeInstance" : "updateInstance");
193 return envelope(type, 0, payload);
194 }
195
196 // Produces a device event message to the client.
197 protected ObjectNode deviceMessage(DeviceEvent event) {
198 Device device = event.subject();
199 ObjectNode payload = mapper.createObjectNode()
200 .put("id", device.id().toString())
201 .put("type", device.type().toString().toLowerCase())
202 .put("online", deviceService.isAvailable(device.id()))
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800203 .put("master", master(device.id()));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800204
205 // Generate labels: id, chassis id, no-label, optional-name
206 ArrayNode labels = mapper.createArrayNode();
207 labels.add(device.id().toString());
208 labels.add(device.chassisId().toString());
209 labels.add(""); // compact no-label view
210 labels.add(device.annotations().value("name"));
211
212 // Add labels, props and stuff the payload into envelope.
213 payload.set("labels", labels);
214 payload.set("props", props(device.annotations()));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800215 addGeoLocation(device, payload);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800216 addMetaUi(device.id().toString(), payload);
217
218 String type = (event.type() == DEVICE_ADDED) ? "addDevice" :
219 ((event.type() == DEVICE_REMOVED) ? "removeDevice" : "updateDevice");
220 return envelope(type, 0, payload);
221 }
222
223 // Produces a link event message to the client.
224 protected ObjectNode linkMessage(LinkEvent event) {
225 Link link = event.subject();
226 ObjectNode payload = mapper.createObjectNode()
227 .put("id", compactLinkString(link))
228 .put("type", link.type().toString().toLowerCase())
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800229 .put("online", true) // TODO: add link state field
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800230 .put("linkWidth", 2)
231 .put("src", link.src().deviceId().toString())
232 .put("srcPort", link.src().port().toString())
233 .put("dst", link.dst().deviceId().toString())
234 .put("dstPort", link.dst().port().toString());
235 String type = (event.type() == LINK_ADDED) ? "addLink" :
236 ((event.type() == LINK_REMOVED) ? "removeLink" : "updateLink");
237 return envelope(type, 0, payload);
238 }
239
240 // Produces a host event message to the client.
241 protected ObjectNode hostMessage(HostEvent event) {
242 Host host = event.subject();
243 ObjectNode payload = mapper.createObjectNode()
244 .put("id", host.id().toString())
245 .put("ingress", compactLinkString(edgeLink(host, true)))
246 .put("egress", compactLinkString(edgeLink(host, false)));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800247 payload.set("cp", hostConnect(mapper, host.location()));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800248 payload.set("labels", labels(mapper, ip(host.ipAddresses()),
249 host.mac().toString()));
250 payload.set("props", props(host.annotations()));
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800251 addGeoLocation(host, payload);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800252 addMetaUi(host.id().toString(), payload);
253
254 String type = (event.type() == HOST_ADDED) ? "addHost" :
255 ((event.type() == HOST_REMOVED) ? "removeHost" : "updateHost");
256 return envelope(type, 0, payload);
257 }
258
259 // Encodes the specified host location into a JSON object.
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800260 private ObjectNode hostConnect(ObjectMapper mapper, HostLocation location) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800261 return mapper.createObjectNode()
262 .put("device", location.deviceId().toString())
263 .put("port", location.port().toLong());
264 }
265
266 // Encodes the specified list of labels a JSON array.
267 private ArrayNode labels(ObjectMapper mapper, String... labels) {
268 ArrayNode json = mapper.createArrayNode();
269 for (String label : labels) {
270 json.add(label);
271 }
272 return json;
273 }
274
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800275 // Returns the name of the master node for the specified device id.
276 private String master(DeviceId deviceId) {
277 NodeId master = mastershipService.getMasterFor(deviceId);
278 return master != null ? master.toString() : "";
279 }
280
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800281 // Generates an edge link from the specified host location.
282 private EdgeLink edgeLink(Host host, boolean ingress) {
283 return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)),
284 host.location(), ingress);
285 }
286
287 // Adds meta UI information for the specified object.
288 private void addMetaUi(String id, ObjectNode payload) {
289 ObjectNode meta = metaUi.get(id);
290 if (meta != null) {
291 payload.set("metaUi", meta);
292 }
293 }
294
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800295 // Adds a geo location JSON to the specified payload object.
296 private void addGeoLocation(Annotated annotated, ObjectNode payload) {
297 Annotations annotations = annotated.annotations();
298 String slat = annotations.value("latitude");
299 String slng = annotations.value("longitude");
300 try {
301 if (slat != null && slng != null && !slat.isEmpty() && !slng.isEmpty()) {
302 double lat = Double.parseDouble(slat);
303 double lng = Double.parseDouble(slng);
304 ObjectNode loc = mapper.createObjectNode()
305 .put("type", "latlng").put("lat", lat).put("lng", lng);
306 payload.set("location", loc);
307 }
308 } catch (NumberFormatException e) {
309 log.warn("Invalid geo data latitude={}; longiture={}", slat, slng);
310 }
311 }
312
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800313 // Updates meta UI information for the specified object.
314 protected void updateMetaUi(ObjectNode event) {
315 ObjectNode payload = payload(event);
316 metaUi.put(string(payload, "id"), payload);
317 }
318
319 // Returns device details response.
320 protected ObjectNode deviceDetails(DeviceId deviceId, long sid) {
321 Device device = deviceService.getDevice(deviceId);
322 Annotations annot = device.annotations();
323 int portCount = deviceService.getPorts(deviceId).size();
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800324 return envelope("showDetails", sid,
325 json(deviceId.toString(),
326 device.type().toString().toLowerCase(),
327 new Prop("Name", annot.value("name")),
328 new Prop("Vendor", device.manufacturer()),
329 new Prop("H/W Version", device.hwVersion()),
330 new Prop("S/W Version", device.swVersion()),
331 new Prop("Serial Number", device.serialNumber()),
332 new Separator(),
333 new Prop("Latitude", annot.value("latitude")),
334 new Prop("Longitude", annot.value("longitude")),
335 new Prop("Ports", Integer.toString(portCount)),
336 new Separator(),
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800337 new Prop("Master", master(deviceId))));
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800338 }
339
340 // Returns host details response.
341 protected ObjectNode hostDetails(HostId hostId, long sid) {
342 Host host = hostService.getHost(hostId);
343 Annotations annot = host.annotations();
344 return envelope("showDetails", sid,
345 json(hostId.toString(), "host",
346 new Prop("MAC", host.mac().toString()),
347 new Prop("IP", host.ipAddresses().toString()),
348 new Separator(),
349 new Prop("Latitude", annot.value("latitude")),
350 new Prop("Longitude", annot.value("longitude"))));
351 }
352
353
354 // Produces a path message to the client.
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800355 protected ObjectNode pathMessage(Path path, String type) {
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800356 ObjectNode payload = mapper.createObjectNode();
357 ArrayNode links = mapper.createArrayNode();
358 for (Link link : path.links()) {
359 links.add(compactLinkString(link));
360 }
361
Thomas Vachuska0f6baee2014-11-11 15:02:32 -0800362 payload.put("type", type).set("links", links);
Thomas Vachuskaa7c3dd12014-11-11 09:10:19 -0800363 return payload;
364 }
365
366 // Produces compact string representation of a link.
367 private static String compactLinkString(Link link) {
368 return String.format(COMPACT, link.src().elementId(), link.src().port(),
369 link.dst().elementId(), link.dst().port());
370 }
371
372 // Produces JSON property details.
373 private ObjectNode json(String id, String type, Prop... props) {
374 ObjectMapper mapper = new ObjectMapper();
375 ObjectNode result = mapper.createObjectNode()
376 .put("id", id).put("type", type);
377 ObjectNode pnode = mapper.createObjectNode();
378 ArrayNode porder = mapper.createArrayNode();
379 for (Prop p : props) {
380 porder.add(p.key);
381 pnode.put(p.key, p.value);
382 }
383 result.set("propOrder", porder);
384 result.set("props", pnode);
385 return result;
386 }
387
388 // Auxiliary key/value carrier.
389 private class Prop {
390 public final String key;
391 public final String value;
392
393 protected Prop(String key, String value) {
394 this.key = key;
395 this.value = value;
396 }
397 }
398
399 // Auxiliary properties separator
400 private class Separator extends Prop {
401 protected Separator() {
402 super("-", "");
403 }
404 }
405
406}