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