blob: b49814f2fb1c106bb4df35807db26ab0eb24239f [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.impl;
alshabib57044ba2014-09-16 15:58:01 -070017
Marc De Leenheerde47caa2015-04-24 11:27:44 -070018import com.google.common.base.Strings;
Brian O'Connord12267c2015-02-17 18:17:08 -080019import com.google.common.collect.ArrayListMultimap;
20import com.google.common.collect.Iterables;
21import com.google.common.collect.Lists;
22import com.google.common.collect.Maps;
23import com.google.common.collect.Multimap;
24import com.google.common.collect.Sets;
alshabib57044ba2014-09-16 15:58:01 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070028import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
alshabib57044ba2014-09-16 15:58:01 -070030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.apache.felix.scr.annotations.Service;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070033import org.onlab.util.Tools;
34import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080036import org.onosproject.core.CoreService;
37import org.onosproject.core.IdGenerator;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.event.AbstractListenerRegistry;
39import org.onosproject.event.EventDeliveryService;
40import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.device.DeviceService;
43import org.onosproject.net.flow.CompletedBatchOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.flow.FlowEntry;
45import org.onosproject.net.flow.FlowRule;
46import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.flow.FlowRuleBatchEvent;
48import org.onosproject.net.flow.FlowRuleBatchOperation;
49import org.onosproject.net.flow.FlowRuleBatchRequest;
50import org.onosproject.net.flow.FlowRuleEvent;
51import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080052import org.onosproject.net.flow.FlowRuleOperation;
53import org.onosproject.net.flow.FlowRuleOperations;
54import org.onosproject.net.flow.FlowRuleOperationsContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.flow.FlowRuleProvider;
56import org.onosproject.net.flow.FlowRuleProviderRegistry;
57import org.onosproject.net.flow.FlowRuleProviderService;
58import org.onosproject.net.flow.FlowRuleService;
59import org.onosproject.net.flow.FlowRuleStore;
60import org.onosproject.net.flow.FlowRuleStoreDelegate;
61import org.onosproject.net.provider.AbstractProviderRegistry;
62import org.onosproject.net.provider.AbstractProviderService;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070063import org.osgi.service.component.ComponentContext;
alshabib57044ba2014-09-16 15:58:01 -070064import org.slf4j.Logger;
65
Brian O'Connord12267c2015-02-17 18:17:08 -080066import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070067import java.util.Dictionary;
Brian O'Connord12267c2015-02-17 18:17:08 -080068import java.util.List;
69import java.util.Map;
70import java.util.Set;
71import java.util.concurrent.ConcurrentHashMap;
72import java.util.concurrent.ExecutorService;
73import java.util.concurrent.Executors;
74import java.util.concurrent.atomic.AtomicBoolean;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080075
Thomas Vachuska9b2da212014-11-10 19:30:25 -080076import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connord12267c2015-02-17 18:17:08 -080077import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska9b2da212014-11-10 19:30:25 -080078import static org.slf4j.LoggerFactory.getLogger;
alshabiba7f7ca82014-09-22 11:41:23 -070079
tome4729872014-09-23 00:37:37 -070080/**
81 * Provides implementation of the flow NB & SB APIs.
82 */
Brian O'Connord12267c2015-02-17 18:17:08 -080083@Component(immediate = true, enabled = true)
alshabib57044ba2014-09-16 15:58:01 -070084@Service
tom202175a2014-09-19 19:00:11 -070085public class FlowRuleManager
tom9b4030d2014-10-06 10:39:03 -070086 extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
87 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070088
Sho SHIMIZU183b12fd2015-01-20 14:56:39 -080089 enum BatchState { STARTED, FINISHED, CANCELLED }
alshabib193525b2014-10-08 18:58:03 -070090
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070091 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -070092 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
93
94 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
95 label = "Allow flow rules in switch not installed by ONOS")
96 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
97
alshabib57044ba2014-09-16 15:58:01 -070098 private final Logger log = getLogger(getClass());
99
100 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
tom9b4030d2014-10-06 10:39:03 -0700101 listenerRegistry = new AbstractListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -0700102
alshabibbb42cad2014-09-25 11:43:05 -0700103 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -0700104
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800105 protected ExecutorService deviceInstallers =
Brian O'Connord12267c2015-02-17 18:17:08 -0800106 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800107
108 protected ExecutorService operationsService =
Brian O'Connord12267c2015-02-17 18:17:08 -0800109 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800110
111 private IdGenerator idGenerator;
112
Brian O'Connord12267c2015-02-17 18:17:08 -0800113 private Map<Long, FlowOperationsProcessor> pendingFlowOperations
114 = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700115
tombe988312014-09-19 18:38:47 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
117 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700118
alshabib57044ba2014-09-16 15:58:01 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700120 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -0700121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700123 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700124
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected CoreService coreService;
127
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800130
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700131 @Activate
132 public void activate(ComponentContext context) {
133 cfgService.registerProperties(getClass());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800134 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
135
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700136 modified(context);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800137
tomc78acee2014-09-24 15:16:55 -0700138 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700139 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
140 log.info("Started");
141 }
142
143 @Deactivate
144 public void deactivate() {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700145 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800146 deviceInstallers.shutdownNow();
147 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700148 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700149 eventDispatcher.removeSink(FlowRuleEvent.class);
150 log.info("Stopped");
151 }
152
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700153 @Modified
154 public void modified(ComponentContext context) {
155 if (context == null) {
156 return;
157 }
158
159 Dictionary<?, ?> properties = context.getProperties();
160
161 String s = Tools.get(properties, "allowExtraneousRules");
162 allowExtraneousRules = Strings.isNullOrEmpty(s) ? ALLOW_EXTRANEOUS_RULES : Boolean.valueOf(s);
163
164 if (allowExtraneousRules) {
165 log.info("Allowing flow rules not installed by ONOS");
166 }
167 }
168
alshabib57044ba2014-09-16 15:58:01 -0700169 @Override
tom9b4030d2014-10-06 10:39:03 -0700170 public int getFlowRuleCount() {
171 return store.getFlowRuleCount();
172 }
173
174 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700175 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700176 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700177 }
178
179 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700180 public void applyFlowRules(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800181 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700182 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800183 builder.add(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700184 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800185 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700186 }
187
188 @Override
189 public void removeFlowRules(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800190 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700191 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800192 builder.remove(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700193 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800194 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700195 }
alshabib57044ba2014-09-16 15:58:01 -0700196
alshabiba68eb962014-09-24 20:34:13 -0700197 @Override
198 public void removeFlowRulesById(ApplicationId id) {
Madan Jampani6a456162014-10-24 11:36:17 -0700199 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700200 }
201
202 @Override
203 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Madan Jampani6a456162014-10-24 11:36:17 -0700204 Set<FlowRule> flowEntries = Sets.newHashSet();
205 for (Device d : deviceService.getDevices()) {
206 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
207 if (flowEntry.appId() == id.id()) {
208 flowEntries.add(flowEntry);
209 }
210 }
211 }
212 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700213 }
214
215 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800216 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
217 Set<FlowRule> matches = Sets.newHashSet();
218 long toLookUp = ((long) appId.id() << 16) | groupId;
219 for (Device d : deviceService.getDevices()) {
220 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
221 if ((flowEntry.id().value() >>> 32) == toLookUp) {
222 matches.add(flowEntry);
223 }
224 }
225 }
226 return matches;
227 }
228
229 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800230 public void apply(FlowRuleOperations ops) {
231 operationsService.submit(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700232 }
233
234 @Override
alshabib57044ba2014-09-16 15:58:01 -0700235 public void addListener(FlowRuleListener listener) {
236 listenerRegistry.addListener(listener);
237 }
238
239 @Override
240 public void removeListener(FlowRuleListener listener) {
241 listenerRegistry.removeListener(listener);
242 }
243
244 @Override
245 protected FlowRuleProviderService createProviderService(
246 FlowRuleProvider provider) {
247 return new InternalFlowRuleProviderService(provider);
248 }
249
250 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700251 extends AbstractProviderService<FlowRuleProvider>
252 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700253
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700254 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
255
alshabib57044ba2014-09-16 15:58:01 -0700256 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
257 super(provider);
258 }
259
260 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700261 public void flowRemoved(FlowEntry flowEntry) {
262 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700263 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700264 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700265 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700266 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800267 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700268 return;
269 }
alshabib1c319ff2014-10-04 20:29:09 -0700270 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700271 FlowRuleProvider frp = getProvider(device.providerId());
272 FlowRuleEvent event = null;
273 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700274 case ADDED:
275 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700276 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700277 break;
278 case PENDING_REMOVE:
279 case REMOVED:
280 event = store.removeFlowRule(stored);
281 break;
282 default:
283 break;
alshabib57044ba2014-09-16 15:58:01 -0700284
alshabiba68eb962014-09-24 20:34:13 -0700285 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700286 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700287 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700288 post(event);
289 }
alshabib57044ba2014-09-16 15:58:01 -0700290 }
291
alshabibba5ac482014-10-02 17:15:20 -0700292
alshabib1c319ff2014-10-04 20:29:09 -0700293 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700294 checkNotNull(flowRule, FLOW_RULE_NULL);
295 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700296 Device device = deviceService.getDevice(flowRule.deviceId());
297 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700298 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700299 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700300 case PENDING_REMOVE:
301 case REMOVED:
302 event = store.removeFlowRule(flowRule);
303 frp.removeFlowRule(flowRule);
304 break;
305 case ADDED:
306 case PENDING_ADD:
307 frp.applyFlowRule(flowRule);
308 break;
309 default:
310 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700311 }
312
alshabibbb42cad2014-09-25 11:43:05 -0700313 if (event != null) {
314 log.debug("Flow {} removed", flowRule);
315 post(event);
316 }
alshabib57044ba2014-09-16 15:58:01 -0700317
318 }
319
alshabibba5ac482014-10-02 17:15:20 -0700320
321 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700322 checkNotNull(flowRule, FLOW_RULE_NULL);
323 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700324 FlowRuleProvider frp = getProvider(flowRule.deviceId());
325 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700326 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700327 }
328
alshabibba5ac482014-10-02 17:15:20 -0700329
alshabib1c319ff2014-10-04 20:29:09 -0700330 private void flowAdded(FlowEntry flowEntry) {
331 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700332 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700333
alshabib1c319ff2014-10-04 20:29:09 -0700334 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700335
alshabib1c319ff2014-10-04 20:29:09 -0700336 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700337 if (event == null) {
338 log.debug("No flow store event generated.");
339 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800340 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700341 post(event);
342 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700343 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800344 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700345 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700346 }
alshabib219ebaa2014-09-22 15:41:24 -0700347
alshabib57044ba2014-09-16 15:58:01 -0700348 }
349
alshabib1c319ff2014-10-04 20:29:09 -0700350 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
351 if (storedRule == null) {
352 return false;
353 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700354 if (storedRule.isPermanent()) {
355 return true;
356 }
357
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700358 final long timeout = storedRule.timeout() * 1000;
359 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700360 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700361 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700362 return true;
363 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700364 if (!lastSeen.containsKey(storedRule)) {
365 // checking for the first time
366 lastSeen.put(storedRule, storedRule.lastSeen());
367 // Use following if lastSeen attr. was removed.
368 //lastSeen.put(storedRule, currentTime);
369 }
370 Long last = lastSeen.get(storedRule);
371 if (last == null) {
372 // concurrently removed? let the liveness check fail
373 return false;
374 }
alshabib85c41972014-10-03 13:48:39 -0700375
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700376 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700377 return true;
378 }
379 return false;
alshabibba5ac482014-10-02 17:15:20 -0700380 }
381
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700382 // Posts the specified event to the local event dispatcher.
383 private void post(FlowRuleEvent event) {
384 if (event != null) {
385 eventDispatcher.post(event);
386 }
387 }
alshabib5c370ff2014-09-18 10:12:14 -0700388
389 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700390 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
alshabib64def642014-12-02 23:27:37 -0800391 Set<FlowEntry> storedRules = Sets.newHashSet(store.getFlowEntries(deviceId));
Saurav Dasfa2fa932015-03-03 11:29:48 -0800392 for (FlowEntry rule : flowEntries) {
393 try {
394 if (storedRules.remove(rule)) {
395 // we both have the rule, let's update some info then.
396 flowAdded(rule);
397 } else {
398 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700399 if (!allowExtraneousRules) {
400 extraneousFlow(rule);
401 }
alshabib93cb57f2015-02-12 17:43:26 -0800402 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800403 } catch (Throwable e) {
404 log.debug("Can't process added or extra rule {}", e.getMessage());
405 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800406 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800407 }
408 for (FlowEntry rule : storedRules) {
409 try {
410 // there are rules in the store that aren't on the switch
411 flowMissing(rule);
412 } catch (Throwable e) {
413 log.debug("Can't add missing flow rule {}", e.getMessage());
414 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800415 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800416 }
alshabib93cb57f2015-02-12 17:43:26 -0800417
alshabib5c370ff2014-09-18 10:12:14 -0700418 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800419
420 @Override
421 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
422 store.batchOperationComplete(FlowRuleBatchEvent.completed(
423 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
424 operation
425 ));
426 }
alshabib57044ba2014-09-16 15:58:01 -0700427 }
428
tomc78acee2014-09-24 15:16:55 -0700429 // Store delegate to re-post events emitted from the store.
430 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800431
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800432
Madan Jampani117aaae2014-10-23 10:04:05 -0700433 // TODO: Right now we only dispatch events at individual flowEntry level.
434 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700435 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700436 public void notify(FlowRuleBatchEvent event) {
437 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700438 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700439 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800440 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800441 request.ops().stream().forEach(
442 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800443 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700444
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800445 case ADD:
446 eventDispatcher.post(
447 new FlowRuleEvent(
448 FlowRuleEvent.Type.RULE_ADD_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800449 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800450 break;
451 case REMOVE:
452 eventDispatcher.post(
453 new FlowRuleEvent(
454 FlowRuleEvent.Type.RULE_REMOVE_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800455 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800456 break;
457 case MODIFY:
458 //TODO: do something here when the time comes.
459 break;
460 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800461 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800462 }
463 }
464 );
465
466 DeviceId deviceId = event.deviceId();
467
468 FlowRuleBatchOperation batchOperation =
469 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700470
Madan Jampani117aaae2014-10-23 10:04:05 -0700471 FlowRuleProvider flowRuleProvider =
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800472 getProvider(deviceId);
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800473
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800474 flowRuleProvider.executeBatch(batchOperation);
475
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800476 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700477
Madan Jampani117aaae2014-10-23 10:04:05 -0700478 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800479
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800480 FlowOperationsProcessor fops = pendingFlowOperations.remove(
481 event.subject().batchId());
482 if (event.result().isSuccess()) {
483 if (fops != null) {
484 fops.satisfy(event.deviceId());
485 }
486 } else {
487 fops.fail(event.deviceId(), event.result().failedItems());
488 }
489
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700490 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800491
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700492 default:
493 break;
494 }
tomc78acee2014-09-24 15:16:55 -0700495 }
496 }
alshabib902d41b2014-10-07 16:52:05 -0700497
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800498 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700499
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800500 private final List<Set<FlowRuleOperation>> stages;
501 private final FlowRuleOperationsContext context;
502 private final FlowRuleOperations fops;
503 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700504
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800505 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700506
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800507 public FlowOperationsProcessor(FlowRuleOperations ops) {
alshabib902d41b2014-10-07 16:52:05 -0700508
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800509 this.stages = Lists.newArrayList(ops.stages());
510 this.context = ops.callback();
511 this.fops = ops;
512 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700513
alshabib193525b2014-10-08 18:58:03 -0700514
alshabib902d41b2014-10-07 16:52:05 -0700515 }
516
517 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800518 public void run() {
519 if (stages.size() > 0) {
520 process(stages.remove(0));
521 } else if (!hasFailed.get() && context != null) {
522 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700523 }
524 }
525
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800526 private void process(Set<FlowRuleOperation> ops) {
527 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
528 ArrayListMultimap.create();
529
530 FlowRuleBatchEntry fbe;
531 for (FlowRuleOperation flowRuleOperation : ops) {
532 switch (flowRuleOperation.type()) {
533 // FIXME: Brian needs imagination when creating class names.
534 case ADD:
535 fbe = new FlowRuleBatchEntry(
536 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
537 break;
538 case MODIFY:
539 fbe = new FlowRuleBatchEntry(
540 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
541 break;
542 case REMOVE:
543 fbe = new FlowRuleBatchEntry(
544 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
545 break;
546 default:
547 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
548 }
549 pendingDevices.add(flowRuleOperation.rule().deviceId());
550 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
551 }
552
553
554 for (DeviceId deviceId : perDeviceBatches.keySet()) {
555 Long id = idGenerator.getNewId();
556 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
557 deviceId, id);
558 pendingFlowOperations.put(id, this);
559 deviceInstallers.submit(new Runnable() {
560 @Override
561 public void run() {
562 store.storeBatch(b);
alshabib193525b2014-10-08 18:58:03 -0700563 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800564 });
alshabib193525b2014-10-08 18:58:03 -0700565 }
566 }
567
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800568 public void satisfy(DeviceId devId) {
569 pendingDevices.remove(devId);
570 if (pendingDevices.isEmpty()) {
571 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700572 }
alshabib193525b2014-10-08 18:58:03 -0700573 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800574
575
576
577 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
578 hasFailed.set(true);
579 pendingDevices.remove(devId);
580 if (pendingDevices.isEmpty()) {
581 operationsService.submit(this);
582 }
583
584 if (context != null) {
585 final FlowRuleOperations.Builder failedOpsBuilder =
586 FlowRuleOperations.builder();
587 failures.stream().forEach(failedOpsBuilder::add);
588
589 context.onError(failedOpsBuilder.build());
590 }
591 }
592
alshabib902d41b2014-10-07 16:52:05 -0700593 }
alshabib57044ba2014-09-16 15:58:01 -0700594}