blob: e91bb89d3a5d34e40a12f447e610b2e94232d9e0 [file] [log] [blame]
tombe988312014-09-19 18:38:47 -07001package org.onlab.onos.net.flow.impl;
alshabib57044ba2014-09-16 15:58:01 -07002
alshabibbb42cad2014-09-25 11:43:05 -07003import static com.google.common.base.Preconditions.checkNotNull;
4import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI9def0472014-10-23 15:51:10 -07005import static org.onlab.util.Tools.namedThreads;
alshabibbb42cad2014-09-25 11:43:05 -07006
alshabibbb42cad2014-09-25 11:43:05 -07007import java.util.List;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -07008import java.util.Map;
Madan Jampani117aaae2014-10-23 10:04:05 -07009import java.util.Set;
alshabib193525b2014-10-08 18:58:03 -070010import java.util.concurrent.CancellationException;
alshabib902d41b2014-10-07 16:52:05 -070011import java.util.concurrent.ExecutionException;
Yuta HIGUCHI9def0472014-10-23 15:51:10 -070012import java.util.concurrent.ExecutorService;
Madan Jampani117aaae2014-10-23 10:04:05 -070013import java.util.concurrent.Executors;
alshabib902d41b2014-10-07 16:52:05 -070014import java.util.concurrent.Future;
15import java.util.concurrent.TimeUnit;
16import java.util.concurrent.TimeoutException;
alshabib193525b2014-10-08 18:58:03 -070017import java.util.concurrent.atomic.AtomicReference;
alshabibbb42cad2014-09-25 11:43:05 -070018
alshabib57044ba2014-09-16 15:58:01 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
alshabiba68eb962014-09-24 20:34:13 -070025import org.onlab.onos.ApplicationId;
alshabib57044ba2014-09-16 15:58:01 -070026import org.onlab.onos.event.AbstractListenerRegistry;
27import org.onlab.onos.event.EventDeliveryService;
28import org.onlab.onos.net.Device;
29import org.onlab.onos.net.DeviceId;
30import org.onlab.onos.net.device.DeviceService;
alshabib902d41b2014-10-07 16:52:05 -070031import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabibcf369912014-10-13 14:16:42 -070032import org.onlab.onos.net.flow.DefaultFlowEntry;
alshabib1c319ff2014-10-04 20:29:09 -070033import org.onlab.onos.net.flow.FlowEntry;
alshabib57044ba2014-09-16 15:58:01 -070034import org.onlab.onos.net.flow.FlowRule;
alshabib902d41b2014-10-07 16:52:05 -070035import org.onlab.onos.net.flow.FlowRuleBatchEntry;
alshabib193525b2014-10-08 18:58:03 -070036import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
Madan Jampani117aaae2014-10-23 10:04:05 -070037import org.onlab.onos.net.flow.FlowRuleBatchEvent;
alshabib902d41b2014-10-07 16:52:05 -070038import org.onlab.onos.net.flow.FlowRuleBatchOperation;
Madan Jampani117aaae2014-10-23 10:04:05 -070039import org.onlab.onos.net.flow.FlowRuleBatchRequest;
alshabib57044ba2014-09-16 15:58:01 -070040import org.onlab.onos.net.flow.FlowRuleEvent;
41import org.onlab.onos.net.flow.FlowRuleListener;
42import org.onlab.onos.net.flow.FlowRuleProvider;
43import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
44import org.onlab.onos.net.flow.FlowRuleProviderService;
45import org.onlab.onos.net.flow.FlowRuleService;
tombe988312014-09-19 18:38:47 -070046import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070047import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
alshabib57044ba2014-09-16 15:58:01 -070048import org.onlab.onos.net.provider.AbstractProviderRegistry;
49import org.onlab.onos.net.provider.AbstractProviderService;
50import org.slf4j.Logger;
51
alshabib902d41b2014-10-07 16:52:05 -070052import com.google.common.collect.ArrayListMultimap;
alshabibbb42cad2014-09-25 11:43:05 -070053import com.google.common.collect.Lists;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070054import com.google.common.collect.Maps;
alshabib902d41b2014-10-07 16:52:05 -070055import com.google.common.collect.Multimap;
Madan Jampani117aaae2014-10-23 10:04:05 -070056import com.google.common.collect.Sets;
57import com.google.common.util.concurrent.Futures;
58import com.google.common.util.concurrent.ListenableFuture;
alshabiba7f7ca82014-09-22 11:41:23 -070059
tome4729872014-09-23 00:37:37 -070060/**
61 * Provides implementation of the flow NB & SB APIs.
62 */
alshabib57044ba2014-09-16 15:58:01 -070063@Component(immediate = true)
64@Service
tom202175a2014-09-19 19:00:11 -070065public class FlowRuleManager
tom9b4030d2014-10-06 10:39:03 -070066 extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
67 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070068
alshabib193525b2014-10-08 18:58:03 -070069 enum BatchState { STARTED, FINISHED, CANCELLED };
70
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070071 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070072 private final Logger log = getLogger(getClass());
73
74 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
tom9b4030d2014-10-06 10:39:03 -070075 listenerRegistry = new AbstractListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -070076
alshabibbb42cad2014-09-25 11:43:05 -070077 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -070078
Yuta HIGUCHI9def0472014-10-23 15:51:10 -070079 private final ExecutorService futureListeners = Executors.newCachedThreadPool(namedThreads("provider-future-listeners"));
80
tombe988312014-09-19 18:38:47 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070083
alshabib57044ba2014-09-16 15:58:01 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070085 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -070086
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070088 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -070089
90 @Activate
91 public void activate() {
tomc78acee2014-09-24 15:16:55 -070092 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -070093 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
94 log.info("Started");
95 }
96
97 @Deactivate
98 public void deactivate() {
Yuta HIGUCHI9def0472014-10-23 15:51:10 -070099 futureListeners.shutdownNow();
100
tomc78acee2014-09-24 15:16:55 -0700101 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700102 eventDispatcher.removeSink(FlowRuleEvent.class);
103 log.info("Stopped");
104 }
105
106 @Override
tom9b4030d2014-10-06 10:39:03 -0700107 public int getFlowRuleCount() {
108 return store.getFlowRuleCount();
109 }
110
111 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700112 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700113 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700114 }
115
116 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700117 public void applyFlowRules(FlowRule... flowRules) {
alshabib57044ba2014-09-16 15:58:01 -0700118 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -0700119 FlowRule f = flowRules[i];
Madan Jampani117aaae2014-10-23 10:04:05 -0700120 store.storeFlowRule(f);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700121 }
122 }
123
124 private void applyFlowRulesToProviders(FlowRule... flowRules) {
125 DeviceId did = null;
126 FlowRuleProvider frp = null;
127 for (FlowRule f : flowRules) {
128 if (!f.deviceId().equals(did)) {
129 did = f.deviceId();
130 final Device device = deviceService.getDevice(did);
131 frp = getProvider(device.providerId());
132 }
133 if (frp != null) {
134 frp.applyFlowRule(f);
135 }
alshabib57044ba2014-09-16 15:58:01 -0700136 }
alshabib57044ba2014-09-16 15:58:01 -0700137 }
138
139 @Override
140 public void removeFlowRules(FlowRule... flowRules) {
alshabibbb8b1282014-09-22 17:00:18 -0700141 FlowRule f;
alshabib57044ba2014-09-16 15:58:01 -0700142 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -0700143 f = flowRules[i];
Madan Jampani117aaae2014-10-23 10:04:05 -0700144 store.deleteFlowRule(f);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700145 }
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);
tom7951b232014-10-06 13:35:30 -0700155 frp = getProvider(device.providerId());
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700156 }
157 if (frp != null) {
tom7951b232014-10-06 13:35:30 -0700158 frp.removeFlowRule(f);
159 }
alshabib57044ba2014-09-16 15:58:01 -0700160 }
alshabiba68eb962014-09-24 20:34:13 -0700161 }
alshabib57044ba2014-09-16 15:58:01 -0700162
alshabiba68eb962014-09-24 20:34:13 -0700163 @Override
164 public void removeFlowRulesById(ApplicationId id) {
tom9b4030d2014-10-06 10:39:03 -0700165 Iterable<FlowRule> rules = getFlowRulesById(id);
alshabiba68eb962014-09-24 20:34:13 -0700166 FlowRuleProvider frp;
167 Device device;
alshabibbb42cad2014-09-25 11:43:05 -0700168
alshabiba68eb962014-09-24 20:34:13 -0700169 for (FlowRule f : rules) {
170 store.deleteFlowRule(f);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700171 // FIXME: only accept request and push to provider on internal event
alshabiba68eb962014-09-24 20:34:13 -0700172 device = deviceService.getDevice(f.deviceId());
173 frp = getProvider(device.providerId());
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700174 // FIXME: flows removed from store and flows removed from might diverge
175 // get rid of #removeRulesById?
alshabiba68eb962014-09-24 20:34:13 -0700176 frp.removeRulesById(id, f);
177 }
178 }
179
180 @Override
181 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
alshabib1c319ff2014-10-04 20:29:09 -0700182 return store.getFlowRulesByAppId(id);
alshabib57044ba2014-09-16 15:58:01 -0700183 }
184
185 @Override
alshabib902d41b2014-10-07 16:52:05 -0700186 public Future<CompletedBatchOperation> applyBatch(
187 FlowRuleBatchOperation batch) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700188 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
alshabib902d41b2014-10-07 16:52:05 -0700189 ArrayListMultimap.create();
alshabib193525b2014-10-08 18:58:03 -0700190 List<Future<CompletedBatchOperation>> futures = Lists.newArrayList();
alshabib902d41b2014-10-07 16:52:05 -0700191 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
192 final FlowRule f = fbe.getTarget();
Madan Jampani117aaae2014-10-23 10:04:05 -0700193 perDeviceBatches.put(f.deviceId(), fbe);
alshabib902d41b2014-10-07 16:52:05 -0700194 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700195
196 for (DeviceId deviceId : perDeviceBatches.keySet()) {
alshabib902d41b2014-10-07 16:52:05 -0700197 FlowRuleBatchOperation b =
Madan Jampani117aaae2014-10-23 10:04:05 -0700198 new FlowRuleBatchOperation(perDeviceBatches.get(deviceId));
199 Future<CompletedBatchOperation> future = store.storeBatch(b);
alshabib902d41b2014-10-07 16:52:05 -0700200 futures.add(future);
201 }
Madan Jampani117aaae2014-10-23 10:04:05 -0700202 return new FlowRuleBatchFuture(futures, perDeviceBatches);
alshabib902d41b2014-10-07 16:52:05 -0700203 }
204
205 @Override
alshabib57044ba2014-09-16 15:58:01 -0700206 public void addListener(FlowRuleListener listener) {
207 listenerRegistry.addListener(listener);
208 }
209
210 @Override
211 public void removeListener(FlowRuleListener listener) {
212 listenerRegistry.removeListener(listener);
213 }
214
215 @Override
216 protected FlowRuleProviderService createProviderService(
217 FlowRuleProvider provider) {
218 return new InternalFlowRuleProviderService(provider);
219 }
220
221 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700222 extends AbstractProviderService<FlowRuleProvider>
223 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700224
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700225 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
226
alshabib57044ba2014-09-16 15:58:01 -0700227 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
228 super(provider);
229 }
230
231 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700232 public void flowRemoved(FlowEntry flowEntry) {
233 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700234 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700235 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700236 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700237 if (stored == null) {
alshabib1c319ff2014-10-04 20:29:09 -0700238 log.info("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700239 return;
240 }
alshabib1c319ff2014-10-04 20:29:09 -0700241 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700242 FlowRuleProvider frp = getProvider(device.providerId());
243 FlowRuleEvent event = null;
244 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700245 case ADDED:
246 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700247 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700248 break;
249 case PENDING_REMOVE:
250 case REMOVED:
251 event = store.removeFlowRule(stored);
252 break;
253 default:
254 break;
alshabib57044ba2014-09-16 15:58:01 -0700255
alshabiba68eb962014-09-24 20:34:13 -0700256 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700257 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700258 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700259 post(event);
260 }
alshabib57044ba2014-09-16 15:58:01 -0700261 }
262
alshabibba5ac482014-10-02 17:15:20 -0700263
alshabib1c319ff2014-10-04 20:29:09 -0700264 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700265 checkNotNull(flowRule, FLOW_RULE_NULL);
266 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700267 Device device = deviceService.getDevice(flowRule.deviceId());
268 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700269 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700270 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700271 case PENDING_REMOVE:
272 case REMOVED:
273 event = store.removeFlowRule(flowRule);
274 frp.removeFlowRule(flowRule);
275 break;
276 case ADDED:
277 case PENDING_ADD:
278 frp.applyFlowRule(flowRule);
279 break;
280 default:
281 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700282 }
283
alshabibbb42cad2014-09-25 11:43:05 -0700284 if (event != null) {
285 log.debug("Flow {} removed", flowRule);
286 post(event);
287 }
alshabib57044ba2014-09-16 15:58:01 -0700288
289 }
290
alshabibba5ac482014-10-02 17:15:20 -0700291
292 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700293 checkNotNull(flowRule, FLOW_RULE_NULL);
294 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700295 FlowRuleProvider frp = getProvider(flowRule.deviceId());
296 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700297 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700298 }
299
alshabibba5ac482014-10-02 17:15:20 -0700300
alshabib1c319ff2014-10-04 20:29:09 -0700301 private void flowAdded(FlowEntry flowEntry) {
302 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700303 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700304
alshabib1c319ff2014-10-04 20:29:09 -0700305 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700306
alshabib1c319ff2014-10-04 20:29:09 -0700307 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700308 if (event == null) {
309 log.debug("No flow store event generated.");
310 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700311 log.debug("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700312 post(event);
313 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700314 } else {
Madan Jampani117aaae2014-10-23 10:04:05 -0700315 log.info("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700316 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700317 }
alshabib219ebaa2014-09-22 15:41:24 -0700318
alshabib57044ba2014-09-16 15:58:01 -0700319 }
320
alshabib1c319ff2014-10-04 20:29:09 -0700321 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
322 if (storedRule == null) {
323 return false;
324 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700325 if (storedRule.isPermanent()) {
326 return true;
327 }
328
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700329 final long timeout = storedRule.timeout() * 1000;
330 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700331 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700332 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700333 return true;
334 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700335 if (!lastSeen.containsKey(storedRule)) {
336 // checking for the first time
337 lastSeen.put(storedRule, storedRule.lastSeen());
338 // Use following if lastSeen attr. was removed.
339 //lastSeen.put(storedRule, currentTime);
340 }
341 Long last = lastSeen.get(storedRule);
342 if (last == null) {
343 // concurrently removed? let the liveness check fail
344 return false;
345 }
alshabib85c41972014-10-03 13:48:39 -0700346
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700347 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700348 return true;
349 }
350 return false;
alshabibba5ac482014-10-02 17:15:20 -0700351 }
352
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700353 // Posts the specified event to the local event dispatcher.
354 private void post(FlowRuleEvent event) {
355 if (event != null) {
356 eventDispatcher.post(event);
357 }
358 }
alshabib5c370ff2014-09-18 10:12:14 -0700359
360 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700361 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
362 List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
alshabibbb8b1282014-09-22 17:00:18 -0700363
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700364 for (FlowEntry rule : flowEntries) {
alshabiba7f7ca82014-09-22 11:41:23 -0700365 if (storedRules.remove(rule)) {
alshabib219ebaa2014-09-22 15:41:24 -0700366 // we both have the rule, let's update some info then.
alshabiba7f7ca82014-09-22 11:41:23 -0700367 flowAdded(rule);
368 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700369 // the device has a rule the store does not have
370 extraneousFlow(rule);
alshabiba7f7ca82014-09-22 11:41:23 -0700371 }
372 }
alshabib1c319ff2014-10-04 20:29:09 -0700373 for (FlowEntry rule : storedRules) {
alshabiba7f7ca82014-09-22 11:41:23 -0700374 // there are rules in the store that aren't on the switch
375 flowMissing(rule);
alshabib54ce5892014-09-23 17:50:51 -0700376
alshabiba7f7ca82014-09-22 11:41:23 -0700377 }
alshabib5c370ff2014-09-18 10:12:14 -0700378 }
alshabib57044ba2014-09-16 15:58:01 -0700379 }
380
tomc78acee2014-09-24 15:16:55 -0700381 // Store delegate to re-post events emitted from the store.
382 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Madan Jampani117aaae2014-10-23 10:04:05 -0700383 // TODO: Right now we only dispatch events at individual flowEntry level.
384 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700385 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700386 public void notify(FlowRuleBatchEvent event) {
387 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700388 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700389 case BATCH_OPERATION_REQUESTED:
Madan Jampani31961c12014-10-23 12:06:58 -0700390 for (FlowEntry entry : request.toAdd()) {
391 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, entry));
392 }
393 for (FlowEntry entry : request.toRemove()) {
394 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVE_REQUESTED, entry));
395 }
396 // FIXME: what about op.equals(FlowRuleOperation.MODIFY) ?
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700397
Madan Jampani117aaae2014-10-23 10:04:05 -0700398 FlowRuleBatchOperation batchOperation = request.asBatchOperation();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700399
Madan Jampani117aaae2014-10-23 10:04:05 -0700400 FlowRuleProvider flowRuleProvider =
401 getProvider(batchOperation.getOperations().get(0).getTarget().deviceId());
402 final ListenableFuture<CompletedBatchOperation> result =
403 flowRuleProvider.executeBatch(batchOperation);
404 result.addListener(new Runnable() {
405 @Override
406 public void run() {
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700407 store.batchOperationComplete(FlowRuleBatchEvent.completed(request, Futures.getUnchecked(result)));
Madan Jampani117aaae2014-10-23 10:04:05 -0700408 }
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700409 }, futureListeners);
Madan Jampani117aaae2014-10-23 10:04:05 -0700410
411 break;
412 case BATCH_OPERATION_COMPLETED:
413 Set<FlowEntry> failedItems = event.result().failedItems();
414 for (FlowEntry entry : request.toAdd()) {
415 if (!failedItems.contains(entry)) {
416 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADDED, entry));
417 }
418 }
419 for (FlowEntry entry : request.toRemove()) {
420 if (!failedItems.contains(entry)) {
421 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVED, entry));
422 }
423 }
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700424 break;
425 default:
426 break;
427 }
tomc78acee2014-09-24 15:16:55 -0700428 }
429 }
alshabib902d41b2014-10-07 16:52:05 -0700430
Madan Jampani117aaae2014-10-23 10:04:05 -0700431 private class FlowRuleBatchFuture implements Future<CompletedBatchOperation> {
alshabib902d41b2014-10-07 16:52:05 -0700432
alshabib193525b2014-10-08 18:58:03 -0700433 private final List<Future<CompletedBatchOperation>> futures;
Madan Jampani117aaae2014-10-23 10:04:05 -0700434 private final Multimap<DeviceId, FlowRuleBatchEntry> batches;
alshabib193525b2014-10-08 18:58:03 -0700435 private final AtomicReference<BatchState> state;
436 private CompletedBatchOperation overall;
alshabib902d41b2014-10-07 16:52:05 -0700437
alshabib193525b2014-10-08 18:58:03 -0700438 public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures,
Madan Jampani117aaae2014-10-23 10:04:05 -0700439 Multimap<DeviceId, FlowRuleBatchEntry> batches) {
alshabib902d41b2014-10-07 16:52:05 -0700440 this.futures = futures;
alshabib193525b2014-10-08 18:58:03 -0700441 this.batches = batches;
442 state = new AtomicReference<FlowRuleManager.BatchState>();
443 state.set(BatchState.STARTED);
alshabib902d41b2014-10-07 16:52:05 -0700444 }
445
446 @Override
447 public boolean cancel(boolean mayInterruptIfRunning) {
alshabib193525b2014-10-08 18:58:03 -0700448 if (state.get() == BatchState.FINISHED) {
449 return false;
450 }
451 if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) {
452 return false;
453 }
454 cleanUpBatch();
455 for (Future<CompletedBatchOperation> f : futures) {
456 f.cancel(mayInterruptIfRunning);
457 }
458 return true;
alshabib902d41b2014-10-07 16:52:05 -0700459 }
460
461 @Override
462 public boolean isCancelled() {
alshabib193525b2014-10-08 18:58:03 -0700463 return state.get() == BatchState.CANCELLED;
alshabib902d41b2014-10-07 16:52:05 -0700464 }
465
466 @Override
467 public boolean isDone() {
alshabib193525b2014-10-08 18:58:03 -0700468 return state.get() == BatchState.FINISHED;
alshabib902d41b2014-10-07 16:52:05 -0700469 }
470
alshabib193525b2014-10-08 18:58:03 -0700471
alshabib902d41b2014-10-07 16:52:05 -0700472 @Override
473 public CompletedBatchOperation get() throws InterruptedException,
alshabib193525b2014-10-08 18:58:03 -0700474 ExecutionException {
475
476 if (isDone()) {
477 return overall;
alshabib902d41b2014-10-07 16:52:05 -0700478 }
alshabib193525b2014-10-08 18:58:03 -0700479
480 boolean success = true;
Madan Jampani117aaae2014-10-23 10:04:05 -0700481 Set<FlowEntry> failed = Sets.newHashSet();
alshabib193525b2014-10-08 18:58:03 -0700482 CompletedBatchOperation completed;
483 for (Future<CompletedBatchOperation> future : futures) {
484 completed = future.get();
alshabib3effd042014-10-17 12:00:31 -0700485 success = validateBatchOperation(failed, completed);
alshabib193525b2014-10-08 18:58:03 -0700486 }
487
488 return finalizeBatchOperation(success, failed);
489
alshabib902d41b2014-10-07 16:52:05 -0700490 }
491
492 @Override
493 public CompletedBatchOperation get(long timeout, TimeUnit unit)
494 throws InterruptedException, ExecutionException,
495 TimeoutException {
alshabib193525b2014-10-08 18:58:03 -0700496
497 if (isDone()) {
498 return overall;
499 }
500 boolean success = true;
Madan Jampani117aaae2014-10-23 10:04:05 -0700501 Set<FlowEntry> failed = Sets.newHashSet();
alshabib193525b2014-10-08 18:58:03 -0700502 CompletedBatchOperation completed;
alshabib902d41b2014-10-07 16:52:05 -0700503 long start = System.nanoTime();
504 long end = start + unit.toNanos(timeout);
alshabib193525b2014-10-08 18:58:03 -0700505
506 for (Future<CompletedBatchOperation> future : futures) {
alshabib902d41b2014-10-07 16:52:05 -0700507 long now = System.nanoTime();
508 long thisTimeout = end - now;
alshabib193525b2014-10-08 18:58:03 -0700509 completed = future.get(thisTimeout, TimeUnit.NANOSECONDS);
alshabib3effd042014-10-17 12:00:31 -0700510 success = validateBatchOperation(failed, completed);
alshabib902d41b2014-10-07 16:52:05 -0700511 }
alshabib193525b2014-10-08 18:58:03 -0700512 return finalizeBatchOperation(success, failed);
alshabib902d41b2014-10-07 16:52:05 -0700513 }
514
Madan Jampani117aaae2014-10-23 10:04:05 -0700515 private boolean validateBatchOperation(Set<FlowEntry> failed,
alshabib3effd042014-10-17 12:00:31 -0700516 CompletedBatchOperation completed) {
alshabib193525b2014-10-08 18:58:03 -0700517
518 if (isCancelled()) {
519 throw new CancellationException();
520 }
521 if (!completed.isSuccess()) {
522 failed.addAll(completed.failedItems());
523 cleanUpBatch();
524 cancelAllSubBatches();
525 return false;
526 }
527 return true;
528 }
529
530 private void cancelAllSubBatches() {
531 for (Future<CompletedBatchOperation> f : futures) {
532 f.cancel(true);
533 }
534 }
535
536 private CompletedBatchOperation finalizeBatchOperation(boolean success,
Madan Jampani117aaae2014-10-23 10:04:05 -0700537 Set<FlowEntry> failed) {
alshabib26834582014-10-08 20:15:46 -0700538 synchronized (this) {
alshabib193525b2014-10-08 18:58:03 -0700539 if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) {
540 if (state.get() == BatchState.FINISHED) {
541 return overall;
542 }
543 throw new CancellationException();
544 }
545 overall = new CompletedBatchOperation(success, failed);
546 return overall;
547 }
548 }
549
550 private void cleanUpBatch() {
551 for (FlowRuleBatchEntry fbe : batches.values()) {
552 if (fbe.getOperator() == FlowRuleOperation.ADD ||
553 fbe.getOperator() == FlowRuleOperation.MODIFY) {
554 store.deleteFlowRule(fbe.getTarget());
555 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) {
alshabibcf369912014-10-13 14:16:42 -0700556 store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget()));
alshabib193525b2014-10-08 18:58:03 -0700557 store.storeFlowRule(fbe.getTarget());
558 }
559 }
alshabib193525b2014-10-08 18:58:03 -0700560 }
alshabib902d41b2014-10-07 16:52:05 -0700561 }
alshabib57044ba2014-09-16 15:58:01 -0700562}