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