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; |
| 22 | import java.util.concurrent.ExecutorService; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
| 27 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
| 28 | import static org.onlab.util.Tools.namedThreads; |
| 29 | import static org.slf4j.LoggerFactory.getLogger; |
| 30 | |
| 31 | /** |
| 32 | * Entity responsible for tracking installed flows and for monitoring topology |
| 33 | * events to determine what flows are affected by topology changes. |
| 34 | */ |
| 35 | @Component |
| 36 | @Service |
| 37 | public class FlowTracker implements FlowTrackerService { |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | |
| 41 | private final SetMultimap<LinkKey, IntentId> intentsByLink = |
| 42 | synchronizedSetMultimap(HashMultimap.<LinkKey, IntentId>create()); |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected TopologyService topologyService; |
| 46 | |
| 47 | private ExecutorService executorService = |
| 48 | newSingleThreadExecutor(namedThreads("onos-flowtracker")); |
| 49 | |
| 50 | private TopologyListener listener = new InternalTopologyListener(); |
| 51 | private TopologyChangeDelegate delegate; |
| 52 | |
| 53 | @Activate |
| 54 | public void activate() { |
| 55 | topologyService.addListener(listener); |
| 56 | log.info("Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | public void deactivate() { |
| 61 | topologyService.removeListener(listener); |
| 62 | log.info("Stopped"); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void setDelegate(TopologyChangeDelegate delegate) { |
| 67 | checkNotNull(delegate, "Delegate cannot be null"); |
| 68 | checkArgument(this.delegate == null || this.delegate == delegate, |
| 69 | "Another delegate already set"); |
| 70 | this.delegate = delegate; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void unsetDelegate(TopologyChangeDelegate delegate) { |
| 75 | checkArgument(this.delegate == delegate, "Not the current delegate"); |
| 76 | this.delegate = null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void addTrackedResources(IntentId intentId, Collection<Link> resources) { |
| 81 | for (Link link : resources) { |
| 82 | intentsByLink.put(new LinkKey(link), intentId); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void removeTrackedResources(IntentId intentId, Collection<Link> resources) { |
| 88 | for (Link link : resources) { |
| 89 | intentsByLink.remove(new LinkKey(link), intentId); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Internal re-actor to topology change events. |
| 94 | private class InternalTopologyListener implements TopologyListener { |
| 95 | @Override |
| 96 | public void event(TopologyEvent event) { |
| 97 | executorService.execute(new TopologyChangeHandler(event)); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Re-dispatcher of topology change events. |
| 102 | private class TopologyChangeHandler implements Runnable { |
| 103 | |
| 104 | private final TopologyEvent event; |
| 105 | |
| 106 | TopologyChangeHandler(TopologyEvent event) { |
| 107 | this.event = event; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void run() { |
| 112 | if (event.reasons() == null) { |
| 113 | delegate.bumpIntents(intentsByLink.values()); |
| 114 | } else { |
| 115 | for (Event reason : event.reasons()) { |
| 116 | if (reason instanceof LinkEvent) { |
| 117 | LinkEvent linkEvent = (LinkEvent) reason; |
| 118 | if (linkEvent.type() == LinkEvent.Type.LINK_ADDED || |
| 119 | linkEvent.type() == LinkEvent.Type.LINK_UPDATED) { |
| 120 | delegate.bumpIntents(intentsByLink.get(new LinkKey(linkEvent.subject()))); |
| 121 | } else if (linkEvent.type() == LinkEvent.Type.LINK_REMOVED) { |
| 122 | delegate.failIntents(intentsByLink.get(new LinkKey(linkEvent.subject()))); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |