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