blob: 01514d4eef997a77d6ea8cb9d7157ef119394cf9 [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();
alshabib2374fc92014-10-22 11:03:23 -0700302 FlowRuleProvider frp = getProvider(flowRule.deviceId());
303 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700304 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700305 }
306
alshabibba5ac482014-10-02 17:15:20 -0700307
alshabib1c319ff2014-10-04 20:29:09 -0700308 private void flowAdded(FlowEntry flowEntry) {
309 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700310 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700311
alshabib1c319ff2014-10-04 20:29:09 -0700312 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700313
alshabib1c319ff2014-10-04 20:29:09 -0700314 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700315 if (event == null) {
316 log.debug("No flow store event generated.");
317 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700318 log.debug("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700319 post(event);
320 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700321 } else {
alshabib1c319ff2014-10-04 20:29:09 -0700322 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700323 }
alshabib219ebaa2014-09-22 15:41:24 -0700324
alshabib57044ba2014-09-16 15:58:01 -0700325 }
326
alshabib1c319ff2014-10-04 20:29:09 -0700327 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
328 if (storedRule == null) {
329 return false;
330 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700331 if (storedRule.isPermanent()) {
332 return true;
333 }
334
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700335 final long timeout = storedRule.timeout() * 1000;
336 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700337 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700338 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700339 return true;
340 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700341 if (!lastSeen.containsKey(storedRule)) {
342 // checking for the first time
343 lastSeen.put(storedRule, storedRule.lastSeen());
344 // Use following if lastSeen attr. was removed.
345 //lastSeen.put(storedRule, currentTime);
346 }
347 Long last = lastSeen.get(storedRule);
348 if (last == null) {
349 // concurrently removed? let the liveness check fail
350 return false;
351 }
alshabib85c41972014-10-03 13:48:39 -0700352
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700353 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700354 return true;
355 }
356 return false;
alshabibba5ac482014-10-02 17:15:20 -0700357 }
358
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700359 // Posts the specified event to the local event dispatcher.
360 private void post(FlowRuleEvent event) {
361 if (event != null) {
362 eventDispatcher.post(event);
363 }
364 }
alshabib5c370ff2014-09-18 10:12:14 -0700365
366 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700367 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
368 List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
alshabibbb8b1282014-09-22 17:00:18 -0700369
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700370 for (FlowEntry rule : flowEntries) {
alshabiba7f7ca82014-09-22 11:41:23 -0700371 if (storedRules.remove(rule)) {
alshabib219ebaa2014-09-22 15:41:24 -0700372 // we both have the rule, let's update some info then.
alshabiba7f7ca82014-09-22 11:41:23 -0700373 flowAdded(rule);
374 } else {
alshabib219ebaa2014-09-22 15:41:24 -0700375 // the device has a rule the store does not have
376 extraneousFlow(rule);
alshabiba7f7ca82014-09-22 11:41:23 -0700377 }
378 }
alshabib1c319ff2014-10-04 20:29:09 -0700379 for (FlowEntry rule : storedRules) {
alshabiba7f7ca82014-09-22 11:41:23 -0700380 // there are rules in the store that aren't on the switch
381 flowMissing(rule);
alshabib54ce5892014-09-23 17:50:51 -0700382
alshabiba7f7ca82014-09-22 11:41:23 -0700383 }
alshabib5c370ff2014-09-18 10:12:14 -0700384 }
alshabib57044ba2014-09-16 15:58:01 -0700385 }
386
tomc78acee2014-09-24 15:16:55 -0700387 // Store delegate to re-post events emitted from the store.
388 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
389 @Override
390 public void notify(FlowRuleEvent event) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700391 switch (event.type()) {
392 case RULE_ADD_REQUESTED:
393 applyFlowRulesToProviders(event.subject());
394 break;
395 case RULE_REMOVE_REQUESTED:
396 removeFlowRulesFromProviders(event.subject());
397 break;
398
399 case RULE_ADDED:
400 case RULE_REMOVED:
401 case RULE_UPDATED:
402 // only dispatch events related to switch
403 eventDispatcher.post(event);
404 break;
405 default:
406 break;
407 }
tomc78acee2014-09-24 15:16:55 -0700408 }
409 }
alshabib902d41b2014-10-07 16:52:05 -0700410
411 private class FlowRuleBatchFuture
412 implements Future<CompletedBatchOperation> {
413
alshabib193525b2014-10-08 18:58:03 -0700414 private final List<Future<CompletedBatchOperation>> futures;
415 private final Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches;
416 private final AtomicReference<BatchState> state;
417 private CompletedBatchOperation overall;
alshabib902d41b2014-10-07 16:52:05 -0700418
alshabib193525b2014-10-08 18:58:03 -0700419
420
421 public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures,
422 Multimap<FlowRuleProvider, FlowRuleBatchEntry> batches) {
alshabib902d41b2014-10-07 16:52:05 -0700423 this.futures = futures;
alshabib193525b2014-10-08 18:58:03 -0700424 this.batches = batches;
425 state = new AtomicReference<FlowRuleManager.BatchState>();
426 state.set(BatchState.STARTED);
alshabib902d41b2014-10-07 16:52:05 -0700427 }
428
429 @Override
430 public boolean cancel(boolean mayInterruptIfRunning) {
alshabib193525b2014-10-08 18:58:03 -0700431 if (state.get() == BatchState.FINISHED) {
432 return false;
433 }
434 if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) {
435 return false;
436 }
437 cleanUpBatch();
438 for (Future<CompletedBatchOperation> f : futures) {
439 f.cancel(mayInterruptIfRunning);
440 }
441 return true;
alshabib902d41b2014-10-07 16:52:05 -0700442 }
443
444 @Override
445 public boolean isCancelled() {
alshabib193525b2014-10-08 18:58:03 -0700446 return state.get() == BatchState.CANCELLED;
alshabib902d41b2014-10-07 16:52:05 -0700447 }
448
449 @Override
450 public boolean isDone() {
alshabib193525b2014-10-08 18:58:03 -0700451 return state.get() == BatchState.FINISHED;
alshabib902d41b2014-10-07 16:52:05 -0700452 }
453
alshabib193525b2014-10-08 18:58:03 -0700454
alshabib902d41b2014-10-07 16:52:05 -0700455 @Override
456 public CompletedBatchOperation get() throws InterruptedException,
alshabib193525b2014-10-08 18:58:03 -0700457 ExecutionException {
458
459 if (isDone()) {
460 return overall;
alshabib902d41b2014-10-07 16:52:05 -0700461 }
alshabib193525b2014-10-08 18:58:03 -0700462
463 boolean success = true;
464 List<FlowEntry> failed = Lists.newLinkedList();
465 CompletedBatchOperation completed;
466 for (Future<CompletedBatchOperation> future : futures) {
467 completed = future.get();
alshabib3effd042014-10-17 12:00:31 -0700468 success = validateBatchOperation(failed, completed);
alshabib193525b2014-10-08 18:58:03 -0700469 }
470
471 return finalizeBatchOperation(success, failed);
472
alshabib902d41b2014-10-07 16:52:05 -0700473 }
474
475 @Override
476 public CompletedBatchOperation get(long timeout, TimeUnit unit)
477 throws InterruptedException, ExecutionException,
478 TimeoutException {
alshabib193525b2014-10-08 18:58:03 -0700479
480 if (isDone()) {
481 return overall;
482 }
483 boolean success = true;
484 List<FlowEntry> failed = Lists.newLinkedList();
485 CompletedBatchOperation completed;
alshabib902d41b2014-10-07 16:52:05 -0700486 long start = System.nanoTime();
487 long end = start + unit.toNanos(timeout);
alshabib193525b2014-10-08 18:58:03 -0700488
489 for (Future<CompletedBatchOperation> future : futures) {
alshabib902d41b2014-10-07 16:52:05 -0700490 long now = System.nanoTime();
491 long thisTimeout = end - now;
alshabib193525b2014-10-08 18:58:03 -0700492 completed = future.get(thisTimeout, TimeUnit.NANOSECONDS);
alshabib3effd042014-10-17 12:00:31 -0700493 success = validateBatchOperation(failed, completed);
alshabib902d41b2014-10-07 16:52:05 -0700494 }
alshabib193525b2014-10-08 18:58:03 -0700495 return finalizeBatchOperation(success, failed);
alshabib902d41b2014-10-07 16:52:05 -0700496 }
497
alshabib193525b2014-10-08 18:58:03 -0700498 private boolean validateBatchOperation(List<FlowEntry> failed,
alshabib3effd042014-10-17 12:00:31 -0700499 CompletedBatchOperation completed) {
alshabib193525b2014-10-08 18:58:03 -0700500
501 if (isCancelled()) {
502 throw new CancellationException();
503 }
504 if (!completed.isSuccess()) {
505 failed.addAll(completed.failedItems());
506 cleanUpBatch();
507 cancelAllSubBatches();
508 return false;
509 }
510 return true;
511 }
512
513 private void cancelAllSubBatches() {
514 for (Future<CompletedBatchOperation> f : futures) {
515 f.cancel(true);
516 }
517 }
518
519 private CompletedBatchOperation finalizeBatchOperation(boolean success,
520 List<FlowEntry> failed) {
alshabib26834582014-10-08 20:15:46 -0700521 synchronized (this) {
alshabib193525b2014-10-08 18:58:03 -0700522 if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) {
523 if (state.get() == BatchState.FINISHED) {
524 return overall;
525 }
526 throw new CancellationException();
527 }
528 overall = new CompletedBatchOperation(success, failed);
529 return overall;
530 }
531 }
532
533 private void cleanUpBatch() {
534 for (FlowRuleBatchEntry fbe : batches.values()) {
535 if (fbe.getOperator() == FlowRuleOperation.ADD ||
536 fbe.getOperator() == FlowRuleOperation.MODIFY) {
537 store.deleteFlowRule(fbe.getTarget());
538 } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) {
alshabibcf369912014-10-13 14:16:42 -0700539 store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget()));
alshabib193525b2014-10-08 18:58:03 -0700540 store.storeFlowRule(fbe.getTarget());
541 }
542 }
543
544 }
alshabib902d41b2014-10-07 16:52:05 -0700545 }
546
547
alshabib193525b2014-10-08 18:58:03 -0700548
549
alshabib57044ba2014-09-16 15:58:01 -0700550}