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