blob: 5cf57652f1ac8b9a76618c71231202ee2fda9eff [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;
Thomas Vachuska0932ac52017-03-30 13:28:49 -070021import org.onosproject.incubator.net.PortStatisticsService.MetricType;
Simon Hunta17fa672015-08-19 18:42:22 -070022import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
Simon Hunt94f7dae2015-08-26 17:40:59 -070024import org.onosproject.net.ElementId;
Simon Hunta17fa672015-08-19 18:42:22 -070025import org.onosproject.net.Host;
Simon Hunt94f7dae2015-08-26 17:40:59 -070026import org.onosproject.net.HostId;
Simon Hunta17fa672015-08-19 18:42:22 -070027import org.onosproject.net.Link;
Simon Hunta17fa672015-08-19 18:42:22 -070028import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.FlowEntry;
30import org.onosproject.net.flow.TrafficTreatment;
31import org.onosproject.net.flow.instructions.Instruction;
32import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Thomas Vachuskada0665b2016-03-02 19:06:17 -080033import org.onosproject.net.intent.FlowObjectiveIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070034import org.onosproject.net.intent.FlowRuleIntent;
Simon Hunt5328f792017-01-11 17:43:31 -080035import org.onosproject.net.intent.HostToHostIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070036import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.LinkCollectionIntent;
38import org.onosproject.net.intent.OpticalConnectivityIntent;
39import org.onosproject.net.intent.OpticalPathIntent;
40import org.onosproject.net.intent.PathIntent;
41import org.onosproject.net.statistic.Load;
Simon Hunted804d52016-03-30 09:51:40 -070042import org.onosproject.ui.impl.topo.util.IntentSelection;
43import org.onosproject.ui.impl.topo.util.ServicesBundle;
44import org.onosproject.ui.impl.topo.util.TopoIntentFilter;
45import org.onosproject.ui.impl.topo.util.TrafficLink;
46import org.onosproject.ui.impl.topo.util.TrafficLink.StatsType;
47import org.onosproject.ui.impl.topo.util.TrafficLinkMap;
Simon Hunt441c9ae2017-02-03 18:22:31 -080048import org.onosproject.ui.topo.AbstractTopoMonitor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070049import org.onosproject.ui.topo.DeviceHighlight;
Simon Hunta17fa672015-08-19 18:42:22 -070050import org.onosproject.ui.topo.Highlights;
Simon Hunt94f7dae2015-08-26 17:40:59 -070051import org.onosproject.ui.topo.Highlights.Amount;
52import org.onosproject.ui.topo.HostHighlight;
Simon Hunt57830172015-08-26 13:25:17 -070053import org.onosproject.ui.topo.LinkHighlight.Flavor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070054import org.onosproject.ui.topo.NodeHighlight;
Simon Hunt743a8492015-08-25 16:18:19 -070055import org.onosproject.ui.topo.NodeSelection;
56import org.onosproject.ui.topo.TopoUtils;
Simon Hunta17fa672015-08-19 18:42:22 -070057import org.slf4j.Logger;
58import org.slf4j.LoggerFactory;
59
60import java.util.ArrayList;
61import java.util.Collection;
62import java.util.Collections;
63import java.util.HashMap;
64import java.util.HashSet;
65import java.util.List;
66import java.util.Map;
67import java.util.Set;
68import java.util.Timer;
69import java.util.TimerTask;
70
Thomas Vachuska0932ac52017-03-30 13:28:49 -070071import static org.onosproject.incubator.net.PortStatisticsService.MetricType.BYTES;
72import static org.onosproject.incubator.net.PortStatisticsService.MetricType.PACKETS;
Simon Hunta17fa672015-08-19 18:42:22 -070073import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Thomas Vachuska0932ac52017-03-30 13:28:49 -070074import static org.onosproject.ui.impl.TrafficMonitor.Mode.*;
Simon Hunta17fa672015-08-19 18:42:22 -070075
76/**
77 * Encapsulates the behavior of monitoring specific traffic patterns.
78 */
Simon Hunt441c9ae2017-02-03 18:22:31 -080079public class TrafficMonitor extends AbstractTopoMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070080
81 // 4 Kilo Bytes as threshold
Simon Hunt21281fd2017-03-30 22:28:28 -070082 private static final double BPS_THRESHOLD = 4 * TopoUtils.N_KILO;
Simon Hunta17fa672015-08-19 18:42:22 -070083
84 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070085 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070086
87 /**
88 * Designates the different modes of operation.
89 */
90 public enum Mode {
91 IDLE,
Simon Hunt21281fd2017-03-30 22:28:28 -070092 ALL_FLOW_TRAFFIC_BYTES,
93 ALL_PORT_TRAFFIC_BIT_PS,
94 ALL_PORT_TRAFFIC_PKT_PS,
Simon Hunta17fa672015-08-19 18:42:22 -070095 DEV_LINK_FLOWS,
96 RELATED_INTENTS,
Simon Hunt4fc86852015-08-20 17:57:52 -070097 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -070098 }
99
100 private final long trafficPeriod;
101 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700102 private final TopologyViewMessageHandler msgHandler;
103 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -0700104
105 private final Timer timer = new Timer("topo-traffic");
106
107 private TimerTask trafficTask = null;
108 private Mode mode = IDLE;
109 private NodeSelection selectedNodes = null;
110 private IntentSelection selectedIntents = null;
111
112
113 /**
114 * Constructs a traffic monitor.
115 *
Simon Hunt5328f792017-01-11 17:43:31 -0800116 * @param trafficPeriod traffic task period in ms
117 * @param servicesBundle bundle of services
118 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700119 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700120 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
121 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700122 this.trafficPeriod = trafficPeriod;
123 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700124 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700125
Simon Hunt4fc86852015-08-20 17:57:52 -0700126 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700127 }
128
129 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700130 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700131
Simon Hunt4fc86852015-08-20 17:57:52 -0700132 /**
133 * Monitor for traffic data to be sent back to the web client, under
134 * the given mode. This causes a background traffic task to be
135 * scheduled to repeatedly compute and transmit the appropriate traffic
136 * data to the client.
137 * <p>
138 * The monitoring mode is expected to be one of:
139 * <ul>
Simon Hunt21281fd2017-03-30 22:28:28 -0700140 * <li>ALL_FLOW_TRAFFIC_BYTES</li>
141 * <li>ALL_PORT_TRAFFIC_BIT_PS</li>
142 * <li>ALL_PORT_TRAFFIC_PKT_PS</li>
Simon Hunt5328f792017-01-11 17:43:31 -0800143 * <li>SELECTED_INTENT</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700144 * </ul>
145 *
146 * @param mode monitoring mode
147 */
Simon Hunta17fa672015-08-19 18:42:22 -0700148 public synchronized void monitor(Mode mode) {
149 log.debug("monitor: {}", mode);
150 this.mode = mode;
151
152 switch (mode) {
Simon Hunt21281fd2017-03-30 22:28:28 -0700153 case ALL_FLOW_TRAFFIC_BYTES:
Simon Hunta17fa672015-08-19 18:42:22 -0700154 clearSelection();
155 scheduleTask();
156 sendAllFlowTraffic();
157 break;
158
Simon Hunt21281fd2017-03-30 22:28:28 -0700159 case ALL_PORT_TRAFFIC_BIT_PS:
Simon Hunta17fa672015-08-19 18:42:22 -0700160 clearSelection();
161 scheduleTask();
Simon Hunt21281fd2017-03-30 22:28:28 -0700162 sendAllPortTraffic(StatsType.PORT_STATS);
163 break;
164
165 case ALL_PORT_TRAFFIC_PKT_PS:
166 clearSelection();
167 scheduleTask();
168 sendAllPortTraffic(StatsType.PORT_PACKET_STATS);
Simon Hunta17fa672015-08-19 18:42:22 -0700169 break;
170
Simon Hunt4fc86852015-08-20 17:57:52 -0700171 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700172 scheduleTask();
173 sendSelectedIntentTraffic();
174 break;
175
176 default:
177 log.debug("Unexpected call to monitor({})", mode);
178 clearAll();
179 break;
180 }
181 }
182
Simon Hunt4fc86852015-08-20 17:57:52 -0700183 /**
184 * Monitor for traffic data to be sent back to the web client, under
185 * the given mode, using the given selection of devices and hosts.
186 * In the case of "device link flows", this causes a background traffic
187 * task to be scheduled to repeatedly compute and transmit the appropriate
188 * traffic data to the client. In the case of "related intents", no
189 * repeating task is scheduled.
190 * <p>
191 * The monitoring mode is expected to be one of:
192 * <ul>
Simon Hunt5328f792017-01-11 17:43:31 -0800193 * <li>DEV_LINK_FLOWS</li>
194 * <li>RELATED_INTENTS</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700195 * </ul>
196 *
Simon Hunt5328f792017-01-11 17:43:31 -0800197 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700198 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700199 */
Simon Hunta17fa672015-08-19 18:42:22 -0700200 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
201 log.debug("monitor: {} -- {}", mode, nodeSelection);
202 this.mode = mode;
203 this.selectedNodes = nodeSelection;
204
205 switch (mode) {
206 case DEV_LINK_FLOWS:
207 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700208 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700209 sendClearAll();
210 } else {
211 scheduleTask();
212 sendDeviceLinkFlows();
213 }
214 break;
215
216 case RELATED_INTENTS:
217 if (selectedNodes.none()) {
218 sendClearAll();
219 } else {
220 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
221 if (selectedIntents.none()) {
222 sendClearAll();
223 } else {
224 sendSelectedIntents();
225 }
226 }
227 break;
228
229 default:
230 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
231 clearAll();
232 break;
233 }
234 }
235
Simon Hunt4fc86852015-08-20 17:57:52 -0700236 // TODO: move this out to the "h2h/multi-intent app"
Simon Hunt5328f792017-01-11 17:43:31 -0800237
Simon Hunt4fc86852015-08-20 17:57:52 -0700238 /**
239 * Monitor for traffic data to be sent back to the web client, for the
240 * given intent.
241 *
242 * @param intent the intent to monitor
243 */
Simon Hunta17fa672015-08-19 18:42:22 -0700244 public synchronized void monitor(Intent intent) {
245 log.debug("monitor intent: {}", intent.id());
246 selectedNodes = null;
247 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700248 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700249 scheduleTask();
250 sendSelectedIntentTraffic();
251 }
252
Simon Hunt4fc86852015-08-20 17:57:52 -0700253 /**
254 * Selects the next 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 selectNextIntent() {
259 if (selectedIntents != null) {
260 selectedIntents.next();
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 * Selects the previous intent in the select group (if there is one),
270 * and sends highlighting data back to the web client to display
271 * which path is selected.
272 */
Simon Hunta17fa672015-08-19 18:42:22 -0700273 public synchronized void selectPreviousIntent() {
274 if (selectedIntents != null) {
275 selectedIntents.prev();
276 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700277 if (mode == SELECTED_INTENT) {
278 mode = RELATED_INTENTS;
279 }
Simon Hunta17fa672015-08-19 18:42:22 -0700280 }
281 }
282
Simon Hunt4fc86852015-08-20 17:57:52 -0700283 /**
284 * Resends selected intent traffic data. This is called, for example,
285 * when the system detects an intent update happened.
286 */
Simon Hunta17fa672015-08-19 18:42:22 -0700287 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700288 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700289 sendSelectedIntentTraffic();
290 }
291 }
292
Simon Hunt4fc86852015-08-20 17:57:52 -0700293 /**
294 * Stop all traffic monitoring.
295 */
296 public synchronized void stopMonitoring() {
297 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700298 if (mode != IDLE) {
299 sendClearAll();
300 }
301 }
302
303
304 // =======================================================================
305 // === Helper methods ===
306
307 private void sendClearAll() {
308 clearAll();
309 sendClearHighlights();
310 }
311
312 private void clearAll() {
313 this.mode = IDLE;
314 clearSelection();
315 cancelTask();
316 }
317
318 private void clearSelection() {
319 selectedNodes = null;
320 selectedIntents = null;
321 }
322
Simon Hunt5328f792017-01-11 17:43:31 -0800323 private synchronized void scheduleTask() {
Simon Hunta17fa672015-08-19 18:42:22 -0700324 if (trafficTask == null) {
325 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700326 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700327 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
328 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700329 log.debug("(traffic task already running)");
330 }
331 }
332
333 private synchronized void cancelTask() {
334 if (trafficTask != null) {
335 trafficTask.cancel();
336 trafficTask = null;
337 }
338 }
339
Simon Hunta17fa672015-08-19 18:42:22 -0700340 private void sendAllFlowTraffic() {
341 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700342 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700343 }
344
Simon Hunt21281fd2017-03-30 22:28:28 -0700345 private void sendAllPortTraffic(StatsType t) {
346 log.debug("sendAllPortTraffic: {}", t);
347 msgHandler.sendHighlights(trafficSummary(t));
Simon Hunta17fa672015-08-19 18:42:22 -0700348 }
349
350 private void sendDeviceLinkFlows() {
351 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700352 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700353 }
354
355 private void sendSelectedIntents() {
356 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700357 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700358 }
359
360 private void sendSelectedIntentTraffic() {
361 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700362 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700363 }
364
365 private void sendClearHighlights() {
366 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700367 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700368 }
369
Simon Hunta17fa672015-08-19 18:42:22 -0700370 // =======================================================================
371 // === Generate messages in JSON object node format
372
Simon Hunt57830172015-08-26 13:25:17 -0700373 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700374 Highlights highlights = new Highlights();
375
Simon Hunt4fc86852015-08-20 17:57:52 -0700376 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700377 compileLinks(linkMap);
378 addEdgeLinks(linkMap);
379
Simon Hunt4fc86852015-08-20 17:57:52 -0700380 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700381 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700382 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700383 } else if (type == StatsType.PORT_STATS) {
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700384 attachPortLoad(tlink, BYTES);
385 } else if (type == StatsType.PORT_PACKET_STATS) {
386 attachPortLoad(tlink, PACKETS);
Simon Hunta17fa672015-08-19 18:42:22 -0700387 }
388
389 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700390 if (tlink.hasTraffic()) {
391 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700392 }
393 }
394 return highlights;
395 }
396
397 // create highlights for links, showing flows for selected devices.
398 private Highlights deviceLinkFlows() {
399 Highlights highlights = new Highlights();
400
Simon Hunt72297212015-08-25 10:15:33 -0700401 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700402 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700403 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700404
Simon Hunt72297212015-08-25 10:15:33 -0700405 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700406 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
407 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700408 TrafficLink tlink = linkMap.add(link);
409 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700410 }
411 }
412
413 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700414 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700415 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700416 }
417
418 }
419 return highlights;
420 }
421
422 private Highlights intentGroup() {
423 Highlights highlights = new Highlights();
424
425 if (selectedIntents != null && !selectedIntents.none()) {
426 // If 'all' intents are selected, they will all have primary
427 // highlighting; otherwise, the specifically selected intent will
428 // have primary highlighting, and the remainder will have secondary
429 // highlighting.
430 Set<Intent> primary;
431 Set<Intent> secondary;
432 int count = selectedIntents.size();
433
434 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
435 Intent current;
436
437 if (selectedIntents.all()) {
438 primary = allBut;
439 secondary = Collections.emptySet();
440 log.debug("Highlight all intents ({})", count);
441 } else {
442 current = selectedIntents.current();
443 primary = new HashSet<>();
444 primary.add(current);
445 allBut.remove(current);
446 secondary = allBut;
447 log.debug("Highlight intent: {} ([{}] of {})",
Simon Hunt5328f792017-01-11 17:43:31 -0800448 current.id(), selectedIntents.index(), count);
Simon Hunta17fa672015-08-19 18:42:22 -0700449 }
Simon Hunt57830172015-08-26 13:25:17 -0700450
451 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700452 }
453 return highlights;
454 }
455
456 private Highlights intentTraffic() {
457 Highlights highlights = new Highlights();
458
459 if (selectedIntents != null && selectedIntents.single()) {
460 Intent current = selectedIntents.current();
461 Set<Intent> primary = new HashSet<>();
462 primary.add(current);
463 log.debug("Highlight traffic for intent: {} ([{}] of {})",
Simon Hunt5328f792017-01-11 17:43:31 -0800464 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700465
466 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700467 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700468 }
469 return highlights;
470 }
471
Simon Hunta17fa672015-08-19 18:42:22 -0700472 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
473
Simon Hunt4fc86852015-08-20 17:57:52 -0700474 private void compileLinks(TrafficLinkMap linkMap) {
475 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700476 }
477
Simon Hunt4fc86852015-08-20 17:57:52 -0700478 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700479 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700480 linkMap.add(createEdgeLink(host, true));
481 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700482 });
483 }
484
485 private Load getLinkFlowLoad(Link link) {
486 if (link != null && link.src().elementId() instanceof DeviceId) {
487 return servicesBundle.flowStatsService().load(link);
488 }
489 return null;
490 }
491
Simon Hunt4fc86852015-08-20 17:57:52 -0700492 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700493 link.addLoad(getLinkFlowLoad(link.one()));
494 link.addLoad(getLinkFlowLoad(link.two()));
495 }
496
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700497 private void attachPortLoad(TrafficLink link, MetricType metricType) {
Simon Hunta17fa672015-08-19 18:42:22 -0700498 // For bi-directional traffic links, use
499 // the max link rate of either direction
500 // (we choose 'one' since we know that is never null)
501 Link one = link.one();
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700502 Load egressSrc = servicesBundle.portStatsService().load(one.src(), metricType);
503 Load egressDst = servicesBundle.portStatsService().load(one.dst(), metricType);
504 link.addLoad(maxLoad(egressSrc, egressDst), metricType == BYTES ? BPS_THRESHOLD : 0);
Simon Hunta17fa672015-08-19 18:42:22 -0700505 }
506
507 private Load maxLoad(Load a, Load b) {
508 if (a == null) {
509 return b;
510 }
511 if (b == null) {
512 return a;
513 }
514 return a.rate() > b.rate() ? a : b;
515 }
516
Simon Hunta17fa672015-08-19 18:42:22 -0700517 // Counts all flow entries that egress on the links of the given device.
518 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
519 // get the flows for the device
520 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt5328f792017-01-11 17:43:31 -0800521 for (FlowEntry flowEntry : servicesBundle.flowService().getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700522 entries.add(flowEntry);
523 }
524
525 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700526 Set<Link> links = new HashSet<>(servicesBundle.linkService()
Simon Hunt5328f792017-01-11 17:43:31 -0800527 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700528 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
529 if (hosts != null) {
530 for (Host host : hosts) {
531 links.add(createEdgeLink(host, false));
532 }
533 }
534
535 // compile flow counts per link
536 Map<Link, Integer> counts = new HashMap<>();
537 for (Link link : links) {
538 counts.put(link, getEgressFlows(link, entries));
539 }
540 return counts;
541 }
542
543 // Counts all entries that egress on the link source port.
544 private int getEgressFlows(Link link, List<FlowEntry> entries) {
545 int count = 0;
546 PortNumber out = link.src().port();
547 for (FlowEntry entry : entries) {
548 TrafficTreatment treatment = entry.treatment();
549 for (Instruction instruction : treatment.allInstructions()) {
550 if (instruction.type() == Instruction.Type.OUTPUT &&
551 ((OutputInstruction) instruction).port().equals(out)) {
552 count++;
553 }
554 }
555 }
556 return count;
557 }
558
Simon Hunt57830172015-08-26 13:25:17 -0700559 private void highlightIntentLinks(Highlights highlights,
560 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700561 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700562 // NOTE: highlight secondary first, then primary, so that links shared
563 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700564 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
565 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700566 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700567 }
568
Simon Hunt57830172015-08-26 13:25:17 -0700569 private void highlightIntentLinksWithTraffic(Highlights highlights,
570 Set<Intent> primary) {
571 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700572 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700573 colorLinks(highlights, linkMap);
574 }
575
Simon Hunt94f7dae2015-08-26 17:40:59 -0700576 private void createTrafficLinks(Highlights highlights,
577 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700578 Flavor flavor, boolean showTraffic) {
579 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700580 List<Intent> installables = servicesBundle.intentService()
581 .getInstallableIntents(intent.key());
582 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700583 if (installables != null) {
584 for (Intent installable : installables) {
585
586 if (installable instanceof PathIntent) {
587 links = ((PathIntent) installable).path().links();
588 } else if (installable instanceof FlowRuleIntent) {
Simon Hunt5328f792017-01-11 17:43:31 -0800589 links = addEdgeLinksIfNeeded(intent, linkResources(installable));
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800590 } else if (installable instanceof FlowObjectiveIntent) {
Simon Hunt5328f792017-01-11 17:43:31 -0800591 links = addEdgeLinksIfNeeded(intent, linkResources(installable));
Simon Hunta17fa672015-08-19 18:42:22 -0700592 } else if (installable instanceof LinkCollectionIntent) {
593 links = ((LinkCollectionIntent) installable).links();
594 } else if (installable instanceof OpticalPathIntent) {
595 links = ((OpticalPathIntent) installable).path().links();
596 }
597
Simon Hunt57830172015-08-26 13:25:17 -0700598 boolean isOptical = intent instanceof OpticalConnectivityIntent;
599 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700600 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700601 }
602 }
603 }
604 }
605
Simon Hunt5328f792017-01-11 17:43:31 -0800606 private Iterable<Link> addEdgeLinksIfNeeded(Intent parentIntent,
607 Collection<Link> links) {
608 if (parentIntent instanceof HostToHostIntent) {
609 links = new HashSet<>(links);
610 HostToHostIntent h2h = (HostToHostIntent) parentIntent;
611 Host h1 = servicesBundle.hostService().getHost(h2h.one());
612 Host h2 = servicesBundle.hostService().getHost(h2h.two());
613 links.add(createEdgeLink(h1, true));
614 links.add(createEdgeLink(h2, true));
615 }
616 return links;
617 }
618
Simon Hunt94f7dae2015-08-26 17:40:59 -0700619 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
620 for (Link link : links) {
621 ensureNodePresent(highlights, link.src().elementId());
622 ensureNodePresent(highlights, link.dst().elementId());
623 }
624 }
625
626 private void ensureNodePresent(Highlights highlights, ElementId eid) {
627 String id = eid.toString();
628 NodeHighlight nh = highlights.getNode(id);
629 if (nh == null) {
630 if (eid instanceof DeviceId) {
631 nh = new DeviceHighlight(id);
632 highlights.add((DeviceHighlight) nh);
633 } else if (eid instanceof HostId) {
634 nh = new HostHighlight(id);
635 highlights.add((HostHighlight) nh);
636 }
637 }
638 }
639
Simon Hunta17fa672015-08-19 18:42:22 -0700640 // Extracts links from the specified flow rule intent resources
641 private Collection<Link> linkResources(Intent installable) {
642 ImmutableList.Builder<Link> builder = ImmutableList.builder();
643 installable.resources().stream().filter(r -> r instanceof Link)
644 .forEach(r -> builder.add((Link) r));
645 return builder.build();
646 }
647
Simon Hunt57830172015-08-26 13:25:17 -0700648 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
649 Flavor flavor, boolean isOptical,
650 boolean showTraffic) {
651 if (links != null) {
652 for (Link link : links) {
653 TrafficLink tlink = linkMap.add(link);
654 tlink.tagFlavor(flavor);
655 tlink.optical(isOptical);
656 if (showTraffic) {
657 tlink.addLoad(getLinkFlowLoad(link));
658 tlink.antMarch(true);
659 }
660 }
661 }
662 }
663
664 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
665 for (TrafficLink tlink : linkMap.biLinks()) {
666 highlights.add(tlink.highlight(StatsType.TAGGED));
667 }
668 }
669
Simon Hunta17fa672015-08-19 18:42:22 -0700670 // =======================================================================
671 // === Background Task
672
673 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700674 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700675 @Override
676 public void run() {
677 try {
678 switch (mode) {
Simon Hunt21281fd2017-03-30 22:28:28 -0700679 case ALL_FLOW_TRAFFIC_BYTES:
Simon Hunta17fa672015-08-19 18:42:22 -0700680 sendAllFlowTraffic();
681 break;
Simon Hunt21281fd2017-03-30 22:28:28 -0700682 case ALL_PORT_TRAFFIC_BIT_PS:
683 sendAllPortTraffic(StatsType.PORT_STATS);
684 break;
685 case ALL_PORT_TRAFFIC_PKT_PS:
686 sendAllPortTraffic(StatsType.PORT_PACKET_STATS);
Simon Hunta17fa672015-08-19 18:42:22 -0700687 break;
688 case DEV_LINK_FLOWS:
689 sendDeviceLinkFlows();
690 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700691 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700692 sendSelectedIntentTraffic();
693 break;
694
695 default:
696 // RELATED_INTENTS and IDLE modes should never invoke
697 // the background task, but if they do, they have
698 // nothing to do
699 break;
700 }
701
702 } catch (Exception e) {
703 log.warn("Unable to process traffic task due to {}", e.getMessage());
704 log.warn("Boom!", e);
705 }
706 }
707 }
Simon Hunta17fa672015-08-19 18:42:22 -0700708}