blob: 1de86dea91a6544f82ea1c10896bea27797719c2 [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;
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 Hunta17fa672015-08-19 18:42:22 -070040import org.onosproject.ui.impl.topo.IntentSelection;
Simon Hunta17fa672015-08-19 18:42:22 -070041import org.onosproject.ui.impl.topo.ServicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070042import org.onosproject.ui.impl.topo.TopoIntentFilter;
Simon Hunt4fc86852015-08-20 17:57:52 -070043import org.onosproject.ui.impl.topo.TrafficLink;
Simon Hunt57830172015-08-26 13:25:17 -070044import org.onosproject.ui.impl.topo.TrafficLink.StatsType;
Simon Hunt4fc86852015-08-20 17:57:52 -070045import org.onosproject.ui.impl.topo.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 Hunt94f7dae2015-08-26 17:40:59 -070069import static org.onosproject.ui.impl.TrafficMonitor.Mode.*;
Simon Hunta17fa672015-08-19 18:42:22 -070070
71/**
72 * Encapsulates the behavior of monitoring specific traffic patterns.
73 */
Simon Hunt4fc86852015-08-20 17:57:52 -070074public class TrafficMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070075
76 // 4 Kilo Bytes as threshold
77 private static final double BPS_THRESHOLD = 4 * TopoUtils.KILO;
78
79 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070080 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070081
82 /**
83 * Designates the different modes of operation.
84 */
85 public enum Mode {
86 IDLE,
87 ALL_FLOW_TRAFFIC,
88 ALL_PORT_TRAFFIC,
89 DEV_LINK_FLOWS,
90 RELATED_INTENTS,
Simon Hunt4fc86852015-08-20 17:57:52 -070091 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -070092 }
93
94 private final long trafficPeriod;
95 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -070096 private final TopologyViewMessageHandler msgHandler;
97 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -070098
99 private final Timer timer = new Timer("topo-traffic");
100
101 private TimerTask trafficTask = null;
102 private Mode mode = IDLE;
103 private NodeSelection selectedNodes = null;
104 private IntentSelection selectedIntents = null;
105
106
107 /**
108 * Constructs a traffic monitor.
109 *
110 * @param trafficPeriod traffic task period in ms
111 * @param servicesBundle bundle of services
Simon Hunt4fc86852015-08-20 17:57:52 -0700112 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700113 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700114 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
115 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700116 this.trafficPeriod = trafficPeriod;
117 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700118 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700119
Simon Hunt4fc86852015-08-20 17:57:52 -0700120 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700121 }
122
123 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700124 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700125
Simon Hunt4fc86852015-08-20 17:57:52 -0700126 /**
127 * Monitor for traffic data to be sent back to the web client, under
128 * the given mode. This causes a background traffic task to be
129 * scheduled to repeatedly compute and transmit the appropriate traffic
130 * data to the client.
131 * <p>
132 * The monitoring mode is expected to be one of:
133 * <ul>
134 * <li>ALL_FLOW_TRAFFIC</li>
135 * <li>ALL_PORT_TRAFFIC</li>
136 * <li>SELECTED_INTENT</li>
137 * </ul>
138 *
139 * @param mode monitoring mode
140 */
Simon Hunta17fa672015-08-19 18:42:22 -0700141 public synchronized void monitor(Mode mode) {
142 log.debug("monitor: {}", mode);
143 this.mode = mode;
144
145 switch (mode) {
146 case ALL_FLOW_TRAFFIC:
147 clearSelection();
148 scheduleTask();
149 sendAllFlowTraffic();
150 break;
151
152 case ALL_PORT_TRAFFIC:
153 clearSelection();
154 scheduleTask();
155 sendAllPortTraffic();
156 break;
157
Simon Hunt4fc86852015-08-20 17:57:52 -0700158 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700159 scheduleTask();
160 sendSelectedIntentTraffic();
161 break;
162
163 default:
164 log.debug("Unexpected call to monitor({})", mode);
165 clearAll();
166 break;
167 }
168 }
169
Simon Hunt4fc86852015-08-20 17:57:52 -0700170 /**
171 * Monitor for traffic data to be sent back to the web client, under
172 * the given mode, using the given selection of devices and hosts.
173 * In the case of "device link flows", this causes a background traffic
174 * task to be scheduled to repeatedly compute and transmit the appropriate
175 * traffic data to the client. In the case of "related intents", no
176 * repeating task is scheduled.
177 * <p>
178 * The monitoring mode is expected to be one of:
179 * <ul>
180 * <li>DEV_LINK_FLOWS</li>
181 * <li>RELATED_INTENTS</li>
182 * </ul>
183 *
184 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700185 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700186 */
Simon Hunta17fa672015-08-19 18:42:22 -0700187 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
188 log.debug("monitor: {} -- {}", mode, nodeSelection);
189 this.mode = mode;
190 this.selectedNodes = nodeSelection;
191
192 switch (mode) {
193 case DEV_LINK_FLOWS:
194 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700195 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700196 sendClearAll();
197 } else {
198 scheduleTask();
199 sendDeviceLinkFlows();
200 }
201 break;
202
203 case RELATED_INTENTS:
204 if (selectedNodes.none()) {
205 sendClearAll();
206 } else {
207 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
208 if (selectedIntents.none()) {
209 sendClearAll();
210 } else {
211 sendSelectedIntents();
212 }
213 }
214 break;
215
216 default:
217 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
218 clearAll();
219 break;
220 }
221 }
222
Simon Hunt4fc86852015-08-20 17:57:52 -0700223 // TODO: move this out to the "h2h/multi-intent app"
224 /**
225 * Monitor for traffic data to be sent back to the web client, for the
226 * given intent.
227 *
228 * @param intent the intent to monitor
229 */
Simon Hunta17fa672015-08-19 18:42:22 -0700230 public synchronized void monitor(Intent intent) {
231 log.debug("monitor intent: {}", intent.id());
232 selectedNodes = null;
233 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700234 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700235 scheduleTask();
236 sendSelectedIntentTraffic();
237 }
238
Simon Hunt4fc86852015-08-20 17:57:52 -0700239 /**
240 * Selects the next intent in the select group (if there is one),
241 * and sends highlighting data back to the web client to display
242 * which path is selected.
243 */
Simon Hunta17fa672015-08-19 18:42:22 -0700244 public synchronized void selectNextIntent() {
245 if (selectedIntents != null) {
246 selectedIntents.next();
247 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700248 if (mode == SELECTED_INTENT) {
249 mode = RELATED_INTENTS;
250 }
Simon Hunta17fa672015-08-19 18:42:22 -0700251 }
252 }
253
Simon Hunt4fc86852015-08-20 17:57:52 -0700254 /**
255 * Selects the previous intent in the select group (if there is one),
256 * and sends highlighting data back to the web client to display
257 * which path is selected.
258 */
Simon Hunta17fa672015-08-19 18:42:22 -0700259 public synchronized void selectPreviousIntent() {
260 if (selectedIntents != null) {
261 selectedIntents.prev();
262 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700263 if (mode == SELECTED_INTENT) {
264 mode = RELATED_INTENTS;
265 }
Simon Hunta17fa672015-08-19 18:42:22 -0700266 }
267 }
268
Simon Hunt4fc86852015-08-20 17:57:52 -0700269 /**
270 * Resends selected intent traffic data. This is called, for example,
271 * when the system detects an intent update happened.
272 */
Simon Hunta17fa672015-08-19 18:42:22 -0700273 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700274 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700275 sendSelectedIntentTraffic();
276 }
277 }
278
Simon Hunt4fc86852015-08-20 17:57:52 -0700279 /**
280 * Stop all traffic monitoring.
281 */
282 public synchronized void stopMonitoring() {
283 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700284 if (mode != IDLE) {
285 sendClearAll();
286 }
287 }
288
289
290 // =======================================================================
291 // === Helper methods ===
292
293 private void sendClearAll() {
294 clearAll();
295 sendClearHighlights();
296 }
297
298 private void clearAll() {
299 this.mode = IDLE;
300 clearSelection();
301 cancelTask();
302 }
303
304 private void clearSelection() {
305 selectedNodes = null;
306 selectedIntents = null;
307 }
308
309 private synchronized void scheduleTask() {
310 if (trafficTask == null) {
311 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700312 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700313 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
314 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700315 log.debug("(traffic task already running)");
316 }
317 }
318
319 private synchronized void cancelTask() {
320 if (trafficTask != null) {
321 trafficTask.cancel();
322 trafficTask = null;
323 }
324 }
325
Simon Hunta17fa672015-08-19 18:42:22 -0700326 private void sendAllFlowTraffic() {
327 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700328 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700329 }
330
331 private void sendAllPortTraffic() {
332 log.debug("sendAllPortTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700333 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700334 }
335
336 private void sendDeviceLinkFlows() {
337 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700338 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700339 }
340
341 private void sendSelectedIntents() {
342 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700343 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700344 }
345
346 private void sendSelectedIntentTraffic() {
347 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700348 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700349 }
350
351 private void sendClearHighlights() {
352 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700353 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700354 }
355
Simon Hunta17fa672015-08-19 18:42:22 -0700356 // =======================================================================
357 // === Generate messages in JSON object node format
358
Simon Hunt57830172015-08-26 13:25:17 -0700359 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700360 Highlights highlights = new Highlights();
361
Simon Hunt4fc86852015-08-20 17:57:52 -0700362 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700363 compileLinks(linkMap);
364 addEdgeLinks(linkMap);
365
Simon Hunt4fc86852015-08-20 17:57:52 -0700366 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700367 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700368 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700369 } else if (type == StatsType.PORT_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700370 attachPortLoad(tlink);
Simon Hunta17fa672015-08-19 18:42:22 -0700371 }
372
373 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700374 if (tlink.hasTraffic()) {
375 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700376 }
377 }
378 return highlights;
379 }
380
381 // create highlights for links, showing flows for selected devices.
382 private Highlights deviceLinkFlows() {
383 Highlights highlights = new Highlights();
384
Simon Hunt72297212015-08-25 10:15:33 -0700385 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700386 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700387 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700388
Simon Hunt72297212015-08-25 10:15:33 -0700389 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700390 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
391 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700392 TrafficLink tlink = linkMap.add(link);
393 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700394 }
395 }
396
397 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700398 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700399 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700400 }
401
402 }
403 return highlights;
404 }
405
406 private Highlights intentGroup() {
407 Highlights highlights = new Highlights();
408
409 if (selectedIntents != null && !selectedIntents.none()) {
410 // If 'all' intents are selected, they will all have primary
411 // highlighting; otherwise, the specifically selected intent will
412 // have primary highlighting, and the remainder will have secondary
413 // highlighting.
414 Set<Intent> primary;
415 Set<Intent> secondary;
416 int count = selectedIntents.size();
417
418 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
419 Intent current;
420
421 if (selectedIntents.all()) {
422 primary = allBut;
423 secondary = Collections.emptySet();
424 log.debug("Highlight all intents ({})", count);
425 } else {
426 current = selectedIntents.current();
427 primary = new HashSet<>();
428 primary.add(current);
429 allBut.remove(current);
430 secondary = allBut;
431 log.debug("Highlight intent: {} ([{}] of {})",
432 current.id(), selectedIntents.index(), count);
433 }
Simon Hunt57830172015-08-26 13:25:17 -0700434
435 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700436 }
437 return highlights;
438 }
439
440 private Highlights intentTraffic() {
441 Highlights highlights = new Highlights();
442
443 if (selectedIntents != null && selectedIntents.single()) {
444 Intent current = selectedIntents.current();
445 Set<Intent> primary = new HashSet<>();
446 primary.add(current);
447 log.debug("Highlight traffic for intent: {} ([{}] of {})",
448 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700449
450 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700451 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700452 }
453 return highlights;
454 }
455
Simon Hunta17fa672015-08-19 18:42:22 -0700456 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
457
Simon Hunt4fc86852015-08-20 17:57:52 -0700458 private void compileLinks(TrafficLinkMap linkMap) {
459 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700460 }
461
Simon Hunt4fc86852015-08-20 17:57:52 -0700462 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700463 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700464 linkMap.add(createEdgeLink(host, true));
465 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700466 });
467 }
468
469 private Load getLinkFlowLoad(Link link) {
470 if (link != null && link.src().elementId() instanceof DeviceId) {
471 return servicesBundle.flowStatsService().load(link);
472 }
473 return null;
474 }
475
Simon Hunt4fc86852015-08-20 17:57:52 -0700476 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700477 link.addLoad(getLinkFlowLoad(link.one()));
478 link.addLoad(getLinkFlowLoad(link.two()));
479 }
480
Simon Hunt4fc86852015-08-20 17:57:52 -0700481 private void attachPortLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700482 // For bi-directional traffic links, use
483 // the max link rate of either direction
484 // (we choose 'one' since we know that is never null)
485 Link one = link.one();
486 Load egressSrc = servicesBundle.portStatsService().load(one.src());
487 Load egressDst = servicesBundle.portStatsService().load(one.dst());
Simon Hunt5f31a022015-08-20 08:43:25 -0700488 link.addLoad(maxLoad(egressSrc, egressDst), BPS_THRESHOLD);
Simon Hunt4fc86852015-08-20 17:57:52 -0700489// link.addLoad(maxLoad(egressSrc, egressDst), 10); // DEBUG ONLY!!
Simon Hunta17fa672015-08-19 18:42:22 -0700490 }
491
492 private Load maxLoad(Load a, Load b) {
493 if (a == null) {
494 return b;
495 }
496 if (b == null) {
497 return a;
498 }
499 return a.rate() > b.rate() ? a : b;
500 }
501
Simon Hunta17fa672015-08-19 18:42:22 -0700502 // Counts all flow entries that egress on the links of the given device.
503 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
504 // get the flows for the device
505 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt4fc86852015-08-20 17:57:52 -0700506 for (FlowEntry flowEntry : servicesBundle.flowService()
507 .getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700508 entries.add(flowEntry);
509 }
510
511 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700512 Set<Link> links = new HashSet<>(servicesBundle.linkService()
513 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700514 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
515 if (hosts != null) {
516 for (Host host : hosts) {
517 links.add(createEdgeLink(host, false));
518 }
519 }
520
521 // compile flow counts per link
522 Map<Link, Integer> counts = new HashMap<>();
523 for (Link link : links) {
524 counts.put(link, getEgressFlows(link, entries));
525 }
526 return counts;
527 }
528
529 // Counts all entries that egress on the link source port.
530 private int getEgressFlows(Link link, List<FlowEntry> entries) {
531 int count = 0;
532 PortNumber out = link.src().port();
533 for (FlowEntry entry : entries) {
534 TrafficTreatment treatment = entry.treatment();
535 for (Instruction instruction : treatment.allInstructions()) {
536 if (instruction.type() == Instruction.Type.OUTPUT &&
537 ((OutputInstruction) instruction).port().equals(out)) {
538 count++;
539 }
540 }
541 }
542 return count;
543 }
544
Simon Hunt57830172015-08-26 13:25:17 -0700545 private void highlightIntentLinks(Highlights highlights,
546 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700547 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700548 // NOTE: highlight secondary first, then primary, so that links shared
549 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700550 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
551 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700552 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700553 }
554
Simon Hunt57830172015-08-26 13:25:17 -0700555 private void highlightIntentLinksWithTraffic(Highlights highlights,
556 Set<Intent> primary) {
557 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700558 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700559 colorLinks(highlights, linkMap);
560 }
561
Simon Hunt94f7dae2015-08-26 17:40:59 -0700562 private void createTrafficLinks(Highlights highlights,
563 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700564 Flavor flavor, boolean showTraffic) {
565 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700566 List<Intent> installables = servicesBundle.intentService()
567 .getInstallableIntents(intent.key());
568 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700569 if (installables != null) {
570 for (Intent installable : installables) {
571
572 if (installable instanceof PathIntent) {
573 links = ((PathIntent) installable).path().links();
574 } else if (installable instanceof FlowRuleIntent) {
575 links = linkResources(installable);
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800576 } else if (installable instanceof FlowObjectiveIntent) {
577 links = linkResources(installable);
Simon Hunta17fa672015-08-19 18:42:22 -0700578 } else if (installable instanceof LinkCollectionIntent) {
579 links = ((LinkCollectionIntent) installable).links();
580 } else if (installable instanceof OpticalPathIntent) {
581 links = ((OpticalPathIntent) installable).path().links();
582 }
583
Simon Hunt57830172015-08-26 13:25:17 -0700584 boolean isOptical = intent instanceof OpticalConnectivityIntent;
585 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700586 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700587 }
588 }
589 }
590 }
591
Simon Hunt94f7dae2015-08-26 17:40:59 -0700592 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
593 for (Link link : links) {
594 ensureNodePresent(highlights, link.src().elementId());
595 ensureNodePresent(highlights, link.dst().elementId());
596 }
597 }
598
599 private void ensureNodePresent(Highlights highlights, ElementId eid) {
600 String id = eid.toString();
601 NodeHighlight nh = highlights.getNode(id);
602 if (nh == null) {
603 if (eid instanceof DeviceId) {
604 nh = new DeviceHighlight(id);
605 highlights.add((DeviceHighlight) nh);
606 } else if (eid instanceof HostId) {
607 nh = new HostHighlight(id);
608 highlights.add((HostHighlight) nh);
609 }
610 }
611 }
612
Simon Hunta17fa672015-08-19 18:42:22 -0700613 // Extracts links from the specified flow rule intent resources
614 private Collection<Link> linkResources(Intent installable) {
615 ImmutableList.Builder<Link> builder = ImmutableList.builder();
616 installable.resources().stream().filter(r -> r instanceof Link)
617 .forEach(r -> builder.add((Link) r));
618 return builder.build();
619 }
620
Simon Hunt57830172015-08-26 13:25:17 -0700621 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
622 Flavor flavor, boolean isOptical,
623 boolean showTraffic) {
624 if (links != null) {
625 for (Link link : links) {
626 TrafficLink tlink = linkMap.add(link);
627 tlink.tagFlavor(flavor);
628 tlink.optical(isOptical);
629 if (showTraffic) {
630 tlink.addLoad(getLinkFlowLoad(link));
631 tlink.antMarch(true);
632 }
633 }
634 }
635 }
636
637 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
638 for (TrafficLink tlink : linkMap.biLinks()) {
639 highlights.add(tlink.highlight(StatsType.TAGGED));
640 }
641 }
642
Simon Hunta17fa672015-08-19 18:42:22 -0700643 // =======================================================================
644 // === Background Task
645
646 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700647 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700648 @Override
649 public void run() {
650 try {
651 switch (mode) {
652 case ALL_FLOW_TRAFFIC:
653 sendAllFlowTraffic();
654 break;
655 case ALL_PORT_TRAFFIC:
656 sendAllPortTraffic();
657 break;
658 case DEV_LINK_FLOWS:
659 sendDeviceLinkFlows();
660 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700661 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700662 sendSelectedIntentTraffic();
663 break;
664
665 default:
666 // RELATED_INTENTS and IDLE modes should never invoke
667 // the background task, but if they do, they have
668 // nothing to do
669 break;
670 }
671
672 } catch (Exception e) {
673 log.warn("Unable to process traffic task due to {}", e.getMessage());
674 log.warn("Boom!", e);
675 }
676 }
677 }
Simon Hunta17fa672015-08-19 18:42:22 -0700678}