blob: 2dc8e44e8ef42c008a7922b7b6f28aa288bdf234 [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
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
18package org.onosproject.ui.impl;
19
20import com.google.common.collect.ImmutableList;
21import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.Host;
24import org.onosproject.net.Link;
Simon Hunta17fa672015-08-19 18:42:22 -070025import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.FlowEntry;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.flow.instructions.Instruction;
29import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
30import org.onosproject.net.intent.FlowRuleIntent;
31import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.LinkCollectionIntent;
33import org.onosproject.net.intent.OpticalConnectivityIntent;
34import org.onosproject.net.intent.OpticalPathIntent;
35import org.onosproject.net.intent.PathIntent;
36import org.onosproject.net.statistic.Load;
Simon Hunta17fa672015-08-19 18:42:22 -070037import org.onosproject.ui.impl.topo.IntentSelection;
Simon Hunta17fa672015-08-19 18:42:22 -070038import org.onosproject.ui.impl.topo.ServicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070039import org.onosproject.ui.impl.topo.TopoIntentFilter;
Simon Hunt4fc86852015-08-20 17:57:52 -070040import org.onosproject.ui.impl.topo.TrafficLink;
Simon Hunt57830172015-08-26 13:25:17 -070041import org.onosproject.ui.impl.topo.TrafficLink.StatsType;
Simon Hunt4fc86852015-08-20 17:57:52 -070042import org.onosproject.ui.impl.topo.TrafficLinkMap;
Simon Hunta17fa672015-08-19 18:42:22 -070043import org.onosproject.ui.topo.Highlights;
Simon Hunt57830172015-08-26 13:25:17 -070044import org.onosproject.ui.topo.LinkHighlight.Flavor;
Simon Hunt743a8492015-08-25 16:18:19 -070045import org.onosproject.ui.topo.NodeSelection;
46import org.onosproject.ui.topo.TopoUtils;
Simon Hunta17fa672015-08-19 18:42:22 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.ArrayList;
51import java.util.Collection;
52import java.util.Collections;
53import java.util.HashMap;
54import java.util.HashSet;
55import java.util.List;
56import java.util.Map;
57import java.util.Set;
58import java.util.Timer;
59import java.util.TimerTask;
60
61import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunt4fc86852015-08-20 17:57:52 -070062import static org.onosproject.ui.impl.TrafficMonitor.Mode.IDLE;
Simon Hunt57830172015-08-26 13:25:17 -070063import static org.onosproject.ui.impl.TrafficMonitor.Mode.RELATED_INTENTS;
Simon Hunt4fc86852015-08-20 17:57:52 -070064import static org.onosproject.ui.impl.TrafficMonitor.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070065
66/**
67 * Encapsulates the behavior of monitoring specific traffic patterns.
68 */
Simon Hunt4fc86852015-08-20 17:57:52 -070069public class TrafficMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070070
71 // 4 Kilo Bytes as threshold
72 private static final double BPS_THRESHOLD = 4 * TopoUtils.KILO;
73
74 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070075 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070076
77 /**
78 * Designates the different modes of operation.
79 */
80 public enum Mode {
81 IDLE,
82 ALL_FLOW_TRAFFIC,
83 ALL_PORT_TRAFFIC,
84 DEV_LINK_FLOWS,
85 RELATED_INTENTS,
Simon Hunt4fc86852015-08-20 17:57:52 -070086 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -070087 }
88
89 private final long trafficPeriod;
90 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070091 private final TopologyViewMessageHandler msgHandler;
92 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -070093
94 private final Timer timer = new Timer("topo-traffic");
95
96 private TimerTask trafficTask = null;
97 private Mode mode = IDLE;
98 private NodeSelection selectedNodes = null;
99 private IntentSelection selectedIntents = null;
100
101
102 /**
103 * Constructs a traffic monitor.
104 *
105 * @param trafficPeriod traffic task period in ms
106 * @param servicesBundle bundle of services
Simon Hunt4fc86852015-08-20 17:57:52 -0700107 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700108 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700109 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
110 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700111 this.trafficPeriod = trafficPeriod;
112 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700113 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700114
Simon Hunt4fc86852015-08-20 17:57:52 -0700115 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700116 }
117
118 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700119 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700120
Simon Hunt4fc86852015-08-20 17:57:52 -0700121 /**
122 * Monitor for traffic data to be sent back to the web client, under
123 * the given mode. This causes a background traffic task to be
124 * scheduled to repeatedly compute and transmit the appropriate traffic
125 * data to the client.
126 * <p>
127 * The monitoring mode is expected to be one of:
128 * <ul>
129 * <li>ALL_FLOW_TRAFFIC</li>
130 * <li>ALL_PORT_TRAFFIC</li>
131 * <li>SELECTED_INTENT</li>
132 * </ul>
133 *
134 * @param mode monitoring mode
135 */
Simon Hunta17fa672015-08-19 18:42:22 -0700136 public synchronized void monitor(Mode mode) {
137 log.debug("monitor: {}", mode);
138 this.mode = mode;
139
140 switch (mode) {
141 case ALL_FLOW_TRAFFIC:
142 clearSelection();
143 scheduleTask();
144 sendAllFlowTraffic();
145 break;
146
147 case ALL_PORT_TRAFFIC:
148 clearSelection();
149 scheduleTask();
150 sendAllPortTraffic();
151 break;
152
Simon Hunt4fc86852015-08-20 17:57:52 -0700153 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700154 scheduleTask();
155 sendSelectedIntentTraffic();
156 break;
157
158 default:
159 log.debug("Unexpected call to monitor({})", mode);
160 clearAll();
161 break;
162 }
163 }
164
Simon Hunt4fc86852015-08-20 17:57:52 -0700165 /**
166 * Monitor for traffic data to be sent back to the web client, under
167 * the given mode, using the given selection of devices and hosts.
168 * In the case of "device link flows", this causes a background traffic
169 * task to be scheduled to repeatedly compute and transmit the appropriate
170 * traffic data to the client. In the case of "related intents", no
171 * repeating task is scheduled.
172 * <p>
173 * The monitoring mode is expected to be one of:
174 * <ul>
175 * <li>DEV_LINK_FLOWS</li>
176 * <li>RELATED_INTENTS</li>
177 * </ul>
178 *
179 * @param mode monitoring mode
180 */
Simon Hunta17fa672015-08-19 18:42:22 -0700181 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
182 log.debug("monitor: {} -- {}", mode, nodeSelection);
183 this.mode = mode;
184 this.selectedNodes = nodeSelection;
185
186 switch (mode) {
187 case DEV_LINK_FLOWS:
188 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700189 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700190 sendClearAll();
191 } else {
192 scheduleTask();
193 sendDeviceLinkFlows();
194 }
195 break;
196
197 case RELATED_INTENTS:
198 if (selectedNodes.none()) {
199 sendClearAll();
200 } else {
201 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
202 if (selectedIntents.none()) {
203 sendClearAll();
204 } else {
205 sendSelectedIntents();
206 }
207 }
208 break;
209
210 default:
211 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
212 clearAll();
213 break;
214 }
215 }
216
Simon Hunt4fc86852015-08-20 17:57:52 -0700217 // TODO: move this out to the "h2h/multi-intent app"
218 /**
219 * Monitor for traffic data to be sent back to the web client, for the
220 * given intent.
221 *
222 * @param intent the intent to monitor
223 */
Simon Hunta17fa672015-08-19 18:42:22 -0700224 public synchronized void monitor(Intent intent) {
225 log.debug("monitor intent: {}", intent.id());
226 selectedNodes = null;
227 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700228 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700229 scheduleTask();
230 sendSelectedIntentTraffic();
231 }
232
Simon Hunt4fc86852015-08-20 17:57:52 -0700233 /**
234 * Selects the next intent in the select group (if there is one),
235 * and sends highlighting data back to the web client to display
236 * which path is selected.
237 */
Simon Hunta17fa672015-08-19 18:42:22 -0700238 public synchronized void selectNextIntent() {
239 if (selectedIntents != null) {
240 selectedIntents.next();
241 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700242 if (mode == SELECTED_INTENT) {
243 mode = RELATED_INTENTS;
244 }
Simon Hunta17fa672015-08-19 18:42:22 -0700245 }
246 }
247
Simon Hunt4fc86852015-08-20 17:57:52 -0700248 /**
249 * Selects the previous intent in the select group (if there is one),
250 * and sends highlighting data back to the web client to display
251 * which path is selected.
252 */
Simon Hunta17fa672015-08-19 18:42:22 -0700253 public synchronized void selectPreviousIntent() {
254 if (selectedIntents != null) {
255 selectedIntents.prev();
256 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700257 if (mode == SELECTED_INTENT) {
258 mode = RELATED_INTENTS;
259 }
Simon Hunta17fa672015-08-19 18:42:22 -0700260 }
261 }
262
Simon Hunt4fc86852015-08-20 17:57:52 -0700263 /**
264 * Resends selected intent traffic data. This is called, for example,
265 * when the system detects an intent update happened.
266 */
Simon Hunta17fa672015-08-19 18:42:22 -0700267 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700268 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700269 sendSelectedIntentTraffic();
270 }
271 }
272
Simon Hunt4fc86852015-08-20 17:57:52 -0700273 /**
274 * Stop all traffic monitoring.
275 */
276 public synchronized void stopMonitoring() {
277 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700278 if (mode != IDLE) {
279 sendClearAll();
280 }
281 }
282
283
284 // =======================================================================
285 // === Helper methods ===
286
287 private void sendClearAll() {
288 clearAll();
289 sendClearHighlights();
290 }
291
292 private void clearAll() {
293 this.mode = IDLE;
294 clearSelection();
295 cancelTask();
296 }
297
298 private void clearSelection() {
299 selectedNodes = null;
300 selectedIntents = null;
301 }
302
303 private synchronized void scheduleTask() {
304 if (trafficTask == null) {
305 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700306 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700307 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
308 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700309 log.debug("(traffic task already running)");
310 }
311 }
312
313 private synchronized void cancelTask() {
314 if (trafficTask != null) {
315 trafficTask.cancel();
316 trafficTask = null;
317 }
318 }
319
Simon Hunta17fa672015-08-19 18:42:22 -0700320 private void sendAllFlowTraffic() {
321 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700322 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700323 }
324
325 private void sendAllPortTraffic() {
326 log.debug("sendAllPortTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700327 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700328 }
329
330 private void sendDeviceLinkFlows() {
331 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700332 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700333 }
334
335 private void sendSelectedIntents() {
336 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700337 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700338 }
339
340 private void sendSelectedIntentTraffic() {
341 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700342 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700343 }
344
345 private void sendClearHighlights() {
346 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700347 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700348 }
349
Simon Hunta17fa672015-08-19 18:42:22 -0700350 // =======================================================================
351 // === Generate messages in JSON object node format
352
Simon Hunt57830172015-08-26 13:25:17 -0700353 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700354 Highlights highlights = new Highlights();
355
Simon Hunt4fc86852015-08-20 17:57:52 -0700356 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700357 compileLinks(linkMap);
358 addEdgeLinks(linkMap);
359
Simon Hunt4fc86852015-08-20 17:57:52 -0700360 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700361 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700362 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700363 } else if (type == StatsType.PORT_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700364 attachPortLoad(tlink);
Simon Hunta17fa672015-08-19 18:42:22 -0700365 }
366
367 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700368 if (tlink.hasTraffic()) {
369 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700370 }
371 }
372 return highlights;
373 }
374
375 // create highlights for links, showing flows for selected devices.
376 private Highlights deviceLinkFlows() {
377 Highlights highlights = new Highlights();
378
Simon Hunt72297212015-08-25 10:15:33 -0700379 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700380 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700381 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700382
Simon Hunt72297212015-08-25 10:15:33 -0700383 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700384 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
385 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700386 TrafficLink tlink = linkMap.add(link);
387 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700388 }
389 }
390
391 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700392 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700393 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700394 }
395
396 }
397 return highlights;
398 }
399
400 private Highlights intentGroup() {
401 Highlights highlights = new Highlights();
402
403 if (selectedIntents != null && !selectedIntents.none()) {
404 // If 'all' intents are selected, they will all have primary
405 // highlighting; otherwise, the specifically selected intent will
406 // have primary highlighting, and the remainder will have secondary
407 // highlighting.
408 Set<Intent> primary;
409 Set<Intent> secondary;
410 int count = selectedIntents.size();
411
412 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
413 Intent current;
414
415 if (selectedIntents.all()) {
416 primary = allBut;
417 secondary = Collections.emptySet();
418 log.debug("Highlight all intents ({})", count);
419 } else {
420 current = selectedIntents.current();
421 primary = new HashSet<>();
422 primary.add(current);
423 allBut.remove(current);
424 secondary = allBut;
425 log.debug("Highlight intent: {} ([{}] of {})",
426 current.id(), selectedIntents.index(), count);
427 }
Simon Hunt57830172015-08-26 13:25:17 -0700428
429 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700430 }
431 return highlights;
432 }
433
434 private Highlights intentTraffic() {
435 Highlights highlights = new Highlights();
436
437 if (selectedIntents != null && selectedIntents.single()) {
438 Intent current = selectedIntents.current();
439 Set<Intent> primary = new HashSet<>();
440 primary.add(current);
441 log.debug("Highlight traffic for intent: {} ([{}] of {})",
442 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700443
444 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunta17fa672015-08-19 18:42:22 -0700445 }
446 return highlights;
447 }
448
Simon Hunta17fa672015-08-19 18:42:22 -0700449 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
450
Simon Hunt4fc86852015-08-20 17:57:52 -0700451 private void compileLinks(TrafficLinkMap linkMap) {
452 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700453 }
454
Simon Hunt4fc86852015-08-20 17:57:52 -0700455 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700456 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700457 linkMap.add(createEdgeLink(host, true));
458 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700459 });
460 }
461
462 private Load getLinkFlowLoad(Link link) {
463 if (link != null && link.src().elementId() instanceof DeviceId) {
464 return servicesBundle.flowStatsService().load(link);
465 }
466 return null;
467 }
468
Simon Hunt4fc86852015-08-20 17:57:52 -0700469 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700470 link.addLoad(getLinkFlowLoad(link.one()));
471 link.addLoad(getLinkFlowLoad(link.two()));
472 }
473
Simon Hunt4fc86852015-08-20 17:57:52 -0700474 private void attachPortLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700475 // For bi-directional traffic links, use
476 // the max link rate of either direction
477 // (we choose 'one' since we know that is never null)
478 Link one = link.one();
479 Load egressSrc = servicesBundle.portStatsService().load(one.src());
480 Load egressDst = servicesBundle.portStatsService().load(one.dst());
Simon Hunt5f31a022015-08-20 08:43:25 -0700481 link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD);
Simon Hunt4fc86852015-08-20 17:57:52 -0700482// link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!!
Simon Hunta17fa672015-08-19 18:42:22 -0700483 }
484
485 private Load maxLoad(Load a, Load b) {
486 if (a == null) {
487 return b;
488 }
489 if (b == null) {
490 return a;
491 }
492 return a.rate() > b.rate() ? a : b;
493 }
494
Simon Hunta17fa672015-08-19 18:42:22 -0700495 // Counts all flow entries that egress on the links of the given device.
496 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
497 // get the flows for the device
498 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt4fc86852015-08-20 17:57:52 -0700499 for (FlowEntry flowEntry : servicesBundle.flowService()
500 .getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700501 entries.add(flowEntry);
502 }
503
504 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700505 Set<Link> links = new HashSet<>(servicesBundle.linkService()
506 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700507 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
508 if (hosts != null) {
509 for (Host host : hosts) {
510 links.add(createEdgeLink(host, false));
511 }
512 }
513
514 // compile flow counts per link
515 Map<Link, Integer> counts = new HashMap<>();
516 for (Link link : links) {
517 counts.put(link, getEgressFlows(link, entries));
518 }
519 return counts;
520 }
521
522 // Counts all entries that egress on the link source port.
523 private int getEgressFlows(Link link, List<FlowEntry> entries) {
524 int count = 0;
525 PortNumber out = link.src().port();
526 for (FlowEntry entry : entries) {
527 TrafficTreatment treatment = entry.treatment();
528 for (Instruction instruction : treatment.allInstructions()) {
529 if (instruction.type() == Instruction.Type.OUTPUT &&
530 ((OutputInstruction) instruction).port().equals(out)) {
531 count++;
532 }
533 }
534 }
535 return count;
536 }
537
Simon Hunt57830172015-08-26 13:25:17 -0700538 private void highlightIntentLinks(Highlights highlights,
539 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700540 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700541 // NOTE: highlight secondary first, then primary, so that links shared
542 // by intents are colored correctly ("last man wins")
543 createTrafficLinks(linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
544 createTrafficLinks(linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
545 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700546 }
547
Simon Hunt57830172015-08-26 13:25:17 -0700548 private void highlightIntentLinksWithTraffic(Highlights highlights,
549 Set<Intent> primary) {
550 TrafficLinkMap linkMap = new TrafficLinkMap();
551 createTrafficLinks(linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
552 colorLinks(highlights, linkMap);
553 }
554
555 private void createTrafficLinks(TrafficLinkMap linkMap, Set<Intent> intents,
556 Flavor flavor, boolean showTraffic) {
557 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700558 List<Intent> installables = servicesBundle.intentService()
559 .getInstallableIntents(intent.key());
560 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700561 if (installables != null) {
562 for (Intent installable : installables) {
563
564 if (installable instanceof PathIntent) {
565 links = ((PathIntent) installable).path().links();
566 } else if (installable instanceof FlowRuleIntent) {
567 links = linkResources(installable);
568 } else if (installable instanceof LinkCollectionIntent) {
569 links = ((LinkCollectionIntent) installable).links();
570 } else if (installable instanceof OpticalPathIntent) {
571 links = ((OpticalPathIntent) installable).path().links();
572 }
573
Simon Hunt57830172015-08-26 13:25:17 -0700574 boolean isOptical = intent instanceof OpticalConnectivityIntent;
575 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunta17fa672015-08-19 18:42:22 -0700576 }
577 }
578 }
579 }
580
Simon Hunta17fa672015-08-19 18:42:22 -0700581 // Extracts links from the specified flow rule intent resources
582 private Collection<Link> linkResources(Intent installable) {
583 ImmutableList.Builder<Link> builder = ImmutableList.builder();
584 installable.resources().stream().filter(r -> r instanceof Link)
585 .forEach(r -> builder.add((Link) r));
586 return builder.build();
587 }
588
Simon Hunt57830172015-08-26 13:25:17 -0700589 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
590 Flavor flavor, boolean isOptical,
591 boolean showTraffic) {
592 if (links != null) {
593 for (Link link : links) {
594 TrafficLink tlink = linkMap.add(link);
595 tlink.tagFlavor(flavor);
596 tlink.optical(isOptical);
597 if (showTraffic) {
598 tlink.addLoad(getLinkFlowLoad(link));
599 tlink.antMarch(true);
600 }
601 }
602 }
603 }
604
605 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
606 for (TrafficLink tlink : linkMap.biLinks()) {
607 highlights.add(tlink.highlight(StatsType.TAGGED));
608 }
609 }
610
Simon Hunta17fa672015-08-19 18:42:22 -0700611 // =======================================================================
612 // === Background Task
613
614 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700615 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700616 @Override
617 public void run() {
618 try {
619 switch (mode) {
620 case ALL_FLOW_TRAFFIC:
621 sendAllFlowTraffic();
622 break;
623 case ALL_PORT_TRAFFIC:
624 sendAllPortTraffic();
625 break;
626 case DEV_LINK_FLOWS:
627 sendDeviceLinkFlows();
628 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700629 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700630 sendSelectedIntentTraffic();
631 break;
632
633 default:
634 // RELATED_INTENTS and IDLE modes should never invoke
635 // the background task, but if they do, they have
636 // nothing to do
637 break;
638 }
639
640 } catch (Exception e) {
641 log.warn("Unable to process traffic task due to {}", e.getMessage());
642 log.warn("Boom!", e);
643 }
644 }
645 }
Simon Hunta17fa672015-08-19 18:42:22 -0700646}