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