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