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