Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -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 | */ |
| 17 | |
| 18 | package org.onosproject.ui.impl; |
| 19 | |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 23 | import org.onosproject.net.ElementId; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Host; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 25 | import org.onosproject.net.HostId; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Link; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.FlowEntry; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.flow.instructions.Instruction; |
| 31 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 32 | import org.onosproject.net.intent.FlowRuleIntent; |
| 33 | import org.onosproject.net.intent.Intent; |
| 34 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 35 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 36 | import org.onosproject.net.intent.OpticalPathIntent; |
| 37 | import org.onosproject.net.intent.PathIntent; |
| 38 | import org.onosproject.net.statistic.Load; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.impl.topo.IntentSelection; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.impl.topo.ServicesBundle; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 41 | import org.onosproject.ui.impl.topo.TopoIntentFilter; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 42 | import org.onosproject.ui.impl.topo.TrafficLink; |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 43 | import org.onosproject.ui.impl.topo.TrafficLink.StatsType; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 44 | import org.onosproject.ui.impl.topo.TrafficLinkMap; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 45 | import org.onosproject.ui.topo.DeviceHighlight; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 46 | import org.onosproject.ui.topo.Highlights; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 47 | import org.onosproject.ui.topo.Highlights.Amount; |
| 48 | import org.onosproject.ui.topo.HostHighlight; |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 49 | import org.onosproject.ui.topo.LinkHighlight.Flavor; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 50 | import org.onosproject.ui.topo.NodeHighlight; |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 51 | import org.onosproject.ui.topo.NodeSelection; |
| 52 | import org.onosproject.ui.topo.TopoUtils; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
| 55 | |
| 56 | import java.util.ArrayList; |
| 57 | import java.util.Collection; |
| 58 | import java.util.Collections; |
| 59 | import java.util.HashMap; |
| 60 | import java.util.HashSet; |
| 61 | import java.util.List; |
| 62 | import java.util.Map; |
| 63 | import java.util.Set; |
| 64 | import java.util.Timer; |
| 65 | import java.util.TimerTask; |
| 66 | |
| 67 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 68 | import static org.onosproject.ui.impl.TrafficMonitor.Mode.*; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Encapsulates the behavior of monitoring specific traffic patterns. |
| 72 | */ |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 73 | public class TrafficMonitor { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 74 | |
| 75 | // 4 Kilo Bytes as threshold |
| 76 | private static final double BPS_THRESHOLD = 4 * TopoUtils.KILO; |
| 77 | |
| 78 | private static final Logger log = |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 79 | LoggerFactory.getLogger(TrafficMonitor.class); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Designates the different modes of operation. |
| 83 | */ |
| 84 | public enum Mode { |
| 85 | IDLE, |
| 86 | ALL_FLOW_TRAFFIC, |
| 87 | ALL_PORT_TRAFFIC, |
| 88 | DEV_LINK_FLOWS, |
| 89 | RELATED_INTENTS, |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 90 | SELECTED_INTENT |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private final long trafficPeriod; |
| 94 | private final ServicesBundle servicesBundle; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 95 | private final TopologyViewMessageHandler msgHandler; |
| 96 | private final TopoIntentFilter intentFilter; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 97 | |
| 98 | private final Timer timer = new Timer("topo-traffic"); |
| 99 | |
| 100 | private TimerTask trafficTask = null; |
| 101 | private Mode mode = IDLE; |
| 102 | private NodeSelection selectedNodes = null; |
| 103 | private IntentSelection selectedIntents = null; |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Constructs a traffic monitor. |
| 108 | * |
| 109 | * @param trafficPeriod traffic task period in ms |
| 110 | * @param servicesBundle bundle of services |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 111 | * @param msgHandler our message handler |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 112 | */ |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 113 | public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle, |
| 114 | TopologyViewMessageHandler msgHandler) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 115 | this.trafficPeriod = trafficPeriod; |
| 116 | this.servicesBundle = servicesBundle; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 117 | this.msgHandler = msgHandler; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 118 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 119 | intentFilter = new TopoIntentFilter(servicesBundle); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // ======================================================================= |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 123 | // === API === |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 124 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Monitor for traffic data to be sent back to the web client, under |
| 127 | * the given mode. This causes a background traffic task to be |
| 128 | * scheduled to repeatedly compute and transmit the appropriate traffic |
| 129 | * data to the client. |
| 130 | * <p> |
| 131 | * The monitoring mode is expected to be one of: |
| 132 | * <ul> |
| 133 | * <li>ALL_FLOW_TRAFFIC</li> |
| 134 | * <li>ALL_PORT_TRAFFIC</li> |
| 135 | * <li>SELECTED_INTENT</li> |
| 136 | * </ul> |
| 137 | * |
| 138 | * @param mode monitoring mode |
| 139 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 140 | public synchronized void monitor(Mode mode) { |
| 141 | log.debug("monitor: {}", mode); |
| 142 | this.mode = mode; |
| 143 | |
| 144 | switch (mode) { |
| 145 | case ALL_FLOW_TRAFFIC: |
| 146 | clearSelection(); |
| 147 | scheduleTask(); |
| 148 | sendAllFlowTraffic(); |
| 149 | break; |
| 150 | |
| 151 | case ALL_PORT_TRAFFIC: |
| 152 | clearSelection(); |
| 153 | scheduleTask(); |
| 154 | sendAllPortTraffic(); |
| 155 | break; |
| 156 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 157 | case SELECTED_INTENT: |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 158 | scheduleTask(); |
| 159 | sendSelectedIntentTraffic(); |
| 160 | break; |
| 161 | |
| 162 | default: |
| 163 | log.debug("Unexpected call to monitor({})", mode); |
| 164 | clearAll(); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Monitor for traffic data to be sent back to the web client, under |
| 171 | * the given mode, using the given selection of devices and hosts. |
| 172 | * In the case of "device link flows", this causes a background traffic |
| 173 | * task to be scheduled to repeatedly compute and transmit the appropriate |
| 174 | * traffic data to the client. In the case of "related intents", no |
| 175 | * repeating task is scheduled. |
| 176 | * <p> |
| 177 | * The monitoring mode is expected to be one of: |
| 178 | * <ul> |
| 179 | * <li>DEV_LINK_FLOWS</li> |
| 180 | * <li>RELATED_INTENTS</li> |
| 181 | * </ul> |
| 182 | * |
| 183 | * @param mode monitoring mode |
| 184 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 185 | public synchronized void monitor(Mode mode, NodeSelection nodeSelection) { |
| 186 | log.debug("monitor: {} -- {}", mode, nodeSelection); |
| 187 | this.mode = mode; |
| 188 | this.selectedNodes = nodeSelection; |
| 189 | |
| 190 | switch (mode) { |
| 191 | case DEV_LINK_FLOWS: |
| 192 | // only care about devices (not hosts) |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 193 | if (selectedNodes.devicesWithHover().isEmpty()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 194 | sendClearAll(); |
| 195 | } else { |
| 196 | scheduleTask(); |
| 197 | sendDeviceLinkFlows(); |
| 198 | } |
| 199 | break; |
| 200 | |
| 201 | case RELATED_INTENTS: |
| 202 | if (selectedNodes.none()) { |
| 203 | sendClearAll(); |
| 204 | } else { |
| 205 | selectedIntents = new IntentSelection(selectedNodes, intentFilter); |
| 206 | if (selectedIntents.none()) { |
| 207 | sendClearAll(); |
| 208 | } else { |
| 209 | sendSelectedIntents(); |
| 210 | } |
| 211 | } |
| 212 | break; |
| 213 | |
| 214 | default: |
| 215 | log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection); |
| 216 | clearAll(); |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 221 | // TODO: move this out to the "h2h/multi-intent app" |
| 222 | /** |
| 223 | * Monitor for traffic data to be sent back to the web client, for the |
| 224 | * given intent. |
| 225 | * |
| 226 | * @param intent the intent to monitor |
| 227 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 228 | public synchronized void monitor(Intent intent) { |
| 229 | log.debug("monitor intent: {}", intent.id()); |
| 230 | selectedNodes = null; |
| 231 | selectedIntents = new IntentSelection(intent); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 232 | mode = SELECTED_INTENT; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 233 | scheduleTask(); |
| 234 | sendSelectedIntentTraffic(); |
| 235 | } |
| 236 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 237 | /** |
| 238 | * Selects the next intent in the select group (if there is one), |
| 239 | * and sends highlighting data back to the web client to display |
| 240 | * which path is selected. |
| 241 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 242 | public synchronized void selectNextIntent() { |
| 243 | if (selectedIntents != null) { |
| 244 | selectedIntents.next(); |
| 245 | sendSelectedIntents(); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 246 | if (mode == SELECTED_INTENT) { |
| 247 | mode = RELATED_INTENTS; |
| 248 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 252 | /** |
| 253 | * Selects the previous intent in the select group (if there is one), |
| 254 | * and sends highlighting data back to the web client to display |
| 255 | * which path is selected. |
| 256 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 257 | public synchronized void selectPreviousIntent() { |
| 258 | if (selectedIntents != null) { |
| 259 | selectedIntents.prev(); |
| 260 | sendSelectedIntents(); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 261 | if (mode == SELECTED_INTENT) { |
| 262 | mode = RELATED_INTENTS; |
| 263 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Resends selected intent traffic data. This is called, for example, |
| 269 | * when the system detects an intent update happened. |
| 270 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 271 | public synchronized void pokeIntent() { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 272 | if (mode == SELECTED_INTENT) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 273 | sendSelectedIntentTraffic(); |
| 274 | } |
| 275 | } |
| 276 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Stop all traffic monitoring. |
| 279 | */ |
| 280 | public synchronized void stopMonitoring() { |
| 281 | log.debug("STOP monitoring"); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 282 | if (mode != IDLE) { |
| 283 | sendClearAll(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | |
| 288 | // ======================================================================= |
| 289 | // === Helper methods === |
| 290 | |
| 291 | private void sendClearAll() { |
| 292 | clearAll(); |
| 293 | sendClearHighlights(); |
| 294 | } |
| 295 | |
| 296 | private void clearAll() { |
| 297 | this.mode = IDLE; |
| 298 | clearSelection(); |
| 299 | cancelTask(); |
| 300 | } |
| 301 | |
| 302 | private void clearSelection() { |
| 303 | selectedNodes = null; |
| 304 | selectedIntents = null; |
| 305 | } |
| 306 | |
| 307 | private synchronized void scheduleTask() { |
| 308 | if (trafficTask == null) { |
| 309 | log.debug("Starting up background traffic task..."); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 310 | trafficTask = new TrafficUpdateTask(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 311 | timer.schedule(trafficTask, trafficPeriod, trafficPeriod); |
| 312 | } else { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 313 | log.debug("(traffic task already running)"); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private synchronized void cancelTask() { |
| 318 | if (trafficTask != null) { |
| 319 | trafficTask.cancel(); |
| 320 | trafficTask = null; |
| 321 | } |
| 322 | } |
| 323 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 324 | private void sendAllFlowTraffic() { |
| 325 | log.debug("sendAllFlowTraffic"); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 326 | msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | private void sendAllPortTraffic() { |
| 330 | log.debug("sendAllPortTraffic"); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 331 | msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | private void sendDeviceLinkFlows() { |
| 335 | log.debug("sendDeviceLinkFlows: {}", selectedNodes); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 336 | msgHandler.sendHighlights(deviceLinkFlows()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | private void sendSelectedIntents() { |
| 340 | log.debug("sendSelectedIntents: {}", selectedIntents); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 341 | msgHandler.sendHighlights(intentGroup()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | private void sendSelectedIntentTraffic() { |
| 345 | log.debug("sendSelectedIntentTraffic: {}", selectedIntents); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 346 | msgHandler.sendHighlights(intentTraffic()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private void sendClearHighlights() { |
| 350 | log.debug("sendClearHighlights"); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 351 | msgHandler.sendHighlights(new Highlights()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 354 | // ======================================================================= |
| 355 | // === Generate messages in JSON object node format |
| 356 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 357 | private Highlights trafficSummary(StatsType type) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 358 | Highlights highlights = new Highlights(); |
| 359 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 360 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 361 | compileLinks(linkMap); |
| 362 | addEdgeLinks(linkMap); |
| 363 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 364 | for (TrafficLink tlink : linkMap.biLinks()) { |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 365 | if (type == StatsType.FLOW_STATS) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 366 | attachFlowLoad(tlink); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 367 | } else if (type == StatsType.PORT_STATS) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 368 | attachPortLoad(tlink); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // we only want to report on links deemed to have traffic |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 372 | if (tlink.hasTraffic()) { |
| 373 | highlights.add(tlink.highlight(type)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | return highlights; |
| 377 | } |
| 378 | |
| 379 | // create highlights for links, showing flows for selected devices. |
| 380 | private Highlights deviceLinkFlows() { |
| 381 | Highlights highlights = new Highlights(); |
| 382 | |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 383 | if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 384 | // capture flow counts on bilinks |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 385 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 386 | |
Simon Hunt | 7229721 | 2015-08-25 10:15:33 -0700 | [diff] [blame] | 387 | for (Device device : selectedNodes.devicesWithHover()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 388 | Map<Link, Integer> counts = getLinkFlowCounts(device.id()); |
| 389 | for (Link link : counts.keySet()) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 390 | TrafficLink tlink = linkMap.add(link); |
| 391 | tlink.addFlows(counts.get(link)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | // now report on our collated links |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 396 | for (TrafficLink tlink : linkMap.biLinks()) { |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 397 | highlights.add(tlink.highlight(StatsType.FLOW_COUNT)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | } |
| 401 | return highlights; |
| 402 | } |
| 403 | |
| 404 | private Highlights intentGroup() { |
| 405 | Highlights highlights = new Highlights(); |
| 406 | |
| 407 | if (selectedIntents != null && !selectedIntents.none()) { |
| 408 | // If 'all' intents are selected, they will all have primary |
| 409 | // highlighting; otherwise, the specifically selected intent will |
| 410 | // have primary highlighting, and the remainder will have secondary |
| 411 | // highlighting. |
| 412 | Set<Intent> primary; |
| 413 | Set<Intent> secondary; |
| 414 | int count = selectedIntents.size(); |
| 415 | |
| 416 | Set<Intent> allBut = new HashSet<>(selectedIntents.intents()); |
| 417 | Intent current; |
| 418 | |
| 419 | if (selectedIntents.all()) { |
| 420 | primary = allBut; |
| 421 | secondary = Collections.emptySet(); |
| 422 | log.debug("Highlight all intents ({})", count); |
| 423 | } else { |
| 424 | current = selectedIntents.current(); |
| 425 | primary = new HashSet<>(); |
| 426 | primary.add(current); |
| 427 | allBut.remove(current); |
| 428 | secondary = allBut; |
| 429 | log.debug("Highlight intent: {} ([{}] of {})", |
| 430 | current.id(), selectedIntents.index(), count); |
| 431 | } |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 432 | |
| 433 | highlightIntentLinks(highlights, primary, secondary); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 434 | } |
| 435 | return highlights; |
| 436 | } |
| 437 | |
| 438 | private Highlights intentTraffic() { |
| 439 | Highlights highlights = new Highlights(); |
| 440 | |
| 441 | if (selectedIntents != null && selectedIntents.single()) { |
| 442 | Intent current = selectedIntents.current(); |
| 443 | Set<Intent> primary = new HashSet<>(); |
| 444 | primary.add(current); |
| 445 | log.debug("Highlight traffic for intent: {} ([{}] of {})", |
| 446 | current.id(), selectedIntents.index(), selectedIntents.size()); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 447 | |
| 448 | highlightIntentLinksWithTraffic(highlights, primary); |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 449 | highlights.subdueAllElse(Amount.MINIMALLY); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 450 | } |
| 451 | return highlights; |
| 452 | } |
| 453 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 454 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 455 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 456 | private void compileLinks(TrafficLinkMap linkMap) { |
| 457 | servicesBundle.linkService().getLinks().forEach(linkMap::add); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 460 | private void addEdgeLinks(TrafficLinkMap linkMap) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 461 | servicesBundle.hostService().getHosts().forEach(host -> { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 462 | linkMap.add(createEdgeLink(host, true)); |
| 463 | linkMap.add(createEdgeLink(host, false)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 464 | }); |
| 465 | } |
| 466 | |
| 467 | private Load getLinkFlowLoad(Link link) { |
| 468 | if (link != null && link.src().elementId() instanceof DeviceId) { |
| 469 | return servicesBundle.flowStatsService().load(link); |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 474 | private void attachFlowLoad(TrafficLink link) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 475 | link.addLoad(getLinkFlowLoad(link.one())); |
| 476 | link.addLoad(getLinkFlowLoad(link.two())); |
| 477 | } |
| 478 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 479 | private void attachPortLoad(TrafficLink link) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 480 | // For bi-directional traffic links, use |
| 481 | // the max link rate of either direction |
| 482 | // (we choose 'one' since we know that is never null) |
| 483 | Link one = link.one(); |
| 484 | Load egressSrc = servicesBundle.portStatsService().load(one.src()); |
| 485 | Load egressDst = servicesBundle.portStatsService().load(one.dst()); |
Simon Hunt | 5f31a02 | 2015-08-20 08:43:25 -0700 | [diff] [blame] | 486 | link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 487 | // link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!! |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | private Load maxLoad(Load a, Load b) { |
| 491 | if (a == null) { |
| 492 | return b; |
| 493 | } |
| 494 | if (b == null) { |
| 495 | return a; |
| 496 | } |
| 497 | return a.rate() > b.rate() ? a : b; |
| 498 | } |
| 499 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 500 | // Counts all flow entries that egress on the links of the given device. |
| 501 | private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) { |
| 502 | // get the flows for the device |
| 503 | List<FlowEntry> entries = new ArrayList<>(); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 504 | for (FlowEntry flowEntry : servicesBundle.flowService() |
| 505 | .getFlowEntries(deviceId)) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 506 | entries.add(flowEntry); |
| 507 | } |
| 508 | |
| 509 | // get egress links from device, and include edge links |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 510 | Set<Link> links = new HashSet<>(servicesBundle.linkService() |
| 511 | .getDeviceEgressLinks(deviceId)); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 512 | Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId); |
| 513 | if (hosts != null) { |
| 514 | for (Host host : hosts) { |
| 515 | links.add(createEdgeLink(host, false)); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // compile flow counts per link |
| 520 | Map<Link, Integer> counts = new HashMap<>(); |
| 521 | for (Link link : links) { |
| 522 | counts.put(link, getEgressFlows(link, entries)); |
| 523 | } |
| 524 | return counts; |
| 525 | } |
| 526 | |
| 527 | // Counts all entries that egress on the link source port. |
| 528 | private int getEgressFlows(Link link, List<FlowEntry> entries) { |
| 529 | int count = 0; |
| 530 | PortNumber out = link.src().port(); |
| 531 | for (FlowEntry entry : entries) { |
| 532 | TrafficTreatment treatment = entry.treatment(); |
| 533 | for (Instruction instruction : treatment.allInstructions()) { |
| 534 | if (instruction.type() == Instruction.Type.OUTPUT && |
| 535 | ((OutputInstruction) instruction).port().equals(out)) { |
| 536 | count++; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | return count; |
| 541 | } |
| 542 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 543 | private void highlightIntentLinks(Highlights highlights, |
| 544 | Set<Intent> primary, Set<Intent> secondary) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 545 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 546 | // NOTE: highlight secondary first, then primary, so that links shared |
| 547 | // by intents are colored correctly ("last man wins") |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 548 | createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false); |
| 549 | createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 550 | colorLinks(highlights, linkMap); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 553 | private void highlightIntentLinksWithTraffic(Highlights highlights, |
| 554 | Set<Intent> primary) { |
| 555 | TrafficLinkMap linkMap = new TrafficLinkMap(); |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 556 | createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true); |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 557 | colorLinks(highlights, linkMap); |
| 558 | } |
| 559 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 560 | private void createTrafficLinks(Highlights highlights, |
| 561 | TrafficLinkMap linkMap, Set<Intent> intents, |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 562 | Flavor flavor, boolean showTraffic) { |
| 563 | for (Intent intent : intents) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 564 | List<Intent> installables = servicesBundle.intentService() |
| 565 | .getInstallableIntents(intent.key()); |
| 566 | Iterable<Link> links = null; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 567 | if (installables != null) { |
| 568 | for (Intent installable : installables) { |
| 569 | |
| 570 | if (installable instanceof PathIntent) { |
| 571 | links = ((PathIntent) installable).path().links(); |
| 572 | } else if (installable instanceof FlowRuleIntent) { |
| 573 | links = linkResources(installable); |
| 574 | } else if (installable instanceof LinkCollectionIntent) { |
| 575 | links = ((LinkCollectionIntent) installable).links(); |
| 576 | } else if (installable instanceof OpticalPathIntent) { |
| 577 | links = ((OpticalPathIntent) installable).path().links(); |
| 578 | } |
| 579 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 580 | boolean isOptical = intent instanceof OpticalConnectivityIntent; |
| 581 | processLinks(linkMap, links, flavor, isOptical, showTraffic); |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 582 | updateHighlights(highlights, links); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 588 | private void updateHighlights(Highlights highlights, Iterable<Link> links) { |
| 589 | for (Link link : links) { |
| 590 | ensureNodePresent(highlights, link.src().elementId()); |
| 591 | ensureNodePresent(highlights, link.dst().elementId()); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | private void ensureNodePresent(Highlights highlights, ElementId eid) { |
| 596 | String id = eid.toString(); |
| 597 | NodeHighlight nh = highlights.getNode(id); |
| 598 | if (nh == null) { |
| 599 | if (eid instanceof DeviceId) { |
| 600 | nh = new DeviceHighlight(id); |
| 601 | highlights.add((DeviceHighlight) nh); |
| 602 | } else if (eid instanceof HostId) { |
| 603 | nh = new HostHighlight(id); |
| 604 | highlights.add((HostHighlight) nh); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 609 | // Extracts links from the specified flow rule intent resources |
| 610 | private Collection<Link> linkResources(Intent installable) { |
| 611 | ImmutableList.Builder<Link> builder = ImmutableList.builder(); |
| 612 | installable.resources().stream().filter(r -> r instanceof Link) |
| 613 | .forEach(r -> builder.add((Link) r)); |
| 614 | return builder.build(); |
| 615 | } |
| 616 | |
Simon Hunt | 5783017 | 2015-08-26 13:25:17 -0700 | [diff] [blame] | 617 | private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links, |
| 618 | Flavor flavor, boolean isOptical, |
| 619 | boolean showTraffic) { |
| 620 | if (links != null) { |
| 621 | for (Link link : links) { |
| 622 | TrafficLink tlink = linkMap.add(link); |
| 623 | tlink.tagFlavor(flavor); |
| 624 | tlink.optical(isOptical); |
| 625 | if (showTraffic) { |
| 626 | tlink.addLoad(getLinkFlowLoad(link)); |
| 627 | tlink.antMarch(true); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) { |
| 634 | for (TrafficLink tlink : linkMap.biLinks()) { |
| 635 | highlights.add(tlink.highlight(StatsType.TAGGED)); |
| 636 | } |
| 637 | } |
| 638 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 639 | // ======================================================================= |
| 640 | // === Background Task |
| 641 | |
| 642 | // Provides periodic update of traffic information to the client |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 643 | private class TrafficUpdateTask extends TimerTask { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 644 | @Override |
| 645 | public void run() { |
| 646 | try { |
| 647 | switch (mode) { |
| 648 | case ALL_FLOW_TRAFFIC: |
| 649 | sendAllFlowTraffic(); |
| 650 | break; |
| 651 | case ALL_PORT_TRAFFIC: |
| 652 | sendAllPortTraffic(); |
| 653 | break; |
| 654 | case DEV_LINK_FLOWS: |
| 655 | sendDeviceLinkFlows(); |
| 656 | break; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 657 | case SELECTED_INTENT: |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 658 | sendSelectedIntentTraffic(); |
| 659 | break; |
| 660 | |
| 661 | default: |
| 662 | // RELATED_INTENTS and IDLE modes should never invoke |
| 663 | // the background task, but if they do, they have |
| 664 | // nothing to do |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | } catch (Exception e) { |
| 669 | log.warn("Unable to process traffic task due to {}", e.getMessage()); |
| 670 | log.warn("Boom!", e); |
| 671 | } |
| 672 | } |
| 673 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 674 | } |