blob: cc03524522b005473f2997c04c60ba6209a4839f [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;
Marc De Leenheer20913c62017-04-12 14:45:15 -070029import org.onosproject.net.device.DeviceService;
Simon Hunta17fa672015-08-19 18:42:22 -070030import org.onosproject.net.flow.FlowEntry;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.flow.instructions.Instruction;
33import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Thomas Vachuskada0665b2016-03-02 19:06:17 -080034import org.onosproject.net.intent.FlowObjectiveIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070035import org.onosproject.net.intent.FlowRuleIntent;
Simon Hunt5328f792017-01-11 17:43:31 -080036import org.onosproject.net.intent.HostToHostIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070037import org.onosproject.net.intent.Intent;
38import org.onosproject.net.intent.LinkCollectionIntent;
39import org.onosproject.net.intent.OpticalConnectivityIntent;
40import org.onosproject.net.intent.OpticalPathIntent;
41import org.onosproject.net.intent.PathIntent;
Marc De Leenheer20913c62017-04-12 14:45:15 -070042import org.onosproject.net.link.LinkService;
Simon Hunta17fa672015-08-19 18:42:22 -070043import org.onosproject.net.statistic.Load;
Simon Hunted804d52016-03-30 09:51:40 -070044import org.onosproject.ui.impl.topo.util.IntentSelection;
45import org.onosproject.ui.impl.topo.util.ServicesBundle;
46import org.onosproject.ui.impl.topo.util.TopoIntentFilter;
47import org.onosproject.ui.impl.topo.util.TrafficLink;
48import org.onosproject.ui.impl.topo.util.TrafficLink.StatsType;
49import org.onosproject.ui.impl.topo.util.TrafficLinkMap;
Simon Hunt441c9ae2017-02-03 18:22:31 -080050import org.onosproject.ui.topo.AbstractTopoMonitor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070051import org.onosproject.ui.topo.DeviceHighlight;
Simon Hunta17fa672015-08-19 18:42:22 -070052import org.onosproject.ui.topo.Highlights;
Simon Hunt94f7dae2015-08-26 17:40:59 -070053import org.onosproject.ui.topo.Highlights.Amount;
54import org.onosproject.ui.topo.HostHighlight;
Simon Hunt57830172015-08-26 13:25:17 -070055import org.onosproject.ui.topo.LinkHighlight.Flavor;
Simon Hunt94f7dae2015-08-26 17:40:59 -070056import org.onosproject.ui.topo.NodeHighlight;
Simon Hunt743a8492015-08-25 16:18:19 -070057import org.onosproject.ui.topo.NodeSelection;
58import org.onosproject.ui.topo.TopoUtils;
Simon Hunta17fa672015-08-19 18:42:22 -070059import org.slf4j.Logger;
60import org.slf4j.LoggerFactory;
61
62import java.util.ArrayList;
63import java.util.Collection;
64import java.util.Collections;
65import java.util.HashMap;
66import java.util.HashSet;
67import java.util.List;
68import java.util.Map;
69import java.util.Set;
70import java.util.Timer;
71import java.util.TimerTask;
Marc De Leenheer20913c62017-04-12 14:45:15 -070072import java.util.stream.Collectors;
Simon Hunta17fa672015-08-19 18:42:22 -070073
Thomas Vachuska0932ac52017-03-30 13:28:49 -070074import static org.onosproject.incubator.net.PortStatisticsService.MetricType.BYTES;
75import static org.onosproject.incubator.net.PortStatisticsService.MetricType.PACKETS;
Simon Hunta17fa672015-08-19 18:42:22 -070076import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Marc De Leenheer20913c62017-04-12 14:45:15 -070077import static org.onosproject.ui.impl.TrafficMonitor.Mode.IDLE;
78import static org.onosproject.ui.impl.TrafficMonitor.Mode.RELATED_INTENTS;
79import static org.onosproject.ui.impl.TrafficMonitor.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070080
81/**
82 * Encapsulates the behavior of monitoring specific traffic patterns.
83 */
Simon Hunt441c9ae2017-02-03 18:22:31 -080084public class TrafficMonitor extends AbstractTopoMonitor {
Simon Hunta17fa672015-08-19 18:42:22 -070085
86 // 4 Kilo Bytes as threshold
Simon Hunt21281fd2017-03-30 22:28:28 -070087 private static final double BPS_THRESHOLD = 4 * TopoUtils.N_KILO;
Simon Hunta17fa672015-08-19 18:42:22 -070088
89 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070090 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070091
92 /**
93 * Designates the different modes of operation.
94 */
95 public enum Mode {
96 IDLE,
Simon Hunt21281fd2017-03-30 22:28:28 -070097 ALL_FLOW_TRAFFIC_BYTES,
98 ALL_PORT_TRAFFIC_BIT_PS,
99 ALL_PORT_TRAFFIC_PKT_PS,
Simon Hunta17fa672015-08-19 18:42:22 -0700100 DEV_LINK_FLOWS,
101 RELATED_INTENTS,
Simon Hunt4fc86852015-08-20 17:57:52 -0700102 SELECTED_INTENT
Simon Hunta17fa672015-08-19 18:42:22 -0700103 }
104
105 private final long trafficPeriod;
106 private final ServicesBundle servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700107 private final TopologyViewMessageHandler msgHandler;
108 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -0700109
110 private final Timer timer = new Timer("topo-traffic");
111
112 private TimerTask trafficTask = null;
113 private Mode mode = IDLE;
114 private NodeSelection selectedNodes = null;
115 private IntentSelection selectedIntents = null;
116
117
118 /**
119 * Constructs a traffic monitor.
120 *
Simon Hunt5328f792017-01-11 17:43:31 -0800121 * @param trafficPeriod traffic task period in ms
122 * @param servicesBundle bundle of services
123 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -0700124 */
Simon Hunt4fc86852015-08-20 17:57:52 -0700125 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
126 TopologyViewMessageHandler msgHandler) {
Simon Hunta17fa672015-08-19 18:42:22 -0700127 this.trafficPeriod = trafficPeriod;
128 this.servicesBundle = servicesBundle;
Simon Hunt4fc86852015-08-20 17:57:52 -0700129 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700130
Simon Hunt4fc86852015-08-20 17:57:52 -0700131 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700132 }
133
134 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700135 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700136
Simon Hunt4fc86852015-08-20 17:57:52 -0700137 /**
138 * Monitor for traffic data to be sent back to the web client, under
139 * the given mode. This causes a background traffic task to be
140 * scheduled to repeatedly compute and transmit the appropriate traffic
141 * data to the client.
142 * <p>
143 * The monitoring mode is expected to be one of:
144 * <ul>
Simon Hunt21281fd2017-03-30 22:28:28 -0700145 * <li>ALL_FLOW_TRAFFIC_BYTES</li>
146 * <li>ALL_PORT_TRAFFIC_BIT_PS</li>
147 * <li>ALL_PORT_TRAFFIC_PKT_PS</li>
Simon Hunt5328f792017-01-11 17:43:31 -0800148 * <li>SELECTED_INTENT</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700149 * </ul>
150 *
151 * @param mode monitoring mode
152 */
Simon Hunta17fa672015-08-19 18:42:22 -0700153 public synchronized void monitor(Mode mode) {
154 log.debug("monitor: {}", mode);
155 this.mode = mode;
156
157 switch (mode) {
Simon Hunt21281fd2017-03-30 22:28:28 -0700158 case ALL_FLOW_TRAFFIC_BYTES:
Simon Hunta17fa672015-08-19 18:42:22 -0700159 clearSelection();
160 scheduleTask();
161 sendAllFlowTraffic();
162 break;
163
Simon Hunt21281fd2017-03-30 22:28:28 -0700164 case ALL_PORT_TRAFFIC_BIT_PS:
Simon Hunta17fa672015-08-19 18:42:22 -0700165 clearSelection();
166 scheduleTask();
Simon Hunt21281fd2017-03-30 22:28:28 -0700167 sendAllPortTraffic(StatsType.PORT_STATS);
168 break;
169
170 case ALL_PORT_TRAFFIC_PKT_PS:
171 clearSelection();
172 scheduleTask();
173 sendAllPortTraffic(StatsType.PORT_PACKET_STATS);
Simon Hunta17fa672015-08-19 18:42:22 -0700174 break;
175
Simon Hunt4fc86852015-08-20 17:57:52 -0700176 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700177 scheduleTask();
178 sendSelectedIntentTraffic();
179 break;
180
181 default:
182 log.debug("Unexpected call to monitor({})", mode);
183 clearAll();
184 break;
185 }
186 }
187
Simon Hunt4fc86852015-08-20 17:57:52 -0700188 /**
189 * Monitor for traffic data to be sent back to the web client, under
190 * the given mode, using the given selection of devices and hosts.
191 * In the case of "device link flows", this causes a background traffic
192 * task to be scheduled to repeatedly compute and transmit the appropriate
193 * traffic data to the client. In the case of "related intents", no
194 * repeating task is scheduled.
195 * <p>
196 * The monitoring mode is expected to be one of:
197 * <ul>
Simon Hunt5328f792017-01-11 17:43:31 -0800198 * <li>DEV_LINK_FLOWS</li>
199 * <li>RELATED_INTENTS</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700200 * </ul>
201 *
Simon Hunt5328f792017-01-11 17:43:31 -0800202 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700203 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700204 */
Simon Hunta17fa672015-08-19 18:42:22 -0700205 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
206 log.debug("monitor: {} -- {}", mode, nodeSelection);
207 this.mode = mode;
208 this.selectedNodes = nodeSelection;
209
210 switch (mode) {
211 case DEV_LINK_FLOWS:
212 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700213 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700214 sendClearAll();
215 } else {
216 scheduleTask();
217 sendDeviceLinkFlows();
218 }
219 break;
220
221 case RELATED_INTENTS:
222 if (selectedNodes.none()) {
223 sendClearAll();
224 } else {
225 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
226 if (selectedIntents.none()) {
227 sendClearAll();
228 } else {
229 sendSelectedIntents();
230 }
231 }
232 break;
233
234 default:
235 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
236 clearAll();
237 break;
238 }
239 }
240
Simon Hunt4fc86852015-08-20 17:57:52 -0700241 // TODO: move this out to the "h2h/multi-intent app"
Simon Hunt5328f792017-01-11 17:43:31 -0800242
Simon Hunt4fc86852015-08-20 17:57:52 -0700243 /**
244 * Monitor for traffic data to be sent back to the web client, for the
245 * given intent.
246 *
247 * @param intent the intent to monitor
248 */
Simon Hunta17fa672015-08-19 18:42:22 -0700249 public synchronized void monitor(Intent intent) {
250 log.debug("monitor intent: {}", intent.id());
251 selectedNodes = null;
252 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700253 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700254 scheduleTask();
255 sendSelectedIntentTraffic();
256 }
257
Simon Hunt4fc86852015-08-20 17:57:52 -0700258 /**
259 * Selects the next intent in the select group (if there is one),
260 * and sends highlighting data back to the web client to display
261 * which path is selected.
262 */
Simon Hunta17fa672015-08-19 18:42:22 -0700263 public synchronized void selectNextIntent() {
264 if (selectedIntents != null) {
265 selectedIntents.next();
266 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700267 if (mode == SELECTED_INTENT) {
268 mode = RELATED_INTENTS;
269 }
Simon Hunta17fa672015-08-19 18:42:22 -0700270 }
271 }
272
Simon Hunt4fc86852015-08-20 17:57:52 -0700273 /**
274 * Selects the previous intent in the select group (if there is one),
275 * and sends highlighting data back to the web client to display
276 * which path is selected.
277 */
Simon Hunta17fa672015-08-19 18:42:22 -0700278 public synchronized void selectPreviousIntent() {
279 if (selectedIntents != null) {
280 selectedIntents.prev();
281 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700282 if (mode == SELECTED_INTENT) {
283 mode = RELATED_INTENTS;
284 }
Simon Hunta17fa672015-08-19 18:42:22 -0700285 }
286 }
287
Simon Hunt4fc86852015-08-20 17:57:52 -0700288 /**
289 * Resends selected intent traffic data. This is called, for example,
290 * when the system detects an intent update happened.
291 */
Simon Hunta17fa672015-08-19 18:42:22 -0700292 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700293 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700294 sendSelectedIntentTraffic();
295 }
296 }
297
Simon Hunt4fc86852015-08-20 17:57:52 -0700298 /**
299 * Stop all traffic monitoring.
300 */
301 public synchronized void stopMonitoring() {
302 log.debug("STOP monitoring");
Simon Hunta17fa672015-08-19 18:42:22 -0700303 if (mode != IDLE) {
304 sendClearAll();
305 }
306 }
307
308
309 // =======================================================================
310 // === Helper methods ===
311
312 private void sendClearAll() {
313 clearAll();
314 sendClearHighlights();
315 }
316
317 private void clearAll() {
318 this.mode = IDLE;
319 clearSelection();
320 cancelTask();
321 }
322
323 private void clearSelection() {
324 selectedNodes = null;
325 selectedIntents = null;
326 }
327
Simon Hunt5328f792017-01-11 17:43:31 -0800328 private synchronized void scheduleTask() {
Simon Hunta17fa672015-08-19 18:42:22 -0700329 if (trafficTask == null) {
330 log.debug("Starting up background traffic task...");
Simon Hunt4fc86852015-08-20 17:57:52 -0700331 trafficTask = new TrafficUpdateTask();
Simon Hunta17fa672015-08-19 18:42:22 -0700332 timer.schedule(trafficTask, trafficPeriod, trafficPeriod);
333 } else {
Simon Hunta17fa672015-08-19 18:42:22 -0700334 log.debug("(traffic task already running)");
335 }
336 }
337
338 private synchronized void cancelTask() {
339 if (trafficTask != null) {
340 trafficTask.cancel();
341 trafficTask = null;
342 }
343 }
344
Simon Hunta17fa672015-08-19 18:42:22 -0700345 private void sendAllFlowTraffic() {
346 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700347 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700348 }
349
Simon Hunt21281fd2017-03-30 22:28:28 -0700350 private void sendAllPortTraffic(StatsType t) {
351 log.debug("sendAllPortTraffic: {}", t);
352 msgHandler.sendHighlights(trafficSummary(t));
Simon Hunta17fa672015-08-19 18:42:22 -0700353 }
354
355 private void sendDeviceLinkFlows() {
356 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700357 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700358 }
359
360 private void sendSelectedIntents() {
361 log.debug("sendSelectedIntents: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700362 msgHandler.sendHighlights(intentGroup());
Simon Hunta17fa672015-08-19 18:42:22 -0700363 }
364
365 private void sendSelectedIntentTraffic() {
366 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700367 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700368 }
369
370 private void sendClearHighlights() {
371 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700372 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700373 }
374
Simon Hunta17fa672015-08-19 18:42:22 -0700375 // =======================================================================
376 // === Generate messages in JSON object node format
377
Simon Hunt57830172015-08-26 13:25:17 -0700378 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700379 Highlights highlights = new Highlights();
380
Simon Hunt4fc86852015-08-20 17:57:52 -0700381 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700382 compileLinks(linkMap);
383 addEdgeLinks(linkMap);
384
Simon Hunt4fc86852015-08-20 17:57:52 -0700385 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700386 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700387 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700388 } else if (type == StatsType.PORT_STATS) {
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700389 attachPortLoad(tlink, BYTES);
390 } else if (type == StatsType.PORT_PACKET_STATS) {
391 attachPortLoad(tlink, PACKETS);
Simon Hunta17fa672015-08-19 18:42:22 -0700392 }
393
394 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700395 if (tlink.hasTraffic()) {
396 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700397 }
398 }
399 return highlights;
400 }
401
402 // create highlights for links, showing flows for selected devices.
403 private Highlights deviceLinkFlows() {
404 Highlights highlights = new Highlights();
405
Simon Hunt72297212015-08-25 10:15:33 -0700406 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700407 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700408 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700409
Simon Hunt72297212015-08-25 10:15:33 -0700410 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700411 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
412 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700413 TrafficLink tlink = linkMap.add(link);
414 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700415 }
416 }
417
418 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700419 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700420 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700421 }
422
423 }
424 return highlights;
425 }
426
427 private Highlights intentGroup() {
428 Highlights highlights = new Highlights();
429
430 if (selectedIntents != null && !selectedIntents.none()) {
431 // If 'all' intents are selected, they will all have primary
432 // highlighting; otherwise, the specifically selected intent will
433 // have primary highlighting, and the remainder will have secondary
434 // highlighting.
435 Set<Intent> primary;
436 Set<Intent> secondary;
437 int count = selectedIntents.size();
438
439 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
440 Intent current;
441
442 if (selectedIntents.all()) {
443 primary = allBut;
444 secondary = Collections.emptySet();
445 log.debug("Highlight all intents ({})", count);
446 } else {
447 current = selectedIntents.current();
448 primary = new HashSet<>();
449 primary.add(current);
450 allBut.remove(current);
451 secondary = allBut;
452 log.debug("Highlight intent: {} ([{}] of {})",
Simon Hunt5328f792017-01-11 17:43:31 -0800453 current.id(), selectedIntents.index(), count);
Simon Hunta17fa672015-08-19 18:42:22 -0700454 }
Simon Hunt57830172015-08-26 13:25:17 -0700455
456 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700457 }
458 return highlights;
459 }
460
461 private Highlights intentTraffic() {
462 Highlights highlights = new Highlights();
463
464 if (selectedIntents != null && selectedIntents.single()) {
465 Intent current = selectedIntents.current();
466 Set<Intent> primary = new HashSet<>();
467 primary.add(current);
468 log.debug("Highlight traffic for intent: {} ([{}] of {})",
Simon Hunt5328f792017-01-11 17:43:31 -0800469 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700470
471 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700472 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700473 }
474 return highlights;
475 }
476
Simon Hunta17fa672015-08-19 18:42:22 -0700477 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
478
Simon Hunt4fc86852015-08-20 17:57:52 -0700479 private void compileLinks(TrafficLinkMap linkMap) {
480 servicesBundle.linkService().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700481 }
482
Simon Hunt4fc86852015-08-20 17:57:52 -0700483 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunta17fa672015-08-19 18:42:22 -0700484 servicesBundle.hostService().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700485 linkMap.add(createEdgeLink(host, true));
486 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700487 });
488 }
489
490 private Load getLinkFlowLoad(Link link) {
491 if (link != null && link.src().elementId() instanceof DeviceId) {
492 return servicesBundle.flowStatsService().load(link);
493 }
494 return null;
495 }
496
Simon Hunt4fc86852015-08-20 17:57:52 -0700497 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700498 link.addLoad(getLinkFlowLoad(link.one()));
499 link.addLoad(getLinkFlowLoad(link.two()));
500 }
501
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700502 private void attachPortLoad(TrafficLink link, MetricType metricType) {
Simon Hunta17fa672015-08-19 18:42:22 -0700503 // For bi-directional traffic links, use
504 // the max link rate of either direction
505 // (we choose 'one' since we know that is never null)
506 Link one = link.one();
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700507 Load egressSrc = servicesBundle.portStatsService().load(one.src(), metricType);
508 Load egressDst = servicesBundle.portStatsService().load(one.dst(), metricType);
509 link.addLoad(maxLoad(egressSrc, egressDst), metricType == BYTES ? BPS_THRESHOLD : 0);
Simon Hunta17fa672015-08-19 18:42:22 -0700510 }
511
512 private Load maxLoad(Load a, Load b) {
513 if (a == null) {
514 return b;
515 }
516 if (b == null) {
517 return a;
518 }
519 return a.rate() > b.rate() ? a : b;
520 }
521
Simon Hunta17fa672015-08-19 18:42:22 -0700522 // Counts all flow entries that egress on the links of the given device.
523 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
524 // get the flows for the device
525 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt5328f792017-01-11 17:43:31 -0800526 for (FlowEntry flowEntry : servicesBundle.flowService().getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700527 entries.add(flowEntry);
528 }
529
530 // get egress links from device, and include edge links
Simon Hunt4fc86852015-08-20 17:57:52 -0700531 Set<Link> links = new HashSet<>(servicesBundle.linkService()
Simon Hunt5328f792017-01-11 17:43:31 -0800532 .getDeviceEgressLinks(deviceId));
Simon Hunta17fa672015-08-19 18:42:22 -0700533 Set<Host> hosts = servicesBundle.hostService().getConnectedHosts(deviceId);
534 if (hosts != null) {
535 for (Host host : hosts) {
536 links.add(createEdgeLink(host, false));
537 }
538 }
539
540 // compile flow counts per link
541 Map<Link, Integer> counts = new HashMap<>();
542 for (Link link : links) {
543 counts.put(link, getEgressFlows(link, entries));
544 }
545 return counts;
546 }
547
548 // Counts all entries that egress on the link source port.
549 private int getEgressFlows(Link link, List<FlowEntry> entries) {
550 int count = 0;
551 PortNumber out = link.src().port();
552 for (FlowEntry entry : entries) {
553 TrafficTreatment treatment = entry.treatment();
554 for (Instruction instruction : treatment.allInstructions()) {
555 if (instruction.type() == Instruction.Type.OUTPUT &&
556 ((OutputInstruction) instruction).port().equals(out)) {
557 count++;
558 }
559 }
560 }
561 return count;
562 }
563
Simon Hunt57830172015-08-26 13:25:17 -0700564 private void highlightIntentLinks(Highlights highlights,
565 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700566 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700567 // NOTE: highlight secondary first, then primary, so that links shared
568 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700569 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
570 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700571 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700572 }
573
Simon Hunt57830172015-08-26 13:25:17 -0700574 private void highlightIntentLinksWithTraffic(Highlights highlights,
575 Set<Intent> primary) {
576 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700577 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700578 colorLinks(highlights, linkMap);
579 }
580
Simon Hunt94f7dae2015-08-26 17:40:59 -0700581 private void createTrafficLinks(Highlights highlights,
582 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700583 Flavor flavor, boolean showTraffic) {
584 for (Intent intent : intents) {
Simon Hunta17fa672015-08-19 18:42:22 -0700585 List<Intent> installables = servicesBundle.intentService()
586 .getInstallableIntents(intent.key());
587 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700588 if (installables != null) {
589 for (Intent installable : installables) {
590
591 if (installable instanceof PathIntent) {
592 links = ((PathIntent) installable).path().links();
593 } else if (installable instanceof FlowRuleIntent) {
Marc De Leenheer20913c62017-04-12 14:45:15 -0700594 Collection<Link> l = new ArrayList<>();
595 l.addAll(linkResources(installable));
596 // Add cross connect links
597 if (intent instanceof OpticalConnectivityIntent) {
598 OpticalConnectivityIntent ocIntent = (OpticalConnectivityIntent) intent;
599 LinkService linkService = servicesBundle.linkService();
600 DeviceService deviceService = servicesBundle.deviceService();
601 l.addAll(linkService.getDeviceIngressLinks(ocIntent.getSrc().deviceId()).stream()
602 .filter(i ->
603 deviceService.getDevice(i.src().deviceId()).type() == Device.Type.SWITCH)
604 .collect(Collectors.toList()));
605 l.addAll(linkService.getDeviceEgressLinks(ocIntent.getDst().deviceId()).stream()
606 .filter(e ->
607 deviceService.getDevice(e.dst().deviceId()).type() == Device.Type.SWITCH)
608 .collect(Collectors.toList()));
609 }
610 links = l;
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800611 } else if (installable instanceof FlowObjectiveIntent) {
Simon Hunt5328f792017-01-11 17:43:31 -0800612 links = addEdgeLinksIfNeeded(intent, linkResources(installable));
Simon Hunta17fa672015-08-19 18:42:22 -0700613 } else if (installable instanceof LinkCollectionIntent) {
614 links = ((LinkCollectionIntent) installable).links();
615 } else if (installable instanceof OpticalPathIntent) {
616 links = ((OpticalPathIntent) installable).path().links();
617 }
618
Simon Hunt57830172015-08-26 13:25:17 -0700619 boolean isOptical = intent instanceof OpticalConnectivityIntent;
620 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700621 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700622 }
623 }
624 }
625 }
626
Simon Hunt5328f792017-01-11 17:43:31 -0800627 private Iterable<Link> addEdgeLinksIfNeeded(Intent parentIntent,
628 Collection<Link> links) {
629 if (parentIntent instanceof HostToHostIntent) {
630 links = new HashSet<>(links);
631 HostToHostIntent h2h = (HostToHostIntent) parentIntent;
632 Host h1 = servicesBundle.hostService().getHost(h2h.one());
633 Host h2 = servicesBundle.hostService().getHost(h2h.two());
634 links.add(createEdgeLink(h1, true));
635 links.add(createEdgeLink(h2, true));
636 }
637 return links;
638 }
639
Simon Hunt94f7dae2015-08-26 17:40:59 -0700640 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
641 for (Link link : links) {
642 ensureNodePresent(highlights, link.src().elementId());
643 ensureNodePresent(highlights, link.dst().elementId());
644 }
645 }
646
647 private void ensureNodePresent(Highlights highlights, ElementId eid) {
648 String id = eid.toString();
649 NodeHighlight nh = highlights.getNode(id);
650 if (nh == null) {
651 if (eid instanceof DeviceId) {
652 nh = new DeviceHighlight(id);
653 highlights.add((DeviceHighlight) nh);
654 } else if (eid instanceof HostId) {
655 nh = new HostHighlight(id);
656 highlights.add((HostHighlight) nh);
657 }
658 }
659 }
660
Simon Hunta17fa672015-08-19 18:42:22 -0700661 // Extracts links from the specified flow rule intent resources
662 private Collection<Link> linkResources(Intent installable) {
663 ImmutableList.Builder<Link> builder = ImmutableList.builder();
664 installable.resources().stream().filter(r -> r instanceof Link)
665 .forEach(r -> builder.add((Link) r));
666 return builder.build();
667 }
668
Simon Hunt57830172015-08-26 13:25:17 -0700669 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
670 Flavor flavor, boolean isOptical,
671 boolean showTraffic) {
672 if (links != null) {
673 for (Link link : links) {
674 TrafficLink tlink = linkMap.add(link);
675 tlink.tagFlavor(flavor);
676 tlink.optical(isOptical);
677 if (showTraffic) {
678 tlink.addLoad(getLinkFlowLoad(link));
679 tlink.antMarch(true);
680 }
681 }
682 }
683 }
684
685 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
686 for (TrafficLink tlink : linkMap.biLinks()) {
687 highlights.add(tlink.highlight(StatsType.TAGGED));
688 }
689 }
690
Simon Hunta17fa672015-08-19 18:42:22 -0700691 // =======================================================================
692 // === Background Task
693
694 // Provides periodic update of traffic information to the client
Simon Hunt4fc86852015-08-20 17:57:52 -0700695 private class TrafficUpdateTask extends TimerTask {
Simon Hunta17fa672015-08-19 18:42:22 -0700696 @Override
697 public void run() {
698 try {
699 switch (mode) {
Simon Hunt21281fd2017-03-30 22:28:28 -0700700 case ALL_FLOW_TRAFFIC_BYTES:
Simon Hunta17fa672015-08-19 18:42:22 -0700701 sendAllFlowTraffic();
702 break;
Simon Hunt21281fd2017-03-30 22:28:28 -0700703 case ALL_PORT_TRAFFIC_BIT_PS:
704 sendAllPortTraffic(StatsType.PORT_STATS);
705 break;
706 case ALL_PORT_TRAFFIC_PKT_PS:
707 sendAllPortTraffic(StatsType.PORT_PACKET_STATS);
Simon Hunta17fa672015-08-19 18:42:22 -0700708 break;
709 case DEV_LINK_FLOWS:
710 sendDeviceLinkFlows();
711 break;
Simon Hunt4fc86852015-08-20 17:57:52 -0700712 case SELECTED_INTENT:
Simon Hunta17fa672015-08-19 18:42:22 -0700713 sendSelectedIntentTraffic();
714 break;
715
716 default:
717 // RELATED_INTENTS and IDLE modes should never invoke
718 // the background task, but if they do, they have
719 // nothing to do
720 break;
721 }
722
723 } catch (Exception e) {
724 log.warn("Unable to process traffic task due to {}", e.getMessage());
725 log.warn("Boom!", e);
726 }
727 }
728 }
Simon Hunta17fa672015-08-19 18:42:22 -0700729}