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