blob: 59048cd076b92fad7d1bab8ea1a9ecc0c89e76be [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;
Simon Huntff663742015-05-14 13:33:05 -070038import org.onosproject.event.ListenerRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import 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;
Charles M.C. Chan1229eca2015-05-18 06:27:52 +080044import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.flow.FlowEntry;
46import org.onosproject.net.flow.FlowRule;
47import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.flow.FlowRuleBatchEvent;
49import org.onosproject.net.flow.FlowRuleBatchOperation;
50import org.onosproject.net.flow.FlowRuleBatchRequest;
51import org.onosproject.net.flow.FlowRuleEvent;
52import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080053import org.onosproject.net.flow.FlowRuleOperation;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080056import org.onosproject.net.flow.FlowRuleProvider;
57import org.onosproject.net.flow.FlowRuleProviderRegistry;
58import org.onosproject.net.flow.FlowRuleProviderService;
59import org.onosproject.net.flow.FlowRuleService;
60import org.onosproject.net.flow.FlowRuleStore;
61import org.onosproject.net.flow.FlowRuleStoreDelegate;
62import org.onosproject.net.provider.AbstractProviderRegistry;
63import org.onosproject.net.provider.AbstractProviderService;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070064import org.osgi.service.component.ComponentContext;
alshabib57044ba2014-09-16 15:58:01 -070065import org.slf4j.Logger;
66
Brian O'Connord12267c2015-02-17 18:17:08 -080067import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070068import java.util.Dictionary;
Brian O'Connord12267c2015-02-17 18:17:08 -080069import java.util.List;
70import java.util.Map;
71import java.util.Set;
72import java.util.concurrent.ConcurrentHashMap;
73import java.util.concurrent.ExecutorService;
74import java.util.concurrent.Executors;
75import java.util.concurrent.atomic.AtomicBoolean;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080076
Thomas Vachuska9b2da212014-11-10 19:30:25 -080077import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connord12267c2015-02-17 18:17:08 -080078import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska9b2da212014-11-10 19:30:25 -080079import static org.slf4j.LoggerFactory.getLogger;
alshabiba7f7ca82014-09-22 11:41:23 -070080
tome4729872014-09-23 00:37:37 -070081/**
82 * Provides implementation of the flow NB & SB APIs.
83 */
Brian O'Connord12267c2015-02-17 18:17:08 -080084@Component(immediate = true, enabled = true)
alshabib57044ba2014-09-16 15:58:01 -070085@Service
tom202175a2014-09-19 19:00:11 -070086public class FlowRuleManager
tom9b4030d2014-10-06 10:39:03 -070087 extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
88 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070089
Sho SHIMIZU183b12fd2015-01-20 14:56:39 -080090 enum BatchState { STARTED, FINISHED, CANCELLED }
alshabib193525b2014-10-08 18:58:03 -070091
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070092 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -070093 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
94
95 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
96 label = "Allow flow rules in switch not installed by ONOS")
97 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
98
alshabib57044ba2014-09-16 15:58:01 -070099 private final Logger log = getLogger(getClass());
100
Simon Huntff663742015-05-14 13:33:05 -0700101 private final ListenerRegistry<FlowRuleEvent, FlowRuleListener>
102 listenerRegistry = new ListenerRegistry<>();
alshabib57044ba2014-09-16 15:58:01 -0700103
alshabibbb42cad2014-09-25 11:43:05 -0700104 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -0700105
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800106 protected ExecutorService deviceInstallers =
Brian O'Connord12267c2015-02-17 18:17:08 -0800107 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800108
109 protected ExecutorService operationsService =
Brian O'Connord12267c2015-02-17 18:17:08 -0800110 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800111
112 private IdGenerator idGenerator;
113
Brian O'Connord12267c2015-02-17 18:17:08 -0800114 private Map<Long, FlowOperationsProcessor> pendingFlowOperations
115 = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700116
tombe988312014-09-19 18:38:47 -0700117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
118 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700119
alshabib57044ba2014-09-16 15:58:01 -0700120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700121 protected EventDeliveryService eventDispatcher;
alshabib57044ba2014-09-16 15:58:01 -0700122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700124 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700125
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected CoreService coreService;
128
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800131
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700132 @Activate
133 public void activate(ComponentContext context) {
134 cfgService.registerProperties(getClass());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800135 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
136
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700137 modified(context);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800138
tomc78acee2014-09-24 15:16:55 -0700139 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700140 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
141 log.info("Started");
142 }
143
144 @Deactivate
145 public void deactivate() {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700146 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800147 deviceInstallers.shutdownNow();
148 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700149 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700150 eventDispatcher.removeSink(FlowRuleEvent.class);
151 log.info("Stopped");
152 }
153
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700154 @Modified
155 public void modified(ComponentContext context) {
156 if (context == null) {
157 return;
158 }
159
160 Dictionary<?, ?> properties = context.getProperties();
161
162 String s = Tools.get(properties, "allowExtraneousRules");
163 allowExtraneousRules = Strings.isNullOrEmpty(s) ? ALLOW_EXTRANEOUS_RULES : Boolean.valueOf(s);
164
165 if (allowExtraneousRules) {
166 log.info("Allowing flow rules not installed by ONOS");
167 }
168 }
169
alshabib57044ba2014-09-16 15:58:01 -0700170 @Override
tom9b4030d2014-10-06 10:39:03 -0700171 public int getFlowRuleCount() {
172 return store.getFlowRuleCount();
173 }
174
175 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700176 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700177 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700178 }
179
180 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700181 public void applyFlowRules(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800182 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700183 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800184 builder.add(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700185 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800186 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700187 }
188
189 @Override
190 public void removeFlowRules(FlowRule... flowRules) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800191 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700192 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800193 builder.remove(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700194 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800195 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700196 }
alshabib57044ba2014-09-16 15:58:01 -0700197
alshabiba68eb962014-09-24 20:34:13 -0700198 @Override
199 public void removeFlowRulesById(ApplicationId id) {
Madan Jampani6a456162014-10-24 11:36:17 -0700200 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700201 }
202
203 @Override
204 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Madan Jampani6a456162014-10-24 11:36:17 -0700205 Set<FlowRule> flowEntries = Sets.newHashSet();
206 for (Device d : deviceService.getDevices()) {
207 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
208 if (flowEntry.appId() == id.id()) {
209 flowEntries.add(flowEntry);
210 }
211 }
212 }
213 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700214 }
215
216 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800217 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
218 Set<FlowRule> matches = Sets.newHashSet();
219 long toLookUp = ((long) appId.id() << 16) | groupId;
220 for (Device d : deviceService.getDevices()) {
221 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
222 if ((flowEntry.id().value() >>> 32) == toLookUp) {
223 matches.add(flowEntry);
224 }
225 }
226 }
227 return matches;
228 }
229
230 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800231 public void apply(FlowRuleOperations ops) {
232 operationsService.submit(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700233 }
234
235 @Override
alshabib57044ba2014-09-16 15:58:01 -0700236 public void addListener(FlowRuleListener listener) {
237 listenerRegistry.addListener(listener);
238 }
239
240 @Override
241 public void removeListener(FlowRuleListener listener) {
242 listenerRegistry.removeListener(listener);
243 }
244
245 @Override
246 protected FlowRuleProviderService createProviderService(
247 FlowRuleProvider provider) {
248 return new InternalFlowRuleProviderService(provider);
249 }
250
251 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700252 extends AbstractProviderService<FlowRuleProvider>
253 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700254
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700255 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
256
alshabib57044ba2014-09-16 15:58:01 -0700257 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
258 super(provider);
259 }
260
261 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700262 public void flowRemoved(FlowEntry flowEntry) {
263 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700264 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700265 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700266 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700267 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800268 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700269 return;
270 }
alshabib1c319ff2014-10-04 20:29:09 -0700271 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700272 FlowRuleProvider frp = getProvider(device.providerId());
273 FlowRuleEvent event = null;
274 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700275 case ADDED:
276 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700277 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700278 break;
279 case PENDING_REMOVE:
280 case REMOVED:
281 event = store.removeFlowRule(stored);
282 break;
283 default:
284 break;
alshabib57044ba2014-09-16 15:58:01 -0700285
alshabiba68eb962014-09-24 20:34:13 -0700286 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700287 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700288 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700289 post(event);
290 }
alshabib57044ba2014-09-16 15:58:01 -0700291 }
292
alshabibba5ac482014-10-02 17:15:20 -0700293
alshabib1c319ff2014-10-04 20:29:09 -0700294 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700295 checkNotNull(flowRule, FLOW_RULE_NULL);
296 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700297 Device device = deviceService.getDevice(flowRule.deviceId());
298 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700299 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700300 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700301 case PENDING_REMOVE:
302 case REMOVED:
303 event = store.removeFlowRule(flowRule);
304 frp.removeFlowRule(flowRule);
305 break;
306 case ADDED:
307 case PENDING_ADD:
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800308 try {
309 frp.applyFlowRule(flowRule);
310 } catch (UnsupportedOperationException e) {
311 log.warn(e.getMessage());
312 if (flowRule instanceof DefaultFlowEntry) {
313 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
314 }
315 }
tom9b4030d2014-10-06 10:39:03 -0700316 break;
317 default:
318 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700319 }
320
alshabibbb42cad2014-09-25 11:43:05 -0700321 if (event != null) {
322 log.debug("Flow {} removed", flowRule);
323 post(event);
324 }
alshabib57044ba2014-09-16 15:58:01 -0700325
326 }
327
alshabibba5ac482014-10-02 17:15:20 -0700328
329 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700330 checkNotNull(flowRule, FLOW_RULE_NULL);
331 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700332 FlowRuleProvider frp = getProvider(flowRule.deviceId());
333 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700334 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700335 }
336
alshabibba5ac482014-10-02 17:15:20 -0700337
alshabib1c319ff2014-10-04 20:29:09 -0700338 private void flowAdded(FlowEntry flowEntry) {
339 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700340 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700341
alshabib1c319ff2014-10-04 20:29:09 -0700342 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700343
alshabib1c319ff2014-10-04 20:29:09 -0700344 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700345 if (event == null) {
346 log.debug("No flow store event generated.");
347 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800348 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700349 post(event);
350 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700351 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800352 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700353 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700354 }
alshabib219ebaa2014-09-22 15:41:24 -0700355
alshabib57044ba2014-09-16 15:58:01 -0700356 }
357
alshabib1c319ff2014-10-04 20:29:09 -0700358 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
359 if (storedRule == null) {
360 return false;
361 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700362 if (storedRule.isPermanent()) {
363 return true;
364 }
365
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700366 final long timeout = storedRule.timeout() * 1000;
367 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700368 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700369 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700370 return true;
371 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700372 if (!lastSeen.containsKey(storedRule)) {
373 // checking for the first time
374 lastSeen.put(storedRule, storedRule.lastSeen());
375 // Use following if lastSeen attr. was removed.
376 //lastSeen.put(storedRule, currentTime);
377 }
378 Long last = lastSeen.get(storedRule);
379 if (last == null) {
380 // concurrently removed? let the liveness check fail
381 return false;
382 }
alshabib85c41972014-10-03 13:48:39 -0700383
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700384 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700385 return true;
386 }
387 return false;
alshabibba5ac482014-10-02 17:15:20 -0700388 }
389
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700390 // Posts the specified event to the local event dispatcher.
391 private void post(FlowRuleEvent event) {
392 if (event != null) {
393 eventDispatcher.post(event);
394 }
395 }
alshabib5c370ff2014-09-18 10:12:14 -0700396
397 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700398 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
alshabib64def642014-12-02 23:27:37 -0800399 Set<FlowEntry> storedRules = Sets.newHashSet(store.getFlowEntries(deviceId));
Saurav Dasfa2fa932015-03-03 11:29:48 -0800400 for (FlowEntry rule : flowEntries) {
401 try {
402 if (storedRules.remove(rule)) {
403 // we both have the rule, let's update some info then.
404 flowAdded(rule);
405 } else {
406 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700407 if (!allowExtraneousRules) {
408 extraneousFlow(rule);
409 }
alshabib93cb57f2015-02-12 17:43:26 -0800410 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700411 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800412 log.debug("Can't process added or extra rule {}", e.getMessage());
413 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800414 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800415 }
416 for (FlowEntry rule : storedRules) {
417 try {
418 // there are rules in the store that aren't on the switch
419 flowMissing(rule);
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700420 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800421 log.debug("Can't add missing flow rule {}", e.getMessage());
422 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800423 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800424 }
alshabib93cb57f2015-02-12 17:43:26 -0800425
alshabib5c370ff2014-09-18 10:12:14 -0700426 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800427
428 @Override
429 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
430 store.batchOperationComplete(FlowRuleBatchEvent.completed(
431 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
432 operation
433 ));
434 }
alshabib57044ba2014-09-16 15:58:01 -0700435 }
436
tomc78acee2014-09-24 15:16:55 -0700437 // Store delegate to re-post events emitted from the store.
438 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800439
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800440
Madan Jampani117aaae2014-10-23 10:04:05 -0700441 // TODO: Right now we only dispatch events at individual flowEntry level.
442 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700443 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700444 public void notify(FlowRuleBatchEvent event) {
445 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700446 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700447 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800448 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800449 request.ops().stream().forEach(
450 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800451 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700452
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800453 case ADD:
454 eventDispatcher.post(
455 new FlowRuleEvent(
456 FlowRuleEvent.Type.RULE_ADD_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800457 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800458 break;
459 case REMOVE:
460 eventDispatcher.post(
461 new FlowRuleEvent(
462 FlowRuleEvent.Type.RULE_REMOVE_REQUESTED,
Ray Milkeyf7329c72015-02-17 11:37:01 -0800463 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800464 break;
465 case MODIFY:
466 //TODO: do something here when the time comes.
467 break;
468 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800469 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800470 }
471 }
472 );
473
474 DeviceId deviceId = event.deviceId();
475
476 FlowRuleBatchOperation batchOperation =
477 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700478
Madan Jampani117aaae2014-10-23 10:04:05 -0700479 FlowRuleProvider flowRuleProvider =
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800480 getProvider(deviceId);
Yuta HIGUCHIf1ccee82014-11-11 20:39:58 -0800481
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800482 flowRuleProvider.executeBatch(batchOperation);
483
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800484 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700485
Madan Jampani117aaae2014-10-23 10:04:05 -0700486 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800487
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800488 FlowOperationsProcessor fops = pendingFlowOperations.remove(
489 event.subject().batchId());
490 if (event.result().isSuccess()) {
491 if (fops != null) {
492 fops.satisfy(event.deviceId());
493 }
494 } else {
495 fops.fail(event.deviceId(), event.result().failedItems());
496 }
497
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700498 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800499
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700500 default:
501 break;
502 }
tomc78acee2014-09-24 15:16:55 -0700503 }
504 }
alshabib902d41b2014-10-07 16:52:05 -0700505
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800506 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700507
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800508 private final List<Set<FlowRuleOperation>> stages;
509 private final FlowRuleOperationsContext context;
510 private final FlowRuleOperations fops;
511 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700512
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800513 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700514
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800515 public FlowOperationsProcessor(FlowRuleOperations ops) {
alshabib902d41b2014-10-07 16:52:05 -0700516
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800517 this.stages = Lists.newArrayList(ops.stages());
518 this.context = ops.callback();
519 this.fops = ops;
520 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700521
alshabib193525b2014-10-08 18:58:03 -0700522
alshabib902d41b2014-10-07 16:52:05 -0700523 }
524
525 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800526 public void run() {
527 if (stages.size() > 0) {
528 process(stages.remove(0));
529 } else if (!hasFailed.get() && context != null) {
530 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700531 }
532 }
533
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800534 private void process(Set<FlowRuleOperation> ops) {
535 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
536 ArrayListMultimap.create();
537
538 FlowRuleBatchEntry fbe;
539 for (FlowRuleOperation flowRuleOperation : ops) {
540 switch (flowRuleOperation.type()) {
541 // FIXME: Brian needs imagination when creating class names.
542 case ADD:
543 fbe = new FlowRuleBatchEntry(
544 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
545 break;
546 case MODIFY:
547 fbe = new FlowRuleBatchEntry(
548 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
549 break;
550 case REMOVE:
551 fbe = new FlowRuleBatchEntry(
552 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
553 break;
554 default:
555 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
556 }
557 pendingDevices.add(flowRuleOperation.rule().deviceId());
558 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
559 }
560
561
562 for (DeviceId deviceId : perDeviceBatches.keySet()) {
563 Long id = idGenerator.getNewId();
564 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
565 deviceId, id);
566 pendingFlowOperations.put(id, this);
567 deviceInstallers.submit(new Runnable() {
568 @Override
569 public void run() {
570 store.storeBatch(b);
alshabib193525b2014-10-08 18:58:03 -0700571 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800572 });
alshabib193525b2014-10-08 18:58:03 -0700573 }
574 }
575
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800576 public void satisfy(DeviceId devId) {
577 pendingDevices.remove(devId);
578 if (pendingDevices.isEmpty()) {
579 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700580 }
alshabib193525b2014-10-08 18:58:03 -0700581 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800582
583
584
585 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
586 hasFailed.set(true);
587 pendingDevices.remove(devId);
588 if (pendingDevices.isEmpty()) {
589 operationsService.submit(this);
590 }
591
592 if (context != null) {
593 final FlowRuleOperations.Builder failedOpsBuilder =
594 FlowRuleOperations.builder();
595 failures.stream().forEach(failedOpsBuilder::add);
596
597 context.onError(failedOpsBuilder.build());
598 }
599 }
600
alshabib902d41b2014-10-07 16:52:05 -0700601 }
alshabib57044ba2014-09-16 15:58:01 -0700602}