blob: 525946e38a5894a2b581385a2093937a692ab4f1 [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;
5
alshabibbb42cad2014-09-25 11:43:05 -07006import java.util.List;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -07007import java.util.Map;
alshabib193525b2014-10-08 18:58:03 -07008import java.util.concurrent.CancellationException;
alshabib902d41b2014-10-07 16:52:05 -07009import java.util.concurrent.ExecutionException;
10import java.util.concurrent.Future;
11import java.util.concurrent.TimeUnit;
12import java.util.concurrent.TimeoutException;
alshabib193525b2014-10-08 18:58:03 -070013import java.util.concurrent.atomic.AtomicReference;
alshabibbb42cad2014-09-25 11:43:05 -070014
alshabib57044ba2014-09-16 15:58:01 -070015import org.apache.felix.scr.annotations.Activate;
16import org.apache.felix.scr.annotations.Component;
17import org.apache.felix.scr.annotations.Deactivate;
18import org.apache.felix.scr.annotations.Reference;
19import org.apache.felix.scr.annotations.ReferenceCardinality;
20import org.apache.felix.scr.annotations.Service;
alshabiba68eb962014-09-24 20:34:13 -070021import org.onlab.onos.ApplicationId;
alshabib57044ba2014-09-16 15:58:01 -070022import org.onlab.onos.event.AbstractListenerRegistry;
23import org.onlab.onos.event.EventDeliveryService;
24import org.onlab.onos.net.Device;
25import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.device.DeviceService;
alshabib902d41b2014-10-07 16:52:05 -070027import org.onlab.onos.net.flow.CompletedBatchOperation;
alshabibcf369912014-10-13 14:16:42 -070028import org.onlab.onos.net.flow.DefaultFlowEntry;
alshabib1c319ff2014-10-04 20:29:09 -070029import org.onlab.onos.net.flow.FlowEntry;
alshabib57044ba2014-09-16 15:58:01 -070030import org.onlab.onos.net.flow.FlowRule;
alshabib902d41b2014-10-07 16:52:05 -070031import org.onlab.onos.net.flow.FlowRuleBatchEntry;
alshabib193525b2014-10-08 18:58:03 -070032import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
alshabib902d41b2014-10-07 16:52:05 -070033import org.onlab.onos.net.flow.FlowRuleBatchOperation;
alshabib57044ba2014-09-16 15:58:01 -070034import org.onlab.onos.net.flow.FlowRuleEvent;
35import org.onlab.onos.net.flow.FlowRuleListener;
36import org.onlab.onos.net.flow.FlowRuleProvider;
37import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
38import org.onlab.onos.net.flow.FlowRuleProviderService;
39import org.onlab.onos.net.flow.FlowRuleService;
tombe988312014-09-19 18:38:47 -070040import org.onlab.onos.net.flow.FlowRuleStore;
tomc78acee2014-09-24 15:16:55 -070041import org.onlab.onos.net.flow.FlowRuleStoreDelegate;
alshabib57044ba2014-09-16 15:58:01 -070042import org.onlab.onos.net.provider.AbstractProviderRegistry;
43import org.onlab.onos.net.provider.AbstractProviderService;
44import org.slf4j.Logger;
45
alshabib902d41b2014-10-07 16:52:05 -070046import com.google.common.collect.ArrayListMultimap;
alshabibbb42cad2014-09-25 11:43:05 -070047import com.google.common.collect.Lists;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070048import com.google.common.collect.Maps;
alshabib902d41b2014-10-07 16:52:05 -070049import com.google.common.collect.Multimap;
alshabiba7f7ca82014-09-22 11:41:23 -070050
tome4729872014-09-23 00:37:37 -070051/**
52 * Provides implementation of the flow NB & SB APIs.
53 */
alshabib57044ba2014-09-16 15:58:01 -070054@Component(immediate = true)
55@Service
tom202175a2014-09-19 19:00:11 -070056public class FlowRuleManager
tom9b4030d2014-10-06 10:39:03 -070057 extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
58 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070059
alshabib193525b2014-10-08 18:58:03 -070060 enum BatchState { STARTED, FINISHED, CANCELLED };
61
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070062 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
alshabib57044ba2014-09-16 15:58:01 -070063 private final Logger log = getLogger(getClass());
64
65 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
tom9b4030d2014-10-06 10:39:03 -070066 listenerRegistry = new AbstractListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -070067
alshabibbb42cad2014-09-25 11:43:05 -070068 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -070069
tombe988312014-09-19 18:38:47 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070072
alshabib57044ba2014-09-16 15:58:01 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070074 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -070075
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -070077 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -070078
79 @Activate
80 public void activate() {
tomc78acee2014-09-24 15:16:55 -070081 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -070082 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
83 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
tomc78acee2014-09-24 15:16:55 -070088 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -070089 eventDispatcher.removeSink(FlowRuleEvent.class);
90 log.info("Stopped");
91 }
92
93 @Override
tom9b4030d2014-10-06 10:39:03 -070094 public int getFlowRuleCount() {
95 return store.getFlowRuleCount();
96 }
97
98 @Override
alshabib1c319ff2014-10-04 20:29:09 -070099 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700100 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700101 }
102
103 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700104 public void applyFlowRules(FlowRule... flowRules) {
alshabib57044ba2014-09-16 15:58:01 -0700105 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -0700106 FlowRule f = flowRules[i];
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700107 boolean local = store.storeFlowRule(f);
108 if (local) {
109 // TODO: aggregate all local rules and push down once?
110 applyFlowRulesToProviders(f);
111 }
112 }
113 }
114
115 private void applyFlowRulesToProviders(FlowRule... flowRules) {
116 DeviceId did = null;
117 FlowRuleProvider frp = null;
118 for (FlowRule f : flowRules) {
119 if (!f.deviceId().equals(did)) {
120 did = f.deviceId();
121 final Device device = deviceService.getDevice(did);
122 frp = getProvider(device.providerId());
123 }
124 if (frp != null) {
125 frp.applyFlowRule(f);
126 }
alshabib57044ba2014-09-16 15:58:01 -0700127 }
alshabib57044ba2014-09-16 15:58:01 -0700128 }
129
130 @Override
131 public void removeFlowRules(FlowRule... flowRules) {
alshabibbb8b1282014-09-22 17:00:18 -0700132 FlowRule f;
alshabib57044ba2014-09-16 15:58:01 -0700133 for (int i = 0; i < flowRules.length; i++) {
alshabiba68eb962014-09-24 20:34:13 -0700134 f = flowRules[i];
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700135 boolean local = store.deleteFlowRule(f);
136 if (local) {
137 // TODO: aggregate all local rules and push down once?
138 removeFlowRulesFromProviders(f);
139 }
140 }
141 }
142
143 private void removeFlowRulesFromProviders(FlowRule... flowRules) {
144 DeviceId did = null;
145 FlowRuleProvider frp = null;
146 for (FlowRule f : flowRules) {
147 if (!f.deviceId().equals(did)) {
148 did = f.deviceId();
149 final Device device = deviceService.getDevice(did);
tom7951b232014-10-06 13:35:30 -0700150 frp = getProvider(device.providerId());
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700151 }
152 if (frp != null) {
tom7951b232014-10-06 13:35:30 -0700153 frp.removeFlowRule(f);
154 }
alshabib57044ba2014-09-16 15:58:01 -0700155 }
alshabiba68eb962014-09-24 20:34:13 -0700156 }
alshabib57044ba2014-09-16 15:58:01 -0700157
alshabiba68eb962014-09-24 20:34:13 -0700158 @Override
159 public void removeFlowRulesById(ApplicationId id) {
tom9b4030d2014-10-06 10:39:03 -0700160 Iterable<FlowRule> rules = getFlowRulesById(id);
alshabiba68eb962014-09-24 20:34:13 -0700161 FlowRuleProvider frp;
162 Device device;
alshabibbb42cad2014-09-25 11:43:05 -0700163
alshabiba68eb962014-09-24 20:34:13 -0700164 for (FlowRule f : rules) {
165 store.deleteFlowRule(f);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700166 // FIXME: only accept request and push to provider on internal event
alshabiba68eb962014-09-24 20:34:13 -0700167 device = deviceService.getDevice(f.deviceId());
168 frp = getProvider(device.providerId());
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700169 // FIXME: flows removed from store and flows removed from might diverge
170 // get rid of #removeRulesById?
alshabiba68eb962014-09-24 20:34:13 -0700171 frp.removeRulesById(id, f);
172 }
173 }
174
175 @Override
176 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
alshabib1c319ff2014-10-04 20:29:09 -0700177 return store.getFlowRulesByAppId(id);
alshabib57044ba2014-09-16 15:58:01 -0700178 }
179
180 @Override
alshabib902d41b2014-10-07 16:52:05 -0700181 public Future<CompletedBatchOperation> applyBatch(
182 FlowRuleBatchOperation batch) {
183 Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches =
184 ArrayListMultimap.create();
alshabib193525b2014-10-08 18:58:03 -0700185 List<Future<CompletedBatchOperation>> futures = Lists.newArrayList();
alshabib902d41b2014-10-07 16:52:05 -0700186 for (FlowRuleBatchEntry fbe : batch.getOperations()) {
187 final FlowRule f = fbe.getTarget();
188 final Device device = deviceService.getDevice(f.deviceId());
189 final FlowRuleProvider frp = getProvider(device.providerId());
190 batches.put(frp, fbe);
191 switch (fbe.getOperator()) {
192 case ADD:
193 store.storeFlowRule(f);
194 break;
195 case REMOVE:
196 store.deleteFlowRule(f);
197 break;
198 case MODIFY:
199 default:
200 log.error("Batch operation type {} unsupported.", fbe.getOperator());
201 }
202 }
203 for (FlowRuleProvider provider : batches.keySet()) {
204 FlowRuleBatchOperation b =
205 new FlowRuleBatchOperation(batches.get(provider));
alshabib193525b2014-10-08 18:58:03 -0700206 Future<CompletedBatchOperation> future = provider.executeBatch(b);
alshabib902d41b2014-10-07 16:52:05 -0700207 futures.add(future);
208 }
alshabib193525b2014-10-08 18:58:03 -0700209 return new FlowRuleBatchFuture(futures, batches);
alshabib902d41b2014-10-07 16:52:05 -0700210 }
211
212 @Override
alshabib57044ba2014-09-16 15:58:01 -0700213 public void addListener(FlowRuleListener listener) {
214 listenerRegistry.addListener(listener);
215 }
216
217 @Override
218 public void removeListener(FlowRuleListener listener) {
219 listenerRegistry.removeListener(listener);
220 }
221
222 @Override
223 protected FlowRuleProviderService createProviderService(
224 FlowRuleProvider provider) {
225 return new InternalFlowRuleProviderService(provider);
226 }
227
228 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700229 extends AbstractProviderService<FlowRuleProvider>
230 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700231
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700232 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
233
alshabib57044ba2014-09-16 15:58:01 -0700234 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
235 super(provider);
236 }
237
238 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700239 public void flowRemoved(FlowEntry flowEntry) {
240 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700241 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700242 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700243 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700244 if (stored == null) {
alshabib1c319ff2014-10-04 20:29:09 -0700245 log.info("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700246 return;
247 }
alshabib1c319ff2014-10-04 20:29:09 -0700248 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700249 FlowRuleProvider frp = getProvider(device.providerId());
250 FlowRuleEvent event = null;
251 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700252 case ADDED:
253 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700254 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700255 break;
256 case PENDING_REMOVE:
257 case REMOVED:
258 event = store.removeFlowRule(stored);
259 break;
260 default:
261 break;
alshabib57044ba2014-09-16 15:58:01 -0700262
alshabiba68eb962014-09-24 20:34:13 -0700263 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700264 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700265 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700266 post(event);
267 }
alshabib57044ba2014-09-16 15:58:01 -0700268 }
269
alshabibba5ac482014-10-02 17:15:20 -0700270
alshabib1c319ff2014-10-04 20:29:09 -0700271 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700272 checkNotNull(flowRule, FLOW_RULE_NULL);
273 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700274 Device device = deviceService.getDevice(flowRule.deviceId());
275 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700276 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700277 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700278 case PENDING_REMOVE:
279 case REMOVED:
280 event = store.removeFlowRule(flowRule);
281 frp.removeFlowRule(flowRule);
282 break;
283 case ADDED:
284 case PENDING_ADD:
285 frp.applyFlowRule(flowRule);
286 break;
287 default:
288 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700289 }
290
alshabibbb42cad2014-09-25 11:43:05 -0700291 if (event != null) {
292 log.debug("Flow {} removed", flowRule);
293 post(event);
294 }
alshabib57044ba2014-09-16 15:58:01 -0700295
296 }
297
alshabibba5ac482014-10-02 17:15:20 -0700298
299 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700300 checkNotNull(flowRule, FLOW_RULE_NULL);
301 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700302 removeFlowRules(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700303 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700304 }
305
alshabibba5ac482014-10-02 17:15:20 -0700306
alshabib1c319ff2014-10-04 20:29:09 -0700307 private void flowAdded(FlowEntry flowEntry) {
308 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700309 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700310
alshabib1c319ff2014-10-04 20:29:09 -0700311 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700312
alshabib1c319ff2014-10-04 20:29:09 -0700313 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700314 if (event == null) {
315 log.debug("No flow store event generated.");
316 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700317 log.debug("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700318 post(event);
319 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700320 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700321 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700322 }
alshabib219ebaa2014-09-22 15:41:24 -0700323
alshabib57044ba2014-09-16 15:58:01 -0700324 }
325
alshabib1c319ff2014-10-04 20:29:09 -0700326 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
327 if (storedRule == null) {
328 return false;
329 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700330 final long timeout = storedRule.timeout() * 1000;
331 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700332 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700333 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700334 return true;
335 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700336 if (!lastSeen.containsKey(storedRule)) {
337 // checking for the first time
338 lastSeen.put(storedRule, storedRule.lastSeen());
339 // Use following if lastSeen attr. was removed.
340 //lastSeen.put(storedRule, currentTime);
341 }
342 Long last = lastSeen.get(storedRule);
343 if (last == null) {
344 // concurrently removed? let the liveness check fail
345 return false;
346 }
alshabib85c41972014-10-03 13:48:39 -0700347
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700348 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700349 return true;
350 }
351 return false;
alshabibba5ac482014-10-02 17:15:20 -0700352 }
353
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700354 // Posts the specified event to the local event dispatcher.
355 private void post(FlowRuleEvent event) {
356 if (event != null) {
357 eventDispatcher.post(event);
358 }
359 }
alshabib5c370ff2014-09-18 10:12:14 -0700360
361 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700362 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
363 List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
alshabibbb8b1282014-09-22 17:00:18 -0700364
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700365 for (FlowEntry rule : flowEntries) {
alshabiba7f7ca82014-09-22 11:41:23 -0700366 if (storedRules.remove(rule)) {
alshabib219ebaa2014-09-22 15:41:24 -0700367 // we both have the rule, let's update some info then.
alshabiba7f7ca82014-09-22 11:41:23 -0700368 flowAdded(rule);
369 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700370 // the device has a rule the store does not have
371 extraneousFlow(rule);
alshabiba7f7ca82014-09-22 11:41:23 -0700372 }
373 }
alshabib1c319ff2014-10-04 20:29:09 -0700374 for (FlowEntry rule : storedRules) {
alshabiba7f7ca82014-09-22 11:41:23 -0700375 // there are rules in the store that aren't on the switch
376 flowMissing(rule);
alshabib54ce5892014-09-23 17:50:51 -0700377
alshabiba7f7ca82014-09-22 11:41:23 -0700378 }
alshabib5c370ff2014-09-18 10:12:14 -0700379 }
alshabib57044ba2014-09-16 15:58:01 -0700380 }
381
tomc78acee2014-09-24 15:16:55 -0700382 // Store delegate to re-post events emitted from the store.
383 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
384 @Override
385 public void notify(FlowRuleEvent event) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700386 switch (event.type()) {
387 case RULE_ADD_REQUESTED:
388 applyFlowRulesToProviders(event.subject());
389 break;
390 case RULE_REMOVE_REQUESTED:
391 removeFlowRulesFromProviders(event.subject());
392 break;
393
394 case RULE_ADDED:
395 case RULE_REMOVED:
396 case RULE_UPDATED:
397 // only dispatch events related to switch
398 eventDispatcher.post(event);
399 break;
400 default:
401 break;
402 }
tomc78acee2014-09-24 15:16:55 -0700403 }
404 }
alshabib902d41b2014-10-07 16:52:05 -0700405
406 private class FlowRuleBatchFuture
407 implements Future<CompletedBatchOperation> {
408
alshabib193525b2014-10-08 18:58:03 -0700409 private final List<Future<CompletedBatchOperation>> futures;
410 private final Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches;
411 private final AtomicReference<BatchState> state;
412 private CompletedBatchOperation overall;
alshabib902d41b2014-10-07 16:52:05 -0700413
alshabib193525b2014-10-08 18:58:03 -0700414
415
416 public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures,
417 Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches) {
alshabib902d41b2014-10-07 16:52:05 -0700418 this.futures = futures;
alshabib193525b2014-10-08 18:58:03 -0700419 this.batches = batches;
420 state = new AtomicReference<FlowRuleManager.BatchState>();
421 state.set(BatchState.STARTED);
alshabib902d41b2014-10-07 16:52:05 -0700422 }
423
424 @Override
425 public boolean cancel(boolean mayInterruptIfRunning) {
alshabib193525b2014-10-08 18:58:03 -0700426 if (state.get() == BatchState.FINISHED) {
427 return false;
428 }
429 if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) {
430 return false;
431 }
432 cleanUpBatch();
433 for (Future<CompletedBatchOperation> f : futures) {
434 f.cancel(mayInterruptIfRunning);
435 }
436 return true;
alshabib902d41b2014-10-07 16:52:05 -0700437 }
438
439 @Override
440 public boolean isCancelled() {
alshabib193525b2014-10-08 18:58:03 -0700441 return state.get() == BatchState.CANCELLED;
alshabib902d41b2014-10-07 16:52:05 -0700442 }
443
444 @Override
445 public boolean isDone() {
alshabib193525b2014-10-08 18:58:03 -0700446 return state.get() == BatchState.FINISHED;
alshabib902d41b2014-10-07 16:52:05 -0700447 }
448
alshabib193525b2014-10-08 18:58:03 -0700449
alshabib902d41b2014-10-07 16:52:05 -0700450 @Override
451 public CompletedBatchOperation get() throws InterruptedException,
alshabib193525b2014-10-08 18:58:03 -0700452 ExecutionException {
453
454 if (isDone()) {
455 return overall;
alshabib902d41b2014-10-07 16:52:05 -0700456 }
alshabib193525b2014-10-08 18:58:03 -0700457
458 boolean success = true;
459 List<FlowEntry> failed = Lists.newLinkedList();
460 CompletedBatchOperation completed;
461 for (Future<CompletedBatchOperation> future : futures) {
462 completed = future.get();
alshabib3effd042014-10-17 12:00:31 -0700463 success = validateBatchOperation(failed, completed);
alshabib193525b2014-10-08 18:58:03 -0700464 }
465
466 return finalizeBatchOperation(success, failed);
467
alshabib902d41b2014-10-07 16:52:05 -0700468 }
469
470 @Override
471 public CompletedBatchOperation get(long timeout, TimeUnit unit)
472 throws InterruptedException, ExecutionException,
473 TimeoutException {
alshabib193525b2014-10-08 18:58:03 -0700474
475 if (isDone()) {
476 return overall;
477 }
478 boolean success = true;
479 List<FlowEntry> failed = Lists.newLinkedList();
480 CompletedBatchOperation completed;
alshabib902d41b2014-10-07 16:52:05 -0700481 long start = System.nanoTime();
482 long end = start + unit.toNanos(timeout);
alshabib193525b2014-10-08 18:58:03 -0700483
484 for (Future<CompletedBatchOperation> future : futures) {
alshabib902d41b2014-10-07 16:52:05 -0700485 long now = System.nanoTime();
486 long thisTimeout = end - now;
alshabib193525b2014-10-08 18:58:03 -0700487 completed = future.get(thisTimeout, TimeUnit.NANOSECONDS);
alshabib3effd042014-10-17 12:00:31 -0700488 success = validateBatchOperation(failed, completed);
alshabib902d41b2014-10-07 16:52:05 -0700489 }
alshabib193525b2014-10-08 18:58:03 -0700490 return finalizeBatchOperation(success, failed);
alshabib902d41b2014-10-07 16:52:05 -0700491 }
492
alshabib193525b2014-10-08 18:58:03 -0700493 private boolean validateBatchOperation(List<FlowEntry> failed,
alshabib3effd042014-10-17 12:00:31 -0700494 CompletedBatchOperation completed) {
alshabib193525b2014-10-08 18:58:03 -0700495
496 if (isCancelled()) {
497 throw new CancellationException();
498 }
499 if (!completed.isSuccess()) {
500 failed.addAll(completed.failedItems());
501 cleanUpBatch();
502 cancelAllSubBatches();
503 return false;
504 }
505 return true;
506 }
507
508 private void cancelAllSubBatches() {
509 for (Future<CompletedBatchOperation> f : futures) {
510 f.cancel(true);
511 }
512 }
513
514 private CompletedBatchOperation finalizeBatchOperation(boolean success,
515 List<FlowEntry> failed) {
alshabib26834582014-10-08 20:15:46 -0700516 synchronized (this) {
alshabib193525b2014-10-08 18:58:03 -0700517 if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) {
518 if (state.get() == BatchState.FINISHED) {
519 return overall;
520 }
521 throw new CancellationException();
522 }
523 overall = new CompletedBatchOperation(success, failed);
524 return overall;
525 }
526 }
527
528 private void cleanUpBatch() {
529 for (FlowRuleBatchEntry fbe : batches.values()) {
530 if (fbe.getOperator() == FlowRuleOperation.ADD ||
531 fbe.getOperator() == FlowRuleOperation.MODIFY) {
532 store.deleteFlowRule(fbe.getTarget());
533 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) {
alshabibcf369912014-10-13 14:16:42 -0700534 store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget()));
alshabib193525b2014-10-08 18:58:03 -0700535 store.storeFlowRule(fbe.getTarget());
536 }
537 }
538
539 }
alshabib902d41b2014-10-07 16:52:05 -0700540 }
541
542
alshabib193525b2014-10-08 18:58:03 -0700543
544
alshabib57044ba2014-09-16 15:58:01 -0700545}