tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow.impl; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 2 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 6 | import java.util.List; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 7 | import java.util.Map; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 8 | import java.util.Set; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 9 | import java.util.concurrent.CancellationException; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 10 | import java.util.concurrent.ExecutionException; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 11 | import java.util.concurrent.Executors; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 12 | import java.util.concurrent.Future; |
| 13 | import java.util.concurrent.TimeUnit; |
| 14 | import java.util.concurrent.TimeoutException; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 15 | import java.util.concurrent.atomic.AtomicReference; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 16 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 17 | import org.apache.felix.scr.annotations.Activate; |
| 18 | import org.apache.felix.scr.annotations.Component; |
| 19 | import org.apache.felix.scr.annotations.Deactivate; |
| 20 | import org.apache.felix.scr.annotations.Reference; |
| 21 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 22 | import org.apache.felix.scr.annotations.Service; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 23 | import org.onlab.onos.ApplicationId; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 24 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 25 | import org.onlab.onos.event.EventDeliveryService; |
| 26 | import org.onlab.onos.net.Device; |
| 27 | import org.onlab.onos.net.DeviceId; |
| 28 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.flow.FlowEntry; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 35 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 37 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 39 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 40 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 41 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 42 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 43 | import org.onlab.onos.net.flow.FlowRuleService; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 47 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 48 | import org.slf4j.Logger; |
| 49 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 50 | import com.google.common.collect.ArrayListMultimap; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 51 | import com.google.common.collect.Lists; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 52 | import com.google.common.collect.Maps; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 53 | import com.google.common.collect.Multimap; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 54 | import com.google.common.collect.Sets; |
| 55 | import com.google.common.util.concurrent.Futures; |
| 56 | import com.google.common.util.concurrent.ListenableFuture; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 57 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Provides implementation of the flow NB & SB APIs. |
| 60 | */ |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 61 | @Component(immediate = true) |
| 62 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 63 | public class FlowRuleManager |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 64 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 65 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 66 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 67 | enum BatchState { STARTED, FINISHED, CANCELLED }; |
| 68 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 69 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 70 | private final Logger log = getLogger(getClass()); |
| 71 | |
| 72 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 73 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 74 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 75 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 76 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 79 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 81 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 84 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 85 | |
| 86 | @Activate |
| 87 | public void activate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 88 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 89 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 95 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 96 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
| 100 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 101 | public int getFlowRuleCount() { |
| 102 | return store.getFlowRuleCount(); |
| 103 | } |
| 104 | |
| 105 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 106 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 107 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 111 | public void applyFlowRules(FlowRule... flowRules) { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 112 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 113 | FlowRule f = flowRules[i]; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 114 | store.storeFlowRule(f); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | private void applyFlowRulesToProviders(FlowRule... flowRules) { |
| 119 | DeviceId did = null; |
| 120 | FlowRuleProvider frp = null; |
| 121 | for (FlowRule f : flowRules) { |
| 122 | if (!f.deviceId().equals(did)) { |
| 123 | did = f.deviceId(); |
| 124 | final Device device = deviceService.getDevice(did); |
| 125 | frp = getProvider(device.providerId()); |
| 126 | } |
| 127 | if (frp != null) { |
| 128 | frp.applyFlowRule(f); |
| 129 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 130 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void removeFlowRules(FlowRule... flowRules) { |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 135 | FlowRule f; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 136 | for (int i = 0; i < flowRules.length; i++) { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 137 | f = flowRules[i]; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 138 | store.deleteFlowRule(f); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | private void removeFlowRulesFromProviders(FlowRule... flowRules) { |
| 143 | DeviceId did = null; |
| 144 | FlowRuleProvider frp = null; |
| 145 | for (FlowRule f : flowRules) { |
| 146 | if (!f.deviceId().equals(did)) { |
| 147 | did = f.deviceId(); |
| 148 | final Device device = deviceService.getDevice(did); |
tom | 7951b23 | 2014-10-06 13:35:30 -0700 | [diff] [blame] | 149 | frp = getProvider(device.providerId()); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 150 | } |
| 151 | if (frp != null) { |
tom | 7951b23 | 2014-10-06 13:35:30 -0700 | [diff] [blame] | 152 | frp.removeFlowRule(f); |
| 153 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 154 | } |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 155 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 156 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public void removeFlowRulesById(ApplicationId id) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 159 | Iterable<FlowRule> rules = getFlowRulesById(id); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 160 | FlowRuleProvider frp; |
| 161 | Device device; |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 162 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 163 | for (FlowRule f : rules) { |
| 164 | store.deleteFlowRule(f); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 165 | // FIXME: only accept request and push to provider on internal event |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 166 | device = deviceService.getDevice(f.deviceId()); |
| 167 | frp = getProvider(device.providerId()); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 168 | // FIXME: flows removed from store and flows removed from might diverge |
| 169 | // get rid of #removeRulesById? |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 170 | frp.removeRulesById(id, f); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 176 | return store.getFlowRulesByAppId(id); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 180 | public Future<CompletedBatchOperation> applyBatch( |
| 181 | FlowRuleBatchOperation batch) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 182 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 183 | ArrayListMultimap.create(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 184 | List<Future<CompletedBatchOperation>> futures = Lists.newArrayList(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 185 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 186 | final FlowRule f = fbe.getTarget(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 187 | perDeviceBatches.put(f.deviceId(), fbe); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 188 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 189 | |
| 190 | for (DeviceId deviceId : perDeviceBatches.keySet()) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 191 | FlowRuleBatchOperation b = |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 192 | new FlowRuleBatchOperation(perDeviceBatches.get(deviceId)); |
| 193 | Future<CompletedBatchOperation> future = store.storeBatch(b); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 194 | futures.add(future); |
| 195 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 196 | return new FlowRuleBatchFuture(futures, perDeviceBatches); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 200 | public void addListener(FlowRuleListener listener) { |
| 201 | listenerRegistry.addListener(listener); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void removeListener(FlowRuleListener listener) { |
| 206 | listenerRegistry.removeListener(listener); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | protected FlowRuleProviderService createProviderService( |
| 211 | FlowRuleProvider provider) { |
| 212 | return new InternalFlowRuleProviderService(provider); |
| 213 | } |
| 214 | |
| 215 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 216 | extends AbstractProviderService<FlowRuleProvider> |
| 217 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 218 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 219 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 220 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 221 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 222 | super(provider); |
| 223 | } |
| 224 | |
| 225 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 226 | public void flowRemoved(FlowEntry flowEntry) { |
| 227 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 228 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 229 | lastSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 230 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 231 | if (stored == null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 232 | log.info("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 233 | return; |
| 234 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 235 | Device device = deviceService.getDevice(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 236 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 237 | FlowRuleEvent event = null; |
| 238 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 239 | case ADDED: |
| 240 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 241 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 242 | break; |
| 243 | case PENDING_REMOVE: |
| 244 | case REMOVED: |
| 245 | event = store.removeFlowRule(stored); |
| 246 | break; |
| 247 | default: |
| 248 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 249 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 250 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 251 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 252 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 253 | post(event); |
| 254 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 255 | } |
| 256 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 257 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 258 | private void flowMissing(FlowEntry flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 259 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 260 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 261 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 262 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 263 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 264 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 265 | case PENDING_REMOVE: |
| 266 | case REMOVED: |
| 267 | event = store.removeFlowRule(flowRule); |
| 268 | frp.removeFlowRule(flowRule); |
| 269 | break; |
| 270 | case ADDED: |
| 271 | case PENDING_ADD: |
| 272 | frp.applyFlowRule(flowRule); |
| 273 | break; |
| 274 | default: |
| 275 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 276 | } |
| 277 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 278 | if (event != null) { |
| 279 | log.debug("Flow {} removed", flowRule); |
| 280 | post(event); |
| 281 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 282 | |
| 283 | } |
| 284 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 285 | |
| 286 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 287 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 288 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 289 | removeFlowRules(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 290 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 291 | } |
| 292 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 293 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 294 | private void flowAdded(FlowEntry flowEntry) { |
| 295 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 296 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 297 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 298 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 299 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 300 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 301 | if (event == null) { |
| 302 | log.debug("No flow store event generated."); |
| 303 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 304 | log.debug("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 305 | post(event); |
| 306 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 307 | } else { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 308 | log.info("Removing flow rules...."); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 309 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 310 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 311 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 312 | } |
| 313 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 314 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 315 | if (storedRule == null) { |
| 316 | return false; |
| 317 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 318 | if (storedRule.isPermanent()) { |
| 319 | return true; |
| 320 | } |
| 321 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 322 | final long timeout = storedRule.timeout() * 1000; |
| 323 | final long currentTime = System.currentTimeMillis(); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 324 | if (storedRule.packets() != swRule.packets()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 325 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 326 | return true; |
| 327 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 328 | if (!lastSeen.containsKey(storedRule)) { |
| 329 | // checking for the first time |
| 330 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 331 | // Use following if lastSeen attr. was removed. |
| 332 | //lastSeen.put(storedRule, currentTime); |
| 333 | } |
| 334 | Long last = lastSeen.get(storedRule); |
| 335 | if (last == null) { |
| 336 | // concurrently removed? let the liveness check fail |
| 337 | return false; |
| 338 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 339 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 340 | if ((currentTime - last) <= timeout) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 346 | // Posts the specified event to the local event dispatcher. |
| 347 | private void post(FlowRuleEvent event) { |
| 348 | if (event != null) { |
| 349 | eventDispatcher.post(event); |
| 350 | } |
| 351 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 352 | |
| 353 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 354 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 355 | List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 356 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 357 | for (FlowEntry rule : flowEntries) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 358 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 359 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 360 | flowAdded(rule); |
| 361 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 362 | // the device has a rule the store does not have |
| 363 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 366 | for (FlowEntry rule : storedRules) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 367 | // there are rules in the store that aren't on the switch |
| 368 | flowMissing(rule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 369 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 370 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 371 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 372 | } |
| 373 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 374 | // Store delegate to re-post events emitted from the store. |
| 375 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 376 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 377 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 378 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 379 | public void notify(FlowRuleBatchEvent event) { |
| 380 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 381 | switch (event.type()) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 382 | case BATCH_OPERATION_REQUESTED: |
| 383 | // for (FlowEntry entry : request.toAdd()) { |
| 384 | // //eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, entry)); |
| 385 | // } |
| 386 | // for (FlowEntry entry : request.toRemove()) { |
| 387 | // //eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVE_REQUESTED, entry)); |
| 388 | // } |
| 389 | // // FIXME: what about op.equals(FlowRuleOperation.MODIFY) ? |
| 390 | // |
| 391 | FlowRuleBatchOperation batchOperation = request.asBatchOperation(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 392 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 393 | FlowRuleProvider flowRuleProvider = |
| 394 | getProvider(batchOperation.getOperations().get(0).getTarget().deviceId()); |
| 395 | final ListenableFuture<CompletedBatchOperation> result = |
| 396 | flowRuleProvider.executeBatch(batchOperation); |
| 397 | result.addListener(new Runnable() { |
| 398 | @Override |
| 399 | public void run() { |
| 400 | store.batchOperationComplete(FlowRuleBatchEvent.create(request, Futures.getUnchecked(result))); |
| 401 | } |
| 402 | }, Executors.newCachedThreadPool()); |
| 403 | |
| 404 | break; |
| 405 | case BATCH_OPERATION_COMPLETED: |
| 406 | Set<FlowEntry> failedItems = event.result().failedItems(); |
| 407 | for (FlowEntry entry : request.toAdd()) { |
| 408 | if (!failedItems.contains(entry)) { |
| 409 | eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADDED, entry)); |
| 410 | } |
| 411 | } |
| 412 | for (FlowEntry entry : request.toRemove()) { |
| 413 | if (!failedItems.contains(entry)) { |
| 414 | eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVED, entry)); |
| 415 | } |
| 416 | } |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 417 | break; |
| 418 | default: |
| 419 | break; |
| 420 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 423 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 424 | private class FlowRuleBatchFuture implements Future<CompletedBatchOperation> { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 425 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 426 | private final List<Future<CompletedBatchOperation>> futures; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 427 | private final Multimap<DeviceId, FlowRuleBatchEntry> batches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 428 | private final AtomicReference<BatchState> state; |
| 429 | private CompletedBatchOperation overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 430 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 431 | public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 432 | Multimap<DeviceId, FlowRuleBatchEntry> batches) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 433 | this.futures = futures; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 434 | this.batches = batches; |
| 435 | state = new AtomicReference<FlowRuleManager.BatchState>(); |
| 436 | state.set(BatchState.STARTED); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public boolean cancel(boolean mayInterruptIfRunning) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 441 | if (state.get() == BatchState.FINISHED) { |
| 442 | return false; |
| 443 | } |
| 444 | if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) { |
| 445 | return false; |
| 446 | } |
| 447 | cleanUpBatch(); |
| 448 | for (Future<CompletedBatchOperation> f : futures) { |
| 449 | f.cancel(mayInterruptIfRunning); |
| 450 | } |
| 451 | return true; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 456 | return state.get() == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public boolean isDone() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 461 | return state.get() == BatchState.FINISHED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 462 | } |
| 463 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 464 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 465 | @Override |
| 466 | public CompletedBatchOperation get() throws InterruptedException, |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 467 | ExecutionException { |
| 468 | |
| 469 | if (isDone()) { |
| 470 | return overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 471 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 472 | |
| 473 | boolean success = true; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 474 | Set<FlowEntry> failed = Sets.newHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 475 | CompletedBatchOperation completed; |
| 476 | for (Future<CompletedBatchOperation> future : futures) { |
| 477 | completed = future.get(); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 478 | success = validateBatchOperation(failed, completed); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | return finalizeBatchOperation(success, failed); |
| 482 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 487 | throws InterruptedException, ExecutionException, |
| 488 | TimeoutException { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 489 | |
| 490 | if (isDone()) { |
| 491 | return overall; |
| 492 | } |
| 493 | boolean success = true; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 494 | Set<FlowEntry> failed = Sets.newHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 495 | CompletedBatchOperation completed; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 496 | long start = System.nanoTime(); |
| 497 | long end = start + unit.toNanos(timeout); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 498 | |
| 499 | for (Future<CompletedBatchOperation> future : futures) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 500 | long now = System.nanoTime(); |
| 501 | long thisTimeout = end - now; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 502 | completed = future.get(thisTimeout, TimeUnit.NANOSECONDS); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 503 | success = validateBatchOperation(failed, completed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 504 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 505 | return finalizeBatchOperation(success, failed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 508 | private boolean validateBatchOperation(Set<FlowEntry> failed, |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 509 | CompletedBatchOperation completed) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 510 | |
| 511 | if (isCancelled()) { |
| 512 | throw new CancellationException(); |
| 513 | } |
| 514 | if (!completed.isSuccess()) { |
| 515 | failed.addAll(completed.failedItems()); |
| 516 | cleanUpBatch(); |
| 517 | cancelAllSubBatches(); |
| 518 | return false; |
| 519 | } |
| 520 | return true; |
| 521 | } |
| 522 | |
| 523 | private void cancelAllSubBatches() { |
| 524 | for (Future<CompletedBatchOperation> f : futures) { |
| 525 | f.cancel(true); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | private CompletedBatchOperation finalizeBatchOperation(boolean success, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame^] | 530 | Set<FlowEntry> failed) { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 531 | synchronized (this) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 532 | if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) { |
| 533 | if (state.get() == BatchState.FINISHED) { |
| 534 | return overall; |
| 535 | } |
| 536 | throw new CancellationException(); |
| 537 | } |
| 538 | overall = new CompletedBatchOperation(success, failed); |
| 539 | return overall; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | private void cleanUpBatch() { |
| 544 | for (FlowRuleBatchEntry fbe : batches.values()) { |
| 545 | if (fbe.getOperator() == FlowRuleOperation.ADD || |
| 546 | fbe.getOperator() == FlowRuleOperation.MODIFY) { |
| 547 | store.deleteFlowRule(fbe.getTarget()); |
| 548 | } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 549 | store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget())); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 550 | store.storeFlowRule(fbe.getTarget()); |
| 551 | } |
| 552 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 553 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 554 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 555 | } |