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