blob: 1e2ae833fe0e8d900e5e4c1d94bbf0464bf4c2e5 [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
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 Hunted804d52016-03-30 09:51:40 -070043import org.onosproject.ui.impl.topo.util.IntentSelection;
44import org.onosproject.ui.impl.topo.util.ServicesBundle;
45import org.onosproject.ui.impl.topo.util.TopoIntentFilter;
46import org.onosproject.ui.impl.topo.util.TrafficLink;
47import org.onosproject.ui.impl.topo.util.TrafficLink.StatsType;
48import org.onosproject.ui.impl.topo.util.TrafficLinkMap;
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;
Simon Hunta17fa672015-08-19 18:42:22 -070056import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
59import java.util.ArrayList;
60import java.util.Collection;
61import java.util.Collections;
62import java.util.HashMap;
63import java.util.HashSet;
64import java.util.List;
65import java.util.Map;
66import java.util.Set;
Marc De Leenheer20913c62017-04-12 14:45:15 -070067import java.util.stream.Collectors;
Simon Hunta17fa672015-08-19 18:42:22 -070068
69import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunt1911fe42017-05-02 18:25:58 -070070import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.RELATED_INTENTS;
71import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070072
73/**
74 * Encapsulates the behavior of monitoring specific traffic patterns.
75 */
Simon Hunt1911fe42017-05-02 18:25:58 -070076public class TrafficMonitor extends TrafficMonitorBase {
Simon Hunta17fa672015-08-19 18:42:22 -070077
78 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070079 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070080
Simon Hunt4fc86852015-08-20 17:57:52 -070081 private final TopologyViewMessageHandler msgHandler;
82 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -070083
Simon Hunta17fa672015-08-19 18:42:22 -070084 private NodeSelection selectedNodes = null;
85 private IntentSelection selectedIntents = null;
86
87
88 /**
89 * Constructs a traffic monitor.
90 *
Simon Hunt5328f792017-01-11 17:43:31 -080091 * @param trafficPeriod traffic task period in ms
92 * @param servicesBundle bundle of services
93 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -070094 */
Simon Hunt4fc86852015-08-20 17:57:52 -070095 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
96 TopologyViewMessageHandler msgHandler) {
Simon Hunt1911fe42017-05-02 18:25:58 -070097 super(trafficPeriod, servicesBundle);
Simon Hunt4fc86852015-08-20 17:57:52 -070098 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -070099
Simon Hunt4fc86852015-08-20 17:57:52 -0700100 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700101 }
102
103 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700104 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700105
Simon Hunta7aea842017-05-03 19:42:50 -0700106 // monitor(Mode) is now implemented in the super class
Simon Hunta17fa672015-08-19 18:42:22 -0700107
Simon Hunt4fc86852015-08-20 17:57:52 -0700108 /**
109 * Monitor for traffic data to be sent back to the web client, under
110 * the given mode, using the given selection of devices and hosts.
111 * In the case of "device link flows", this causes a background traffic
112 * task to be scheduled to repeatedly compute and transmit the appropriate
113 * traffic data to the client. In the case of "related intents", no
114 * repeating task is scheduled.
115 * <p>
116 * The monitoring mode is expected to be one of:
117 * <ul>
Simon Hunt5328f792017-01-11 17:43:31 -0800118 * <li>DEV_LINK_FLOWS</li>
119 * <li>RELATED_INTENTS</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700120 * </ul>
121 *
Simon Hunt5328f792017-01-11 17:43:31 -0800122 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700123 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700124 */
Simon Hunta17fa672015-08-19 18:42:22 -0700125 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
126 log.debug("monitor: {} -- {}", mode, nodeSelection);
127 this.mode = mode;
128 this.selectedNodes = nodeSelection;
129
130 switch (mode) {
131 case DEV_LINK_FLOWS:
132 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700133 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700134 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700135 } else {
136 scheduleTask();
137 sendDeviceLinkFlows();
138 }
139 break;
140
141 case RELATED_INTENTS:
142 if (selectedNodes.none()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700143 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700144 } else {
145 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
146 if (selectedIntents.none()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700147 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700148 } else {
149 sendSelectedIntents();
150 }
151 }
152 break;
153
154 default:
155 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
156 clearAll();
157 break;
158 }
159 }
160
Simon Hunt4fc86852015-08-20 17:57:52 -0700161 // TODO: move this out to the "h2h/multi-intent app"
Simon Hunt5328f792017-01-11 17:43:31 -0800162
Simon Hunt4fc86852015-08-20 17:57:52 -0700163 /**
164 * Monitor for traffic data to be sent back to the web client, for the
165 * given intent.
166 *
167 * @param intent the intent to monitor
168 */
Simon Hunta17fa672015-08-19 18:42:22 -0700169 public synchronized void monitor(Intent intent) {
170 log.debug("monitor intent: {}", intent.id());
171 selectedNodes = null;
172 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700173 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700174 scheduleTask();
175 sendSelectedIntentTraffic();
176 }
177
Simon Hunt4fc86852015-08-20 17:57:52 -0700178 /**
179 * Selects the next intent in the select group (if there is one),
180 * and sends highlighting data back to the web client to display
181 * which path is selected.
182 */
Simon Hunta17fa672015-08-19 18:42:22 -0700183 public synchronized void selectNextIntent() {
184 if (selectedIntents != null) {
185 selectedIntents.next();
186 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700187 if (mode == SELECTED_INTENT) {
188 mode = RELATED_INTENTS;
189 }
Simon Hunta17fa672015-08-19 18:42:22 -0700190 }
191 }
192
Simon Hunt4fc86852015-08-20 17:57:52 -0700193 /**
194 * Selects the previous intent in the select group (if there is one),
195 * and sends highlighting data back to the web client to display
196 * which path is selected.
197 */
Simon Hunta17fa672015-08-19 18:42:22 -0700198 public synchronized void selectPreviousIntent() {
199 if (selectedIntents != null) {
200 selectedIntents.prev();
201 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700202 if (mode == SELECTED_INTENT) {
203 mode = RELATED_INTENTS;
204 }
Simon Hunta17fa672015-08-19 18:42:22 -0700205 }
206 }
207
Simon Hunt4fc86852015-08-20 17:57:52 -0700208 /**
209 * Resends selected intent traffic data. This is called, for example,
210 * when the system detects an intent update happened.
211 */
Simon Hunta17fa672015-08-19 18:42:22 -0700212 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700213 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700214 sendSelectedIntentTraffic();
215 }
216 }
217
Simon Hunta17fa672015-08-19 18:42:22 -0700218 // =======================================================================
Simon Hunt1911fe42017-05-02 18:25:58 -0700219 // === Abstract method implementations ===
Simon Hunta17fa672015-08-19 18:42:22 -0700220
Simon Hunt1911fe42017-05-02 18:25:58 -0700221 @Override
222 protected void sendAllFlowTraffic() {
Simon Hunta17fa672015-08-19 18:42:22 -0700223 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700224 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700225 }
226
Simon Hunt1911fe42017-05-02 18:25:58 -0700227 @Override
228 protected void sendAllPortTrafficBits() {
229 log.debug("sendAllPortTrafficBits");
230 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700231 }
232
Simon Hunt1911fe42017-05-02 18:25:58 -0700233 @Override
234 protected void sendAllPortTrafficPackets() {
235 log.debug("sendAllPortTrafficPackets");
236 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_PACKET_STATS));
237 }
238
239 @Override
240 protected void sendDeviceLinkFlows() {
Simon Hunta17fa672015-08-19 18:42:22 -0700241 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700242 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700243 }
244
Simon Hunt1911fe42017-05-02 18:25:58 -0700245 @Override
246 protected void sendSelectedIntentTraffic() {
Simon Hunta17fa672015-08-19 18:42:22 -0700247 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700248 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700249 }
250
Simon Hunt1911fe42017-05-02 18:25:58 -0700251 @Override
252 protected void sendClearHighlights() {
Simon Hunta17fa672015-08-19 18:42:22 -0700253 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700254 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700255 }
256
Simon Hunt1911fe42017-05-02 18:25:58 -0700257 @Override
258 protected void clearSelection() {
259 selectedNodes = null;
260 selectedIntents = null;
261 }
262
263
264 private void sendSelectedIntents() {
265 log.debug("sendSelectedIntents: {}", selectedIntents);
266 msgHandler.sendHighlights(intentGroup());
267 }
268
Simon Hunta17fa672015-08-19 18:42:22 -0700269 // =======================================================================
270 // === Generate messages in JSON object node format
271
Simon Hunta7aea842017-05-03 19:42:50 -0700272 // NOTE: trafficSummary(StatsType) => Highlights
273 // has been moved to the superclass
Simon Hunta17fa672015-08-19 18:42:22 -0700274
275 // create highlights for links, showing flows for selected devices.
276 private Highlights deviceLinkFlows() {
277 Highlights highlights = new Highlights();
278
Simon Hunt72297212015-08-25 10:15:33 -0700279 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700280 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700281 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700282
Simon Hunt72297212015-08-25 10:15:33 -0700283 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700284 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
285 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700286 TrafficLink tlink = linkMap.add(link);
287 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700288 }
289 }
290
291 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700292 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700293 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700294 }
295
296 }
297 return highlights;
298 }
299
300 private Highlights intentGroup() {
301 Highlights highlights = new Highlights();
302
303 if (selectedIntents != null && !selectedIntents.none()) {
304 // If 'all' intents are selected, they will all have primary
305 // highlighting; otherwise, the specifically selected intent will
306 // have primary highlighting, and the remainder will have secondary
307 // highlighting.
308 Set<Intent> primary;
309 Set<Intent> secondary;
310 int count = selectedIntents.size();
311
312 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
313 Intent current;
314
315 if (selectedIntents.all()) {
316 primary = allBut;
317 secondary = Collections.emptySet();
318 log.debug("Highlight all intents ({})", count);
319 } else {
320 current = selectedIntents.current();
321 primary = new HashSet<>();
322 primary.add(current);
323 allBut.remove(current);
324 secondary = allBut;
325 log.debug("Highlight intent: {} ([{}] of {})",
Simon Hunt1911fe42017-05-02 18:25:58 -0700326 current.id(), selectedIntents.index(), count);
Simon Hunta17fa672015-08-19 18:42:22 -0700327 }
Simon Hunt57830172015-08-26 13:25:17 -0700328
329 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700330 }
331 return highlights;
332 }
333
334 private Highlights intentTraffic() {
335 Highlights highlights = new Highlights();
336
337 if (selectedIntents != null && selectedIntents.single()) {
338 Intent current = selectedIntents.current();
339 Set<Intent> primary = new HashSet<>();
340 primary.add(current);
341 log.debug("Highlight traffic for intent: {} ([{}] of {})",
Simon Hunt1911fe42017-05-02 18:25:58 -0700342 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700343
344 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700345 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700346 }
347 return highlights;
348 }
349
Simon Hunta17fa672015-08-19 18:42:22 -0700350 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
351
Simon Hunta17fa672015-08-19 18:42:22 -0700352 // Counts all flow entries that egress on the links of the given device.
353 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
354 // get the flows for the device
355 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt1911fe42017-05-02 18:25:58 -0700356 for (FlowEntry flowEntry : services.flow().getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700357 entries.add(flowEntry);
358 }
359
360 // get egress links from device, and include edge links
Simon Hunt1911fe42017-05-02 18:25:58 -0700361 Set<Link> links = new HashSet<>(services.link()
362 .getDeviceEgressLinks(deviceId));
363 Set<Host> hosts = services.host().getConnectedHosts(deviceId);
Simon Hunta17fa672015-08-19 18:42:22 -0700364 if (hosts != null) {
365 for (Host host : hosts) {
366 links.add(createEdgeLink(host, false));
367 }
368 }
369
370 // compile flow counts per link
371 Map<Link, Integer> counts = new HashMap<>();
372 for (Link link : links) {
373 counts.put(link, getEgressFlows(link, entries));
374 }
375 return counts;
376 }
377
378 // Counts all entries that egress on the link source port.
379 private int getEgressFlows(Link link, List<FlowEntry> entries) {
380 int count = 0;
381 PortNumber out = link.src().port();
382 for (FlowEntry entry : entries) {
383 TrafficTreatment treatment = entry.treatment();
384 for (Instruction instruction : treatment.allInstructions()) {
385 if (instruction.type() == Instruction.Type.OUTPUT &&
386 ((OutputInstruction) instruction).port().equals(out)) {
387 count++;
388 }
389 }
390 }
391 return count;
392 }
393
Simon Hunt57830172015-08-26 13:25:17 -0700394 private void highlightIntentLinks(Highlights highlights,
395 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700396 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700397 // NOTE: highlight secondary first, then primary, so that links shared
398 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700399 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
400 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700401 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700402 }
403
Simon Hunt57830172015-08-26 13:25:17 -0700404 private void highlightIntentLinksWithTraffic(Highlights highlights,
405 Set<Intent> primary) {
406 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700407 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700408 colorLinks(highlights, linkMap);
409 }
410
Simon Hunt94f7dae2015-08-26 17:40:59 -0700411 private void createTrafficLinks(Highlights highlights,
412 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700413 Flavor flavor, boolean showTraffic) {
414 for (Intent intent : intents) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700415 List<Intent> installables = services.intent()
Simon Hunta17fa672015-08-19 18:42:22 -0700416 .getInstallableIntents(intent.key());
417 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700418 if (installables != null) {
419 for (Intent installable : installables) {
420
421 if (installable instanceof PathIntent) {
422 links = ((PathIntent) installable).path().links();
423 } else if (installable instanceof FlowRuleIntent) {
Marc De Leenheer20913c62017-04-12 14:45:15 -0700424 Collection<Link> l = new ArrayList<>();
425 l.addAll(linkResources(installable));
426 // Add cross connect links
427 if (intent instanceof OpticalConnectivityIntent) {
428 OpticalConnectivityIntent ocIntent = (OpticalConnectivityIntent) intent;
Simon Hunt1911fe42017-05-02 18:25:58 -0700429 LinkService linkService = services.link();
430 DeviceService deviceService = services.device();
Marc De Leenheer20913c62017-04-12 14:45:15 -0700431 l.addAll(linkService.getDeviceIngressLinks(ocIntent.getSrc().deviceId()).stream()
432 .filter(i ->
433 deviceService.getDevice(i.src().deviceId()).type() == Device.Type.SWITCH)
434 .collect(Collectors.toList()));
435 l.addAll(linkService.getDeviceEgressLinks(ocIntent.getDst().deviceId()).stream()
436 .filter(e ->
437 deviceService.getDevice(e.dst().deviceId()).type() == Device.Type.SWITCH)
438 .collect(Collectors.toList()));
439 }
440 links = l;
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800441 } else if (installable instanceof FlowObjectiveIntent) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700442 links = linkResources(installable);
Simon Hunta17fa672015-08-19 18:42:22 -0700443 } else if (installable instanceof LinkCollectionIntent) {
444 links = ((LinkCollectionIntent) installable).links();
445 } else if (installable instanceof OpticalPathIntent) {
446 links = ((OpticalPathIntent) installable).path().links();
447 }
448
Simon Hunt1911fe42017-05-02 18:25:58 -0700449 if (links == null) {
450 links = Lists.newArrayList();
451 }
452
453 links = addEdgeLinksIfNeeded(intent, Lists.newArrayList(links));
454
Simon Hunt57830172015-08-26 13:25:17 -0700455 boolean isOptical = intent instanceof OpticalConnectivityIntent;
456 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700457 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700458 }
459 }
460 }
461 }
462
Simon Hunt5328f792017-01-11 17:43:31 -0800463 private Iterable<Link> addEdgeLinksIfNeeded(Intent parentIntent,
464 Collection<Link> links) {
465 if (parentIntent instanceof HostToHostIntent) {
466 links = new HashSet<>(links);
467 HostToHostIntent h2h = (HostToHostIntent) parentIntent;
Simon Hunt1911fe42017-05-02 18:25:58 -0700468 Host h1 = services.host().getHost(h2h.one());
469 Host h2 = services.host().getHost(h2h.two());
Simon Hunt5328f792017-01-11 17:43:31 -0800470 links.add(createEdgeLink(h1, true));
471 links.add(createEdgeLink(h2, true));
472 }
473 return links;
474 }
475
Simon Hunt94f7dae2015-08-26 17:40:59 -0700476 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
477 for (Link link : links) {
478 ensureNodePresent(highlights, link.src().elementId());
479 ensureNodePresent(highlights, link.dst().elementId());
480 }
481 }
482
483 private void ensureNodePresent(Highlights highlights, ElementId eid) {
484 String id = eid.toString();
485 NodeHighlight nh = highlights.getNode(id);
486 if (nh == null) {
487 if (eid instanceof DeviceId) {
488 nh = new DeviceHighlight(id);
489 highlights.add((DeviceHighlight) nh);
490 } else if (eid instanceof HostId) {
491 nh = new HostHighlight(id);
492 highlights.add((HostHighlight) nh);
493 }
494 }
495 }
496
Simon Hunta17fa672015-08-19 18:42:22 -0700497 // Extracts links from the specified flow rule intent resources
498 private Collection<Link> linkResources(Intent installable) {
499 ImmutableList.Builder<Link> builder = ImmutableList.builder();
500 installable.resources().stream().filter(r -> r instanceof Link)
501 .forEach(r -> builder.add((Link) r));
502 return builder.build();
503 }
504
Simon Hunt57830172015-08-26 13:25:17 -0700505 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
506 Flavor flavor, boolean isOptical,
507 boolean showTraffic) {
508 if (links != null) {
509 for (Link link : links) {
510 TrafficLink tlink = linkMap.add(link);
511 tlink.tagFlavor(flavor);
512 tlink.optical(isOptical);
513 if (showTraffic) {
514 tlink.addLoad(getLinkFlowLoad(link));
515 tlink.antMarch(true);
516 }
517 }
518 }
519 }
520
521 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
522 for (TrafficLink tlink : linkMap.biLinks()) {
523 highlights.add(tlink.highlight(StatsType.TAGGED));
524 }
525 }
526
Simon Hunta17fa672015-08-19 18:42:22 -0700527}