blob: e97b7dd5f837e6533cfe9cd03d46ef32670e8970 [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 Hunt441c9ae2017-02-03 18:22:31 -080047import org.onosproject.ui.topo.AbstractTopoMonitor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070048import org.onosproject.ui.topo.DeviceHighlight;
Simon Hunta17fa672015-08-19 18:42:22 -070049import org.onosproject.ui.topo.Highlights;
Simon Hunt94f7dae2015-08-26 17:40:59 -070050import org.onosproject.ui.topo.Highlights.Amount;
51import org.onosproject.ui.topo.HostHighlight;
Simon Hunt57830172015-08-26 13:25:17 -070052import org.onosproject.ui.topo.LinkHighlight.Flavor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070053import org.onosproject.ui.topo.NodeHighlight;
Simon Hunt743a8492015-08-25 16:18:19 -070054import org.onosproject.ui.topo.NodeSelection;
55import org.onosproject.ui.topo.TopoUtils;
Simon Hunta17fa672015-08-19 18:42:22 -070056import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
59import java.util.ArrayList;
60import java.util.Collection;
61import java.util.Collections;
62import java.util.HashMap;
63import java.util.HashSet;
64import java.util.List;
65import java.util.Map;
66import java.util.Set;
67import java.util.Timer;
68import java.util.TimerTask;
69
70import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunted804d52016-03-30 09:51:40 -070071import static org.onosproject.ui.impl.TrafficMonitor.Mode.IDLE;
72import static org.onosproject.ui.impl.TrafficMonitor.Mode.RELATED_INTENTS;
73import static org.onosproject.ui.impl.TrafficMonitor.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070074
75/**
76 * Encapsulates the behavior of monitoring specific traffic patterns.
77 */
Simon Hunt441c9ae2017-02-03 18:22:31 -080078public class TrafficMonitor extends AbstractTopoMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070079
80 // 4 Kilo Bytes as threshold
81 private static final double BPS_THRESHOLD = 4 * TopoUtils.KILO;
82
83 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070084 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070085
86 /**
87 * Designates the different modes of operation.
88 */
89 public enum Mode {
90 IDLE,
91 ALL_FLOW_TRAFFIC,
92 ALL_PORT_TRAFFIC,
93 DEV_LINK_FLOWS,
94 RELATED_INTENTS,
Simon Hunt4fc86852015-08-20 17:57:52 -070095 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -070096 }
97
98 private final long trafficPeriod;
99 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700100 private final TopologyViewMessageHandler msgHandler;
101 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -0700102
103 private final Timer timer = new Timer("topo-traffic");
104
105 private TimerTask trafficTask = null;
106 private Mode mode = IDLE;
107 private NodeSelection selectedNodes = null;
108 private IntentSelection selectedIntents = null;
109
110
111 /**
112 * Constructs a traffic monitor.
113 *
Simon Hunt5328f792017-01-11 17:43:31 -0800114 * @param trafficPeriod traffic task period in ms
115 * @param servicesBundle bundle of services
116 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700117 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700118 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
119 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700120 this.trafficPeriod = trafficPeriod;
121 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700122 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700123
Simon Hunt4fc86852015-08-20 17:57:52 -0700124 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700125 }
126
127 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700128 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700129
Simon Hunt4fc86852015-08-20 17:57:52 -0700130 /**
131 * Monitor for traffic data to be sent back to the web client, under
132 * the given mode. This causes a background traffic task to be
133 * scheduled to repeatedly compute and transmit the appropriate traffic
134 * data to the client.
135 * <p>
136 * The monitoring mode is expected to be one of:
137 * <ul>
Simon Hunt5328f792017-01-11 17:43:31 -0800138 * <li>ALL_FLOW_TRAFFIC</li>
139 * <li>ALL_PORT_TRAFFIC</li>
140 * <li>SELECTED_INTENT</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700141 * </ul>
142 *
143 * @param mode monitoring mode
144 */
Simon Hunta17fa672015-08-19 18:42:22 -0700145 public synchronized void monitor(Mode mode) {
146 log.debug("monitor: {}", mode);
147 this.mode = mode;
148
149 switch (mode) {
150 case ALL_FLOW_TRAFFIC:
151 clearSelection();
152 scheduleTask();
153 sendAllFlowTraffic();
154 break;
155
156 case ALL_PORT_TRAFFIC:
157 clearSelection();
158 scheduleTask();
159 sendAllPortTraffic();
160 break;
161
Simon Hunt4fc86852015-08-20 17:57:52 -0700162 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700163 scheduleTask();
164 sendSelectedIntentTraffic();
165 break;
166
167 default:
168 log.debug("Unexpected call to monitor({})", mode);
169 clearAll();
170 break;
171 }
172 }
173
Simon Hunt4fc86852015-08-20 17:57:52 -0700174 /**
175 * Monitor for traffic data to be sent back to the web client, under
176 * the given mode, using the given selection of devices and hosts.
177 * In the case of "device link flows", this causes a background traffic
178 * task to be scheduled to repeatedly compute and transmit the appropriate
179 * traffic data to the client. In the case of "related intents", no
180 * repeating task is scheduled.
181 * <p>
182 * The monitoring mode is expected to be one of:
183 * <ul>
Simon Hunt5328f792017-01-11 17:43:31 -0800184 * <li>DEV_LINK_FLOWS</li>
185 * <li>RELATED_INTENTS</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700186 * </ul>
187 *
Simon Hunt5328f792017-01-11 17:43:31 -0800188 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700189 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700190 */
Simon Hunta17fa672015-08-19 18:42:22 -0700191 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
192 log.debug("monitor: {} -- {}", mode, nodeSelection);
193 this.mode = mode;
194 this.selectedNodes = nodeSelection;
195
196 switch (mode) {
197 case DEV_LINK_FLOWS:
198 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700199 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700200 sendClearAll();
201 } else {
202 scheduleTask();
203 sendDeviceLinkFlows();
204 }
205 break;
206
207 case RELATED_INTENTS:
208 if (selectedNodes.none()) {
209 sendClearAll();
210 } else {
211 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
212 if (selectedIntents.none()) {
213 sendClearAll();
214 } else {
215 sendSelectedIntents();
216 }
217 }
218 break;
219
220 default:
221 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
222 clearAll();
223 break;
224 }
225 }
226
Simon Hunt4fc86852015-08-20 17:57:52 -0700227 // TODO: move this out to the "h2h/multi-intent app"
Simon Hunt5328f792017-01-11 17:43:31 -0800228
Simon Hunt4fc86852015-08-20 17:57:52 -0700229 /**
230 * Monitor for traffic data to be sent back to the web client, for the
231 * given intent.
232 *
233 * @param intent the intent to monitor
234 */
Simon Hunta17fa672015-08-19 18:42:22 -0700235 public synchronized void monitor(Intent intent) {
236 log.debug("monitor intent: {}", intent.id());
237 selectedNodes = null;
238 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700239 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700240 scheduleTask();
241 sendSelectedIntentTraffic();
242 }
243
Simon Hunt4fc86852015-08-20 17:57:52 -0700244 /**
245 * Selects the next intent in the select group (if there is one),
246 * and sends highlighting data back to the web client to display
247 * which path is selected.
248 */
Simon Hunta17fa672015-08-19 18:42:22 -0700249 public synchronized void selectNextIntent() {
250 if (selectedIntents != null) {
251 selectedIntents.next();
252 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700253 if (mode == SELECTED_INTENT) {
254 mode = RELATED_INTENTS;
255 }
Simon Hunta17fa672015-08-19 18:42:22 -0700256 }
257 }
258
Simon Hunt4fc86852015-08-20 17:57:52 -0700259 /**
260 * Selects the previous intent in the select group (if there is one),
261 * and sends highlighting data back to the web client to display
262 * which path is selected.
263 */
Simon Hunta17fa672015-08-19 18:42:22 -0700264 public synchronized void selectPreviousIntent() {
265 if (selectedIntents != null) {
266 selectedIntents.prev();
267 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700268 if (mode == SELECTED_INTENT) {
269 mode = RELATED_INTENTS;
270 }
Simon Hunta17fa672015-08-19 18:42:22 -0700271 }
272 }
273
Simon Hunt4fc86852015-08-20 17:57:52 -0700274 /**
275 * Resends selected intent traffic data. This is called, for example,
276 * when the system detects an intent update happened.
277 */
Simon Hunta17fa672015-08-19 18:42:22 -0700278 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700279 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700280 sendSelectedIntentTraffic();
281 }
282 }
283
Simon Hunt4fc86852015-08-20 17:57:52 -0700284 /**
285 * Stop all traffic monitoring.
286 */
287 public synchronized void stopMonitoring() {
288 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700289 if (mode != IDLE) {
290 sendClearAll();
291 }
292 }
293
294
295 // =======================================================================
296 // === Helper methods ===
297
298 private void sendClearAll() {
299 clearAll();
300 sendClearHighlights();
301 }
302
303 private void clearAll() {
304 this.mode = IDLE;
305 clearSelection();
306 cancelTask();
307 }
308
309 private void clearSelection() {
310 selectedNodes = null;
311 selectedIntents = null;
312 }
313
Simon Hunt5328f792017-01-11 17:43:31 -0800314 private synchronized void scheduleTask() {
Simon Hunta17fa672015-08-19 18:42:22 -0700315 if (trafficTask == null) {
316 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700317 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700318 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
319 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700320 log.debug("(traffic task already running)");
321 }
322 }
323
324 private synchronized void cancelTask() {
325 if (trafficTask != null) {
326 trafficTask.cancel();
327 trafficTask = null;
328 }
329 }
330
Simon Hunta17fa672015-08-19 18:42:22 -0700331 private void sendAllFlowTraffic() {
332 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700333 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700334 }
335
336 private void sendAllPortTraffic() {
337 log.debug("sendAllPortTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700338 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700339 }
340
341 private void sendDeviceLinkFlows() {
342 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700343 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700344 }
345
346 private void sendSelectedIntents() {
347 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700348 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700349 }
350
351 private void sendSelectedIntentTraffic() {
352 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700353 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700354 }
355
356 private void sendClearHighlights() {
357 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700358 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700359 }
360
Simon Hunta17fa672015-08-19 18:42:22 -0700361 // =======================================================================
362 // === Generate messages in JSON object node format
363
Simon Hunt57830172015-08-26 13:25:17 -0700364 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700365 Highlights highlights = new Highlights();
366
Simon Hunt4fc86852015-08-20 17:57:52 -0700367 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700368 compileLinks(linkMap);
369 addEdgeLinks(linkMap);
370
Simon Hunt4fc86852015-08-20 17:57:52 -0700371 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700372 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700373 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700374 } else if (type == StatsType.PORT_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700375 attachPortLoad(tlink);
Simon Hunta17fa672015-08-19 18:42:22 -0700376 }
377
378 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700379 if (tlink.hasTraffic()) {
380 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700381 }
382 }
383 return highlights;
384 }
385
386 // create highlights for links, showing flows for selected devices.
387 private Highlights deviceLinkFlows() {
388 Highlights highlights = new Highlights();
389
Simon Hunt72297212015-08-25 10:15:33 -0700390 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700391 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700392 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700393
Simon Hunt72297212015-08-25 10:15:33 -0700394 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700395 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
396 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700397 TrafficLink tlink = linkMap.add(link);
398 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700399 }
400 }
401
402 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700403 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700404 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700405 }
406
407 }
408 return highlights;
409 }
410
411 private Highlights intentGroup() {
412 Highlights highlights = new Highlights();
413
414 if (selectedIntents != null && !selectedIntents.none()) {
415 // If 'all' intents are selected, they will all have primary
416 // highlighting; otherwise, the specifically selected intent will
417 // have primary highlighting, and the remainder will have secondary
418 // highlighting.
419 Set<Intent> primary;
420 Set<Intent> secondary;
421 int count = selectedIntents.size();
422
423 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
424 Intent current;
425
426 if (selectedIntents.all()) {
427 primary = allBut;
428 secondary = Collections.emptySet();
429 log.debug("Highlight all intents ({})", count);
430 } else {
431 current = selectedIntents.current();
432 primary = new HashSet<>();
433 primary.add(current);
434 allBut.remove(current);
435 secondary = allBut;
436 log.debug("Highlight intent: {} ([{}] of {})",
Simon Hunt5328f792017-01-11 17:43:31 -0800437 current.id(), selectedIntents.index(), count);
Simon Hunta17fa672015-08-19 18:42:22 -0700438 }
Simon Hunt57830172015-08-26 13:25:17 -0700439
440 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700441 }
442 return highlights;
443 }
444
445 private Highlights intentTraffic() {
446 Highlights highlights = new Highlights();
447
448 if (selectedIntents != null && selectedIntents.single()) {
449 Intent current = selectedIntents.current();
450 Set<Intent> primary = new HashSet<>();
451 primary.add(current);
452 log.debug("Highlight traffic for intent: {} ([{}] of {})",
Simon Hunt5328f792017-01-11 17:43:31 -0800453 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700454
455 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700456 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700457 }
458 return highlights;
459 }
460
Simon Hunta17fa672015-08-19 18:42:22 -0700461 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
462
Simon Hunt4fc86852015-08-20 17:57:52 -0700463 private void compileLinks(TrafficLinkMap linkMap) {
464 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700465 }
466
Simon Hunt4fc86852015-08-20 17:57:52 -0700467 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700468 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700469 linkMap.add(createEdgeLink(host, true));
470 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700471 });
472 }
473
474 private Load getLinkFlowLoad(Link link) {
475 if (link != null && link.src().elementId() instanceof DeviceId) {
476 return servicesBundle.flowStatsService().load(link);
477 }
478 return null;
479 }
480
Simon Hunt4fc86852015-08-20 17:57:52 -0700481 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700482 link.addLoad(getLinkFlowLoad(link.one()));
483 link.addLoad(getLinkFlowLoad(link.two()));
484 }
485
Simon Hunt4fc86852015-08-20 17:57:52 -0700486 private void attachPortLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700487 // For bi-directional traffic links, use
488 // the max link rate of either direction
489 // (we choose 'one' since we know that is never null)
490 Link one = link.one();
491 Load egressSrc = servicesBundle.portStatsService().load(one.src());
492 Load egressDst = servicesBundle.portStatsService().load(one.dst());
Simon Hunt5f31a022015-08-20 08:43:25 -0700493 link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD);
Simon Hunt4fc86852015-08-20 17:57:52 -0700494// link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!!
Simon Hunta17fa672015-08-19 18:42:22 -0700495 }
496
497 private Load maxLoad(Load a, Load b) {
498 if (a == null) {
499 return b;
500 }
501 if (b == null) {
502 return a;
503 }
504 return a.rate() > b.rate() ? a : b;
505 }
506
Simon Hunta17fa672015-08-19 18:42:22 -0700507 // Counts all flow entries that egress on the links of the given device.
508 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
509 // get the flows for the device
510 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt5328f792017-01-11 17:43:31 -0800511 for (FlowEntry flowEntry : servicesBundle.flowService().getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700512 entries.add(flowEntry);
513 }
514
515 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700516 Set<Link> links = new HashSet<>(servicesBundle.linkService()
Simon Hunt5328f792017-01-11 17:43:31 -0800517 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700518 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
519 if (hosts != null) {
520 for (Host host : hosts) {
521 links.add(createEdgeLink(host, false));
522 }
523 }
524
525 // compile flow counts per link
526 Map<Link, Integer> counts = new HashMap<>();
527 for (Link link : links) {
528 counts.put(link, getEgressFlows(link, entries));
529 }
530 return counts;
531 }
532
533 // Counts all entries that egress on the link source port.
534 private int getEgressFlows(Link link, List<FlowEntry> entries) {
535 int count = 0;
536 PortNumber out = link.src().port();
537 for (FlowEntry entry : entries) {
538 TrafficTreatment treatment = entry.treatment();
539 for (Instruction instruction : treatment.allInstructions()) {
540 if (instruction.type() == Instruction.Type.OUTPUT &&
541 ((OutputInstruction) instruction).port().equals(out)) {
542 count++;
543 }
544 }
545 }
546 return count;
547 }
548
Simon Hunt57830172015-08-26 13:25:17 -0700549 private void highlightIntentLinks(Highlights highlights,
550 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700551 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700552 // NOTE: highlight secondary first, then primary, so that links shared
553 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700554 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
555 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700556 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700557 }
558
Simon Hunt57830172015-08-26 13:25:17 -0700559 private void highlightIntentLinksWithTraffic(Highlights highlights,
560 Set<Intent> primary) {
561 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700562 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700563 colorLinks(highlights, linkMap);
564 }
565
Simon Hunt94f7dae2015-08-26 17:40:59 -0700566 private void createTrafficLinks(Highlights highlights,
567 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700568 Flavor flavor, boolean showTraffic) {
569 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700570 List<Intent> installables = servicesBundle.intentService()
571 .getInstallableIntents(intent.key());
572 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700573 if (installables != null) {
574 for (Intent installable : installables) {
575
576 if (installable instanceof PathIntent) {
577 links = ((PathIntent) installable).path().links();
578 } else if (installable instanceof FlowRuleIntent) {
Simon Hunt5328f792017-01-11 17:43:31 -0800579 links = addEdgeLinksIfNeeded(intent, linkResources(installable));
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800580 } else if (installable instanceof FlowObjectiveIntent) {
Simon Hunt5328f792017-01-11 17:43:31 -0800581 links = addEdgeLinksIfNeeded(intent, linkResources(installable));
Simon Hunta17fa672015-08-19 18:42:22 -0700582 } else if (installable instanceof LinkCollectionIntent) {
583 links = ((LinkCollectionIntent) installable).links();
584 } else if (installable instanceof OpticalPathIntent) {
585 links = ((OpticalPathIntent) installable).path().links();
586 }
587
Simon Hunt57830172015-08-26 13:25:17 -0700588 boolean isOptical = intent instanceof OpticalConnectivityIntent;
589 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700590 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700591 }
592 }
593 }
594 }
595
Simon Hunt5328f792017-01-11 17:43:31 -0800596 private Iterable<Link> addEdgeLinksIfNeeded(Intent parentIntent,
597 Collection<Link> links) {
598 if (parentIntent instanceof HostToHostIntent) {
599 links = new HashSet<>(links);
600 HostToHostIntent h2h = (HostToHostIntent) parentIntent;
601 Host h1 = servicesBundle.hostService().getHost(h2h.one());
602 Host h2 = servicesBundle.hostService().getHost(h2h.two());
603 links.add(createEdgeLink(h1, true));
604 links.add(createEdgeLink(h2, true));
605 }
606 return links;
607 }
608
Simon Hunt94f7dae2015-08-26 17:40:59 -0700609 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
610 for (Link link : links) {
611 ensureNodePresent(highlights, link.src().elementId());
612 ensureNodePresent(highlights, link.dst().elementId());
613 }
614 }
615
616 private void ensureNodePresent(Highlights highlights, ElementId eid) {
617 String id = eid.toString();
618 NodeHighlight nh = highlights.getNode(id);
619 if (nh == null) {
620 if (eid instanceof DeviceId) {
621 nh = new DeviceHighlight(id);
622 highlights.add((DeviceHighlight) nh);
623 } else if (eid instanceof HostId) {
624 nh = new HostHighlight(id);
625 highlights.add((HostHighlight) nh);
626 }
627 }
628 }
629
Simon Hunta17fa672015-08-19 18:42:22 -0700630 // Extracts links from the specified flow rule intent resources
631 private Collection<Link> linkResources(Intent installable) {
632 ImmutableList.Builder<Link> builder = ImmutableList.builder();
633 installable.resources().stream().filter(r -> r instanceof Link)
634 .forEach(r -> builder.add((Link) r));
635 return builder.build();
636 }
637
Simon Hunt57830172015-08-26 13:25:17 -0700638 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
639 Flavor flavor, boolean isOptical,
640 boolean showTraffic) {
641 if (links != null) {
642 for (Link link : links) {
643 TrafficLink tlink = linkMap.add(link);
644 tlink.tagFlavor(flavor);
645 tlink.optical(isOptical);
646 if (showTraffic) {
647 tlink.addLoad(getLinkFlowLoad(link));
648 tlink.antMarch(true);
649 }
650 }
651 }
652 }
653
654 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
655 for (TrafficLink tlink : linkMap.biLinks()) {
656 highlights.add(tlink.highlight(StatsType.TAGGED));
657 }
658 }
659
Simon Hunta17fa672015-08-19 18:42:22 -0700660 // =======================================================================
661 // === Background Task
662
663 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700664 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700665 @Override
666 public void run() {
667 try {
668 switch (mode) {
669 case ALL_FLOW_TRAFFIC:
670 sendAllFlowTraffic();
671 break;
672 case ALL_PORT_TRAFFIC:
673 sendAllPortTraffic();
674 break;
675 case DEV_LINK_FLOWS:
676 sendDeviceLinkFlows();
677 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700678 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700679 sendSelectedIntentTraffic();
680 break;
681
682 default:
683 // RELATED_INTENTS and IDLE modes should never invoke
684 // the background task, but if they do, they have
685 // nothing to do
686 break;
687 }
688
689 } catch (Exception e) {
690 log.warn("Unable to process traffic task due to {}", e.getMessage());
691 log.warn("Boom!", e);
692 }
693 }
694 }
Simon Hunta17fa672015-08-19 18:42:22 -0700695}