blob: 3d733f9ebad9649aeeeea4fa92231197c88f4bc3 [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
Ray Milkey9b36d812015-09-09 15:24:54 -0700184 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700185 */
Simon Hunta17fa672015-08-19 18:42:22 -0700186 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
187 log.debug("monitor: {} -- {}", mode, nodeSelection);
188 this.mode = mode;
189 this.selectedNodes = nodeSelection;
190
191 switch (mode) {
192 case DEV_LINK_FLOWS:
193 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700194 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700195 sendClearAll();
196 } else {
197 scheduleTask();
198 sendDeviceLinkFlows();
199 }
200 break;
201
202 case RELATED_INTENTS:
203 if (selectedNodes.none()) {
204 sendClearAll();
205 } else {
206 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
207 if (selectedIntents.none()) {
208 sendClearAll();
209 } else {
210 sendSelectedIntents();
211 }
212 }
213 break;
214
215 default:
216 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
217 clearAll();
218 break;
219 }
220 }
221
Simon Hunt4fc86852015-08-20 17:57:52 -0700222 // TODO: move this out to the "h2h/multi-intent app"
223 /**
224 * Monitor for traffic data to be sent back to the web client, for the
225 * given intent.
226 *
227 * @param intent the intent to monitor
228 */
Simon Hunta17fa672015-08-19 18:42:22 -0700229 public synchronized void monitor(Intent intent) {
230 log.debug("monitor intent: {}", intent.id());
231 selectedNodes = null;
232 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700233 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700234 scheduleTask();
235 sendSelectedIntentTraffic();
236 }
237
Simon Hunt4fc86852015-08-20 17:57:52 -0700238 /**
239 * Selects the next intent in the select group (if there is one),
240 * and sends highlighting data back to the web client to display
241 * which path is selected.
242 */
Simon Hunta17fa672015-08-19 18:42:22 -0700243 public synchronized void selectNextIntent() {
244 if (selectedIntents != null) {
245 selectedIntents.next();
246 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700247 if (mode == SELECTED_INTENT) {
248 mode = RELATED_INTENTS;
249 }
Simon Hunta17fa672015-08-19 18:42:22 -0700250 }
251 }
252
Simon Hunt4fc86852015-08-20 17:57:52 -0700253 /**
254 * Selects the previous intent in the select group (if there is one),
255 * and sends highlighting data back to the web client to display
256 * which path is selected.
257 */
Simon Hunta17fa672015-08-19 18:42:22 -0700258 public synchronized void selectPreviousIntent() {
259 if (selectedIntents != null) {
260 selectedIntents.prev();
261 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700262 if (mode == SELECTED_INTENT) {
263 mode = RELATED_INTENTS;
264 }
Simon Hunta17fa672015-08-19 18:42:22 -0700265 }
266 }
267
Simon Hunt4fc86852015-08-20 17:57:52 -0700268 /**
269 * Resends selected intent traffic data. This is called, for example,
270 * when the system detects an intent update happened.
271 */
Simon Hunta17fa672015-08-19 18:42:22 -0700272 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700273 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700274 sendSelectedIntentTraffic();
275 }
276 }
277
Simon Hunt4fc86852015-08-20 17:57:52 -0700278 /**
279 * Stop all traffic monitoring.
280 */
281 public synchronized void stopMonitoring() {
282 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700283 if (mode != IDLE) {
284 sendClearAll();
285 }
286 }
287
288
289 // =======================================================================
290 // === Helper methods ===
291
292 private void sendClearAll() {
293 clearAll();
294 sendClearHighlights();
295 }
296
297 private void clearAll() {
298 this.mode = IDLE;
299 clearSelection();
300 cancelTask();
301 }
302
303 private void clearSelection() {
304 selectedNodes = null;
305 selectedIntents = null;
306 }
307
308 private synchronized void scheduleTask() {
309 if (trafficTask == null) {
310 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700311 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700312 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
313 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700314 log.debug("(traffic task already running)");
315 }
316 }
317
318 private synchronized void cancelTask() {
319 if (trafficTask != null) {
320 trafficTask.cancel();
321 trafficTask = null;
322 }
323 }
324
Simon Hunta17fa672015-08-19 18:42:22 -0700325 private void sendAllFlowTraffic() {
326 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700327 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700328 }
329
330 private void sendAllPortTraffic() {
331 log.debug("sendAllPortTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700332 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700333 }
334
335 private void sendDeviceLinkFlows() {
336 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700337 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700338 }
339
340 private void sendSelectedIntents() {
341 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700342 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700343 }
344
345 private void sendSelectedIntentTraffic() {
346 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700347 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700348 }
349
350 private void sendClearHighlights() {
351 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700352 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700353 }
354
Simon Hunta17fa672015-08-19 18:42:22 -0700355 // =======================================================================
356 // === Generate messages in JSON object node format
357
Simon Hunt57830172015-08-26 13:25:17 -0700358 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700359 Highlights highlights = new Highlights();
360
Simon Hunt4fc86852015-08-20 17:57:52 -0700361 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700362 compileLinks(linkMap);
363 addEdgeLinks(linkMap);
364
Simon Hunt4fc86852015-08-20 17:57:52 -0700365 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700366 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700367 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700368 } else if (type == StatsType.PORT_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700369 attachPortLoad(tlink);
Simon Hunta17fa672015-08-19 18:42:22 -0700370 }
371
372 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700373 if (tlink.hasTraffic()) {
374 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700375 }
376 }
377 return highlights;
378 }
379
380 // create highlights for links, showing flows for selected devices.
381 private Highlights deviceLinkFlows() {
382 Highlights highlights = new Highlights();
383
Simon Hunt72297212015-08-25 10:15:33 -0700384 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700385 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700386 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700387
Simon Hunt72297212015-08-25 10:15:33 -0700388 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700389 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
390 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700391 TrafficLink tlink = linkMap.add(link);
392 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700393 }
394 }
395
396 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700397 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700398 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700399 }
400
401 }
402 return highlights;
403 }
404
405 private Highlights intentGroup() {
406 Highlights highlights = new Highlights();
407
408 if (selectedIntents != null && !selectedIntents.none()) {
409 // If 'all' intents are selected, they will all have primary
410 // highlighting; otherwise, the specifically selected intent will
411 // have primary highlighting, and the remainder will have secondary
412 // highlighting.
413 Set<Intent> primary;
414 Set<Intent> secondary;
415 int count = selectedIntents.size();
416
417 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
418 Intent current;
419
420 if (selectedIntents.all()) {
421 primary = allBut;
422 secondary = Collections.emptySet();
423 log.debug("Highlight all intents ({})", count);
424 } else {
425 current = selectedIntents.current();
426 primary = new HashSet<>();
427 primary.add(current);
428 allBut.remove(current);
429 secondary = allBut;
430 log.debug("Highlight intent: {} ([{}] of {})",
431 current.id(), selectedIntents.index(), count);
432 }
Simon Hunt57830172015-08-26 13:25:17 -0700433
434 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700435 }
436 return highlights;
437 }
438
439 private Highlights intentTraffic() {
440 Highlights highlights = new Highlights();
441
442 if (selectedIntents != null && selectedIntents.single()) {
443 Intent current = selectedIntents.current();
444 Set<Intent> primary = new HashSet<>();
445 primary.add(current);
446 log.debug("Highlight traffic for intent: {} ([{}] of {})",
447 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700448
449 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700450 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700451 }
452 return highlights;
453 }
454
Simon Hunta17fa672015-08-19 18:42:22 -0700455 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
456
Simon Hunt4fc86852015-08-20 17:57:52 -0700457 private void compileLinks(TrafficLinkMap linkMap) {
458 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700459 }
460
Simon Hunt4fc86852015-08-20 17:57:52 -0700461 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700462 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700463 linkMap.add(createEdgeLink(host, true));
464 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700465 });
466 }
467
468 private Load getLinkFlowLoad(Link link) {
469 if (link != null && link.src().elementId() instanceof DeviceId) {
470 return servicesBundle.flowStatsService().load(link);
471 }
472 return null;
473 }
474
Simon Hunt4fc86852015-08-20 17:57:52 -0700475 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700476 link.addLoad(getLinkFlowLoad(link.one()));
477 link.addLoad(getLinkFlowLoad(link.two()));
478 }
479
Simon Hunt4fc86852015-08-20 17:57:52 -0700480 private void attachPortLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700481 // For bi-directional traffic links, use
482 // the max link rate of either direction
483 // (we choose 'one' since we know that is never null)
484 Link one = link.one();
485 Load egressSrc = servicesBundle.portStatsService().load(one.src());
486 Load egressDst = servicesBundle.portStatsService().load(one.dst());
Simon Hunt5f31a022015-08-20 08:43:25 -0700487 link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD);
Simon Hunt4fc86852015-08-20 17:57:52 -0700488// link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!!
Simon Hunta17fa672015-08-19 18:42:22 -0700489 }
490
491 private Load maxLoad(Load a, Load b) {
492 if (a == null) {
493 return b;
494 }
495 if (b == null) {
496 return a;
497 }
498 return a.rate() > b.rate() ? a : b;
499 }
500
Simon Hunta17fa672015-08-19 18:42:22 -0700501 // Counts all flow entries that egress on the links of the given device.
502 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
503 // get the flows for the device
504 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt4fc86852015-08-20 17:57:52 -0700505 for (FlowEntry flowEntry : servicesBundle.flowService()
506 .getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700507 entries.add(flowEntry);
508 }
509
510 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700511 Set<Link> links = new HashSet<>(servicesBundle.linkService()
512 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700513 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
514 if (hosts != null) {
515 for (Host host : hosts) {
516 links.add(createEdgeLink(host, false));
517 }
518 }
519
520 // compile flow counts per link
521 Map<Link, Integer> counts = new HashMap<>();
522 for (Link link : links) {
523 counts.put(link, getEgressFlows(link, entries));
524 }
525 return counts;
526 }
527
528 // Counts all entries that egress on the link source port.
529 private int getEgressFlows(Link link, List<FlowEntry> entries) {
530 int count = 0;
531 PortNumber out = link.src().port();
532 for (FlowEntry entry : entries) {
533 TrafficTreatment treatment = entry.treatment();
534 for (Instruction instruction : treatment.allInstructions()) {
535 if (instruction.type() == Instruction.Type.OUTPUT &&
536 ((OutputInstruction) instruction).port().equals(out)) {
537 count++;
538 }
539 }
540 }
541 return count;
542 }
543
Simon Hunt57830172015-08-26 13:25:17 -0700544 private void highlightIntentLinks(Highlights highlights,
545 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700546 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700547 // NOTE: highlight secondary first, then primary, so that links shared
548 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700549 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
550 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700551 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700552 }
553
Simon Hunt57830172015-08-26 13:25:17 -0700554 private void highlightIntentLinksWithTraffic(Highlights highlights,
555 Set<Intent> primary) {
556 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700557 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700558 colorLinks(highlights, linkMap);
559 }
560
Simon Hunt94f7dae2015-08-26 17:40:59 -0700561 private void createTrafficLinks(Highlights highlights,
562 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700563 Flavor flavor, boolean showTraffic) {
564 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700565 List<Intent> installables = servicesBundle.intentService()
566 .getInstallableIntents(intent.key());
567 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700568 if (installables != null) {
569 for (Intent installable : installables) {
570
571 if (installable instanceof PathIntent) {
572 links = ((PathIntent) installable).path().links();
573 } else if (installable instanceof FlowRuleIntent) {
574 links = linkResources(installable);
575 } else if (installable instanceof LinkCollectionIntent) {
576 links = ((LinkCollectionIntent) installable).links();
577 } else if (installable instanceof OpticalPathIntent) {
578 links = ((OpticalPathIntent) installable).path().links();
579 }
580
Simon Hunt57830172015-08-26 13:25:17 -0700581 boolean isOptical = intent instanceof OpticalConnectivityIntent;
582 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700583 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700584 }
585 }
586 }
587 }
588
Simon Hunt94f7dae2015-08-26 17:40:59 -0700589 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
590 for (Link link : links) {
591 ensureNodePresent(highlights, link.src().elementId());
592 ensureNodePresent(highlights, link.dst().elementId());
593 }
594 }
595
596 private void ensureNodePresent(Highlights highlights, ElementId eid) {
597 String id = eid.toString();
598 NodeHighlight nh = highlights.getNode(id);
599 if (nh == null) {
600 if (eid instanceof DeviceId) {
601 nh = new DeviceHighlight(id);
602 highlights.add((DeviceHighlight) nh);
603 } else if (eid instanceof HostId) {
604 nh = new HostHighlight(id);
605 highlights.add((HostHighlight) nh);
606 }
607 }
608 }
609
Simon Hunta17fa672015-08-19 18:42:22 -0700610 // Extracts links from the specified flow rule intent resources
611 private Collection<Link> linkResources(Intent installable) {
612 ImmutableList.Builder<Link> builder = ImmutableList.builder();
613 installable.resources().stream().filter(r -> r instanceof Link)
614 .forEach(r -> builder.add((Link) r));
615 return builder.build();
616 }
617
Simon Hunt57830172015-08-26 13:25:17 -0700618 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
619 Flavor flavor, boolean isOptical,
620 boolean showTraffic) {
621 if (links != null) {
622 for (Link link : links) {
623 TrafficLink tlink = linkMap.add(link);
624 tlink.tagFlavor(flavor);
625 tlink.optical(isOptical);
626 if (showTraffic) {
627 tlink.addLoad(getLinkFlowLoad(link));
628 tlink.antMarch(true);
629 }
630 }
631 }
632 }
633
634 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
635 for (TrafficLink tlink : linkMap.biLinks()) {
636 highlights.add(tlink.highlight(StatsType.TAGGED));
637 }
638 }
639
Simon Hunta17fa672015-08-19 18:42:22 -0700640 // =======================================================================
641 // === Background Task
642
643 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700644 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700645 @Override
646 public void run() {
647 try {
648 switch (mode) {
649 case ALL_FLOW_TRAFFIC:
650 sendAllFlowTraffic();
651 break;
652 case ALL_PORT_TRAFFIC:
653 sendAllPortTraffic();
654 break;
655 case DEV_LINK_FLOWS:
656 sendDeviceLinkFlows();
657 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700658 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700659 sendSelectedIntentTraffic();
660 break;
661
662 default:
663 // RELATED_INTENTS and IDLE modes should never invoke
664 // the background task, but if they do, they have
665 // nothing to do
666 break;
667 }
668
669 } catch (Exception e) {
670 log.warn("Unable to process traffic task due to {}", e.getMessage());
671 log.warn("Boom!", e);
672 }
673 }
674 }
Simon Hunta17fa672015-08-19 18:42:22 -0700675}