blob: b92067fc7e5bba998796090230c08f8798a895b6 [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;
Changhoon Yoon541ef712015-05-23 17:18:34 +090038import org.onosproject.core.Permission;
Simon Huntff663742015-05-14 13:33:05 -070039import org.onosproject.event.ListenerRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.event.EventDeliveryService;
41import org.onosproject.net.Device;
42import org.onosproject.net.DeviceId;
43import org.onosproject.net.device.DeviceService;
44import org.onosproject.net.flow.CompletedBatchOperation;
Charles M.C. Chan1229eca2015-05-18 06:27:52 +080045import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.flow.FlowEntry;
47import org.onosproject.net.flow.FlowRule;
48import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.flow.FlowRuleBatchEvent;
50import org.onosproject.net.flow.FlowRuleBatchOperation;
51import org.onosproject.net.flow.FlowRuleBatchRequest;
52import org.onosproject.net.flow.FlowRuleEvent;
53import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080054import org.onosproject.net.flow.FlowRuleOperation;
55import org.onosproject.net.flow.FlowRuleOperations;
56import org.onosproject.net.flow.FlowRuleOperationsContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.flow.FlowRuleProvider;
58import org.onosproject.net.flow.FlowRuleProviderRegistry;
59import org.onosproject.net.flow.FlowRuleProviderService;
60import org.onosproject.net.flow.FlowRuleService;
61import org.onosproject.net.flow.FlowRuleStore;
62import org.onosproject.net.flow.FlowRuleStoreDelegate;
63import org.onosproject.net.provider.AbstractProviderRegistry;
64import org.onosproject.net.provider.AbstractProviderService;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070065import org.osgi.service.component.ComponentContext;
alshabib57044ba2014-09-16 15:58:01 -070066import org.slf4j.Logger;
67
Brian O'Connord12267c2015-02-17 18:17:08 -080068import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070069import java.util.Dictionary;
Brian O'Connord12267c2015-02-17 18:17:08 -080070import java.util.List;
71import java.util.Map;
72import java.util.Set;
73import java.util.concurrent.ConcurrentHashMap;
74import java.util.concurrent.ExecutorService;
75import java.util.concurrent.Executors;
76import java.util.concurrent.atomic.AtomicBoolean;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080077
Thomas Vachuska9b2da212014-11-10 19:30:25 -080078import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connord12267c2015-02-17 18:17:08 -080079import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska9b2da212014-11-10 19:30:25 -080080import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090081import static org.onosproject.security.AppGuard.checkPermission;
82
alshabiba7f7ca82014-09-22 11:41:23 -070083
tome4729872014-09-23 00:37:37 -070084/**
85 * Provides implementation of the flow NB & SB APIs.
86 */
Brian O'Connord12267c2015-02-17 18:17:08 -080087@Component(immediate = true, enabled = true)
alshabib57044ba2014-09-16 15:58:01 -070088@Service
tom202175a2014-09-19 19:00:11 -070089public class FlowRuleManager
tom9b4030d2014-10-06 10:39:03 -070090 extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
91 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070092
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070093 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -070094 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
95
96 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
97 label = "Allow flow rules in switch not installed by ONOS")
98 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
99
alshabib57044ba2014-09-16 15:58:01 -0700100 private final Logger log = getLogger(getClass());
101
Simon Huntff663742015-05-14 13:33:05 -0700102 private final ListenerRegistry<FlowRuleEvent, FlowRuleListener>
103 listenerRegistry = new ListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -0700104
alshabibbb42cad2014-09-25 11:43:05 -0700105 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -0700106
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800107 protected ExecutorService deviceInstallers =
Brian O'Connord12267c2015-02-17 18:17:08 -0800108 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800109
110 protected ExecutorService operationsService =
Brian O'Connord12267c2015-02-17 18:17:08 -0800111 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800112
113 private IdGenerator idGenerator;
114
Brian O'Connord12267c2015-02-17 18:17:08 -0800115 private Map<Long, FlowOperationsProcessor> pendingFlowOperations
116 = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700117
tombe988312014-09-19 18:38:47 -0700118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
119 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700120
alshabib57044ba2014-09-16 15:58:01 -0700121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700122 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700125 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700126
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected CoreService coreService;
129
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800132
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700133 @Activate
134 public void activate(ComponentContext context) {
135 cfgService.registerProperties(getClass());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800136 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
137
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700138 modified(context);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800139
tomc78acee2014-09-24 15:16:55 -0700140 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700141 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
142 log.info("Started");
143 }
144
145 @Deactivate
146 public void deactivate() {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700147 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800148 deviceInstallers.shutdownNow();
149 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700150 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700151 eventDispatcher.removeSink(FlowRuleEvent.class);
152 log.info("Stopped");
153 }
154
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700155 @Modified
156 public void modified(ComponentContext context) {
157 if (context == null) {
158 return;
159 }
160
161 Dictionary<?, ?> properties = context.getProperties();
162
163 String s = Tools.get(properties, "allowExtraneousRules");
164 allowExtraneousRules = Strings.isNullOrEmpty(s) ? ALLOW_EXTRANEOUS_RULES : Boolean.valueOf(s);
165
166 if (allowExtraneousRules) {
167 log.info("Allowing flow rules not installed by ONOS");
168 }
169 }
170
alshabib57044ba2014-09-16 15:58:01 -0700171 @Override
tom9b4030d2014-10-06 10:39:03 -0700172 public int getFlowRuleCount() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900173 checkPermission(Permission.FLOWRULE_READ);
174
tom9b4030d2014-10-06 10:39:03 -0700175 return store.getFlowRuleCount();
176 }
177
178 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700179 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900180 checkPermission(Permission.FLOWRULE_READ);
181
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700182 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700183 }
184
185 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700186 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900187 checkPermission(Permission.FLOWRULE_WRITE);
188
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800189 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700190 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800191 builder.add(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700192 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800193 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700194 }
195
196 @Override
197 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900198 checkPermission(Permission.FLOWRULE_WRITE);
199
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800200 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700201 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800202 builder.remove(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700203 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800204 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700205 }
alshabib57044ba2014-09-16 15:58:01 -0700206
alshabiba68eb962014-09-24 20:34:13 -0700207 @Override
208 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900209 checkPermission(Permission.FLOWRULE_WRITE);
210
Madan Jampani6a456162014-10-24 11:36:17 -0700211 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700212 }
213
214 @Override
215 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900216 checkPermission(Permission.FLOWRULE_READ);
217
Madan Jampani6a456162014-10-24 11:36:17 -0700218 Set<FlowRule> flowEntries = Sets.newHashSet();
219 for (Device d : deviceService.getDevices()) {
220 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
221 if (flowEntry.appId() == id.id()) {
222 flowEntries.add(flowEntry);
223 }
224 }
225 }
226 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700227 }
228
229 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800230 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900231 checkPermission(Permission.FLOWRULE_READ);
232
alshabibaa7e7de2014-11-12 19:20:44 -0800233 Set<FlowRule> matches = Sets.newHashSet();
234 long toLookUp = ((long) appId.id() << 16) | groupId;
235 for (Device d : deviceService.getDevices()) {
236 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
237 if ((flowEntry.id().value() >>> 32) == toLookUp) {
238 matches.add(flowEntry);
239 }
240 }
241 }
242 return matches;
243 }
244
245 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800246 public void apply(FlowRuleOperations ops) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900247 checkPermission(Permission.FLOWRULE_WRITE);
248
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800249 operationsService.submit(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700250 }
251
252 @Override
alshabib57044ba2014-09-16 15:58:01 -0700253 public void addListener(FlowRuleListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900254 checkPermission(Permission.FLOWRULE_EVENT);
255
alshabib57044ba2014-09-16 15:58:01 -0700256 listenerRegistry.addListener(listener);
257 }
258
259 @Override
260 public void removeListener(FlowRuleListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900261 checkPermission(Permission.FLOWRULE_EVENT);
262
alshabib57044ba2014-09-16 15:58:01 -0700263 listenerRegistry.removeListener(listener);
264 }
265
266 @Override
267 protected FlowRuleProviderService createProviderService(
268 FlowRuleProvider provider) {
269 return new InternalFlowRuleProviderService(provider);
270 }
271
272 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700273 extends AbstractProviderService<FlowRuleProvider>
274 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700275
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700276 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
277
alshabib57044ba2014-09-16 15:58:01 -0700278 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
279 super(provider);
280 }
281
282 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700283 public void flowRemoved(FlowEntry flowEntry) {
284 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700285 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700286 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700287 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700288 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800289 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700290 return;
291 }
alshabib1c319ff2014-10-04 20:29:09 -0700292 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700293 FlowRuleProvider frp = getProvider(device.providerId());
294 FlowRuleEvent event = null;
295 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700296 case ADDED:
297 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700298 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700299 break;
300 case PENDING_REMOVE:
301 case REMOVED:
302 event = store.removeFlowRule(stored);
303 break;
304 default:
305 break;
alshabib57044ba2014-09-16 15:58:01 -0700306
alshabiba68eb962014-09-24 20:34:13 -0700307 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700308 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700309 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700310 post(event);
311 }
alshabib57044ba2014-09-16 15:58:01 -0700312 }
313
alshabibba5ac482014-10-02 17:15:20 -0700314
alshabib1c319ff2014-10-04 20:29:09 -0700315 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700316 checkNotNull(flowRule, FLOW_RULE_NULL);
317 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700318 Device device = deviceService.getDevice(flowRule.deviceId());
319 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700320 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700321 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700322 case PENDING_REMOVE:
323 case REMOVED:
324 event = store.removeFlowRule(flowRule);
325 frp.removeFlowRule(flowRule);
326 break;
327 case ADDED:
328 case PENDING_ADD:
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800329 try {
330 frp.applyFlowRule(flowRule);
331 } catch (UnsupportedOperationException e) {
332 log.warn(e.getMessage());
333 if (flowRule instanceof DefaultFlowEntry) {
334 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
335 }
336 }
tom9b4030d2014-10-06 10:39:03 -0700337 break;
338 default:
339 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700340 }
341
alshabibbb42cad2014-09-25 11:43:05 -0700342 if (event != null) {
343 log.debug("Flow {} removed", flowRule);
344 post(event);
345 }
alshabib57044ba2014-09-16 15:58:01 -0700346
347 }
348
alshabibba5ac482014-10-02 17:15:20 -0700349
350 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700351 checkNotNull(flowRule, FLOW_RULE_NULL);
352 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700353 FlowRuleProvider frp = getProvider(flowRule.deviceId());
354 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700355 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700356 }
357
alshabibba5ac482014-10-02 17:15:20 -0700358
alshabib1c319ff2014-10-04 20:29:09 -0700359 private void flowAdded(FlowEntry flowEntry) {
360 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700361 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700362
alshabib1c319ff2014-10-04 20:29:09 -0700363 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700364
alshabib1c319ff2014-10-04 20:29:09 -0700365 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700366 if (event == null) {
367 log.debug("No flow store event generated.");
368 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800369 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700370 post(event);
371 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700372 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800373 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700374 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700375 }
alshabib219ebaa2014-09-22 15:41:24 -0700376
alshabib57044ba2014-09-16 15:58:01 -0700377 }
378
alshabib1c319ff2014-10-04 20:29:09 -0700379 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
380 if (storedRule == null) {
381 return false;
382 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700383 if (storedRule.isPermanent()) {
384 return true;
385 }
386
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700387 final long timeout = storedRule.timeout() * 1000;
388 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700389 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700390 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700391 return true;
392 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700393 if (!lastSeen.containsKey(storedRule)) {
394 // checking for the first time
395 lastSeen.put(storedRule, storedRule.lastSeen());
396 // Use following if lastSeen attr. was removed.
397 //lastSeen.put(storedRule, currentTime);
398 }
399 Long last = lastSeen.get(storedRule);
400 if (last == null) {
401 // concurrently removed? let the liveness check fail
402 return false;
403 }
alshabib85c41972014-10-03 13:48:39 -0700404
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700405 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700406 return true;
407 }
408 return false;
alshabibba5ac482014-10-02 17:15:20 -0700409 }
410
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700411 // Posts the specified event to the local event dispatcher.
412 private void post(FlowRuleEvent event) {
413 if (event != null) {
414 eventDispatcher.post(event);
415 }
416 }
alshabib5c370ff2014-09-18 10:12:14 -0700417
418 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700419 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
alshabib64def642014-12-02 23:27:37 -0800420 Set<FlowEntry> storedRules = Sets.newHashSet(store.getFlowEntries(deviceId));
Saurav Dasfa2fa932015-03-03 11:29:48 -0800421 for (FlowEntry rule : flowEntries) {
422 try {
423 if (storedRules.remove(rule)) {
424 // we both have the rule, let's update some info then.
425 flowAdded(rule);
426 } else {
427 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700428 if (!allowExtraneousRules) {
429 extraneousFlow(rule);
430 }
alshabib93cb57f2015-02-12 17:43:26 -0800431 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700432 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800433 log.debug("Can't process added or extra rule {}", e.getMessage());
434 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800435 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800436 }
437 for (FlowEntry rule : storedRules) {
438 try {
439 // there are rules in the store that aren't on the switch
Saurav Das6c44a632015-05-30 22:05:22 -0700440 log.debug("Adding rule in store, but not on switch {}", rule);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800441 flowMissing(rule);
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700442 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800443 log.debug("Can't add missing flow rule {}", e.getMessage());
444 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800445 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800446 }
alshabib93cb57f2015-02-12 17:43:26 -0800447
alshabib5c370ff2014-09-18 10:12:14 -0700448 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800449
450 @Override
451 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
452 store.batchOperationComplete(FlowRuleBatchEvent.completed(
453 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
454 operation
455 ));
456 }
alshabib57044ba2014-09-16 15:58:01 -0700457 }
458
tomc78acee2014-09-24 15:16:55 -0700459 // Store delegate to re-post events emitted from the store.
460 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800461
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800462
Madan Jampani117aaae2014-10-23 10:04:05 -0700463 // TODO: Right now we only dispatch events at individual flowEntry level.
464 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700465 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700466 public void notify(FlowRuleBatchEvent event) {
467 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700468 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700469 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800470 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800471 request.ops().stream().forEach(
472 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800473 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700474
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800475 case ADD:
476 eventDispatcher.post(
477 new FlowRuleEvent(
478 FlowRuleEvent.Type.RULE_ADD_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800479 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800480 break;
481 case REMOVE:
482 eventDispatcher.post(
483 new FlowRuleEvent(
484 FlowRuleEvent.Type.RULE_REMOVE_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800485 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800486 break;
487 case MODIFY:
488 //TODO: do something here when the time comes.
489 break;
490 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800491 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800492 }
493 }
494 );
495
496 DeviceId deviceId = event.deviceId();
497
498 FlowRuleBatchOperation batchOperation =
499 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700500
Thomas Vachuska27bee092015-06-23 19:03:10 -0700501 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
502 if (flowRuleProvider != null) {
503 flowRuleProvider.executeBatch(batchOperation);
504 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800505
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800506 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700507
Madan Jampani117aaae2014-10-23 10:04:05 -0700508 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800509
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800510 FlowOperationsProcessor fops = pendingFlowOperations.remove(
511 event.subject().batchId());
512 if (event.result().isSuccess()) {
513 if (fops != null) {
514 fops.satisfy(event.deviceId());
515 }
516 } else {
517 fops.fail(event.deviceId(), event.result().failedItems());
518 }
519
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700520 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800521
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700522 default:
523 break;
524 }
tomc78acee2014-09-24 15:16:55 -0700525 }
526 }
alshabib902d41b2014-10-07 16:52:05 -0700527
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800528 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700529
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800530 private final List<Set<FlowRuleOperation>> stages;
531 private final FlowRuleOperationsContext context;
532 private final FlowRuleOperations fops;
533 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700534
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800535 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700536
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800537 public FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800538 this.stages = Lists.newArrayList(ops.stages());
539 this.context = ops.callback();
540 this.fops = ops;
541 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700542 }
543
544 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800545 public void run() {
546 if (stages.size() > 0) {
547 process(stages.remove(0));
548 } else if (!hasFailed.get() && context != null) {
549 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700550 }
551 }
552
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800553 private void process(Set<FlowRuleOperation> ops) {
554 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
555 ArrayListMultimap.create();
556
557 FlowRuleBatchEntry fbe;
558 for (FlowRuleOperation flowRuleOperation : ops) {
559 switch (flowRuleOperation.type()) {
560 // FIXME: Brian needs imagination when creating class names.
561 case ADD:
562 fbe = new FlowRuleBatchEntry(
563 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
564 break;
565 case MODIFY:
566 fbe = new FlowRuleBatchEntry(
567 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
568 break;
569 case REMOVE:
570 fbe = new FlowRuleBatchEntry(
571 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
572 break;
573 default:
574 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
575 }
576 pendingDevices.add(flowRuleOperation.rule().deviceId());
577 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
578 }
579
580
581 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700582 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800583 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
584 deviceId, id);
585 pendingFlowOperations.put(id, this);
Sho SHIMIZUf88e5932015-05-27 12:03:51 -0700586 deviceInstallers.submit(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700587 }
588 }
589
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800590 public void satisfy(DeviceId devId) {
591 pendingDevices.remove(devId);
592 if (pendingDevices.isEmpty()) {
593 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700594 }
alshabib193525b2014-10-08 18:58:03 -0700595 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800596
597
598
599 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
600 hasFailed.set(true);
601 pendingDevices.remove(devId);
602 if (pendingDevices.isEmpty()) {
603 operationsService.submit(this);
604 }
605
606 if (context != null) {
607 final FlowRuleOperations.Builder failedOpsBuilder =
608 FlowRuleOperations.builder();
609 failures.stream().forEach(failedOpsBuilder::add);
610
611 context.onError(failedOpsBuilder.build());
612 }
613 }
614
alshabib902d41b2014-10-07 16:52:05 -0700615 }
alshabib57044ba2014-09-16 15:58:01 -0700616}