blob: b1de54070f6d82aed6db342f8c450d9fd8b02292 [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
440 flowMissing(rule);
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700441 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800442 log.debug("Can't add missing flow rule {}", e.getMessage());
443 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800444 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800445 }
alshabib93cb57f2015-02-12 17:43:26 -0800446
alshabib5c370ff2014-09-18 10:12:14 -0700447 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800448
449 @Override
450 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
451 store.batchOperationComplete(FlowRuleBatchEvent.completed(
452 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
453 operation
454 ));
455 }
alshabib57044ba2014-09-16 15:58:01 -0700456 }
457
tomc78acee2014-09-24 15:16:55 -0700458 // Store delegate to re-post events emitted from the store.
459 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800460
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800461
Madan Jampani117aaae2014-10-23 10:04:05 -0700462 // TODO: Right now we only dispatch events at individual flowEntry level.
463 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700464 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700465 public void notify(FlowRuleBatchEvent event) {
466 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700467 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700468 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800469 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800470 request.ops().stream().forEach(
471 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800472 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700473
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800474 case ADD:
475 eventDispatcher.post(
476 new FlowRuleEvent(
477 FlowRuleEvent.Type.RULE_ADD_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800478 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800479 break;
480 case REMOVE:
481 eventDispatcher.post(
482 new FlowRuleEvent(
483 FlowRuleEvent.Type.RULE_REMOVE_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800484 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800485 break;
486 case MODIFY:
487 //TODO: do something here when the time comes.
488 break;
489 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800490 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800491 }
492 }
493 );
494
495 DeviceId deviceId = event.deviceId();
496
497 FlowRuleBatchOperation batchOperation =
498 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700499
Madan Jampani117aaae2014-10-23 10:04:05 -0700500 FlowRuleProvider flowRuleProvider =
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800501 getProvider(deviceId);
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800502
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800503 flowRuleProvider.executeBatch(batchOperation);
504
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800505 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700506
Madan Jampani117aaae2014-10-23 10:04:05 -0700507 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800508
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800509 FlowOperationsProcessor fops = pendingFlowOperations.remove(
510 event.subject().batchId());
511 if (event.result().isSuccess()) {
512 if (fops != null) {
513 fops.satisfy(event.deviceId());
514 }
515 } else {
516 fops.fail(event.deviceId(), event.result().failedItems());
517 }
518
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700519 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800520
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700521 default:
522 break;
523 }
tomc78acee2014-09-24 15:16:55 -0700524 }
525 }
alshabib902d41b2014-10-07 16:52:05 -0700526
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800527 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700528
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800529 private final List<Set<FlowRuleOperation>> stages;
530 private final FlowRuleOperationsContext context;
531 private final FlowRuleOperations fops;
532 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700533
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800534 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700535
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800536 public FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800537 this.stages = Lists.newArrayList(ops.stages());
538 this.context = ops.callback();
539 this.fops = ops;
540 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700541 }
542
543 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800544 public void run() {
545 if (stages.size() > 0) {
546 process(stages.remove(0));
547 } else if (!hasFailed.get() && context != null) {
548 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700549 }
550 }
551
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800552 private void process(Set<FlowRuleOperation> ops) {
553 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
554 ArrayListMultimap.create();
555
556 FlowRuleBatchEntry fbe;
557 for (FlowRuleOperation flowRuleOperation : ops) {
558 switch (flowRuleOperation.type()) {
559 // FIXME: Brian needs imagination when creating class names.
560 case ADD:
561 fbe = new FlowRuleBatchEntry(
562 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
563 break;
564 case MODIFY:
565 fbe = new FlowRuleBatchEntry(
566 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
567 break;
568 case REMOVE:
569 fbe = new FlowRuleBatchEntry(
570 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
571 break;
572 default:
573 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
574 }
575 pendingDevices.add(flowRuleOperation.rule().deviceId());
576 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
577 }
578
579
580 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700581 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800582 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
583 deviceId, id);
584 pendingFlowOperations.put(id, this);
Sho SHIMIZUf88e5932015-05-27 12:03:51 -0700585 deviceInstallers.submit(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700586 }
587 }
588
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800589 public void satisfy(DeviceId devId) {
590 pendingDevices.remove(devId);
591 if (pendingDevices.isEmpty()) {
592 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700593 }
alshabib193525b2014-10-08 18:58:03 -0700594 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800595
596
597
598 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
599 hasFailed.set(true);
600 pendingDevices.remove(devId);
601 if (pendingDevices.isEmpty()) {
602 operationsService.submit(this);
603 }
604
605 if (context != null) {
606 final FlowRuleOperations.Builder failedOpsBuilder =
607 FlowRuleOperations.builder();
608 failures.stream().forEach(failedOpsBuilder::add);
609
610 context.onError(failedOpsBuilder.build());
611 }
612 }
613
alshabib902d41b2014-10-07 16:52:05 -0700614 }
alshabib57044ba2014-09-16 15:58:01 -0700615}