blob: 2849fd9a351f7e3cee39b980519368ba05b52269 [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;
34import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.LinkCollectionIntent;
36import org.onosproject.net.intent.OpticalConnectivityIntent;
37import org.onosproject.net.intent.OpticalPathIntent;
38import org.onosproject.net.intent.PathIntent;
39import org.onosproject.net.statistic.Load;
Simon Hunted804d52016-03-30 09:51:40 -070040import org.onosproject.ui.impl.topo.util.IntentSelection;
41import org.onosproject.ui.impl.topo.util.ServicesBundle;
42import org.onosproject.ui.impl.topo.util.TopoIntentFilter;
43import org.onosproject.ui.impl.topo.util.TrafficLink;
44import org.onosproject.ui.impl.topo.util.TrafficLink.StatsType;
45import org.onosproject.ui.impl.topo.util.TrafficLinkMap;
Simon Hunt94f7dae2015-08-26 17:40:59 -070046import org.onosproject.ui.topo.DeviceHighlight;
Simon Hunta17fa672015-08-19 18:42:22 -070047import org.onosproject.ui.topo.Highlights;
Simon Hunt94f7dae2015-08-26 17:40:59 -070048import org.onosproject.ui.topo.Highlights.Amount;
49import org.onosproject.ui.topo.HostHighlight;
Simon Hunt57830172015-08-26 13:25:17 -070050import org.onosproject.ui.topo.LinkHighlight.Flavor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070051import org.onosproject.ui.topo.NodeHighlight;
Simon Hunt743a8492015-08-25 16:18:19 -070052import org.onosproject.ui.topo.NodeSelection;
53import org.onosproject.ui.topo.TopoUtils;
Simon Hunta17fa672015-08-19 18:42:22 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import java.util.ArrayList;
58import java.util.Collection;
59import java.util.Collections;
60import java.util.HashMap;
61import java.util.HashSet;
62import java.util.List;
63import java.util.Map;
64import java.util.Set;
65import java.util.Timer;
66import java.util.TimerTask;
67
68import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunted804d52016-03-30 09:51:40 -070069import static org.onosproject.ui.impl.TrafficMonitor.Mode.IDLE;
70import static org.onosproject.ui.impl.TrafficMonitor.Mode.RELATED_INTENTS;
71import static org.onosproject.ui.impl.TrafficMonitor.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070072
73/**
74 * Encapsulates the behavior of monitoring specific traffic patterns.
75 */
Simon Hunt4fc86852015-08-20 17:57:52 -070076public class TrafficMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070077
78 // 4 Kilo Bytes as threshold
79 private static final double BPS_THRESHOLD = 4 * TopoUtils.KILO;
80
81 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070082 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070083
84 /**
85 * Designates the different modes of operation.
86 */
87 public enum Mode {
88 IDLE,
89 ALL_FLOW_TRAFFIC,
90 ALL_PORT_TRAFFIC,
91 DEV_LINK_FLOWS,
92 RELATED_INTENTS,
Simon Hunt4fc86852015-08-20 17:57:52 -070093 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -070094 }
95
96 private final long trafficPeriod;
97 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070098 private final TopologyViewMessageHandler msgHandler;
99 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -0700100
101 private final Timer timer = new Timer("topo-traffic");
102
103 private TimerTask trafficTask = null;
104 private Mode mode = IDLE;
105 private NodeSelection selectedNodes = null;
106 private IntentSelection selectedIntents = null;
107
108
109 /**
110 * Constructs a traffic monitor.
111 *
112 * @param trafficPeriod traffic task period in ms
113 * @param servicesBundle bundle of services
Simon Hunt4fc86852015-08-20 17:57:52 -0700114 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700115 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700116 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
117 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700118 this.trafficPeriod = trafficPeriod;
119 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700120 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700121
Simon Hunt4fc86852015-08-20 17:57:52 -0700122 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700123 }
124
125 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700126 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700127
Simon Hunt4fc86852015-08-20 17:57:52 -0700128 /**
129 * Monitor for traffic data to be sent back to the web client, under
130 * the given mode. This causes a background traffic task to be
131 * scheduled to repeatedly compute and transmit the appropriate traffic
132 * data to the client.
133 * <p>
134 * The monitoring mode is expected to be one of:
135 * <ul>
136 * <li>ALL_FLOW_TRAFFIC</li>
137 * <li>ALL_PORT_TRAFFIC</li>
138 * <li>SELECTED_INTENT</li>
139 * </ul>
140 *
141 * @param mode monitoring mode
142 */
Simon Hunta17fa672015-08-19 18:42:22 -0700143 public synchronized void monitor(Mode mode) {
144 log.debug("monitor: {}", mode);
145 this.mode = mode;
146
147 switch (mode) {
148 case ALL_FLOW_TRAFFIC:
149 clearSelection();
150 scheduleTask();
151 sendAllFlowTraffic();
152 break;
153
154 case ALL_PORT_TRAFFIC:
155 clearSelection();
156 scheduleTask();
157 sendAllPortTraffic();
158 break;
159
Simon Hunt4fc86852015-08-20 17:57:52 -0700160 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700161 scheduleTask();
162 sendSelectedIntentTraffic();
163 break;
164
165 default:
166 log.debug("Unexpected call to monitor({})", mode);
167 clearAll();
168 break;
169 }
170 }
171
Simon Hunt4fc86852015-08-20 17:57:52 -0700172 /**
173 * Monitor for traffic data to be sent back to the web client, under
174 * the given mode, using the given selection of devices and hosts.
175 * In the case of "device link flows", this causes a background traffic
176 * task to be scheduled to repeatedly compute and transmit the appropriate
177 * traffic data to the client. In the case of "related intents", no
178 * repeating task is scheduled.
179 * <p>
180 * The monitoring mode is expected to be one of:
181 * <ul>
182 * <li>DEV_LINK_FLOWS</li>
183 * <li>RELATED_INTENTS</li>
184 * </ul>
185 *
186 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700187 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700188 */
Simon Hunta17fa672015-08-19 18:42:22 -0700189 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
190 log.debug("monitor: {} -- {}", mode, nodeSelection);
191 this.mode = mode;
192 this.selectedNodes = nodeSelection;
193
194 switch (mode) {
195 case DEV_LINK_FLOWS:
196 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700197 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700198 sendClearAll();
199 } else {
200 scheduleTask();
201 sendDeviceLinkFlows();
202 }
203 break;
204
205 case RELATED_INTENTS:
206 if (selectedNodes.none()) {
207 sendClearAll();
208 } else {
209 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
210 if (selectedIntents.none()) {
211 sendClearAll();
212 } else {
213 sendSelectedIntents();
214 }
215 }
216 break;
217
218 default:
219 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
220 clearAll();
221 break;
222 }
223 }
224
Simon Hunt4fc86852015-08-20 17:57:52 -0700225 // TODO: move this out to the "h2h/multi-intent app"
226 /**
227 * Monitor for traffic data to be sent back to the web client, for the
228 * given intent.
229 *
230 * @param intent the intent to monitor
231 */
Simon Hunta17fa672015-08-19 18:42:22 -0700232 public synchronized void monitor(Intent intent) {
233 log.debug("monitor intent: {}", intent.id());
234 selectedNodes = null;
235 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700236 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700237 scheduleTask();
238 sendSelectedIntentTraffic();
239 }
240
Simon Hunt4fc86852015-08-20 17:57:52 -0700241 /**
242 * Selects the next intent in the select group (if there is one),
243 * and sends highlighting data back to the web client to display
244 * which path is selected.
245 */
Simon Hunta17fa672015-08-19 18:42:22 -0700246 public synchronized void selectNextIntent() {
247 if (selectedIntents != null) {
248 selectedIntents.next();
249 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700250 if (mode == SELECTED_INTENT) {
251 mode = RELATED_INTENTS;
252 }
Simon Hunta17fa672015-08-19 18:42:22 -0700253 }
254 }
255
Simon Hunt4fc86852015-08-20 17:57:52 -0700256 /**
257 * Selects the previous intent in the select group (if there is one),
258 * and sends highlighting data back to the web client to display
259 * which path is selected.
260 */
Simon Hunta17fa672015-08-19 18:42:22 -0700261 public synchronized void selectPreviousIntent() {
262 if (selectedIntents != null) {
263 selectedIntents.prev();
264 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700265 if (mode == SELECTED_INTENT) {
266 mode = RELATED_INTENTS;
267 }
Simon Hunta17fa672015-08-19 18:42:22 -0700268 }
269 }
270
Simon Hunt4fc86852015-08-20 17:57:52 -0700271 /**
272 * Resends selected intent traffic data. This is called, for example,
273 * when the system detects an intent update happened.
274 */
Simon Hunta17fa672015-08-19 18:42:22 -0700275 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700276 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700277 sendSelectedIntentTraffic();
278 }
279 }
280
Simon Hunt4fc86852015-08-20 17:57:52 -0700281 /**
282 * Stop all traffic monitoring.
283 */
284 public synchronized void stopMonitoring() {
285 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700286 if (mode != IDLE) {
287 sendClearAll();
288 }
289 }
290
291
292 // =======================================================================
293 // === Helper methods ===
294
295 private void sendClearAll() {
296 clearAll();
297 sendClearHighlights();
298 }
299
300 private void clearAll() {
301 this.mode = IDLE;
302 clearSelection();
303 cancelTask();
304 }
305
306 private void clearSelection() {
307 selectedNodes = null;
308 selectedIntents = null;
309 }
310
311 private synchronized void scheduleTask() {
312 if (trafficTask == null) {
313 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700314 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700315 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
316 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700317 log.debug("(traffic task already running)");
318 }
319 }
320
321 private synchronized void cancelTask() {
322 if (trafficTask != null) {
323 trafficTask.cancel();
324 trafficTask = null;
325 }
326 }
327
Simon Hunta17fa672015-08-19 18:42:22 -0700328 private void sendAllFlowTraffic() {
329 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700330 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700331 }
332
333 private void sendAllPortTraffic() {
334 log.debug("sendAllPortTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700335 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700336 }
337
338 private void sendDeviceLinkFlows() {
339 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700340 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700341 }
342
343 private void sendSelectedIntents() {
344 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700345 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700346 }
347
348 private void sendSelectedIntentTraffic() {
349 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700350 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700351 }
352
353 private void sendClearHighlights() {
354 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700355 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700356 }
357
Simon Hunta17fa672015-08-19 18:42:22 -0700358 // =======================================================================
359 // === Generate messages in JSON object node format
360
Simon Hunt57830172015-08-26 13:25:17 -0700361 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700362 Highlights highlights = new Highlights();
363
Simon Hunt4fc86852015-08-20 17:57:52 -0700364 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700365 compileLinks(linkMap);
366 addEdgeLinks(linkMap);
367
Simon Hunt4fc86852015-08-20 17:57:52 -0700368 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700369 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700370 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700371 } else if (type == StatsType.PORT_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700372 attachPortLoad(tlink);
Simon Hunta17fa672015-08-19 18:42:22 -0700373 }
374
375 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700376 if (tlink.hasTraffic()) {
377 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700378 }
379 }
380 return highlights;
381 }
382
383 // create highlights for links, showing flows for selected devices.
384 private Highlights deviceLinkFlows() {
385 Highlights highlights = new Highlights();
386
Simon Hunt72297212015-08-25 10:15:33 -0700387 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700388 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700389 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700390
Simon Hunt72297212015-08-25 10:15:33 -0700391 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700392 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
393 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700394 TrafficLink tlink = linkMap.add(link);
395 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700396 }
397 }
398
399 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700400 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700401 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700402 }
403
404 }
405 return highlights;
406 }
407
408 private Highlights intentGroup() {
409 Highlights highlights = new Highlights();
410
411 if (selectedIntents != null && !selectedIntents.none()) {
412 // If 'all' intents are selected, they will all have primary
413 // highlighting; otherwise, the specifically selected intent will
414 // have primary highlighting, and the remainder will have secondary
415 // highlighting.
416 Set<Intent> primary;
417 Set<Intent> secondary;
418 int count = selectedIntents.size();
419
420 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
421 Intent current;
422
423 if (selectedIntents.all()) {
424 primary = allBut;
425 secondary = Collections.emptySet();
426 log.debug("Highlight all intents ({})", count);
427 } else {
428 current = selectedIntents.current();
429 primary = new HashSet<>();
430 primary.add(current);
431 allBut.remove(current);
432 secondary = allBut;
433 log.debug("Highlight intent: {} ([{}] of {})",
434 current.id(), selectedIntents.index(), count);
435 }
Simon Hunt57830172015-08-26 13:25:17 -0700436
437 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700438 }
439 return highlights;
440 }
441
442 private Highlights intentTraffic() {
443 Highlights highlights = new Highlights();
444
445 if (selectedIntents != null && selectedIntents.single()) {
446 Intent current = selectedIntents.current();
447 Set<Intent> primary = new HashSet<>();
448 primary.add(current);
449 log.debug("Highlight traffic for intent: {} ([{}] of {})",
450 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700451
452 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700453 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700454 }
455 return highlights;
456 }
457
Simon Hunta17fa672015-08-19 18:42:22 -0700458 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
459
Simon Hunt4fc86852015-08-20 17:57:52 -0700460 private void compileLinks(TrafficLinkMap linkMap) {
461 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700462 }
463
Simon Hunt4fc86852015-08-20 17:57:52 -0700464 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700465 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700466 linkMap.add(createEdgeLink(host, true));
467 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700468 });
469 }
470
471 private Load getLinkFlowLoad(Link link) {
472 if (link != null && link.src().elementId() instanceof DeviceId) {
473 return servicesBundle.flowStatsService().load(link);
474 }
475 return null;
476 }
477
Simon Hunt4fc86852015-08-20 17:57:52 -0700478 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700479 link.addLoad(getLinkFlowLoad(link.one()));
480 link.addLoad(getLinkFlowLoad(link.two()));
481 }
482
Simon Hunt4fc86852015-08-20 17:57:52 -0700483 private void attachPortLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700484 // For bi-directional traffic links, use
485 // the max link rate of either direction
486 // (we choose 'one' since we know that is never null)
487 Link one = link.one();
488 Load egressSrc = servicesBundle.portStatsService().load(one.src());
489 Load egressDst = servicesBundle.portStatsService().load(one.dst());
Simon Hunt5f31a022015-08-20 08:43:25 -0700490 link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD);
Simon Hunt4fc86852015-08-20 17:57:52 -0700491// link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!!
Simon Hunta17fa672015-08-19 18:42:22 -0700492 }
493
494 private Load maxLoad(Load a, Load b) {
495 if (a == null) {
496 return b;
497 }
498 if (b == null) {
499 return a;
500 }
501 return a.rate() > b.rate() ? a : b;
502 }
503
Simon Hunta17fa672015-08-19 18:42:22 -0700504 // Counts all flow entries that egress on the links of the given device.
505 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
506 // get the flows for the device
507 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt4fc86852015-08-20 17:57:52 -0700508 for (FlowEntry flowEntry : servicesBundle.flowService()
509 .getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700510 entries.add(flowEntry);
511 }
512
513 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700514 Set<Link> links = new HashSet<>(servicesBundle.linkService()
515 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700516 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
517 if (hosts != null) {
518 for (Host host : hosts) {
519 links.add(createEdgeLink(host, false));
520 }
521 }
522
523 // compile flow counts per link
524 Map<Link, Integer> counts = new HashMap<>();
525 for (Link link : links) {
526 counts.put(link, getEgressFlows(link, entries));
527 }
528 return counts;
529 }
530
531 // Counts all entries that egress on the link source port.
532 private int getEgressFlows(Link link, List<FlowEntry> entries) {
533 int count = 0;
534 PortNumber out = link.src().port();
535 for (FlowEntry entry : entries) {
536 TrafficTreatment treatment = entry.treatment();
537 for (Instruction instruction : treatment.allInstructions()) {
538 if (instruction.type() == Instruction.Type.OUTPUT &&
539 ((OutputInstruction) instruction).port().equals(out)) {
540 count++;
541 }
542 }
543 }
544 return count;
545 }
546
Simon Hunt57830172015-08-26 13:25:17 -0700547 private void highlightIntentLinks(Highlights highlights,
548 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700549 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700550 // NOTE: highlight secondary first, then primary, so that links shared
551 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700552 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
553 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700554 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700555 }
556
Simon Hunt57830172015-08-26 13:25:17 -0700557 private void highlightIntentLinksWithTraffic(Highlights highlights,
558 Set<Intent> primary) {
559 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700560 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700561 colorLinks(highlights, linkMap);
562 }
563
Simon Hunt94f7dae2015-08-26 17:40:59 -0700564 private void createTrafficLinks(Highlights highlights,
565 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700566 Flavor flavor, boolean showTraffic) {
567 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700568 List<Intent> installables = servicesBundle.intentService()
569 .getInstallableIntents(intent.key());
570 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700571 if (installables != null) {
572 for (Intent installable : installables) {
573
574 if (installable instanceof PathIntent) {
575 links = ((PathIntent) installable).path().links();
576 } else if (installable instanceof FlowRuleIntent) {
577 links = linkResources(installable);
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800578 } else if (installable instanceof FlowObjectiveIntent) {
579 links = linkResources(installable);
Simon Hunta17fa672015-08-19 18:42:22 -0700580 } else if (installable instanceof LinkCollectionIntent) {
581 links = ((LinkCollectionIntent) installable).links();
582 } else if (installable instanceof OpticalPathIntent) {
583 links = ((OpticalPathIntent) installable).path().links();
584 }
585
Simon Hunt57830172015-08-26 13:25:17 -0700586 boolean isOptical = intent instanceof OpticalConnectivityIntent;
587 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700588 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700589 }
590 }
591 }
592 }
593
Simon Hunt94f7dae2015-08-26 17:40:59 -0700594 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
595 for (Link link : links) {
596 ensureNodePresent(highlights, link.src().elementId());
597 ensureNodePresent(highlights, link.dst().elementId());
598 }
599 }
600
601 private void ensureNodePresent(Highlights highlights, ElementId eid) {
602 String id = eid.toString();
603 NodeHighlight nh = highlights.getNode(id);
604 if (nh == null) {
605 if (eid instanceof DeviceId) {
606 nh = new DeviceHighlight(id);
607 highlights.add((DeviceHighlight) nh);
608 } else if (eid instanceof HostId) {
609 nh = new HostHighlight(id);
610 highlights.add((HostHighlight) nh);
611 }
612 }
613 }
614
Simon Hunta17fa672015-08-19 18:42:22 -0700615 // Extracts links from the specified flow rule intent resources
616 private Collection<Link> linkResources(Intent installable) {
617 ImmutableList.Builder<Link> builder = ImmutableList.builder();
618 installable.resources().stream().filter(r -> r instanceof Link)
619 .forEach(r -> builder.add((Link) r));
620 return builder.build();
621 }
622
Simon Hunt57830172015-08-26 13:25:17 -0700623 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
624 Flavor flavor, boolean isOptical,
625 boolean showTraffic) {
626 if (links != null) {
627 for (Link link : links) {
628 TrafficLink tlink = linkMap.add(link);
629 tlink.tagFlavor(flavor);
630 tlink.optical(isOptical);
631 if (showTraffic) {
632 tlink.addLoad(getLinkFlowLoad(link));
633 tlink.antMarch(true);
634 }
635 }
636 }
637 }
638
639 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
640 for (TrafficLink tlink : linkMap.biLinks()) {
641 highlights.add(tlink.highlight(StatsType.TAGGED));
642 }
643 }
644
Simon Hunta17fa672015-08-19 18:42:22 -0700645 // =======================================================================
646 // === Background Task
647
648 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700649 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700650 @Override
651 public void run() {
652 try {
653 switch (mode) {
654 case ALL_FLOW_TRAFFIC:
655 sendAllFlowTraffic();
656 break;
657 case ALL_PORT_TRAFFIC:
658 sendAllPortTraffic();
659 break;
660 case DEV_LINK_FLOWS:
661 sendDeviceLinkFlows();
662 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700663 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700664 sendSelectedIntentTraffic();
665 break;
666
667 default:
668 // RELATED_INTENTS and IDLE modes should never invoke
669 // the background task, but if they do, they have
670 // nothing to do
671 break;
672 }
673
674 } catch (Exception e) {
675 log.warn("Unable to process traffic task due to {}", e.getMessage());
676 log.warn("Boom!", e);
677 }
678 }
679 }
Simon Hunta17fa672015-08-19 18:42:22 -0700680}