Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cluster.ClusterEvent; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.ControllerNode; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.core.CoreService; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.PortStatisticsService; |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 30 | import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint; |
| 31 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 32 | import org.onosproject.incubator.net.tunnel.TunnelService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 33 | import org.onosproject.mastership.MastershipService; |
| 34 | import org.onosproject.net.Annotated; |
| 35 | import org.onosproject.net.AnnotationKeys; |
| 36 | import org.onosproject.net.Annotations; |
| 37 | import org.onosproject.net.ConnectPoint; |
| 38 | import org.onosproject.net.DefaultEdgeLink; |
| 39 | import org.onosproject.net.Device; |
| 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.EdgeLink; |
| 42 | import org.onosproject.net.Host; |
| 43 | import org.onosproject.net.HostId; |
| 44 | import org.onosproject.net.HostLocation; |
| 45 | import org.onosproject.net.Link; |
| 46 | import org.onosproject.net.LinkKey; |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 47 | import org.onosproject.net.NetworkResource; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 48 | import org.onosproject.net.PortNumber; |
| 49 | import org.onosproject.net.device.DeviceEvent; |
| 50 | import org.onosproject.net.device.DeviceService; |
| 51 | import org.onosproject.net.flow.FlowEntry; |
| 52 | import org.onosproject.net.flow.FlowRuleService; |
| 53 | import org.onosproject.net.flow.TrafficTreatment; |
| 54 | import org.onosproject.net.flow.instructions.Instruction; |
| 55 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 56 | import org.onosproject.net.host.HostEvent; |
| 57 | import org.onosproject.net.host.HostService; |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 58 | import org.onosproject.net.intent.FlowRuleIntent; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 59 | import org.onosproject.net.intent.Intent; |
| 60 | import org.onosproject.net.intent.IntentService; |
| 61 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 62 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 63 | import org.onosproject.net.intent.OpticalPathIntent; |
| 64 | import org.onosproject.net.intent.PathIntent; |
| 65 | import org.onosproject.net.link.LinkEvent; |
| 66 | import org.onosproject.net.link.LinkService; |
| 67 | import org.onosproject.net.provider.ProviderId; |
| 68 | import org.onosproject.net.statistic.Load; |
| 69 | import org.onosproject.net.statistic.StatisticService; |
| 70 | import org.onosproject.net.topology.Topology; |
| 71 | import org.onosproject.net.topology.TopologyService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 72 | import org.onosproject.ui.JsonUtils; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 73 | import org.onosproject.ui.UiConnection; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 74 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 75 | import org.onosproject.ui.topo.PropertyPanel; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | import org.slf4j.LoggerFactory; |
| 78 | |
| 79 | import java.text.DecimalFormat; |
| 80 | import java.util.ArrayList; |
| 81 | import java.util.Collection; |
| 82 | import java.util.Collections; |
| 83 | import java.util.HashMap; |
| 84 | import java.util.HashSet; |
| 85 | import java.util.Iterator; |
| 86 | import java.util.List; |
| 87 | import java.util.Map; |
| 88 | import java.util.Set; |
| 89 | import java.util.concurrent.ConcurrentHashMap; |
| 90 | |
| 91 | import static com.google.common.base.Preconditions.checkNotNull; |
| 92 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 93 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
| 94 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_REMOVED; |
| 95 | import static org.onosproject.cluster.ControllerNode.State.ACTIVE; |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 96 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 97 | import static org.onosproject.net.DeviceId.deviceId; |
| 98 | import static org.onosproject.net.HostId.hostId; |
| 99 | import static org.onosproject.net.LinkKey.linkKey; |
| 100 | import static org.onosproject.net.PortNumber.P0; |
| 101 | import static org.onosproject.net.PortNumber.portNumber; |
| 102 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 103 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
| 104 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 105 | import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; |
| 106 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 107 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 108 | import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.StatsType.FLOW; |
| 109 | import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.StatsType.PORT; |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 110 | import static org.onosproject.ui.topo.TopoConstants.*; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Facility for creating messages bound for the topology viewer. |
| 114 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 115 | public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 116 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 117 | protected static final Logger log = |
| 118 | LoggerFactory.getLogger(TopologyViewMessageHandlerBase.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 119 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 120 | private static final ProviderId PID = |
| 121 | new ProviderId("core", "org.onosproject.core", true); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 122 | private static final String COMPACT = "%s/%s-%s/%s"; |
| 123 | |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 124 | private static final double KILO = 1024; |
| 125 | private static final double MEGA = 1024 * KILO; |
| 126 | private static final double GIGA = 1024 * MEGA; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 127 | |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 128 | private static final String GBITS_UNIT = "Gb"; |
| 129 | private static final String MBITS_UNIT = "Mb"; |
| 130 | private static final String KBITS_UNIT = "Kb"; |
| 131 | private static final String BITS_UNIT = "b"; |
| 132 | private static final String GBYTES_UNIT = "GB"; |
| 133 | private static final String MBYTES_UNIT = "MB"; |
| 134 | private static final String KBYTES_UNIT = "KB"; |
| 135 | private static final String BYTES_UNIT = "B"; |
| 136 | //4 Kilo Bytes as threshold |
| 137 | private static final double BPS_THRESHOLD = 4 * KILO; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 138 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 139 | protected ServiceDirectory directory; |
| 140 | protected ClusterService clusterService; |
| 141 | protected DeviceService deviceService; |
| 142 | protected LinkService linkService; |
| 143 | protected HostService hostService; |
| 144 | protected MastershipService mastershipService; |
| 145 | protected IntentService intentService; |
| 146 | protected FlowRuleService flowService; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 147 | protected StatisticService flowStatsService; |
| 148 | protected PortStatisticsService portStatsService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 149 | protected TopologyService topologyService; |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 150 | protected TunnelService tunnelService; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 151 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 152 | protected enum StatsType { |
| 153 | FLOW, PORT |
| 154 | } |
| 155 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 156 | private String version; |
| 157 | |
| 158 | // TODO: extract into an external & durable state; good enough for now and demo |
| 159 | private static Map<String, ObjectNode> metaUi = new ConcurrentHashMap<>(); |
| 160 | |
| 161 | /** |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 162 | * Returns read-only view of the meta-ui information. |
| 163 | * |
| 164 | * @return map of id to meta-ui mementos |
| 165 | */ |
| 166 | static Map<String, ObjectNode> getMetaUi() { |
| 167 | return Collections.unmodifiableMap(metaUi); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 172 | super.init(connection, directory); |
| 173 | this.directory = checkNotNull(directory, "Directory cannot be null"); |
| 174 | clusterService = directory.get(ClusterService.class); |
| 175 | deviceService = directory.get(DeviceService.class); |
| 176 | linkService = directory.get(LinkService.class); |
| 177 | hostService = directory.get(HostService.class); |
| 178 | mastershipService = directory.get(MastershipService.class); |
| 179 | intentService = directory.get(IntentService.class); |
| 180 | flowService = directory.get(FlowRuleService.class); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 181 | flowStatsService = directory.get(StatisticService.class); |
| 182 | portStatsService = directory.get(PortStatisticsService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 183 | topologyService = directory.get(TopologyService.class); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 184 | tunnelService = directory.get(TunnelService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 185 | |
| 186 | String ver = directory.get(CoreService.class).version().toString(); |
| 187 | version = ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", ""); |
| 188 | } |
| 189 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 190 | // Returns the specified set of IP addresses as a string. |
| 191 | private String ip(Set<IpAddress> ipAddresses) { |
| 192 | Iterator<IpAddress> it = ipAddresses.iterator(); |
| 193 | return it.hasNext() ? it.next().toString() : "unknown"; |
| 194 | } |
| 195 | |
| 196 | // Produces JSON structure from annotations. |
| 197 | private JsonNode props(Annotations annotations) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 198 | ObjectNode props = objectNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 199 | if (annotations != null) { |
| 200 | for (String key : annotations.keys()) { |
| 201 | props.put(key, annotations.value(key)); |
| 202 | } |
| 203 | } |
| 204 | return props; |
| 205 | } |
| 206 | |
| 207 | // Produces an informational log message event bound to the client. |
| 208 | protected ObjectNode info(long id, String message) { |
| 209 | return message("info", id, message); |
| 210 | } |
| 211 | |
| 212 | // Produces a warning log message event bound to the client. |
| 213 | protected ObjectNode warning(long id, String message) { |
| 214 | return message("warning", id, message); |
| 215 | } |
| 216 | |
| 217 | // Produces an error log message event bound to the client. |
| 218 | protected ObjectNode error(long id, String message) { |
| 219 | return message("error", id, message); |
| 220 | } |
| 221 | |
| 222 | // Produces a log message event bound to the client. |
| 223 | private ObjectNode message(String severity, long id, String message) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 224 | ObjectNode payload = objectNode() |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 225 | .put("severity", severity) |
| 226 | .put("message", message); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 227 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 228 | return JsonUtils.envelope("message", id, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Produces a set of all hosts listed in the specified JSON array. |
| 232 | protected Set<Host> getHosts(ArrayNode array) { |
| 233 | Set<Host> hosts = new HashSet<>(); |
| 234 | if (array != null) { |
| 235 | for (JsonNode node : array) { |
| 236 | try { |
| 237 | addHost(hosts, hostId(node.asText())); |
| 238 | } catch (IllegalArgumentException e) { |
| 239 | log.debug("Skipping ID {}", node.asText()); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | return hosts; |
| 244 | } |
| 245 | |
| 246 | // Adds the specified host to the set of hosts. |
| 247 | private void addHost(Set<Host> hosts, HostId hostId) { |
| 248 | Host host = hostService.getHost(hostId); |
| 249 | if (host != null) { |
| 250 | hosts.add(host); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | |
| 255 | // Produces a set of all devices listed in the specified JSON array. |
| 256 | protected Set<Device> getDevices(ArrayNode array) { |
| 257 | Set<Device> devices = new HashSet<>(); |
| 258 | if (array != null) { |
| 259 | for (JsonNode node : array) { |
| 260 | try { |
| 261 | addDevice(devices, deviceId(node.asText())); |
| 262 | } catch (IllegalArgumentException e) { |
| 263 | log.debug("Skipping ID {}", node.asText()); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | return devices; |
| 268 | } |
| 269 | |
| 270 | private void addDevice(Set<Device> devices, DeviceId deviceId) { |
| 271 | Device device = deviceService.getDevice(deviceId); |
| 272 | if (device != null) { |
| 273 | devices.add(device); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | protected void addHover(Set<Host> hosts, Set<Device> devices, String hover) { |
| 278 | try { |
| 279 | addHost(hosts, hostId(hover)); |
| 280 | } catch (IllegalArgumentException e) { |
| 281 | try { |
| 282 | addDevice(devices, deviceId(hover)); |
| 283 | } catch (IllegalArgumentException ne) { |
| 284 | log.debug("Skipping ID {}", hover); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Produces a cluster instance message to the client. |
| 290 | protected ObjectNode instanceMessage(ClusterEvent event, String messageType) { |
| 291 | ControllerNode node = event.subject(); |
| 292 | int switchCount = mastershipService.getDevicesOf(node.id()).size(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 293 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 294 | .put("id", node.id().toString()) |
| 295 | .put("ip", node.ip().toString()) |
| 296 | .put("online", clusterService.getState(node.id()) == ACTIVE) |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 297 | .put("uiAttached", node.equals(clusterService.getLocalNode())) |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 298 | .put("switches", switchCount); |
| 299 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 300 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 301 | labels.add(node.id().toString()); |
| 302 | labels.add(node.ip().toString()); |
| 303 | |
| 304 | // Add labels, props and stuff the payload into envelope. |
| 305 | payload.set("labels", labels); |
| 306 | addMetaUi(node.id().toString(), payload); |
| 307 | |
| 308 | String type = messageType != null ? messageType : |
| 309 | ((event.type() == INSTANCE_ADDED) ? "addInstance" : |
| 310 | ((event.type() == INSTANCE_REMOVED ? "removeInstance" : |
| 311 | "addInstance"))); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 312 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Produces a device event message to the client. |
| 316 | protected ObjectNode deviceMessage(DeviceEvent event) { |
| 317 | Device device = event.subject(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 318 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 319 | .put("id", device.id().toString()) |
| 320 | .put("type", device.type().toString().toLowerCase()) |
| 321 | .put("online", deviceService.isAvailable(device.id())) |
| 322 | .put("master", master(device.id())); |
| 323 | |
| 324 | // Generate labels: id, chassis id, no-label, optional-name |
| 325 | String name = device.annotations().value(AnnotationKeys.NAME); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 326 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 327 | labels.add(""); |
| 328 | labels.add(isNullOrEmpty(name) ? device.id().toString() : name); |
| 329 | labels.add(device.id().toString()); |
| 330 | |
| 331 | // Add labels, props and stuff the payload into envelope. |
| 332 | payload.set("labels", labels); |
| 333 | payload.set("props", props(device.annotations())); |
| 334 | addGeoLocation(device, payload); |
| 335 | addMetaUi(device.id().toString(), payload); |
| 336 | |
| 337 | String type = (event.type() == DEVICE_ADDED) ? "addDevice" : |
| 338 | ((event.type() == DEVICE_REMOVED) ? "removeDevice" : "updateDevice"); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 339 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // Produces a link event message to the client. |
| 343 | protected ObjectNode linkMessage(LinkEvent event) { |
| 344 | Link link = event.subject(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 345 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 346 | .put("id", compactLinkString(link)) |
| 347 | .put("type", link.type().toString().toLowerCase()) |
| 348 | .put("online", link.state() == Link.State.ACTIVE) |
| 349 | .put("linkWidth", 1.2) |
| 350 | .put("src", link.src().deviceId().toString()) |
| 351 | .put("srcPort", link.src().port().toString()) |
| 352 | .put("dst", link.dst().deviceId().toString()) |
| 353 | .put("dstPort", link.dst().port().toString()); |
| 354 | String type = (event.type() == LINK_ADDED) ? "addLink" : |
| 355 | ((event.type() == LINK_REMOVED) ? "removeLink" : "updateLink"); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 356 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Produces a host event message to the client. |
| 360 | protected ObjectNode hostMessage(HostEvent event) { |
| 361 | Host host = event.subject(); |
| 362 | String hostType = host.annotations().value(AnnotationKeys.TYPE); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 363 | ObjectNode payload = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 364 | .put("id", host.id().toString()) |
| 365 | .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType) |
| 366 | .put("ingress", compactLinkString(edgeLink(host, true))) |
| 367 | .put("egress", compactLinkString(edgeLink(host, false))); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 368 | payload.set("cp", hostConnect(host.location())); |
| 369 | payload.set("labels", labels(ip(host.ipAddresses()), |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 370 | host.mac().toString())); |
| 371 | payload.set("props", props(host.annotations())); |
| 372 | addGeoLocation(host, payload); |
| 373 | addMetaUi(host.id().toString(), payload); |
| 374 | |
| 375 | String type = (event.type() == HOST_ADDED) ? "addHost" : |
| 376 | ((event.type() == HOST_REMOVED) ? "removeHost" : "updateHost"); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 377 | return JsonUtils.envelope(type, 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Encodes the specified host location into a JSON object. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 381 | private ObjectNode hostConnect(HostLocation location) { |
| 382 | return objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 383 | .put("device", location.deviceId().toString()) |
| 384 | .put("port", location.port().toLong()); |
| 385 | } |
| 386 | |
| 387 | // Encodes the specified list of labels a JSON array. |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 388 | private ArrayNode labels(String... labels) { |
| 389 | ArrayNode json = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 390 | for (String label : labels) { |
| 391 | json.add(label); |
| 392 | } |
| 393 | return json; |
| 394 | } |
| 395 | |
| 396 | // Returns the name of the master node for the specified device id. |
| 397 | private String master(DeviceId deviceId) { |
| 398 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 399 | return master != null ? master.toString() : ""; |
| 400 | } |
| 401 | |
| 402 | // Generates an edge link from the specified host location. |
| 403 | private EdgeLink edgeLink(Host host, boolean ingress) { |
| 404 | return new DefaultEdgeLink(PID, new ConnectPoint(host.id(), portNumber(0)), |
| 405 | host.location(), ingress); |
| 406 | } |
| 407 | |
| 408 | // Adds meta UI information for the specified object. |
| 409 | private void addMetaUi(String id, ObjectNode payload) { |
| 410 | ObjectNode meta = metaUi.get(id); |
| 411 | if (meta != null) { |
| 412 | payload.set("metaUi", meta); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // Adds a geo location JSON to the specified payload object. |
| 417 | private void addGeoLocation(Annotated annotated, ObjectNode payload) { |
| 418 | Annotations annotations = annotated.annotations(); |
| 419 | if (annotations == null) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | String slat = annotations.value(AnnotationKeys.LATITUDE); |
| 424 | String slng = annotations.value(AnnotationKeys.LONGITUDE); |
| 425 | try { |
| 426 | if (slat != null && slng != null && !slat.isEmpty() && !slng.isEmpty()) { |
| 427 | double lat = Double.parseDouble(slat); |
| 428 | double lng = Double.parseDouble(slng); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 429 | ObjectNode loc = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 430 | .put("type", "latlng").put("lat", lat).put("lng", lng); |
| 431 | payload.set("location", loc); |
| 432 | } |
| 433 | } catch (NumberFormatException e) { |
| 434 | log.warn("Invalid geo data latitude={}; longiture={}", slat, slng); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Updates meta UI information for the specified object. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 439 | protected void updateMetaUi(ObjectNode payload) { |
| 440 | metaUi.put(JsonUtils.string(payload, "id"), |
| 441 | JsonUtils.node(payload, "memento")); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 444 | // ----------------------------------------------------------------------- |
| 445 | // Create models of the data to return, that overlays can adjust / augment |
| 446 | |
| 447 | // Returns property panel model for summary response. |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 448 | protected PropertyPanel summmaryMessage(long sid) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 449 | Topology topology = topologyService.currentTopology(); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 450 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 451 | return new PropertyPanel("ONOS Summary", "node") |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 452 | .addProp(Properties.DEVICES, topology.deviceCount()) |
| 453 | .addProp(Properties.LINKS, topology.linkCount()) |
| 454 | .addProp(Properties.HOSTS, hostService.getHostCount()) |
| 455 | .addProp(Properties.TOPOLOGY_SSCS, topology.clusterCount()) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 456 | .addSeparator() |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 457 | .addProp(Properties.INTENTS, intentService.getIntentCount()) |
| 458 | .addProp(Properties.TUNNELS, tunnelService.tunnelCount()) |
| 459 | .addProp(Properties.FLOWS, flowService.getFlowRuleCount()) |
| 460 | .addProp(Properties.VERSION, version); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 463 | // Returns property panel model for device details response. |
| 464 | protected PropertyPanel deviceDetails(DeviceId deviceId, long sid) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 465 | Device device = deviceService.getDevice(deviceId); |
| 466 | Annotations annot = device.annotations(); |
| 467 | String name = annot.value(AnnotationKeys.NAME); |
| 468 | int portCount = deviceService.getPorts(deviceId).size(); |
| 469 | int flowCount = getFlowCount(deviceId); |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 470 | int tunnelCount = getTunnelCount(deviceId); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 471 | |
| 472 | String title = isNullOrEmpty(name) ? deviceId.toString() : name; |
| 473 | String typeId = device.type().toString().toLowerCase(); |
| 474 | |
| 475 | PropertyPanel pp = new PropertyPanel(title, typeId) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 476 | .id(deviceId.toString()) |
| 477 | .addProp(Properties.URI, deviceId.toString()) |
| 478 | .addProp(Properties.VENDOR, device.manufacturer()) |
| 479 | .addProp(Properties.HW_VERSION, device.hwVersion()) |
| 480 | .addProp(Properties.SW_VERSION, device.swVersion()) |
| 481 | .addProp(Properties.SERIAL_NUMBER, device.serialNumber()) |
| 482 | .addProp(Properties.PROTOCOL, annot.value(AnnotationKeys.PROTOCOL)) |
| 483 | .addSeparator() |
| 484 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 485 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)) |
| 486 | .addSeparator() |
| 487 | .addProp(Properties.PORTS, portCount) |
| 488 | .addProp(Properties.FLOWS, flowCount) |
| 489 | .addProp(Properties.TUNNELS, tunnelCount); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 490 | |
| 491 | // TODO: add button descriptors |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 492 | |
| 493 | return pp; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | protected int getFlowCount(DeviceId deviceId) { |
| 497 | int count = 0; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 498 | for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 499 | count++; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 500 | } |
| 501 | return count; |
| 502 | } |
| 503 | |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 504 | protected int getTunnelCount(DeviceId deviceId) { |
| 505 | int count = 0; |
| 506 | Collection<Tunnel> tunnels = tunnelService.queryAllTunnels(); |
| 507 | for (Tunnel tunnel : tunnels) { |
| 508 | OpticalTunnelEndPoint src = (OpticalTunnelEndPoint) tunnel.src(); |
| 509 | OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst(); |
| 510 | DeviceId srcDevice = (DeviceId) src.elementId().get(); |
| 511 | DeviceId dstDevice = (DeviceId) dst.elementId().get(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 512 | if (srcDevice.toString().equals(deviceId.toString()) || |
| 513 | dstDevice.toString().equals(deviceId.toString())) { |
cheng fan | 35dc0f2 | 2015-06-10 06:02:47 +0800 | [diff] [blame] | 514 | count++; |
| 515 | } |
| 516 | } |
| 517 | return count; |
| 518 | } |
| 519 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 520 | // Counts all entries that egress on the given device links. |
| 521 | protected Map<Link, Integer> getFlowCounts(DeviceId deviceId) { |
| 522 | List<FlowEntry> entries = new ArrayList<>(); |
| 523 | Set<Link> links = new HashSet<>(linkService.getDeviceEgressLinks(deviceId)); |
| 524 | Set<Host> hosts = hostService.getConnectedHosts(deviceId); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 525 | for (FlowEntry flowEntry : flowService.getFlowEntries(deviceId)) { |
| 526 | entries.add(flowEntry); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Add all edge links to the set |
| 530 | if (hosts != null) { |
| 531 | for (Host host : hosts) { |
| 532 | links.add(new DefaultEdgeLink(host.providerId(), |
| 533 | new ConnectPoint(host.id(), P0), |
| 534 | host.location(), false)); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | Map<Link, Integer> counts = new HashMap<>(); |
| 539 | for (Link link : links) { |
| 540 | counts.put(link, getEgressFlows(link, entries)); |
| 541 | } |
| 542 | return counts; |
| 543 | } |
| 544 | |
| 545 | // Counts all entries that egress on the link source port. |
| 546 | private Integer getEgressFlows(Link link, List<FlowEntry> entries) { |
| 547 | int count = 0; |
| 548 | PortNumber out = link.src().port(); |
| 549 | for (FlowEntry entry : entries) { |
| 550 | TrafficTreatment treatment = entry.treatment(); |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 551 | for (Instruction instruction : treatment.allInstructions()) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 552 | if (instruction.type() == Instruction.Type.OUTPUT && |
| 553 | ((OutputInstruction) instruction).port().equals(out)) { |
| 554 | count++; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | return count; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | // Returns host details response. |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 563 | protected PropertyPanel hostDetails(HostId hostId, long sid) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 564 | Host host = hostService.getHost(hostId); |
| 565 | Annotations annot = host.annotations(); |
| 566 | String type = annot.value(AnnotationKeys.TYPE); |
| 567 | String name = annot.value(AnnotationKeys.NAME); |
| 568 | String vlan = host.vlan().toString(); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 569 | |
| 570 | String title = isNullOrEmpty(name) ? hostId.toString() : name; |
| 571 | String typeId = isNullOrEmpty(type) ? "endstation" : type; |
| 572 | |
| 573 | PropertyPanel pp = new PropertyPanel(title, typeId) |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 574 | .id(hostId.toString()) |
| 575 | .addProp(Properties.MAC, host.mac()) |
| 576 | .addProp(Properties.IP, host.ipAddresses(), "[\\[\\]]") |
| 577 | .addProp(Properties.VLAN, vlan.equals("-1") ? "none" : vlan) |
| 578 | .addSeparator() |
| 579 | .addProp(Properties.LATITUDE, annot.value(AnnotationKeys.LATITUDE)) |
| 580 | .addProp(Properties.LONGITUDE, annot.value(AnnotationKeys.LONGITUDE)); |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 581 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 582 | // TODO: add button descriptors |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 583 | return pp; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 587 | // TODO: migrate to Traffic overlay |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 588 | // Produces JSON message to trigger flow traffic overview visualization |
| 589 | protected ObjectNode trafficSummaryMessage(StatsType type) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 590 | ObjectNode payload = objectNode(); |
| 591 | ArrayNode paths = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 592 | payload.set("paths", paths); |
| 593 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 594 | ObjectNode pathNodeN = objectNode(); |
| 595 | ArrayNode linksNodeN = arrayNode(); |
| 596 | ArrayNode labelsN = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 597 | |
| 598 | pathNodeN.put("class", "plain").put("traffic", false); |
| 599 | pathNodeN.set("links", linksNodeN); |
| 600 | pathNodeN.set("labels", labelsN); |
| 601 | paths.add(pathNodeN); |
| 602 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 603 | ObjectNode pathNodeT = objectNode(); |
| 604 | ArrayNode linksNodeT = arrayNode(); |
| 605 | ArrayNode labelsT = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 606 | |
| 607 | pathNodeT.put("class", "secondary").put("traffic", true); |
| 608 | pathNodeT.set("links", linksNodeT); |
| 609 | pathNodeT.set("labels", labelsT); |
| 610 | paths.add(pathNodeT); |
| 611 | |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 612 | Map<LinkKey, BiLink> biLinks = consolidateLinks(linkService.getLinks()); |
| 613 | addEdgeLinks(biLinks); |
| 614 | for (BiLink link : biLinks.values()) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 615 | boolean bi = link.two != null; |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 616 | if (type == FLOW) { |
| 617 | link.addLoad(getLinkLoad(link.one)); |
| 618 | link.addLoad(bi ? getLinkLoad(link.two) : null); |
| 619 | } else if (type == PORT) { |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 620 | //For a bi-directional traffic links, use |
| 621 | //the max link rate of either direction |
| 622 | link.addLoad(portStatsService.load(link.one.src()), |
| 623 | BPS_THRESHOLD, |
| 624 | portStatsService.load(link.one.dst()), |
| 625 | BPS_THRESHOLD); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 626 | } |
| 627 | if (link.hasTraffic) { |
| 628 | linksNodeT.add(compactLinkString(link.one)); |
| 629 | labelsT.add(type == PORT ? |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 630 | formatBitRate(link.rate) + "ps" : |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 631 | formatBytes(link.bytes)); |
| 632 | } else { |
| 633 | linksNodeN.add(compactLinkString(link.one)); |
| 634 | labelsN.add(""); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 637 | return JsonUtils.envelope("showTraffic", 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 640 | private Load getLinkLoad(Link link) { |
| 641 | if (link.src().elementId() instanceof DeviceId) { |
| 642 | return flowStatsService.load(link); |
| 643 | } |
| 644 | return null; |
| 645 | } |
| 646 | |
| 647 | private void addEdgeLinks(Map<LinkKey, BiLink> biLinks) { |
| 648 | hostService.getHosts().forEach(host -> { |
Thomas Vachuska | 25455e7 | 2015-06-04 11:31:26 -0700 | [diff] [blame] | 649 | addLink(biLinks, createEdgeLink(host, true)); |
| 650 | addLink(biLinks, createEdgeLink(host, false)); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 651 | }); |
| 652 | } |
| 653 | |
| 654 | private Map<LinkKey, BiLink> consolidateLinks(Iterable<Link> links) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 655 | Map<LinkKey, BiLink> biLinks = new HashMap<>(); |
| 656 | for (Link link : links) { |
| 657 | addLink(biLinks, link); |
| 658 | } |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 659 | return biLinks; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // Produces JSON message to trigger flow overview visualization |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 663 | protected ObjectNode flowSummaryMessage(Set<Device> devices) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 664 | ObjectNode payload = objectNode(); |
| 665 | ArrayNode paths = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 666 | payload.set("paths", paths); |
| 667 | |
| 668 | for (Device device : devices) { |
| 669 | Map<Link, Integer> counts = getFlowCounts(device.id()); |
| 670 | for (Link link : counts.keySet()) { |
| 671 | addLinkFlows(link, paths, counts.get(link)); |
| 672 | } |
| 673 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 674 | return JsonUtils.envelope("showTraffic", 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | private void addLinkFlows(Link link, ArrayNode paths, Integer count) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 678 | ObjectNode pathNode = objectNode(); |
| 679 | ArrayNode linksNode = arrayNode(); |
| 680 | ArrayNode labels = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 681 | boolean noFlows = count == null || count == 0; |
| 682 | pathNode.put("class", noFlows ? "secondary" : "primary"); |
| 683 | pathNode.put("traffic", false); |
| 684 | pathNode.set("links", linksNode.add(compactLinkString(link))); |
| 685 | pathNode.set("labels", labels.add(noFlows ? "" : (count.toString() + |
| 686 | (count == 1 ? " flow" : " flows")))); |
| 687 | paths.add(pathNode); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | // Produces JSON message to trigger traffic visualization |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 692 | protected ObjectNode trafficMessage(TrafficClass... trafficClasses) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 693 | ObjectNode payload = objectNode(); |
| 694 | ArrayNode paths = arrayNode(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 695 | payload.set("paths", paths); |
| 696 | |
| 697 | // Classify links based on their traffic traffic first... |
| 698 | Map<LinkKey, BiLink> biLinks = classifyLinkTraffic(trafficClasses); |
| 699 | |
| 700 | // Then separate the links into their respective classes and send them out. |
| 701 | Map<String, ObjectNode> pathNodes = new HashMap<>(); |
| 702 | for (BiLink biLink : biLinks.values()) { |
| 703 | boolean hasTraffic = biLink.hasTraffic; |
Thomas Vachuska | 5d5858b | 2015-03-23 16:31:10 -0700 | [diff] [blame] | 704 | String tc = (biLink.classes() + (hasTraffic ? " animated" : "")).trim(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 705 | ObjectNode pathNode = pathNodes.get(tc); |
| 706 | if (pathNode == null) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 707 | pathNode = objectNode() |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 708 | .put("class", tc).put("traffic", hasTraffic); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 709 | pathNode.set("links", arrayNode()); |
| 710 | pathNode.set("labels", arrayNode()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 711 | pathNodes.put(tc, pathNode); |
| 712 | paths.add(pathNode); |
| 713 | } |
| 714 | ((ArrayNode) pathNode.path("links")).add(compactLinkString(biLink.one)); |
| 715 | ((ArrayNode) pathNode.path("labels")).add(hasTraffic ? formatBytes(biLink.bytes) : ""); |
| 716 | } |
| 717 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 718 | return JsonUtils.envelope("showTraffic", 0, payload); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | // Classifies the link traffic according to the specified classes. |
| 722 | private Map<LinkKey, BiLink> classifyLinkTraffic(TrafficClass... trafficClasses) { |
| 723 | Map<LinkKey, BiLink> biLinks = new HashMap<>(); |
| 724 | for (TrafficClass trafficClass : trafficClasses) { |
| 725 | for (Intent intent : trafficClass.intents) { |
| 726 | boolean isOptical = intent instanceof OpticalConnectivityIntent; |
| 727 | List<Intent> installables = intentService.getInstallableIntents(intent.key()); |
| 728 | if (installables != null) { |
| 729 | for (Intent installable : installables) { |
| 730 | String type = isOptical ? trafficClass.type + " optical" : trafficClass.type; |
| 731 | if (installable instanceof PathIntent) { |
| 732 | classifyLinks(type, biLinks, trafficClass.showTraffic, |
| 733 | ((PathIntent) installable).path().links()); |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 734 | } else if (installable instanceof FlowRuleIntent) { |
| 735 | classifyLinks(type, biLinks, trafficClass.showTraffic, |
| 736 | linkResources(installable)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 737 | } else if (installable instanceof LinkCollectionIntent) { |
| 738 | classifyLinks(type, biLinks, trafficClass.showTraffic, |
| 739 | ((LinkCollectionIntent) installable).links()); |
| 740 | } else if (installable instanceof OpticalPathIntent) { |
| 741 | classifyLinks(type, biLinks, trafficClass.showTraffic, |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 742 | ((OpticalPathIntent) installable).path().links()); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | return biLinks; |
| 749 | } |
| 750 | |
Thomas Vachuska | c0fe09a | 2015-05-21 12:56:22 -0700 | [diff] [blame] | 751 | // Extracts links from the specified flow rule intent resources |
| 752 | private Collection<Link> linkResources(Intent installable) { |
| 753 | ImmutableList.Builder<Link> builder = ImmutableList.builder(); |
| 754 | for (NetworkResource r : installable.resources()) { |
| 755 | if (r instanceof Link) { |
| 756 | builder.add((Link) r); |
| 757 | } |
| 758 | } |
| 759 | return builder.build(); |
| 760 | } |
| 761 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 762 | |
| 763 | // Adds the link segments (path or tree) associated with the specified |
| 764 | // connectivity intent |
| 765 | private void classifyLinks(String type, Map<LinkKey, BiLink> biLinks, |
| 766 | boolean showTraffic, Iterable<Link> links) { |
| 767 | if (links != null) { |
| 768 | for (Link link : links) { |
| 769 | BiLink biLink = addLink(biLinks, link); |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 770 | if (showTraffic) { |
Thomas Vachuska | 25455e7 | 2015-06-04 11:31:26 -0700 | [diff] [blame] | 771 | biLink.addLoad(getLinkLoad(link)); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 772 | } |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 773 | biLink.addClass(type); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 779 | static BiLink addLink(Map<LinkKey, BiLink> biLinks, Link link) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 780 | LinkKey key = canonicalLinkKey(link); |
| 781 | BiLink biLink = biLinks.get(key); |
| 782 | if (biLink != null) { |
| 783 | biLink.setOther(link); |
| 784 | } else { |
| 785 | biLink = new BiLink(key, link); |
| 786 | biLinks.put(key, biLink); |
| 787 | } |
| 788 | return biLink; |
| 789 | } |
| 790 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 791 | // Poor-mans formatting to get the labels with byte counts looking nice. |
| 792 | private String formatBytes(long bytes) { |
| 793 | String unit; |
| 794 | double value; |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 795 | if (bytes > GIGA) { |
| 796 | value = bytes / GIGA; |
| 797 | unit = GBYTES_UNIT; |
| 798 | } else if (bytes > MEGA) { |
| 799 | value = bytes / MEGA; |
| 800 | unit = MBYTES_UNIT; |
| 801 | } else if (bytes > KILO) { |
| 802 | value = bytes / KILO; |
| 803 | unit = KBYTES_UNIT; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 804 | } else { |
| 805 | value = bytes; |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 806 | unit = BYTES_UNIT; |
| 807 | } |
| 808 | DecimalFormat format = new DecimalFormat("#,###.##"); |
| 809 | return format.format(value) + " " + unit; |
| 810 | } |
| 811 | |
Simon Hunt | 80170a7 | 2015-06-10 19:49:57 -0700 | [diff] [blame] | 812 | // Poor-mans formatting to get the labels with bit rate looking nice. |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 813 | private String formatBitRate(long bytes) { |
| 814 | String unit; |
| 815 | double value; |
| 816 | //Convert to bits |
| 817 | long bits = bytes * 8; |
| 818 | if (bits > GIGA) { |
| 819 | value = bits / GIGA; |
| 820 | unit = GBITS_UNIT; |
Simon Hunt | 80170a7 | 2015-06-10 19:49:57 -0700 | [diff] [blame] | 821 | |
| 822 | // NOTE: temporary hack to clip rate at 10.0 Gbps |
| 823 | // Added for the CORD Fabric demo at ONS 2015 |
| 824 | if (value > 10.0) { |
| 825 | value = 10.0; |
| 826 | } |
| 827 | |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 828 | } else if (bits > MEGA) { |
| 829 | value = bits / MEGA; |
| 830 | unit = MBITS_UNIT; |
| 831 | } else if (bits > KILO) { |
| 832 | value = bits / KILO; |
| 833 | unit = KBITS_UNIT; |
| 834 | } else { |
| 835 | value = bits; |
| 836 | unit = BITS_UNIT; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 837 | } |
| 838 | DecimalFormat format = new DecimalFormat("#,###.##"); |
| 839 | return format.format(value) + " " + unit; |
| 840 | } |
| 841 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 842 | // Produces compact string representation of a link. |
| 843 | private static String compactLinkString(Link link) { |
| 844 | return String.format(COMPACT, link.src().elementId(), link.src().port(), |
| 845 | link.dst().elementId(), link.dst().port()); |
| 846 | } |
| 847 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 848 | // translates the property panel into JSON, for returning to the client |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 849 | protected ObjectNode json(PropertyPanel pp) { |
| 850 | ObjectNode result = objectNode() |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 851 | .put("title", pp.title()) |
| 852 | .put("type", pp.typeId()) |
| 853 | .put("id", pp.id()); |
| 854 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 855 | ObjectNode pnode = objectNode(); |
| 856 | ArrayNode porder = arrayNode(); |
| 857 | for (PropertyPanel.Prop p : pp.properties()) { |
| 858 | porder.add(p.key()); |
| 859 | pnode.put(p.key(), p.value()); |
| 860 | } |
| 861 | result.set("propOrder", porder); |
| 862 | result.set("props", pnode); |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 863 | |
| 864 | ArrayNode buttons = arrayNode(); |
| 865 | for (PropertyPanel.Button b : pp.buttons()) { |
| 866 | buttons.add(b.id()); |
| 867 | } |
| 868 | result.set("buttons", buttons); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 869 | return result; |
| 870 | } |
| 871 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 872 | // Produces canonical link key, i.e. one that will match link and its inverse. |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 873 | static LinkKey canonicalLinkKey(Link link) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 874 | String sn = link.src().elementId().toString(); |
| 875 | String dn = link.dst().elementId().toString(); |
| 876 | return sn.compareTo(dn) < 0 ? |
| 877 | linkKey(link.src(), link.dst()) : linkKey(link.dst(), link.src()); |
| 878 | } |
| 879 | |
| 880 | // Representation of link and its inverse and any traffic data. |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 881 | static class BiLink { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 882 | public final LinkKey key; |
| 883 | public final Link one; |
| 884 | public Link two; |
| 885 | public boolean hasTraffic = false; |
| 886 | public long bytes = 0; |
Thomas Vachuska | 5d5858b | 2015-03-23 16:31:10 -0700 | [diff] [blame] | 887 | |
| 888 | private Set<String> classes = new HashSet<>(); |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 889 | private long rate; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 890 | |
| 891 | BiLink(LinkKey key, Link link) { |
| 892 | this.key = key; |
| 893 | this.one = link; |
| 894 | } |
| 895 | |
| 896 | void setOther(Link link) { |
| 897 | this.two = link; |
| 898 | } |
| 899 | |
| 900 | void addLoad(Load load) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 901 | addLoad(load, 0); |
| 902 | } |
| 903 | |
Thomas Vachuska | 204cb6c | 2015-06-04 00:03:06 -0700 | [diff] [blame] | 904 | void addLoad(Load load, double threshold) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 905 | if (load != null) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 906 | this.hasTraffic = hasTraffic || load.rate() > threshold; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 907 | this.bytes += load.latest(); |
Thomas Vachuska | fdbc4c2 | 2015-05-29 15:53:01 -0700 | [diff] [blame] | 908 | this.rate += load.rate(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
Srikanth Vavilapalli | 8c1ccca | 2015-06-08 19:23:24 -0700 | [diff] [blame] | 912 | void addLoad(Load srcLinkLoad, |
| 913 | double srcLinkThreshold, |
| 914 | Load dstLinkLoad, |
| 915 | double dstLinkThreshold) { |
| 916 | //use the max of link load at source or destination |
| 917 | if (srcLinkLoad != null) { |
| 918 | this.hasTraffic = hasTraffic || srcLinkLoad.rate() > srcLinkThreshold; |
| 919 | this.bytes = srcLinkLoad.latest(); |
| 920 | this.rate = srcLinkLoad.rate(); |
| 921 | } |
| 922 | |
| 923 | if (dstLinkLoad != null) { |
| 924 | if (dstLinkLoad.rate() > this.rate) { |
| 925 | this.bytes = dstLinkLoad.latest(); |
| 926 | this.rate = dstLinkLoad.rate(); |
| 927 | this.hasTraffic = hasTraffic || dstLinkLoad.rate() > dstLinkThreshold; |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 932 | void addClass(String trafficClass) { |
Thomas Vachuska | 5d5858b | 2015-03-23 16:31:10 -0700 | [diff] [blame] | 933 | classes.add(trafficClass); |
| 934 | } |
| 935 | |
| 936 | String classes() { |
| 937 | StringBuilder sb = new StringBuilder(); |
| 938 | classes.forEach(c -> sb.append(c).append(" ")); |
| 939 | return sb.toString().trim(); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 943 | |
Simon Hunt | 93735af | 2015-07-28 12:10:30 -0700 | [diff] [blame] | 944 | // TODO: move this to traffic overlay component |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 945 | // Auxiliary carrier of data for requesting traffic message. |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 946 | static class TrafficClass { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 947 | public final boolean showTraffic; |
| 948 | public final String type; |
| 949 | public final Iterable<Intent> intents; |
| 950 | |
| 951 | TrafficClass(String type, Iterable<Intent> intents) { |
| 952 | this(type, intents, false); |
| 953 | } |
| 954 | |
| 955 | TrafficClass(String type, Iterable<Intent> intents, boolean showTraffic) { |
| 956 | this.type = type; |
| 957 | this.intents = intents; |
| 958 | this.showTraffic = showTraffic; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | } |