blob: f45eb8dc12fe412aff37794ce74af410ac5f1587 [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
Pier59266bc2018-03-15 12:10:24 -070069import static org.onosproject.net.DefaultEdgeLink.createEdgeLinks;
Simon Hunta17fa672015-08-19 18:42:22 -070070import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Simon Hunt1911fe42017-05-02 18:25:58 -070071import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.RELATED_INTENTS;
72import static org.onosproject.ui.impl.TrafficMonitorBase.Mode.SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -070073
74/**
75 * Encapsulates the behavior of monitoring specific traffic patterns.
76 */
Simon Hunt1911fe42017-05-02 18:25:58 -070077public class TrafficMonitor extends TrafficMonitorBase {
Simon Hunta17fa672015-08-19 18:42:22 -070078
79 private static final Logger log =
Simon Hunt4fc86852015-08-20 17:57:52 -070080 LoggerFactory.getLogger(TrafficMonitor.class);
Simon Hunta17fa672015-08-19 18:42:22 -070081
Simon Hunt4fc86852015-08-20 17:57:52 -070082 private final TopologyViewMessageHandler msgHandler;
83 private final TopoIntentFilter intentFilter;
Simon Hunta17fa672015-08-19 18:42:22 -070084
Simon Hunta17fa672015-08-19 18:42:22 -070085 private NodeSelection selectedNodes = null;
86 private IntentSelection selectedIntents = null;
87
88
89 /**
90 * Constructs a traffic monitor.
91 *
Simon Hunt5328f792017-01-11 17:43:31 -080092 * @param trafficPeriod traffic task period in ms
93 * @param servicesBundle bundle of services
94 * @param msgHandler our message handler
Simon Hunta17fa672015-08-19 18:42:22 -070095 */
Simon Hunt4fc86852015-08-20 17:57:52 -070096 public TrafficMonitor(long trafficPeriod, ServicesBundle servicesBundle,
97 TopologyViewMessageHandler msgHandler) {
Simon Hunt1911fe42017-05-02 18:25:58 -070098 super(trafficPeriod, servicesBundle);
Simon Hunt4fc86852015-08-20 17:57:52 -070099 this.msgHandler = msgHandler;
Simon Hunta17fa672015-08-19 18:42:22 -0700100
Simon Hunt4fc86852015-08-20 17:57:52 -0700101 intentFilter = new TopoIntentFilter(servicesBundle);
Simon Hunta17fa672015-08-19 18:42:22 -0700102 }
103
104 // =======================================================================
Simon Hunt4fc86852015-08-20 17:57:52 -0700105 // === API ===
Simon Hunta17fa672015-08-19 18:42:22 -0700106
Simon Hunta7aea842017-05-03 19:42:50 -0700107 // monitor(Mode) is now implemented in the super class
Simon Hunta17fa672015-08-19 18:42:22 -0700108
Simon Hunt4fc86852015-08-20 17:57:52 -0700109 /**
110 * Monitor for traffic data to be sent back to the web client, under
111 * the given mode, using the given selection of devices and hosts.
112 * In the case of "device link flows", this causes a background traffic
113 * task to be scheduled to repeatedly compute and transmit the appropriate
114 * traffic data to the client. In the case of "related intents", no
115 * repeating task is scheduled.
116 * <p>
117 * The monitoring mode is expected to be one of:
118 * <ul>
Simon Hunt5328f792017-01-11 17:43:31 -0800119 * <li>DEV_LINK_FLOWS</li>
120 * <li>RELATED_INTENTS</li>
Simon Hunt4fc86852015-08-20 17:57:52 -0700121 * </ul>
122 *
Simon Hunt5328f792017-01-11 17:43:31 -0800123 * @param mode monitoring mode
Ray Milkey9b36d812015-09-09 15:24:54 -0700124 * @param nodeSelection how to select a node
Simon Hunt4fc86852015-08-20 17:57:52 -0700125 */
Simon Hunta17fa672015-08-19 18:42:22 -0700126 public synchronized void monitor(Mode mode, NodeSelection nodeSelection) {
127 log.debug("monitor: {} -- {}", mode, nodeSelection);
128 this.mode = mode;
129 this.selectedNodes = nodeSelection;
130
131 switch (mode) {
132 case DEV_LINK_FLOWS:
133 // only care about devices (not hosts)
Simon Hunt72297212015-08-25 10:15:33 -0700134 if (selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700135 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700136 } else {
137 scheduleTask();
138 sendDeviceLinkFlows();
139 }
140 break;
141
142 case RELATED_INTENTS:
143 if (selectedNodes.none()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700144 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700145 } else {
146 selectedIntents = new IntentSelection(selectedNodes, intentFilter);
147 if (selectedIntents.none()) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700148 clearAll();
Simon Hunta17fa672015-08-19 18:42:22 -0700149 } else {
150 sendSelectedIntents();
151 }
152 }
153 break;
154
155 default:
156 log.debug("Unexpected call to monitor({}, {})", mode, nodeSelection);
157 clearAll();
158 break;
159 }
160 }
161
Simon Hunt4fc86852015-08-20 17:57:52 -0700162 // TODO: move this out to the "h2h/multi-intent app"
Simon Hunt5328f792017-01-11 17:43:31 -0800163
Simon Hunt4fc86852015-08-20 17:57:52 -0700164 /**
165 * Monitor for traffic data to be sent back to the web client, for the
166 * given intent.
167 *
168 * @param intent the intent to monitor
169 */
Simon Hunta17fa672015-08-19 18:42:22 -0700170 public synchronized void monitor(Intent intent) {
171 log.debug("monitor intent: {}", intent.id());
172 selectedNodes = null;
173 selectedIntents = new IntentSelection(intent);
Simon Hunt4fc86852015-08-20 17:57:52 -0700174 mode = SELECTED_INTENT;
Simon Hunta17fa672015-08-19 18:42:22 -0700175 scheduleTask();
176 sendSelectedIntentTraffic();
177 }
178
Simon Hunt4fc86852015-08-20 17:57:52 -0700179 /**
180 * Selects the next intent in the select group (if there is one),
181 * and sends highlighting data back to the web client to display
182 * which path is selected.
183 */
Simon Hunta17fa672015-08-19 18:42:22 -0700184 public synchronized void selectNextIntent() {
185 if (selectedIntents != null) {
186 selectedIntents.next();
187 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700188 if (mode == SELECTED_INTENT) {
189 mode = RELATED_INTENTS;
190 }
Simon Hunta17fa672015-08-19 18:42:22 -0700191 }
192 }
193
Simon Hunt4fc86852015-08-20 17:57:52 -0700194 /**
195 * Selects the previous intent in the select group (if there is one),
196 * and sends highlighting data back to the web client to display
197 * which path is selected.
198 */
Simon Hunta17fa672015-08-19 18:42:22 -0700199 public synchronized void selectPreviousIntent() {
200 if (selectedIntents != null) {
201 selectedIntents.prev();
202 sendSelectedIntents();
Simon Hunt57830172015-08-26 13:25:17 -0700203 if (mode == SELECTED_INTENT) {
204 mode = RELATED_INTENTS;
205 }
Simon Hunta17fa672015-08-19 18:42:22 -0700206 }
207 }
208
Simon Hunt4fc86852015-08-20 17:57:52 -0700209 /**
210 * Resends selected intent traffic data. This is called, for example,
211 * when the system detects an intent update happened.
212 */
Simon Hunta17fa672015-08-19 18:42:22 -0700213 public synchronized void pokeIntent() {
Simon Hunt4fc86852015-08-20 17:57:52 -0700214 if (mode == SELECTED_INTENT) {
Simon Hunta17fa672015-08-19 18:42:22 -0700215 sendSelectedIntentTraffic();
216 }
217 }
218
Simon Hunta17fa672015-08-19 18:42:22 -0700219 // =======================================================================
Simon Hunt1911fe42017-05-02 18:25:58 -0700220 // === Abstract method implementations ===
Simon Hunta17fa672015-08-19 18:42:22 -0700221
Simon Hunt1911fe42017-05-02 18:25:58 -0700222 @Override
223 protected void sendAllFlowTraffic() {
Simon Hunta17fa672015-08-19 18:42:22 -0700224 log.debug("sendAllFlowTraffic");
Simon Hunt57830172015-08-26 13:25:17 -0700225 msgHandler.sendHighlights(trafficSummary(StatsType.FLOW_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700226 }
227
Simon Hunt1911fe42017-05-02 18:25:58 -0700228 @Override
229 protected void sendAllPortTrafficBits() {
230 log.debug("sendAllPortTrafficBits");
231 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_STATS));
Simon Hunta17fa672015-08-19 18:42:22 -0700232 }
233
Simon Hunt1911fe42017-05-02 18:25:58 -0700234 @Override
235 protected void sendAllPortTrafficPackets() {
236 log.debug("sendAllPortTrafficPackets");
237 msgHandler.sendHighlights(trafficSummary(StatsType.PORT_PACKET_STATS));
238 }
239
240 @Override
241 protected void sendDeviceLinkFlows() {
Simon Hunta17fa672015-08-19 18:42:22 -0700242 log.debug("sendDeviceLinkFlows: {}", selectedNodes);
Simon Hunt4fc86852015-08-20 17:57:52 -0700243 msgHandler.sendHighlights(deviceLinkFlows());
Simon Hunta17fa672015-08-19 18:42:22 -0700244 }
245
Simon Hunt1911fe42017-05-02 18:25:58 -0700246 @Override
247 protected void sendSelectedIntentTraffic() {
Simon Hunta17fa672015-08-19 18:42:22 -0700248 log.debug("sendSelectedIntentTraffic: {}", selectedIntents);
Simon Hunt4fc86852015-08-20 17:57:52 -0700249 msgHandler.sendHighlights(intentTraffic());
Simon Hunta17fa672015-08-19 18:42:22 -0700250 }
251
Simon Hunt1911fe42017-05-02 18:25:58 -0700252 @Override
253 protected void sendClearHighlights() {
Simon Hunta17fa672015-08-19 18:42:22 -0700254 log.debug("sendClearHighlights");
Simon Hunt4fc86852015-08-20 17:57:52 -0700255 msgHandler.sendHighlights(new Highlights());
Simon Hunta17fa672015-08-19 18:42:22 -0700256 }
257
Simon Hunt1911fe42017-05-02 18:25:58 -0700258 @Override
259 protected void clearSelection() {
260 selectedNodes = null;
261 selectedIntents = null;
262 }
263
264
265 private void sendSelectedIntents() {
266 log.debug("sendSelectedIntents: {}", selectedIntents);
267 msgHandler.sendHighlights(intentGroup());
268 }
269
Simon Hunta17fa672015-08-19 18:42:22 -0700270 // =======================================================================
271 // === Generate messages in JSON object node format
272
Simon Hunta7aea842017-05-03 19:42:50 -0700273 // NOTE: trafficSummary(StatsType) => Highlights
274 // has been moved to the superclass
Simon Hunta17fa672015-08-19 18:42:22 -0700275
276 // create highlights for links, showing flows for selected devices.
277 private Highlights deviceLinkFlows() {
278 Highlights highlights = new Highlights();
279
Simon Hunt72297212015-08-25 10:15:33 -0700280 if (selectedNodes != null && !selectedNodes.devicesWithHover().isEmpty()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700281 // capture flow counts on bilinks
Simon Hunt4fc86852015-08-20 17:57:52 -0700282 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunta17fa672015-08-19 18:42:22 -0700283
Simon Hunt72297212015-08-25 10:15:33 -0700284 for (Device device : selectedNodes.devicesWithHover()) {
Simon Hunta17fa672015-08-19 18:42:22 -0700285 Map<Link, Integer> counts = getLinkFlowCounts(device.id());
286 for (Link link : counts.keySet()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700287 TrafficLink tlink = linkMap.add(link);
288 tlink.addFlows(counts.get(link));
Simon Hunta17fa672015-08-19 18:42:22 -0700289 }
290 }
291
292 // now report on our collated links
Simon Hunt4fc86852015-08-20 17:57:52 -0700293 for (TrafficLink tlink : linkMap.biLinks()) {
Simon Hunt57830172015-08-26 13:25:17 -0700294 highlights.add(tlink.highlight(StatsType.FLOW_COUNT));
Simon Hunta17fa672015-08-19 18:42:22 -0700295 }
296
297 }
298 return highlights;
299 }
300
301 private Highlights intentGroup() {
302 Highlights highlights = new Highlights();
303
304 if (selectedIntents != null && !selectedIntents.none()) {
305 // If 'all' intents are selected, they will all have primary
306 // highlighting; otherwise, the specifically selected intent will
307 // have primary highlighting, and the remainder will have secondary
308 // highlighting.
309 Set<Intent> primary;
310 Set<Intent> secondary;
311 int count = selectedIntents.size();
312
313 Set<Intent> allBut = new HashSet<>(selectedIntents.intents());
314 Intent current;
315
316 if (selectedIntents.all()) {
317 primary = allBut;
318 secondary = Collections.emptySet();
319 log.debug("Highlight all intents ({})", count);
320 } else {
321 current = selectedIntents.current();
322 primary = new HashSet<>();
323 primary.add(current);
324 allBut.remove(current);
325 secondary = allBut;
326 log.debug("Highlight intent: {} ([{}] of {})",
Simon Hunt1911fe42017-05-02 18:25:58 -0700327 current.id(), selectedIntents.index(), count);
Simon Hunta17fa672015-08-19 18:42:22 -0700328 }
Simon Hunt57830172015-08-26 13:25:17 -0700329
330 highlightIntentLinks(highlights, primary, secondary);
Simon Hunta17fa672015-08-19 18:42:22 -0700331 }
332 return highlights;
333 }
334
335 private Highlights intentTraffic() {
336 Highlights highlights = new Highlights();
337
338 if (selectedIntents != null && selectedIntents.single()) {
339 Intent current = selectedIntents.current();
340 Set<Intent> primary = new HashSet<>();
341 primary.add(current);
342 log.debug("Highlight traffic for intent: {} ([{}] of {})",
Simon Hunt1911fe42017-05-02 18:25:58 -0700343 current.id(), selectedIntents.index(), selectedIntents.size());
Simon Hunt57830172015-08-26 13:25:17 -0700344
345 highlightIntentLinksWithTraffic(highlights, primary);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700346 highlights.subdueAllElse(Amount.MINIMALLY);
Simon Hunta17fa672015-08-19 18:42:22 -0700347 }
348 return highlights;
349 }
350
Simon Hunta17fa672015-08-19 18:42:22 -0700351 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
352
Simon Hunta17fa672015-08-19 18:42:22 -0700353 // Counts all flow entries that egress on the links of the given device.
354 private Map<Link, Integer> getLinkFlowCounts(DeviceId deviceId) {
355 // get the flows for the device
356 List<FlowEntry> entries = new ArrayList<>();
Simon Hunt1911fe42017-05-02 18:25:58 -0700357 for (FlowEntry flowEntry : services.flow().getFlowEntries(deviceId)) {
Simon Hunta17fa672015-08-19 18:42:22 -0700358 entries.add(flowEntry);
359 }
360
361 // get egress links from device, and include edge links
Simon Hunt1911fe42017-05-02 18:25:58 -0700362 Set<Link> links = new HashSet<>(services.link()
363 .getDeviceEgressLinks(deviceId));
364 Set<Host> hosts = services.host().getConnectedHosts(deviceId);
Simon Hunta17fa672015-08-19 18:42:22 -0700365 if (hosts != null) {
366 for (Host host : hosts) {
Pier59266bc2018-03-15 12:10:24 -0700367 links.addAll(createEdgeLinks(host, false));
Simon Hunta17fa672015-08-19 18:42:22 -0700368 }
369 }
370
371 // compile flow counts per link
372 Map<Link, Integer> counts = new HashMap<>();
373 for (Link link : links) {
374 counts.put(link, getEgressFlows(link, entries));
375 }
376 return counts;
377 }
378
379 // Counts all entries that egress on the link source port.
380 private int getEgressFlows(Link link, List<FlowEntry> entries) {
381 int count = 0;
382 PortNumber out = link.src().port();
383 for (FlowEntry entry : entries) {
384 TrafficTreatment treatment = entry.treatment();
385 for (Instruction instruction : treatment.allInstructions()) {
386 if (instruction.type() == Instruction.Type.OUTPUT &&
387 ((OutputInstruction) instruction).port().equals(out)) {
388 count++;
389 }
390 }
391 }
392 return count;
393 }
394
Simon Hunt57830172015-08-26 13:25:17 -0700395 private void highlightIntentLinks(Highlights highlights,
396 Set<Intent> primary, Set<Intent> secondary) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700397 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt57830172015-08-26 13:25:17 -0700398 // NOTE: highlight secondary first, then primary, so that links shared
399 // by intents are colored correctly ("last man wins")
Simon Hunt94f7dae2015-08-26 17:40:59 -0700400 createTrafficLinks(highlights, linkMap, secondary, Flavor.SECONDARY_HIGHLIGHT, false);
401 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, false);
Simon Hunt57830172015-08-26 13:25:17 -0700402 colorLinks(highlights, linkMap);
Simon Hunta17fa672015-08-19 18:42:22 -0700403 }
404
Simon Hunt57830172015-08-26 13:25:17 -0700405 private void highlightIntentLinksWithTraffic(Highlights highlights,
406 Set<Intent> primary) {
407 TrafficLinkMap linkMap = new TrafficLinkMap();
Simon Hunt94f7dae2015-08-26 17:40:59 -0700408 createTrafficLinks(highlights, linkMap, primary, Flavor.PRIMARY_HIGHLIGHT, true);
Simon Hunt57830172015-08-26 13:25:17 -0700409 colorLinks(highlights, linkMap);
410 }
411
Simon Hunt94f7dae2015-08-26 17:40:59 -0700412 private void createTrafficLinks(Highlights highlights,
413 TrafficLinkMap linkMap, Set<Intent> intents,
Simon Hunt57830172015-08-26 13:25:17 -0700414 Flavor flavor, boolean showTraffic) {
415 for (Intent intent : intents) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700416 List<Intent> installables = services.intent()
Simon Hunta17fa672015-08-19 18:42:22 -0700417 .getInstallableIntents(intent.key());
418 Iterable<Link> links = null;
Simon Hunta17fa672015-08-19 18:42:22 -0700419 if (installables != null) {
420 for (Intent installable : installables) {
421
422 if (installable instanceof PathIntent) {
423 links = ((PathIntent) installable).path().links();
424 } else if (installable instanceof FlowRuleIntent) {
Marc De Leenheer20913c62017-04-12 14:45:15 -0700425 Collection<Link> l = new ArrayList<>();
426 l.addAll(linkResources(installable));
427 // Add cross connect links
428 if (intent instanceof OpticalConnectivityIntent) {
429 OpticalConnectivityIntent ocIntent = (OpticalConnectivityIntent) intent;
Simon Hunt1911fe42017-05-02 18:25:58 -0700430 LinkService linkService = services.link();
431 DeviceService deviceService = services.device();
Marc De Leenheer20913c62017-04-12 14:45:15 -0700432 l.addAll(linkService.getDeviceIngressLinks(ocIntent.getSrc().deviceId()).stream()
433 .filter(i ->
434 deviceService.getDevice(i.src().deviceId()).type() == Device.Type.SWITCH)
435 .collect(Collectors.toList()));
436 l.addAll(linkService.getDeviceEgressLinks(ocIntent.getDst().deviceId()).stream()
437 .filter(e ->
438 deviceService.getDevice(e.dst().deviceId()).type() == Device.Type.SWITCH)
439 .collect(Collectors.toList()));
440 }
441 links = l;
Thomas Vachuskada0665b2016-03-02 19:06:17 -0800442 } else if (installable instanceof FlowObjectiveIntent) {
Simon Hunt1911fe42017-05-02 18:25:58 -0700443 links = linkResources(installable);
Simon Hunta17fa672015-08-19 18:42:22 -0700444 } else if (installable instanceof LinkCollectionIntent) {
445 links = ((LinkCollectionIntent) installable).links();
446 } else if (installable instanceof OpticalPathIntent) {
447 links = ((OpticalPathIntent) installable).path().links();
448 }
449
Simon Hunt1911fe42017-05-02 18:25:58 -0700450 if (links == null) {
451 links = Lists.newArrayList();
452 }
453
454 links = addEdgeLinksIfNeeded(intent, Lists.newArrayList(links));
455
Simon Hunt57830172015-08-26 13:25:17 -0700456 boolean isOptical = intent instanceof OpticalConnectivityIntent;
457 processLinks(linkMap, links, flavor, isOptical, showTraffic);
Simon Hunt94f7dae2015-08-26 17:40:59 -0700458 updateHighlights(highlights, links);
Simon Hunta17fa672015-08-19 18:42:22 -0700459 }
460 }
461 }
462 }
463
Simon Hunt5328f792017-01-11 17:43:31 -0800464 private Iterable<Link> addEdgeLinksIfNeeded(Intent parentIntent,
465 Collection<Link> links) {
466 if (parentIntent instanceof HostToHostIntent) {
467 links = new HashSet<>(links);
468 HostToHostIntent h2h = (HostToHostIntent) parentIntent;
Simon Hunt1911fe42017-05-02 18:25:58 -0700469 Host h1 = services.host().getHost(h2h.one());
470 Host h2 = services.host().getHost(h2h.two());
Simon Hunt5328f792017-01-11 17:43:31 -0800471 links.add(createEdgeLink(h1, true));
472 links.add(createEdgeLink(h2, true));
473 }
474 return links;
475 }
476
Simon Hunt94f7dae2015-08-26 17:40:59 -0700477 private void updateHighlights(Highlights highlights, Iterable<Link> links) {
478 for (Link link : links) {
479 ensureNodePresent(highlights, link.src().elementId());
480 ensureNodePresent(highlights, link.dst().elementId());
481 }
482 }
483
484 private void ensureNodePresent(Highlights highlights, ElementId eid) {
485 String id = eid.toString();
486 NodeHighlight nh = highlights.getNode(id);
487 if (nh == null) {
488 if (eid instanceof DeviceId) {
489 nh = new DeviceHighlight(id);
490 highlights.add((DeviceHighlight) nh);
491 } else if (eid instanceof HostId) {
492 nh = new HostHighlight(id);
493 highlights.add((HostHighlight) nh);
494 }
495 }
496 }
497
Simon Hunta17fa672015-08-19 18:42:22 -0700498 // Extracts links from the specified flow rule intent resources
499 private Collection<Link> linkResources(Intent installable) {
500 ImmutableList.Builder<Link> builder = ImmutableList.builder();
501 installable.resources().stream().filter(r -> r instanceof Link)
502 .forEach(r -> builder.add((Link) r));
503 return builder.build();
504 }
505
Simon Hunt57830172015-08-26 13:25:17 -0700506 private void processLinks(TrafficLinkMap linkMap, Iterable<Link> links,
507 Flavor flavor, boolean isOptical,
508 boolean showTraffic) {
509 if (links != null) {
510 for (Link link : links) {
511 TrafficLink tlink = linkMap.add(link);
512 tlink.tagFlavor(flavor);
513 tlink.optical(isOptical);
514 if (showTraffic) {
515 tlink.addLoad(getLinkFlowLoad(link));
516 tlink.antMarch(true);
517 }
518 }
519 }
520 }
521
522 private void colorLinks(Highlights highlights, TrafficLinkMap linkMap) {
523 for (TrafficLink tlink : linkMap.biLinks()) {
524 highlights.add(tlink.highlight(StatsType.TAGGED));
525 }
526 }
527
Simon Hunta17fa672015-08-19 18:42:22 -0700528}