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