tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
| 3 | import com.google.common.collect.HashMultimap; |
| 4 | import com.google.common.collect.SetMultimap; |
| 5 | import org.apache.felix.scr.annotations.Activate; |
| 6 | import org.apache.felix.scr.annotations.Component; |
| 7 | import org.apache.felix.scr.annotations.Deactivate; |
| 8 | import org.apache.felix.scr.annotations.Reference; |
| 9 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 10 | import org.apache.felix.scr.annotations.Service; |
| 11 | import org.onlab.onos.event.Event; |
| 12 | import org.onlab.onos.net.Link; |
| 13 | import org.onlab.onos.net.LinkKey; |
| 14 | import org.onlab.onos.net.intent.IntentId; |
| 15 | import org.onlab.onos.net.link.LinkEvent; |
| 16 | import org.onlab.onos.net.topology.TopologyEvent; |
| 17 | import org.onlab.onos.net.topology.TopologyListener; |
| 18 | import org.onlab.onos.net.topology.TopologyService; |
| 19 | import org.slf4j.Logger; |
| 20 | |
| 21 | import java.util.Collection; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 24 | import java.util.concurrent.ExecutorService; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
| 29 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 30 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 31 | import static org.onlab.util.Tools.namedThreads; |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * Entity responsible for tracking installed flows and for monitoring topology |
| 36 | * events to determine what flows are affected by topology changes. |
| 37 | */ |
| 38 | @Component |
| 39 | @Service |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 40 | public class ObjectiveTracker implements ObjectiveTrackerService { |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | |
| 44 | private final SetMultimap<LinkKey, IntentId> intentsByLink = |
| 45 | synchronizedSetMultimap(HashMultimap.<LinkKey, IntentId>create()); |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected TopologyService topologyService; |
| 49 | |
| 50 | private ExecutorService executorService = |
| 51 | newSingleThreadExecutor(namedThreads("onos-flowtracker")); |
| 52 | |
| 53 | private TopologyListener listener = new InternalTopologyListener(); |
| 54 | private TopologyChangeDelegate delegate; |
| 55 | |
| 56 | @Activate |
| 57 | public void activate() { |
| 58 | topologyService.addListener(listener); |
| 59 | log.info("Started"); |
| 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | public void deactivate() { |
| 64 | topologyService.removeListener(listener); |
| 65 | log.info("Stopped"); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 70 | checkNotNull(delegate, "Delegate cannot be null"); |
| 71 | checkArgument(this.delegate == null || this.delegate == delegate, |
| 72 | "Another delegate already set"); |
| 73 | this.delegate = delegate; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 78 | checkArgument(this.delegate == delegate, "Not the current delegate"); |
| 79 | this.delegate = null; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void addTrackedResources(IntentId intentId, Collection<Link> resources) { |
| 84 | for (Link link : resources) { |
| 85 | intentsByLink.put(new LinkKey(link), intentId); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void removeTrackedResources(IntentId intentId, Collection<Link> resources) { |
| 91 | for (Link link : resources) { |
| 92 | intentsByLink.remove(new LinkKey(link), intentId); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Internal re-actor to topology change events. |
| 97 | private class InternalTopologyListener implements TopologyListener { |
| 98 | @Override |
| 99 | public void event(TopologyEvent event) { |
| 100 | executorService.execute(new TopologyChangeHandler(event)); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Re-dispatcher of topology change events. |
| 105 | private class TopologyChangeHandler implements Runnable { |
| 106 | |
| 107 | private final TopologyEvent event; |
| 108 | |
| 109 | TopologyChangeHandler(TopologyEvent event) { |
| 110 | this.event = event; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void run() { |
| 115 | if (event.reasons() == null) { |
tom | 1dd08e4 | 2014-10-07 11:40:00 -0700 | [diff] [blame^] | 116 | delegate.triggerCompile(new HashSet<IntentId>(), true); |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 117 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 118 | } else { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 119 | Set<IntentId> toBeRecompiled = new HashSet<>(); |
| 120 | boolean recompileOnly = true; |
| 121 | |
| 122 | // Scan through the list of reasons and keep accruing all |
| 123 | // intents that need to be recompiled. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 124 | for (Event reason : event.reasons()) { |
| 125 | if (reason instanceof LinkEvent) { |
| 126 | LinkEvent linkEvent = (LinkEvent) reason; |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 127 | if (linkEvent.type() == LINK_REMOVED) { |
| 128 | Set<IntentId> intentIds = intentsByLink.get(new LinkKey(linkEvent.subject())); |
| 129 | toBeRecompiled.addAll(intentIds); |
| 130 | } |
| 131 | recompileOnly = recompileOnly && linkEvent.type() == LINK_REMOVED; |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 134 | |
| 135 | delegate.triggerCompile(toBeRecompiled, !recompileOnly); |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } |