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; |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 27 | import org.onlab.onos.net.Annotated; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 28 | import org.onlab.onos.net.Annotations; |
| 29 | import org.onlab.onos.net.ConnectPoint; |
| 30 | import org.onlab.onos.net.DefaultEdgeLink; |
| 31 | import org.onlab.onos.net.Device; |
| 32 | import org.onlab.onos.net.DeviceId; |
| 33 | import org.onlab.onos.net.EdgeLink; |
| 34 | import org.onlab.onos.net.Host; |
| 35 | import org.onlab.onos.net.HostId; |
| 36 | import org.onlab.onos.net.HostLocation; |
| 37 | import org.onlab.onos.net.Link; |
| 38 | import org.onlab.onos.net.Path; |
| 39 | import org.onlab.onos.net.device.DeviceEvent; |
| 40 | import org.onlab.onos.net.device.DeviceService; |
| 41 | import org.onlab.onos.net.host.HostEvent; |
| 42 | import org.onlab.onos.net.host.HostService; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 43 | import org.onlab.onos.net.intent.Intent; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 44 | import org.onlab.onos.net.intent.IntentService; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 45 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 46 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
Thomas Vachuska | dea4cb3 | 2014-11-14 12:14:30 -0800 | [diff] [blame] | 47 | import org.onlab.onos.net.intent.OpticalPathIntent; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 48 | import org.onlab.onos.net.intent.PathIntent; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 49 | import org.onlab.onos.net.link.LinkEvent; |
| 50 | import org.onlab.onos.net.link.LinkService; |
| 51 | import org.onlab.onos.net.provider.ProviderId; |
| 52 | import org.onlab.osgi.ServiceDirectory; |
| 53 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 56 | |
| 57 | import java.util.Iterator; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 58 | import java.util.List; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 59 | import java.util.Map; |
| 60 | import java.util.Set; |
| 61 | import java.util.concurrent.ConcurrentHashMap; |
| 62 | |
| 63 | import static com.google.common.base.Preconditions.checkNotNull; |
| 64 | import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
| 65 | import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_REMOVED; |
| 66 | import static org.onlab.onos.cluster.ControllerNode.State.ACTIVE; |
| 67 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 68 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 69 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
| 70 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED; |
| 71 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED; |
| 72 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED; |
| 73 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED; |
| 74 | |
| 75 | /** |
| 76 | * Facility for creating messages bound for the topology viewer. |
| 77 | */ |
| 78 | public abstract class TopologyMessages { |
| 79 | |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 80 | protected static final Logger log = LoggerFactory.getLogger(TopologyMessages.class); |
| 81 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 82 | 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 Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 92 | // protected final StatisticService statService; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 93 | |
| 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 Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 112 | // statService = directory.get(StatisticService.class); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 113 | } |
| 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 Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 211 | .put("master", master(device.id())); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 212 | |
| 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 Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 223 | addGeoLocation(device, payload); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 224 | 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 Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 237 | .put("online", true) // link.state()) TODO: add link state field |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 238 | .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 Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 255 | payload.set("cp", hostConnect(mapper, host.location())); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 256 | payload.set("labels", labels(mapper, ip(host.ipAddresses()), |
| 257 | host.mac().toString())); |
| 258 | payload.set("props", props(host.annotations())); |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 259 | addGeoLocation(host, payload); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 260 | 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 Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 268 | private ObjectNode hostConnect(ObjectMapper mapper, HostLocation location) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 269 | 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 Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 283 | // 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 Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 289 | // 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 Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 303 | // 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 Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 321 | // Updates meta UI information for the specified object. |
| 322 | protected void updateMetaUi(ObjectNode event) { |
| 323 | ObjectNode payload = payload(event); |
Simon Hunt | 3b9cddb | 2014-11-11 20:50:04 -0800 | [diff] [blame] | 324 | metaUi.put(string(payload, "id"), (ObjectNode) payload.path("memento")); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 325 | } |
| 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 Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 332 | 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 Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 345 | new Prop("Master", master(deviceId)))); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 346 | } |
| 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 Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 361 | // Produces a path payload to the client. |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 362 | protected ObjectNode pathMessage(Path path, String type) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 363 | ObjectNode payload = mapper.createObjectNode(); |
| 364 | ArrayNode links = mapper.createArrayNode(); |
| 365 | for (Link link : path.links()) { |
| 366 | links.add(compactLinkString(link)); |
| 367 | } |
| 368 | |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 369 | payload.put("type", type).set("links", links); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 370 | return payload; |
| 371 | } |
| 372 | |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 373 | |
| 374 | // Produces JSON message to trigger traffic visualization |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 375 | protected ObjectNode trafficMessage(long sid, TrafficClass... trafficClasses) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 376 | ObjectNode payload = mapper.createObjectNode(); |
| 377 | ArrayNode paths = mapper.createArrayNode(); |
| 378 | payload.set("paths", paths); |
| 379 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 380 | for (TrafficClass trafficClass : trafficClasses) { |
| 381 | for (Intent intent : trafficClass.intents) { |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 382 | boolean isOptical = intent instanceof OpticalConnectivityIntent; |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 383 | List<Intent> installables = intentService.getInstallableIntents(intent.id()); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 384 | if (installables != null) { |
| 385 | for (Intent installable : installables) { |
| 386 | String cls = isOptical ? trafficClass.type + " optical" : trafficClass.type; |
Thomas Vachuska | dea4cb3 | 2014-11-14 12:14:30 -0800 | [diff] [blame] | 387 | if (installable instanceof PathIntent) { |
| 388 | addPathTraffic(paths, cls, ((PathIntent) installable).path().links()); |
| 389 | } else if (installable instanceof LinkCollectionIntent) { |
| 390 | addPathTraffic(paths, cls, ((LinkCollectionIntent) installable).links()); |
| 391 | } else if (installable instanceof OpticalPathIntent) { |
| 392 | addPathTraffic(paths, cls, ((OpticalPathIntent) installable).path().links()); |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 393 | } |
Thomas Vachuska | dea4cb3 | 2014-11-14 12:14:30 -0800 | [diff] [blame] | 394 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 395 | } |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | return envelope("showTraffic", sid, payload); |
| 401 | } |
| 402 | |
| 403 | // Adds the link segments (path or tree) associated with the specified |
| 404 | // connectivity intent |
Thomas Vachuska | dea4cb3 | 2014-11-14 12:14:30 -0800 | [diff] [blame] | 405 | protected void addPathTraffic(ArrayNode paths, String type, Iterable<Link> links) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 406 | ObjectNode pathNode = mapper.createObjectNode(); |
| 407 | ArrayNode linksNode = mapper.createArrayNode(); |
| 408 | |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 409 | if (links != null) { |
| 410 | ArrayNode labels = mapper.createArrayNode(); |
| 411 | boolean hasTraffic = true; // FIXME |
| 412 | for (Link link : links) { |
| 413 | linksNode.add(compactLinkString(link)); |
| 414 | // Load load = statService.load(link); |
| 415 | String label = ""; |
| 416 | // if (load.rate() > 0) { |
| 417 | // label = load.toString(); |
| 418 | // } |
| 419 | labels.add(label); |
| 420 | } |
| 421 | pathNode.put("class", hasTraffic ? type + " animated" : type); |
| 422 | pathNode.put("traffic", hasTraffic); |
| 423 | pathNode.set("links", linksNode); |
| 424 | pathNode.set("labels", labels); |
| 425 | paths.add(pathNode); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 426 | } |
Thomas Vachuska | 22e3492 | 2014-11-14 00:40:55 -0800 | [diff] [blame] | 427 | } |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 428 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 429 | // Produces compact string representation of a link. |
| 430 | private static String compactLinkString(Link link) { |
| 431 | return String.format(COMPACT, link.src().elementId(), link.src().port(), |
| 432 | link.dst().elementId(), link.dst().port()); |
| 433 | } |
| 434 | |
| 435 | // Produces JSON property details. |
| 436 | private ObjectNode json(String id, String type, Prop... props) { |
| 437 | ObjectMapper mapper = new ObjectMapper(); |
| 438 | ObjectNode result = mapper.createObjectNode() |
| 439 | .put("id", id).put("type", type); |
| 440 | ObjectNode pnode = mapper.createObjectNode(); |
| 441 | ArrayNode porder = mapper.createArrayNode(); |
| 442 | for (Prop p : props) { |
| 443 | porder.add(p.key); |
| 444 | pnode.put(p.key, p.value); |
| 445 | } |
| 446 | result.set("propOrder", porder); |
| 447 | result.set("props", pnode); |
| 448 | return result; |
| 449 | } |
| 450 | |
| 451 | // Auxiliary key/value carrier. |
| 452 | private class Prop { |
| 453 | public final String key; |
| 454 | public final String value; |
| 455 | |
| 456 | protected Prop(String key, String value) { |
| 457 | this.key = key; |
| 458 | this.value = value; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Auxiliary properties separator |
| 463 | private class Separator extends Prop { |
| 464 | protected Separator() { |
| 465 | super("-", ""); |
| 466 | } |
| 467 | } |
| 468 | |
Thomas Vachuska | e7591e5 | 2014-11-13 21:31:15 -0800 | [diff] [blame] | 469 | // Auxiliary carrier of data for requesting traffic message. |
| 470 | protected class TrafficClass { |
| 471 | public final String type; |
| 472 | public final Set<Intent> intents; |
| 473 | |
| 474 | TrafficClass(String type, Set<Intent> intents) { |
| 475 | this.type = type; |
| 476 | this.intents = intents; |
| 477 | } |
| 478 | } |
| 479 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 480 | } |