blob: 660ba724cdd9ed405792f6d81bc7ad56babaf5a5 [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;
Simon Hunt1911fe42017-05-02 18:25:58 -070021import com.google.common.collect.Lists;
Thomas Vachuska0932ac52017-03-30 13:28:49 -070022import org.onosproject.incubator.net.PortStatisticsService.MetricType;
Simon Hunta17fa672015-08-19 18:42:22 -070023import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
Simon Hunt94f7dae2015-08-26 17:40:59 -070025import org.onosproject.net.ElementId;
Simon Hunta17fa672015-08-19 18:42:22 -070026import org.onosproject.net.Host;
Simon Hunt94f7dae2015-08-26 17:40:59 -070027import org.onosproject.net.HostId;
Simon Hunta17fa672015-08-19 18:42:22 -070028import org.onosproject.net.Link;
Simon Hunta17fa672015-08-19 18:42:22 -070029import org.onosproject.net.PortNumber;
Marc De Leenheer20913c62017-04-12 14:45:15 -070030import org.onosproject.net.device.DeviceService;
Simon Hunta17fa672015-08-19 18:42:22 -070031import org.onosproject.net.flow.FlowEntry;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.flow.instructions.Instruction;
34import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Thomas Vachuskada0665b2016-03-02 19:06:17 -080035import org.onosproject.net.intent.FlowObjectiveIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070036import org.onosproject.net.intent.FlowRuleIntent;
Simon Hunt5328f792017-01-11 17:43:31 -080037import org.onosproject.net.intent.HostToHostIntent;
Simon Hunta17fa672015-08-19 18:42:22 -070038import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.LinkCollectionIntent;
40import org.onosproject.net.intent.OpticalConnectivityIntent;
41import org.onosproject.net.intent.OpticalPathIntent;
42import org.onosproject.net.intent.PathIntent;
Marc De Leenheer20913c62017-04-12 14:45:15 -070043import org.onosproject.net.link.LinkService;
Simon Hunta17fa672015-08-19 18:42:22 -070044import org.onosproject.net.statistic.Load;
Simon Hunted804d52016-03-30 09:51:40 -070045import org.onosproject.ui.impl.topo.util.IntentSelection;
46import org.onosproject.ui.impl.topo.util.ServicesBundle;
47import org.onosproject.ui.impl.topo.util.TopoIntentFilter;
48import org.onosproject.ui.impl.topo.util.TrafficLink;
49import org.onosproject.ui.impl.topo.util.TrafficLink.StatsType;
50import org.onosproject.ui.impl.topo.util.TrafficLinkMap;
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;
Simon Hunta17fa672015-08-19 18:42:22 -070058import org.slf4j.Logger;
59import org.slf4j.LoggerFactory;
60
61import java.util.ArrayList;
62import java.util.Collection;
63import java.util.Collections;
64import java.util.HashMap;
65import java.util.HashSet;
66import java.util.List;
67import java.util.Map;
68import java.util.Set;
Marc De Leenheer20913c62017-04-12 14:45:15 -070069import java.util.stream.Collectors;
Simon Hunta17fa672015-08-19 18:42:22 -070070
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;
Simon Hunt1911fe42017-05-02 18:25:58 -070074import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.RELATED_INTENTS;
75import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070076
77/**
78 * Encapsulates the behavior of monitoring specific traffic patterns.
79 */
Simon Hunt1911fe42017-05-02 18:25:58 -070080public class TrafficMonitor extends TrafficMonitorBase {
Simon Hunta17fa672015-08-19 18:42:22 -070081
82 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070083 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070084
Simon Hunt4fc86852015-08-20 17:57:52 -070085 private final TopologyViewMessageHandler msgHandler;
86 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -070087
Simon Hunta17fa672015-08-19 18:42:22 -070088 private NodeSelection selectedNodes = null;
89 private IntentSelection selectedIntents = null;
90
91
92 /**
93 * Constructs a traffic monitor.
94 *
Simon Hunt5328f792017-01-11 17:43:31 -080095 * @param trafficPeriod traffic task period in ms
96 * @param servicesBundle bundle of services
97 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -070098 */
Simon Hunt4fc86852015-08-20 17:57:52 -070099 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
100 TopologyViewMessageHandler msgHandler) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700101 super(trafficPeriod, servicesBundle);
Simon Hunt4fc86852015-08-20 17:57:52 -0700102 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700103
Simon Hunt4fc86852015-08-20 17:57:52 -0700104 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700105 }
106
107 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700108 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700109
Simon Hunt1911fe42017-05-02 18:25:58 -0700110 // monitor(Mode) is implemented in the super class
Simon Hunta17fa672015-08-19 18:42:22 -0700111
Simon Hunt4fc86852015-08-20 17:57:52 -0700112 /**
113 * Monitor for traffic data to be sent back to the web client, under
114 * the given mode, using the given selection of devices and hosts.
115 * In the case of "device link flows", this causes a background traffic
116 * task to be scheduled to repeatedly compute and transmit the appropriate
117 * traffic data to the client. In the case of "related intents", no
118 * repeating task is scheduled.
119 * <p>
120 * The monitoring mode is expected to be one of:
121 * <ul>
Simon Hunt5328f792017-01-11 17:43:31 -0800122 * <li>DEV_LINK_FLOWS</li>
123 * <li>RELATED_INTENTS</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700124 * </ul>
125 *
Simon Hunt5328f792017-01-11 17:43:31 -0800126 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700127 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700128 */
Simon Hunta17fa672015-08-19 18:42:22 -0700129 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
130 log.debug("monitor: {} -- {}", mode, nodeSelection);
131 this.mode = mode;
132 this.selectedNodes = nodeSelection;
133
134 switch (mode) {
135 case DEV_LINK_FLOWS:
136 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700137 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700138 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700139 } else {
140 scheduleTask();
141 sendDeviceLinkFlows();
142 }
143 break;
144
145 case RELATED_INTENTS:
146 if (selectedNodes.none()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700147 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700148 } else {
149 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
150 if (selectedIntents.none()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700151 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700152 } else {
153 sendSelectedIntents();
154 }
155 }
156 break;
157
158 default:
159 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
160 clearAll();
161 break;
162 }
163 }
164
Simon Hunt4fc86852015-08-20 17:57:52 -0700165 // TODO: move this out to the "h2h/multi-intent app"
Simon Hunt5328f792017-01-11 17:43:31 -0800166
Simon Hunt4fc86852015-08-20 17:57:52 -0700167 /**
168 * Monitor for traffic data to be sent back to the web client, for the
169 * given intent.
170 *
171 * @param intent the intent to monitor
172 */
Simon Hunta17fa672015-08-19 18:42:22 -0700173 public synchronized void monitor(Intent intent) {
174 log.debug("monitor intent: {}", intent.id());
175 selectedNodes = null;
176 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700177 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700178 scheduleTask();
179 sendSelectedIntentTraffic();
180 }
181
Simon Hunt4fc86852015-08-20 17:57:52 -0700182 /**
183 * Selects the next intent in the select group (if there is one),
184 * and sends highlighting data back to the web client to display
185 * which path is selected.
186 */
Simon Hunta17fa672015-08-19 18:42:22 -0700187 public synchronized void selectNextIntent() {
188 if (selectedIntents != null) {
189 selectedIntents.next();
190 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700191 if (mode == SELECTED_INTENT) {
192 mode = RELATED_INTENTS;
193 }
Simon Hunta17fa672015-08-19 18:42:22 -0700194 }
195 }
196
Simon Hunt4fc86852015-08-20 17:57:52 -0700197 /**
198 * Selects the previous intent in the select group (if there is one),
199 * and sends highlighting data back to the web client to display
200 * which path is selected.
201 */
Simon Hunta17fa672015-08-19 18:42:22 -0700202 public synchronized void selectPreviousIntent() {
203 if (selectedIntents != null) {
204 selectedIntents.prev();
205 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700206 if (mode == SELECTED_INTENT) {
207 mode = RELATED_INTENTS;
208 }
Simon Hunta17fa672015-08-19 18:42:22 -0700209 }
210 }
211
Simon Hunt4fc86852015-08-20 17:57:52 -0700212 /**
213 * Resends selected intent traffic data. This is called, for example,
214 * when the system detects an intent update happened.
215 */
Simon Hunta17fa672015-08-19 18:42:22 -0700216 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700217 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700218 sendSelectedIntentTraffic();
219 }
220 }
221
Simon Hunta17fa672015-08-19 18:42:22 -0700222 // =======================================================================
Simon Hunt1911fe42017-05-02 18:25:58 -0700223 // === Abstract method implementations ===
Simon Hunta17fa672015-08-19 18:42:22 -0700224
Simon Hunt1911fe42017-05-02 18:25:58 -0700225 @Override
226 protected void sendAllFlowTraffic() {
Simon Hunta17fa672015-08-19 18:42:22 -0700227 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700228 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700229 }
230
Simon Hunt1911fe42017-05-02 18:25:58 -0700231 @Override
232 protected void sendAllPortTrafficBits() {
233 log.debug("sendAllPortTrafficBits");
234 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700235 }
236
Simon Hunt1911fe42017-05-02 18:25:58 -0700237 @Override
238 protected void sendAllPortTrafficPackets() {
239 log.debug("sendAllPortTrafficPackets");
240 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_PACKET_STATS));
241 }
242
243 @Override
244 protected void sendDeviceLinkFlows() {
Simon Hunta17fa672015-08-19 18:42:22 -0700245 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700246 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700247 }
248
Simon Hunt1911fe42017-05-02 18:25:58 -0700249 @Override
250 protected void sendSelectedIntentTraffic() {
Simon Hunta17fa672015-08-19 18:42:22 -0700251 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700252 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700253 }
254
Simon Hunt1911fe42017-05-02 18:25:58 -0700255 @Override
256 protected void sendClearHighlights() {
Simon Hunta17fa672015-08-19 18:42:22 -0700257 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700258 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700259 }
260
Simon Hunt1911fe42017-05-02 18:25:58 -0700261 @Override
262 protected void clearSelection() {
263 selectedNodes = null;
264 selectedIntents = null;
265 }
266
267
268 private void sendSelectedIntents() {
269 log.debug("sendSelectedIntents: {}", selectedIntents);
270 msgHandler.sendHighlights(intentGroup());
271 }
272
Simon Hunta17fa672015-08-19 18:42:22 -0700273 // =======================================================================
274 // === Generate messages in JSON object node format
275
Simon Hunt57830172015-08-26 13:25:17 -0700276 private Highlights trafficSummary(StatsType type) {
Simon Hunta17fa672015-08-19 18:42:22 -0700277 Highlights highlights = new Highlights();
278
Simon Hunt4fc86852015-08-20 17:57:52 -0700279 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700280 compileLinks(linkMap);
281 addEdgeLinks(linkMap);
282
Simon Hunt4fc86852015-08-20 17:57:52 -0700283 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700284 if (type == StatsType.FLOW_STATS) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700285 attachFlowLoad(tlink);
Simon Hunt57830172015-08-26 13:25:17 -0700286 } else if (type == StatsType.PORT_STATS) {
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700287 attachPortLoad(tlink, BYTES);
288 } else if (type == StatsType.PORT_PACKET_STATS) {
289 attachPortLoad(tlink, PACKETS);
Simon Hunta17fa672015-08-19 18:42:22 -0700290 }
291
292 // we only want to report on links deemed to have traffic
Simon Hunt4fc86852015-08-20 17:57:52 -0700293 if (tlink.hasTraffic()) {
294 highlights.add(tlink.highlight(type));
Simon Hunta17fa672015-08-19 18:42:22 -0700295 }
296 }
297 return highlights;
298 }
299
300 // create highlights for links, showing flows for selected devices.
301 private Highlights deviceLinkFlows() {
302 Highlights highlights = new Highlights();
303
Simon Hunt72297212015-08-25 10:15:33 -0700304 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700305 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700306 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700307
Simon Hunt72297212015-08-25 10:15:33 -0700308 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700309 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
310 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700311 TrafficLink tlink = linkMap.add(link);
312 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700313 }
314 }
315
316 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700317 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700318 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700319 }
320
321 }
322 return highlights;
323 }
324
325 private Highlights intentGroup() {
326 Highlights highlights = new Highlights();
327
328 if (selectedIntents != null && !selectedIntents.none()) {
329 // If 'all' intents are selected, they will all have primary
330 // highlighting; otherwise, the specifically selected intent will
331 // have primary highlighting, and the remainder will have secondary
332 // highlighting.
333 Set<Intent> primary;
334 Set<Intent> secondary;
335 int count = selectedIntents.size();
336
337 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
338 Intent current;
339
340 if (selectedIntents.all()) {
341 primary = allBut;
342 secondary = Collections.emptySet();
343 log.debug("Highlight all intents ({})", count);
344 } else {
345 current = selectedIntents.current();
346 primary = new HashSet<>();
347 primary.add(current);
348 allBut.remove(current);
349 secondary = allBut;
350 log.debug("Highlight intent: {} ([{}] of {})",
Simon Hunt1911fe42017-05-02 18:25:58 -0700351 current.id(), selectedIntents.index(), count);
Simon Hunta17fa672015-08-19 18:42:22 -0700352 }
Simon Hunt57830172015-08-26 13:25:17 -0700353
354 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700355 }
356 return highlights;
357 }
358
359 private Highlights intentTraffic() {
360 Highlights highlights = new Highlights();
361
362 if (selectedIntents != null && selectedIntents.single()) {
363 Intent current = selectedIntents.current();
364 Set<Intent> primary = new HashSet<>();
365 primary.add(current);
366 log.debug("Highlight traffic for intent: {} ([{}] of {})",
Simon Hunt1911fe42017-05-02 18:25:58 -0700367 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700368
369 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700370 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700371 }
372 return highlights;
373 }
374
Simon Hunta17fa672015-08-19 18:42:22 -0700375 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
376
Simon Hunt4fc86852015-08-20 17:57:52 -0700377 private void compileLinks(TrafficLinkMap linkMap) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700378 services.link().getLinks().forEach(linkMap::add);
Simon Hunta17fa672015-08-19 18:42:22 -0700379 }
380
Simon Hunt4fc86852015-08-20 17:57:52 -0700381 private void addEdgeLinks(TrafficLinkMap linkMap) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700382 services.host().getHosts().forEach(host -> {
Simon Hunt4fc86852015-08-20 17:57:52 -0700383 linkMap.add(createEdgeLink(host, true));
384 linkMap.add(createEdgeLink(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700385 });
386 }
387
388 private Load getLinkFlowLoad(Link link) {
389 if (link != null && link.src().elementId() instanceof DeviceId) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700390 return services.flowStats().load(link);
Simon Hunta17fa672015-08-19 18:42:22 -0700391 }
392 return null;
393 }
394
Simon Hunt4fc86852015-08-20 17:57:52 -0700395 private void attachFlowLoad(TrafficLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700396 link.addLoad(getLinkFlowLoad(link.one()));
397 link.addLoad(getLinkFlowLoad(link.two()));
398 }
399
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700400 private void attachPortLoad(TrafficLink link, MetricType metricType) {
Simon Hunta17fa672015-08-19 18:42:22 -0700401 // For bi-directional traffic links, use
402 // the max link rate of either direction
403 // (we choose 'one' since we know that is never null)
404 Link one = link.one();
Simon Hunt1911fe42017-05-02 18:25:58 -0700405 Load egressSrc = services.portStats().load(one.src(), metricType);
406 Load egressDst = services.portStats().load(one.dst(), metricType);
Thomas Vachuska0932ac52017-03-30 13:28:49 -0700407 link.addLoad(maxLoad(egressSrc, egressDst), metricType == BYTES ? BPS_THRESHOLD : 0);
Simon Hunta17fa672015-08-19 18:42:22 -0700408 }
409
410 private Load maxLoad(Load a, Load b) {
411 if (a == null) {
412 return b;
413 }
414 if (b == null) {
415 return a;
416 }
417 return a.rate() > b.rate() ? a : b;
418 }
419
Simon Hunta17fa672015-08-19 18:42:22 -0700420 // Counts all flow entries that egress on the links of the given device.
421 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
422 // get the flows for the device
423 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt1911fe42017-05-02 18:25:58 -0700424 for (FlowEntry flowEntry : services.flow().getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700425 entries.add(flowEntry);
426 }
427
428 // get egress links from device, and include edge links
Simon Hunt1911fe42017-05-02 18:25:58 -0700429 Set<Link> links = new HashSet<>(services.link()
430 .getDeviceEgressLinks(deviceId));
431 Set<Host> hosts = services.host().getConnectedHosts(deviceId);
Simon Hunta17fa672015-08-19 18:42:22 -0700432 if (hosts != null) {
433 for (Host host : hosts) {
434 links.add(createEdgeLink(host, false));
435 }
436 }
437
438 // compile flow counts per link
439 Map<Link, Integer> counts = new HashMap<>();
440 for (Link link : links) {
441 counts.put(link, getEgressFlows(link, entries));
442 }
443 return counts;
444 }
445
446 // Counts all entries that egress on the link source port.
447 private int getEgressFlows(Link link, List<FlowEntry> entries) {
448 int count = 0;
449 PortNumber out = link.src().port();
450 for (FlowEntry entry : entries) {
451 TrafficTreatment treatment = entry.treatment();
452 for (Instruction instruction : treatment.allInstructions()) {
453 if (instruction.type() == Instruction.Type.OUTPUT &&
454 ((OutputInstruction) instruction).port().equals(out)) {
455 count++;
456 }
457 }
458 }
459 return count;
460 }
461
Simon Hunt57830172015-08-26 13:25:17 -0700462 private void highlightIntentLinks(Highlights highlights,
463 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700464 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700465 // NOTE: highlight secondary first, then primary, so that links shared
466 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700467 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
468 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700469 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700470 }
471
Simon Hunt57830172015-08-26 13:25:17 -0700472 private void highlightIntentLinksWithTraffic(Highlights highlights,
473 Set<Intent> primary) {
474 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700475 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700476 colorLinks(highlights, linkMap);
477 }
478
Simon Hunt94f7dae2015-08-26 17:40:59 -0700479 private void createTrafficLinks(Highlights highlights,
480 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700481 Flavor flavor, boolean showTraffic) {
482 for (Intent intent : intents) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700483 List<Intent> installables = services.intent()
Simon Hunta17fa672015-08-19 18:42:22 -0700484 .getInstallableIntents(intent.key());
485 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700486 if (installables != null) {
487 for (Intent installable : installables) {
488
489 if (installable instanceof PathIntent) {
490 links = ((PathIntent) installable).path().links();
491 } else if (installable instanceof FlowRuleIntent) {
Marc De Leenheer20913c62017-04-12 14:45:15 -0700492 Collection<Link> l = new ArrayList<>();
493 l.addAll(linkResources(installable));
494 // Add cross connect links
495 if (intent instanceof OpticalConnectivityIntent) {
496 OpticalConnectivityIntent ocIntent = (OpticalConnectivityIntent) intent;
Simon Hunt1911fe42017-05-02 18:25:58 -0700497 LinkService linkService = services.link();
498 DeviceService deviceService = services.device();
Marc De Leenheer20913c62017-04-12 14:45:15 -0700499 l.addAll(linkService.getDeviceIngressLinks(ocIntent.getSrc().deviceId()).stream()
500 .filter(i ->
501 deviceService.getDevice(i.src().deviceId()).type() == Device.Type.SWITCH)
502 .collect(Collectors.toList()));
503 l.addAll(linkService.getDeviceEgressLinks(ocIntent.getDst().deviceId()).stream()
504 .filter(e ->
505 deviceService.getDevice(e.dst().deviceId()).type() == Device.Type.SWITCH)
506 .collect(Collectors.toList()));
507 }
508 links = l;
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800509 } else if (installable instanceof FlowObjectiveIntent) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700510 links = linkResources(installable);
Simon Hunta17fa672015-08-19 18:42:22 -0700511 } else if (installable instanceof LinkCollectionIntent) {
512 links = ((LinkCollectionIntent) installable).links();
513 } else if (installable instanceof OpticalPathIntent) {
514 links = ((OpticalPathIntent) installable).path().links();
515 }
516
Simon Hunt1911fe42017-05-02 18:25:58 -0700517 if (links == null) {
518 links = Lists.newArrayList();
519 }
520
521 links = addEdgeLinksIfNeeded(intent, Lists.newArrayList(links));
522
Simon Hunt57830172015-08-26 13:25:17 -0700523 boolean isOptical = intent instanceof OpticalConnectivityIntent;
524 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700525 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700526 }
527 }
528 }
529 }
530
Simon Hunt5328f792017-01-11 17:43:31 -0800531 private Iterable<Link> addEdgeLinksIfNeeded(Intent parentIntent,
532 Collection<Link> links) {
533 if (parentIntent instanceof HostToHostIntent) {
534 links = new HashSet<>(links);
535 HostToHostIntent h2h = (HostToHostIntent) parentIntent;
Simon Hunt1911fe42017-05-02 18:25:58 -0700536 Host h1 = services.host().getHost(h2h.one());
537 Host h2 = services.host().getHost(h2h.two());
Simon Hunt5328f792017-01-11 17:43:31 -0800538 links.add(createEdgeLink(h1, true));
539 links.add(createEdgeLink(h2, true));
540 }
541 return links;
542 }
543
Simon Hunt94f7dae2015-08-26 17:40:59 -0700544 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
545 for (Link link : links) {
546 ensureNodePresent(highlights, link.src().elementId());
547 ensureNodePresent(highlights, link.dst().elementId());
548 }
549 }
550
551 private void ensureNodePresent(Highlights highlights, ElementId eid) {
552 String id = eid.toString();
553 NodeHighlight nh = highlights.getNode(id);
554 if (nh == null) {
555 if (eid instanceof DeviceId) {
556 nh = new DeviceHighlight(id);
557 highlights.add((DeviceHighlight) nh);
558 } else if (eid instanceof HostId) {
559 nh = new HostHighlight(id);
560 highlights.add((HostHighlight) nh);
561 }
562 }
563 }
564
Simon Hunta17fa672015-08-19 18:42:22 -0700565 // Extracts links from the specified flow rule intent resources
566 private Collection<Link> linkResources(Intent installable) {
567 ImmutableList.Builder<Link> builder = ImmutableList.builder();
568 installable.resources().stream().filter(r -> r instanceof Link)
569 .forEach(r -> builder.add((Link) r));
570 return builder.build();
571 }
572
Simon Hunt57830172015-08-26 13:25:17 -0700573 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
574 Flavor flavor, boolean isOptical,
575 boolean showTraffic) {
576 if (links != null) {
577 for (Link link : links) {
578 TrafficLink tlink = linkMap.add(link);
579 tlink.tagFlavor(flavor);
580 tlink.optical(isOptical);
581 if (showTraffic) {
582 tlink.addLoad(getLinkFlowLoad(link));
583 tlink.antMarch(true);
584 }
585 }
586 }
587 }
588
589 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
590 for (TrafficLink tlink : linkMap.biLinks()) {
591 highlights.add(tlink.highlight(StatsType.TAGGED));
592 }
593 }
594
Simon Hunta17fa672015-08-19 18:42:22 -0700595}