blob: b2ba156f81636f024109beae6b546c820c2b209b [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunta17fa672015-08-19 18:42:22 -07003 *
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
18package org.onosproject.ui.impl;
19
20import com.google.common.collect.ImmutableList;
21import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
Simon Hunt94f7dae2015-08-26 17:40:59 -070023import org.onosproject.net.ElementId;
Simon Hunta17fa672015-08-19 18:42:22 -070024import org.onosproject.net.Host;
Simon Hunt94f7dae2015-08-26 17:40:59 -070025import org.onosproject.net.HostId;
Simon Hunta17fa672015-08-19 18:42:22 -070026import org.onosproject.net.Link;
Simon Hunta17fa672015-08-19 18:42:22 -070027import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.FlowEntry;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.flow.instructions.Instruction;
31import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Thomas Vachuskada0665b2016-03-02 19:06:17 -080032import org.onosproject.net.intent.FlowObjectiveIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070033import org.onosproject.net.intent.FlowRuleIntent;
Simon Hunt5328f792017-01-11 17:43:31 -080034import org.onosproject.net.intent.HostToHostIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070035import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.LinkCollectionIntent;
37import org.onosproject.net.intent.OpticalConnectivityIntent;
38import org.onosproject.net.intent.OpticalPathIntent;
39import org.onosproject.net.intent.PathIntent;
40import org.onosproject.net.statistic.Load;
Simon Hunted804d52016-03-30 09:51:40 -070041import org.onosproject.ui.impl.topo.util.IntentSelection;
42import org.onosproject.ui.impl.topo.util.ServicesBundle;
43import org.onosproject.ui.impl.topo.util.TopoIntentFilter;
44import org.onosproject.ui.impl.topo.util.TrafficLink;
45import org.onosproject.ui.impl.topo.util.TrafficLink.StatsType;
46import org.onosproject.ui.impl.topo.util.TrafficLinkMap;
Simon Hunt94f7dae2015-08-26 17:40:59 -070047import org.onosproject.ui.topo.DeviceHighlight;
Simon Hunta17fa672015-08-19 18:42:22 -070048import org.onosproject.ui.topo.Highlights;
Simon Hunt94f7dae2015-08-26 17:40:59 -070049import org.onosproject.ui.topo.Highlights.Amount;
50import org.onosproject.ui.topo.HostHighlight;
Simon Hunt57830172015-08-26 13:25:17 -070051import org.onosproject.ui.topo.LinkHighlight.Flavor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070052import org.onosproject.ui.topo.NodeHighlight;
Simon Hunt743a8492015-08-25 16:18:19 -070053import org.onosproject.ui.topo.NodeSelection;
54import org.onosproject.ui.topo.TopoUtils;
Simon Hunta17fa672015-08-19 18:42:22 -070055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58import java.util.ArrayList;
59import java.util.Collection;
60import java.util.Collections;
61import java.util.HashMap;
62import java.util.HashSet;
63import java.util.List;
64import java.util.Map;
65import java.util.Set;
66import java.util.Timer;
67import java.util.TimerTask;
68
69import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunted804d52016-03-30 09:51:40 -070070import static org.onosproject.ui.impl.TrafficMonitor.Mode.IDLE;
71import static org.onosproject.ui.impl.TrafficMonitor.Mode.RELATED_INTENTS;
72import static org.onosproject.ui.impl.TrafficMonitor.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070073
74/**
75 * Encapsulates the behavior of monitoring specific traffic patterns.
76 */
Simon Hunt4fc86852015-08-20 17:57:52 -070077public class TrafficMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070078
79 // 4 Kilo Bytes as threshold
80 private static final double BPS_THRESHOLD = 4 * TopoUtils.KILO;
81
82 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070083 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070084
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 Hunt4fc86852015-08-20 17:57:52 -070094 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -070095 }
96
97 private final long trafficPeriod;
98 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070099 private final TopologyViewMessageHandler msgHandler;
100 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -0700101
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 Hunt5328f792017-01-11 17:43:31 -0800113 * @param trafficPeriod traffic task period in ms
114 * @param servicesBundle bundle of services
115 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700116 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700117 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
118 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700119 this.trafficPeriod = trafficPeriod;
120 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700121 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700122
Simon Hunt4fc86852015-08-20 17:57:52 -0700123 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700124 }
125
126 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700127 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700128
Simon Hunt4fc86852015-08-20 17:57:52 -0700129 /**
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 Hunt5328f792017-01-11 17:43:31 -0800137 * <li>ALL_FLOW_TRAFFIC</li>
138 * <li>ALL_PORT_TRAFFIC</li>
139 * <li>SELECTED_INTENT</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700140 * </ul>
141 *
142 * @param mode monitoring mode
143 */
Simon Hunta17fa672015-08-19 18:42:22 -0700144 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 Hunt4fc86852015-08-20 17:57:52 -0700161 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700162 scheduleTask();
163 sendSelectedIntentTraffic();
164 break;
165
166 default:
167 log.debug("Unexpected call to monitor({})", mode);
168 clearAll();
169 break;
170 }
171 }
172
Simon Hunt4fc86852015-08-20 17:57:52 -0700173 /**
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 Hunt5328f792017-01-11 17:43:31 -0800183 * <li>DEV_LINK_FLOWS</li>
184 * <li>RELATED_INTENTS</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700185 * </ul>
186 *
Simon Hunt5328f792017-01-11 17:43:31 -0800187 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700188 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700189 */
Simon Hunta17fa672015-08-19 18:42:22 -0700190 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 Hunt72297212015-08-25 10:15:33 -0700198 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700199 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 Hunt4fc86852015-08-20 17:57:52 -0700226 // TODO: move this out to the "h2h/multi-intent app"
Simon Hunt5328f792017-01-11 17:43:31 -0800227
Simon Hunt4fc86852015-08-20 17:57:52 -0700228 /**
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 Hunta17fa672015-08-19 18:42:22 -0700234 public synchronized void monitor(Intent intent) {
235 log.debug("monitor intent: {}", intent.id());
236 selectedNodes = null;
237 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700238 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700239 scheduleTask();
240 sendSelectedIntentTraffic();
241 }
242
Simon Hunt4fc86852015-08-20 17:57:52 -0700243 /**
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 Hunta17fa672015-08-19 18:42:22 -0700248 public synchronized void selectNextIntent() {
249 if (selectedIntents != null) {
250 selectedIntents.next();
251 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700252 if (mode == SELECTED_INTENT) {
253 mode = RELATED_INTENTS;
254 }
Simon Hunta17fa672015-08-19 18:42:22 -0700255 }
256 }
257
Simon Hunt4fc86852015-08-20 17:57:52 -0700258 /**
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 Hunta17fa672015-08-19 18:42:22 -0700263 public synchronized void selectPreviousIntent() {
264 if (selectedIntents != null) {
265 selectedIntents.prev();
266 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700267 if (mode == SELECTED_INTENT) {
268 mode = RELATED_INTENTS;
269 }
Simon Hunta17fa672015-08-19 18:42:22 -0700270 }
271 }
272
Simon Hunt4fc86852015-08-20 17:57:52 -0700273 /**
274 * Resends selected intent traffic data. This is called, for example,
275 * when the system detects an intent update happened.
276 */
Simon Hunta17fa672015-08-19 18:42:22 -0700277 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700278 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700279 sendSelectedIntentTraffic();
280 }
281 }
282
Simon Hunt4fc86852015-08-20 17:57:52 -0700283 /**
284 * Stop all traffic monitoring.
285 */
286 public synchronized void stopMonitoring() {
287 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700288 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 Hunt5328f792017-01-11 17:43:31 -0800313 private synchronized void scheduleTask() {
Simon Hunta17fa672015-08-19 18:42:22 -0700314 if (trafficTask == null) {
315 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700316 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700317 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
318 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700319 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 Hunta17fa672015-08-19 18:42:22 -0700330 private void sendAllFlowTraffic() {
331 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700332 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700333 }
334
335 private void sendAllPortTraffic() {
336 log.debug("sendAllPortTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700337 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700338 }
339
340 private void sendDeviceLinkFlows() {
341 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700342 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700343 }
344
345 private void sendSelectedIntents() {
346 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700347 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700348 }
349
350 private void sendSelectedIntentTraffic() {
351 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700352 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700353 }
354
355 private void sendClearHighlights() {
356 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700357 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700358 }
359
Simon Hunta17fa672015-08-19 18:42:22 -0700360 // =======================================================================
361 // === Generate messages in JSON object node format
362
Simon Hunt57830172015-08-26 13:25:17 -0700363 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700364 Highlights highlights = new Highlights();
365
Simon Hunt4fc86852015-08-20 17:57:52 -0700366 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700367 compileLinks(linkMap);
368 addEdgeLinks(linkMap);
369
Simon Hunt4fc86852015-08-20 17:57:52 -0700370 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700371 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700372 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700373 } else if (type == StatsType.PORT_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700374 attachPortLoad(tlink);
Simon Hunta17fa672015-08-19 18:42:22 -0700375 }
376
377 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700378 if (tlink.hasTraffic()) {
379 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700380 }
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 Hunt72297212015-08-25 10:15:33 -0700389 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700390 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700391 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700392
Simon Hunt72297212015-08-25 10:15:33 -0700393 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700394 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
395 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700396 TrafficLink tlink = linkMap.add(link);
397 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700398 }
399 }
400
401 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700402 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700403 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700404 }
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 Hunt5328f792017-01-11 17:43:31 -0800436 current.id(), selectedIntents.index(), count);
Simon Hunta17fa672015-08-19 18:42:22 -0700437 }
Simon Hunt57830172015-08-26 13:25:17 -0700438
439 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700440 }
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 Hunt5328f792017-01-11 17:43:31 -0800452 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700453
454 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700455 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700456 }
457 return highlights;
458 }
459
Simon Hunta17fa672015-08-19 18:42:22 -0700460 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
461
Simon Hunt4fc86852015-08-20 17:57:52 -0700462 private void compileLinks(TrafficLinkMap linkMap) {
463 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700464 }
465
Simon Hunt4fc86852015-08-20 17:57:52 -0700466 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700467 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700468 linkMap.add(createEdgeLink(host, true));
469 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700470 });
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 Hunt4fc86852015-08-20 17:57:52 -0700480 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700481 link.addLoad(getLinkFlowLoad(link.one()));
482 link.addLoad(getLinkFlowLoad(link.two()));
483 }
484
Simon Hunt4fc86852015-08-20 17:57:52 -0700485 private void attachPortLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700486 // 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 Hunt5f31a022015-08-20 08:43:25 -0700492 link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD);
Simon Hunt4fc86852015-08-20 17:57:52 -0700493// link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!!
Simon Hunta17fa672015-08-19 18:42:22 -0700494 }
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 Hunta17fa672015-08-19 18:42:22 -0700506 // 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 Hunt5328f792017-01-11 17:43:31 -0800510 for (FlowEntry flowEntry : servicesBundle.flowService().getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700511 entries.add(flowEntry);
512 }
513
514 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700515 Set<Link> links = new HashSet<>(servicesBundle.linkService()
Simon Hunt5328f792017-01-11 17:43:31 -0800516 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700517 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 Hunt57830172015-08-26 13:25:17 -0700548 private void highlightIntentLinks(Highlights highlights,
549 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700550 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700551 // NOTE: highlight secondary first, then primary, so that links shared
552 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700553 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
554 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700555 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700556 }
557
Simon Hunt57830172015-08-26 13:25:17 -0700558 private void highlightIntentLinksWithTraffic(Highlights highlights,
559 Set<Intent> primary) {
560 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700561 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700562 colorLinks(highlights, linkMap);
563 }
564
Simon Hunt94f7dae2015-08-26 17:40:59 -0700565 private void createTrafficLinks(Highlights highlights,
566 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700567 Flavor flavor, boolean showTraffic) {
568 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700569 List<Intent> installables = servicesBundle.intentService()
570 .getInstallableIntents(intent.key());
571 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700572 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 Hunt5328f792017-01-11 17:43:31 -0800578 links = addEdgeLinksIfNeeded(intent, linkResources(installable));
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800579 } else if (installable instanceof FlowObjectiveIntent) {
Simon Hunt5328f792017-01-11 17:43:31 -0800580 links = addEdgeLinksIfNeeded(intent, linkResources(installable));
Simon Hunta17fa672015-08-19 18:42:22 -0700581 } 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 Hunt57830172015-08-26 13:25:17 -0700587 boolean isOptical = intent instanceof OpticalConnectivityIntent;
588 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700589 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700590 }
591 }
592 }
593 }
594
Simon Hunt5328f792017-01-11 17:43:31 -0800595 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 Hunt94f7dae2015-08-26 17:40:59 -0700608 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 Hunta17fa672015-08-19 18:42:22 -0700629 // 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 Hunt57830172015-08-26 13:25:17 -0700637 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 Hunta17fa672015-08-19 18:42:22 -0700659 // =======================================================================
660 // === Background Task
661
662 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700663 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700664 @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 Hunt4fc86852015-08-20 17:57:52 -0700677 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700678 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 Hunta17fa672015-08-19 18:42:22 -0700694}