blob: 4dcaeb1bb3023856b9ff1ccba86d51cf500c406b [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;
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;
32import org.onosproject.net.intent.FlowRuleIntent;
33import org.onosproject.net.intent.Intent;
34import org.onosproject.net.intent.LinkCollectionIntent;
35import org.onosproject.net.intent.OpticalConnectivityIntent;
36import org.onosproject.net.intent.OpticalPathIntent;
37import org.onosproject.net.intent.PathIntent;
38import org.onosproject.net.statistic.Load;
Simon Hunta17fa672015-08-19 18:42:22 -070039import org.onosproject.ui.impl.topo.IntentSelection;
Simon Hunta17fa672015-08-19 18:42:22 -070040import org.onosproject.ui.impl.topo.ServicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070041import org.onosproject.ui.impl.topo.TopoIntentFilter;
Simon Hunt4fc86852015-08-20 17:57:52 -070042import org.onosproject.ui.impl.topo.TrafficLink;
Simon Hunt57830172015-08-26 13:25:17 -070043import org.onosproject.ui.impl.topo.TrafficLink.StatsType;
Simon Hunt4fc86852015-08-20 17:57:52 -070044import org.onosproject.ui.impl.topo.TrafficLinkMap;
Simon Hunt94f7dae2015-08-26 17:40:59 -070045import org.onosproject.ui.topo.DeviceHighlight;
Simon Hunta17fa672015-08-19 18:42:22 -070046import org.onosproject.ui.topo.Highlights;
Simon Hunt94f7dae2015-08-26 17:40:59 -070047import org.onosproject.ui.topo.Highlights.Amount;
48import org.onosproject.ui.topo.HostHighlight;
Simon Hunt57830172015-08-26 13:25:17 -070049import org.onosproject.ui.topo.LinkHighlight.Flavor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070050import org.onosproject.ui.topo.NodeHighlight;
Simon Hunt743a8492015-08-25 16:18:19 -070051import org.onosproject.ui.topo.NodeSelection;
52import org.onosproject.ui.topo.TopoUtils;
Simon Hunta17fa672015-08-19 18:42:22 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56import java.util.ArrayList;
57import java.util.Collection;
58import java.util.Collections;
59import java.util.HashMap;
60import java.util.HashSet;
61import java.util.List;
62import java.util.Map;
63import java.util.Set;
64import java.util.Timer;
65import java.util.TimerTask;
66
67import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunt94f7dae2015-08-26 17:40:59 -070068import static org.onosproject.ui.impl.TrafficMonitor.Mode.*;
Simon Hunta17fa672015-08-19 18:42:22 -070069
70/**
71 * Encapsulates the behavior of monitoring specific traffic patterns.
72 */
Simon Hunt4fc86852015-08-20 17:57:52 -070073public class TrafficMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070074
75 // 4 Kilo Bytes as threshold
76 private static final double BPS_THRESHOLD = 4 * TopoUtils.KILO;
77
78 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070079 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070080
81 /**
82 * Designates the different modes of operation.
83 */
84 public enum Mode {
85 IDLE,
86 ALL_FLOW_TRAFFIC,
87 ALL_PORT_TRAFFIC,
88 DEV_LINK_FLOWS,
89 RELATED_INTENTS,
Simon Hunt4fc86852015-08-20 17:57:52 -070090 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -070091 }
92
93 private final long trafficPeriod;
94 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070095 private final TopologyViewMessageHandler msgHandler;
96 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -070097
98 private final Timer timer = new Timer("topo-traffic");
99
100 private TimerTask trafficTask = null;
101 private Mode mode = IDLE;
102 private NodeSelection selectedNodes = null;
103 private IntentSelection selectedIntents = null;
104
105
106 /**
107 * Constructs a traffic monitor.
108 *
109 * @param trafficPeriod traffic task period in ms
110 * @param servicesBundle bundle of services
Simon Hunt4fc86852015-08-20 17:57:52 -0700111 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700112 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700113 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
114 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700115 this.trafficPeriod = trafficPeriod;
116 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700117 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700118
Simon Hunt4fc86852015-08-20 17:57:52 -0700119 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700120 }
121
122 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700123 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700124
Simon Hunt4fc86852015-08-20 17:57:52 -0700125 /**
126 * Monitor for traffic data to be sent back to the web client, under
127 * the given mode. This causes a background traffic task to be
128 * scheduled to repeatedly compute and transmit the appropriate traffic
129 * data to the client.
130 * <p>
131 * The monitoring mode is expected to be one of:
132 * <ul>
133 * <li>ALL_FLOW_TRAFFIC</li>
134 * <li>ALL_PORT_TRAFFIC</li>
135 * <li>SELECTED_INTENT</li>
136 * </ul>
137 *
138 * @param mode monitoring mode
139 */
Simon Hunta17fa672015-08-19 18:42:22 -0700140 public synchronized void monitor(Mode mode) {
141 log.debug("monitor: {}", mode);
142 this.mode = mode;
143
144 switch (mode) {
145 case ALL_FLOW_TRAFFIC:
146 clearSelection();
147 scheduleTask();
148 sendAllFlowTraffic();
149 break;
150
151 case ALL_PORT_TRAFFIC:
152 clearSelection();
153 scheduleTask();
154 sendAllPortTraffic();
155 break;
156
Simon Hunt4fc86852015-08-20 17:57:52 -0700157 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700158 scheduleTask();
159 sendSelectedIntentTraffic();
160 break;
161
162 default:
163 log.debug("Unexpected call to monitor({})", mode);
164 clearAll();
165 break;
166 }
167 }
168
Simon Hunt4fc86852015-08-20 17:57:52 -0700169 /**
170 * Monitor for traffic data to be sent back to the web client, under
171 * the given mode, using the given selection of devices and hosts.
172 * In the case of "device link flows", this causes a background traffic
173 * task to be scheduled to repeatedly compute and transmit the appropriate
174 * traffic data to the client. In the case of "related intents", no
175 * repeating task is scheduled.
176 * <p>
177 * The monitoring mode is expected to be one of:
178 * <ul>
179 * <li>DEV_LINK_FLOWS</li>
180 * <li>RELATED_INTENTS</li>
181 * </ul>
182 *
183 * @param mode monitoring mode
184 */
Simon Hunta17fa672015-08-19 18:42:22 -0700185 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
186 log.debug("monitor: {} -- {}", mode, nodeSelection);
187 this.mode = mode;
188 this.selectedNodes = nodeSelection;
189
190 switch (mode) {
191 case DEV_LINK_FLOWS:
192 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700193 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700194 sendClearAll();
195 } else {
196 scheduleTask();
197 sendDeviceLinkFlows();
198 }
199 break;
200
201 case RELATED_INTENTS:
202 if (selectedNodes.none()) {
203 sendClearAll();
204 } else {
205 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
206 if (selectedIntents.none()) {
207 sendClearAll();
208 } else {
209 sendSelectedIntents();
210 }
211 }
212 break;
213
214 default:
215 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
216 clearAll();
217 break;
218 }
219 }
220
Simon Hunt4fc86852015-08-20 17:57:52 -0700221 // TODO: move this out to the "h2h/multi-intent app"
222 /**
223 * Monitor for traffic data to be sent back to the web client, for the
224 * given intent.
225 *
226 * @param intent the intent to monitor
227 */
Simon Hunta17fa672015-08-19 18:42:22 -0700228 public synchronized void monitor(Intent intent) {
229 log.debug("monitor intent: {}", intent.id());
230 selectedNodes = null;
231 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700232 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700233 scheduleTask();
234 sendSelectedIntentTraffic();
235 }
236
Simon Hunt4fc86852015-08-20 17:57:52 -0700237 /**
238 * Selects the next intent in the select group (if there is one),
239 * and sends highlighting data back to the web client to display
240 * which path is selected.
241 */
Simon Hunta17fa672015-08-19 18:42:22 -0700242 public synchronized void selectNextIntent() {
243 if (selectedIntents != null) {
244 selectedIntents.next();
245 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700246 if (mode == SELECTED_INTENT) {
247 mode = RELATED_INTENTS;
248 }
Simon Hunta17fa672015-08-19 18:42:22 -0700249 }
250 }
251
Simon Hunt4fc86852015-08-20 17:57:52 -0700252 /**
253 * Selects the previous intent in the select group (if there is one),
254 * and sends highlighting data back to the web client to display
255 * which path is selected.
256 */
Simon Hunta17fa672015-08-19 18:42:22 -0700257 public synchronized void selectPreviousIntent() {
258 if (selectedIntents != null) {
259 selectedIntents.prev();
260 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700261 if (mode == SELECTED_INTENT) {
262 mode = RELATED_INTENTS;
263 }
Simon Hunta17fa672015-08-19 18:42:22 -0700264 }
265 }
266
Simon Hunt4fc86852015-08-20 17:57:52 -0700267 /**
268 * Resends selected intent traffic data. This is called, for example,
269 * when the system detects an intent update happened.
270 */
Simon Hunta17fa672015-08-19 18:42:22 -0700271 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700272 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700273 sendSelectedIntentTraffic();
274 }
275 }
276
Simon Hunt4fc86852015-08-20 17:57:52 -0700277 /**
278 * Stop all traffic monitoring.
279 */
280 public synchronized void stopMonitoring() {
281 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700282 if (mode != IDLE) {
283 sendClearAll();
284 }
285 }
286
287
288 // =======================================================================
289 // === Helper methods ===
290
291 private void sendClearAll() {
292 clearAll();
293 sendClearHighlights();
294 }
295
296 private void clearAll() {
297 this.mode = IDLE;
298 clearSelection();
299 cancelTask();
300 }
301
302 private void clearSelection() {
303 selectedNodes = null;
304 selectedIntents = null;
305 }
306
307 private synchronized void scheduleTask() {
308 if (trafficTask == null) {
309 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700310 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700311 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
312 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700313 log.debug("(traffic task already running)");
314 }
315 }
316
317 private synchronized void cancelTask() {
318 if (trafficTask != null) {
319 trafficTask.cancel();
320 trafficTask = null;
321 }
322 }
323
Simon Hunta17fa672015-08-19 18:42:22 -0700324 private void sendAllFlowTraffic() {
325 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700326 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700327 }
328
329 private void sendAllPortTraffic() {
330 log.debug("sendAllPortTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700331 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700332 }
333
334 private void sendDeviceLinkFlows() {
335 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700336 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700337 }
338
339 private void sendSelectedIntents() {
340 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700341 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700342 }
343
344 private void sendSelectedIntentTraffic() {
345 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700346 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700347 }
348
349 private void sendClearHighlights() {
350 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700351 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700352 }
353
Simon Hunta17fa672015-08-19 18:42:22 -0700354 // =======================================================================
355 // === Generate messages in JSON object node format
356
Simon Hunt57830172015-08-26 13:25:17 -0700357 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700358 Highlights highlights = new Highlights();
359
Simon Hunt4fc86852015-08-20 17:57:52 -0700360 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700361 compileLinks(linkMap);
362 addEdgeLinks(linkMap);
363
Simon Hunt4fc86852015-08-20 17:57:52 -0700364 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700365 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700366 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700367 } else if (type == StatsType.PORT_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700368 attachPortLoad(tlink);
Simon Hunta17fa672015-08-19 18:42:22 -0700369 }
370
371 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700372 if (tlink.hasTraffic()) {
373 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700374 }
375 }
376 return highlights;
377 }
378
379 // create highlights for links, showing flows for selected devices.
380 private Highlights deviceLinkFlows() {
381 Highlights highlights = new Highlights();
382
Simon Hunt72297212015-08-25 10:15:33 -0700383 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700384 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700385 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700386
Simon Hunt72297212015-08-25 10:15:33 -0700387 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700388 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
389 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700390 TrafficLink tlink = linkMap.add(link);
391 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700392 }
393 }
394
395 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700396 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700397 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700398 }
399
400 }
401 return highlights;
402 }
403
404 private Highlights intentGroup() {
405 Highlights highlights = new Highlights();
406
407 if (selectedIntents != null && !selectedIntents.none()) {
408 // If 'all' intents are selected, they will all have primary
409 // highlighting; otherwise, the specifically selected intent will
410 // have primary highlighting, and the remainder will have secondary
411 // highlighting.
412 Set<Intent> primary;
413 Set<Intent> secondary;
414 int count = selectedIntents.size();
415
416 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
417 Intent current;
418
419 if (selectedIntents.all()) {
420 primary = allBut;
421 secondary = Collections.emptySet();
422 log.debug("Highlight all intents ({})", count);
423 } else {
424 current = selectedIntents.current();
425 primary = new HashSet<>();
426 primary.add(current);
427 allBut.remove(current);
428 secondary = allBut;
429 log.debug("Highlight intent: {} ([{}] of {})",
430 current.id(), selectedIntents.index(), count);
431 }
Simon Hunt57830172015-08-26 13:25:17 -0700432
433 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700434 }
435 return highlights;
436 }
437
438 private Highlights intentTraffic() {
439 Highlights highlights = new Highlights();
440
441 if (selectedIntents != null && selectedIntents.single()) {
442 Intent current = selectedIntents.current();
443 Set<Intent> primary = new HashSet<>();
444 primary.add(current);
445 log.debug("Highlight traffic for intent: {} ([{}] of {})",
446 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700447
448 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700449 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700450 }
451 return highlights;
452 }
453
Simon Hunta17fa672015-08-19 18:42:22 -0700454 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
455
Simon Hunt4fc86852015-08-20 17:57:52 -0700456 private void compileLinks(TrafficLinkMap linkMap) {
457 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700458 }
459
Simon Hunt4fc86852015-08-20 17:57:52 -0700460 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700461 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700462 linkMap.add(createEdgeLink(host, true));
463 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700464 });
465 }
466
467 private Load getLinkFlowLoad(Link link) {
468 if (link != null && link.src().elementId() instanceof DeviceId) {
469 return servicesBundle.flowStatsService().load(link);
470 }
471 return null;
472 }
473
Simon Hunt4fc86852015-08-20 17:57:52 -0700474 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700475 link.addLoad(getLinkFlowLoad(link.one()));
476 link.addLoad(getLinkFlowLoad(link.two()));
477 }
478
Simon Hunt4fc86852015-08-20 17:57:52 -0700479 private void attachPortLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700480 // For bi-directional traffic links, use
481 // the max link rate of either direction
482 // (we choose 'one' since we know that is never null)
483 Link one = link.one();
484 Load egressSrc = servicesBundle.portStatsService().load(one.src());
485 Load egressDst = servicesBundle.portStatsService().load(one.dst());
Simon Hunt5f31a022015-08-20 08:43:25 -0700486 link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD);
Simon Hunt4fc86852015-08-20 17:57:52 -0700487// link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!!
Simon Hunta17fa672015-08-19 18:42:22 -0700488 }
489
490 private Load maxLoad(Load a, Load b) {
491 if (a == null) {
492 return b;
493 }
494 if (b == null) {
495 return a;
496 }
497 return a.rate() > b.rate() ? a : b;
498 }
499
Simon Hunta17fa672015-08-19 18:42:22 -0700500 // Counts all flow entries that egress on the links of the given device.
501 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
502 // get the flows for the device
503 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt4fc86852015-08-20 17:57:52 -0700504 for (FlowEntry flowEntry : servicesBundle.flowService()
505 .getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700506 entries.add(flowEntry);
507 }
508
509 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700510 Set<Link> links = new HashSet<>(servicesBundle.linkService()
511 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700512 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
513 if (hosts != null) {
514 for (Host host : hosts) {
515 links.add(createEdgeLink(host, false));
516 }
517 }
518
519 // compile flow counts per link
520 Map<Link, Integer> counts = new HashMap<>();
521 for (Link link : links) {
522 counts.put(link, getEgressFlows(link, entries));
523 }
524 return counts;
525 }
526
527 // Counts all entries that egress on the link source port.
528 private int getEgressFlows(Link link, List<FlowEntry> entries) {
529 int count = 0;
530 PortNumber out = link.src().port();
531 for (FlowEntry entry : entries) {
532 TrafficTreatment treatment = entry.treatment();
533 for (Instruction instruction : treatment.allInstructions()) {
534 if (instruction.type() == Instruction.Type.OUTPUT &&
535 ((OutputInstruction) instruction).port().equals(out)) {
536 count++;
537 }
538 }
539 }
540 return count;
541 }
542
Simon Hunt57830172015-08-26 13:25:17 -0700543 private void highlightIntentLinks(Highlights highlights,
544 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700545 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700546 // NOTE: highlight secondary first, then primary, so that links shared
547 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700548 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
549 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700550 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700551 }
552
Simon Hunt57830172015-08-26 13:25:17 -0700553 private void highlightIntentLinksWithTraffic(Highlights highlights,
554 Set<Intent> primary) {
555 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700556 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700557 colorLinks(highlights, linkMap);
558 }
559
Simon Hunt94f7dae2015-08-26 17:40:59 -0700560 private void createTrafficLinks(Highlights highlights,
561 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700562 Flavor flavor, boolean showTraffic) {
563 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700564 List<Intent> installables = servicesBundle.intentService()
565 .getInstallableIntents(intent.key());
566 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700567 if (installables != null) {
568 for (Intent installable : installables) {
569
570 if (installable instanceof PathIntent) {
571 links = ((PathIntent) installable).path().links();
572 } else if (installable instanceof FlowRuleIntent) {
573 links = linkResources(installable);
574 } else if (installable instanceof LinkCollectionIntent) {
575 links = ((LinkCollectionIntent) installable).links();
576 } else if (installable instanceof OpticalPathIntent) {
577 links = ((OpticalPathIntent) installable).path().links();
578 }
579
Simon Hunt57830172015-08-26 13:25:17 -0700580 boolean isOptical = intent instanceof OpticalConnectivityIntent;
581 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700582 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700583 }
584 }
585 }
586 }
587
Simon Hunt94f7dae2015-08-26 17:40:59 -0700588 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
589 for (Link link : links) {
590 ensureNodePresent(highlights, link.src().elementId());
591 ensureNodePresent(highlights, link.dst().elementId());
592 }
593 }
594
595 private void ensureNodePresent(Highlights highlights, ElementId eid) {
596 String id = eid.toString();
597 NodeHighlight nh = highlights.getNode(id);
598 if (nh == null) {
599 if (eid instanceof DeviceId) {
600 nh = new DeviceHighlight(id);
601 highlights.add((DeviceHighlight) nh);
602 } else if (eid instanceof HostId) {
603 nh = new HostHighlight(id);
604 highlights.add((HostHighlight) nh);
605 }
606 }
607 }
608
Simon Hunta17fa672015-08-19 18:42:22 -0700609 // Extracts links from the specified flow rule intent resources
610 private Collection<Link> linkResources(Intent installable) {
611 ImmutableList.Builder<Link> builder = ImmutableList.builder();
612 installable.resources().stream().filter(r -> r instanceof Link)
613 .forEach(r -> builder.add((Link) r));
614 return builder.build();
615 }
616
Simon Hunt57830172015-08-26 13:25:17 -0700617 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
618 Flavor flavor, boolean isOptical,
619 boolean showTraffic) {
620 if (links != null) {
621 for (Link link : links) {
622 TrafficLink tlink = linkMap.add(link);
623 tlink.tagFlavor(flavor);
624 tlink.optical(isOptical);
625 if (showTraffic) {
626 tlink.addLoad(getLinkFlowLoad(link));
627 tlink.antMarch(true);
628 }
629 }
630 }
631 }
632
633 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
634 for (TrafficLink tlink : linkMap.biLinks()) {
635 highlights.add(tlink.highlight(StatsType.TAGGED));
636 }
637 }
638
Simon Hunta17fa672015-08-19 18:42:22 -0700639 // =======================================================================
640 // === Background Task
641
642 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700643 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700644 @Override
645 public void run() {
646 try {
647 switch (mode) {
648 case ALL_FLOW_TRAFFIC:
649 sendAllFlowTraffic();
650 break;
651 case ALL_PORT_TRAFFIC:
652 sendAllPortTraffic();
653 break;
654 case DEV_LINK_FLOWS:
655 sendDeviceLinkFlows();
656 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700657 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700658 sendSelectedIntentTraffic();
659 break;
660
661 default:
662 // RELATED_INTENTS and IDLE modes should never invoke
663 // the background task, but if they do, they have
664 // nothing to do
665 break;
666 }
667
668 } catch (Exception e) {
669 log.warn("Unable to process traffic task due to {}", e.getMessage());
670 log.warn("Boom!", e);
671 }
672 }
673 }
Simon Hunta17fa672015-08-19 18:42:22 -0700674}