blob: 488d35ca306d38fcdb93915c73d955d0e1d9d79e [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;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070035import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080037import org.onosproject.core.CoreService;
38import org.onosproject.core.IdGenerator;
Changhoon Yoon541ef712015-05-23 17:18:34 +090039import org.onosproject.core.Permission;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080062import 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 Vachuska42e8cce2015-07-29 19:25:18 -070078import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
79import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090080import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070081import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoon541ef712015-05-23 17:18:34 +090082
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
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070090 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
91 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -070092 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070093
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070094 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -070095 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
96
97 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
98 label = "Allow flow rules in switch not installed by ONOS")
99 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
100
alshabib57044ba2014-09-16 15:58:01 -0700101 private final Logger log = getLogger(getClass());
102
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 DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700121
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected CoreService coreService;
124
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800127
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700128 @Activate
129 public void activate(ComponentContext context) {
130 cfgService.registerProperties(getClass());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800131 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
132
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700133 modified(context);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800134
tomc78acee2014-09-24 15:16:55 -0700135 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700136 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
137 log.info("Started");
138 }
139
140 @Deactivate
141 public void deactivate() {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700142 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800143 deviceInstallers.shutdownNow();
144 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700145 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700146 eventDispatcher.removeSink(FlowRuleEvent.class);
147 log.info("Stopped");
148 }
149
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700150 @Modified
151 public void modified(ComponentContext context) {
152 if (context == null) {
153 return;
154 }
155
156 Dictionary<?, ?> properties = context.getProperties();
157
158 String s = Tools.get(properties, "allowExtraneousRules");
159 allowExtraneousRules = Strings.isNullOrEmpty(s) ? ALLOW_EXTRANEOUS_RULES : Boolean.valueOf(s);
160
161 if (allowExtraneousRules) {
162 log.info("Allowing flow rules not installed by ONOS");
163 }
164 }
165
alshabib57044ba2014-09-16 15:58:01 -0700166 @Override
tom9b4030d2014-10-06 10:39:03 -0700167 public int getFlowRuleCount() {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900168 checkPermission(Permission.FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700169 return store.getFlowRuleCount();
170 }
171
172 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700173 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900174 checkPermission(Permission.FLOWRULE_READ);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700175 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700176 }
177
178 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700179 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900180 checkPermission(Permission.FLOWRULE_WRITE);
181
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) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900191 checkPermission(Permission.FLOWRULE_WRITE);
192
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800193 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700194 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800195 builder.remove(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700196 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800197 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700198 }
alshabib57044ba2014-09-16 15:58:01 -0700199
alshabiba68eb962014-09-24 20:34:13 -0700200 @Override
201 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900202 checkPermission(Permission.FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700203 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700204 }
205
206 @Override
207 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900208 checkPermission(Permission.FLOWRULE_READ);
209
Madan Jampani6a456162014-10-24 11:36:17 -0700210 Set<FlowRule> flowEntries = Sets.newHashSet();
211 for (Device d : deviceService.getDevices()) {
212 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
213 if (flowEntry.appId() == id.id()) {
214 flowEntries.add(flowEntry);
215 }
216 }
217 }
218 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700219 }
220
221 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800222 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900223 checkPermission(Permission.FLOWRULE_READ);
224
alshabibaa7e7de2014-11-12 19:20:44 -0800225 Set<FlowRule> matches = Sets.newHashSet();
226 long toLookUp = ((long) appId.id() << 16) | groupId;
227 for (Device d : deviceService.getDevices()) {
228 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
229 if ((flowEntry.id().value() >>> 32) == toLookUp) {
230 matches.add(flowEntry);
231 }
232 }
233 }
234 return matches;
235 }
236
237 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800238 public void apply(FlowRuleOperations ops) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900239 checkPermission(Permission.FLOWRULE_WRITE);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800240 operationsService.submit(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700241 }
242
243 @Override
alshabib57044ba2014-09-16 15:58:01 -0700244 protected FlowRuleProviderService createProviderService(
245 FlowRuleProvider provider) {
246 return new InternalFlowRuleProviderService(provider);
247 }
248
249 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700250 extends AbstractProviderService<FlowRuleProvider>
251 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700252
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700253 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
254
alshabib57044ba2014-09-16 15:58:01 -0700255 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
256 super(provider);
257 }
258
259 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700260 public void flowRemoved(FlowEntry flowEntry) {
261 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700262 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700263 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700264 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700265 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800266 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700267 return;
268 }
alshabib1c319ff2014-10-04 20:29:09 -0700269 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700270 FlowRuleProvider frp = getProvider(device.providerId());
271 FlowRuleEvent event = null;
272 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700273 case ADDED:
274 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700275 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700276 break;
277 case PENDING_REMOVE:
278 case REMOVED:
279 event = store.removeFlowRule(stored);
280 break;
281 default:
282 break;
alshabib57044ba2014-09-16 15:58:01 -0700283
alshabiba68eb962014-09-24 20:34:13 -0700284 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700285 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700286 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700287 post(event);
288 }
alshabib57044ba2014-09-16 15:58:01 -0700289 }
290
alshabibba5ac482014-10-02 17:15:20 -0700291
alshabib1c319ff2014-10-04 20:29:09 -0700292 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700293 checkNotNull(flowRule, FLOW_RULE_NULL);
294 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700295 Device device = deviceService.getDevice(flowRule.deviceId());
296 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700297 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700298 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700299 case PENDING_REMOVE:
300 case REMOVED:
301 event = store.removeFlowRule(flowRule);
302 frp.removeFlowRule(flowRule);
303 break;
304 case ADDED:
305 case PENDING_ADD:
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800306 try {
307 frp.applyFlowRule(flowRule);
308 } catch (UnsupportedOperationException e) {
309 log.warn(e.getMessage());
310 if (flowRule instanceof DefaultFlowEntry) {
311 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
312 }
313 }
tom9b4030d2014-10-06 10:39:03 -0700314 break;
315 default:
316 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700317 }
318
alshabibbb42cad2014-09-25 11:43:05 -0700319 if (event != null) {
320 log.debug("Flow {} removed", flowRule);
321 post(event);
322 }
alshabib57044ba2014-09-16 15:58:01 -0700323
324 }
325
alshabibba5ac482014-10-02 17:15:20 -0700326
327 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700328 checkNotNull(flowRule, FLOW_RULE_NULL);
329 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700330 FlowRuleProvider frp = getProvider(flowRule.deviceId());
331 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700332 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700333 }
334
alshabibba5ac482014-10-02 17:15:20 -0700335
alshabib1c319ff2014-10-04 20:29:09 -0700336 private void flowAdded(FlowEntry flowEntry) {
337 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700338 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700339
alshabib1c319ff2014-10-04 20:29:09 -0700340 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700341
alshabib1c319ff2014-10-04 20:29:09 -0700342 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700343 if (event == null) {
344 log.debug("No flow store event generated.");
345 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800346 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700347 post(event);
348 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700349 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800350 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700351 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700352 }
alshabib219ebaa2014-09-22 15:41:24 -0700353
alshabib57044ba2014-09-16 15:58:01 -0700354 }
355
alshabib1c319ff2014-10-04 20:29:09 -0700356 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
357 if (storedRule == null) {
358 return false;
359 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700360 if (storedRule.isPermanent()) {
361 return true;
362 }
363
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700364 final long timeout = storedRule.timeout() * 1000;
365 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700366 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700367 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700368 return true;
369 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700370 if (!lastSeen.containsKey(storedRule)) {
371 // checking for the first time
372 lastSeen.put(storedRule, storedRule.lastSeen());
373 // Use following if lastSeen attr. was removed.
374 //lastSeen.put(storedRule, currentTime);
375 }
376 Long last = lastSeen.get(storedRule);
377 if (last == null) {
378 // concurrently removed? let the liveness check fail
379 return false;
380 }
alshabib85c41972014-10-03 13:48:39 -0700381
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700382 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700383 return true;
384 }
385 return false;
alshabibba5ac482014-10-02 17:15:20 -0700386 }
387
alshabib5c370ff2014-09-18 10:12:14 -0700388 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700389 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700390 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
391 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
392
Saurav Dasfa2fa932015-03-03 11:29:48 -0800393 for (FlowEntry rule : flowEntries) {
394 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700395 FlowEntry storedRule = storedRules.remove(rule);
396 if (storedRule != null) {
397 if (storedRule.exactMatch(rule)) {
398 // we both have the rule, let's update some info then.
399 flowAdded(rule);
400 } else {
401 // the two rules are not an exact match - remove the
402 // switch's rule and install our rule
403 extraneousFlow(rule);
404 flowMissing(storedRule);
405 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800406 } else {
407 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700408 if (!allowExtraneousRules) {
409 extraneousFlow(rule);
410 }
alshabib93cb57f2015-02-12 17:43:26 -0800411 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700412 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800413 log.debug("Can't process added or extra rule {}", e.getMessage());
414 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800415 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800416 }
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700417 for (FlowEntry rule : storedRules.keySet()) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800418 try {
419 // there are rules in the store that aren't on the switch
Saurav Das6c44a632015-05-30 22:05:22 -0700420 log.debug("Adding rule in store, but not on switch {}", rule);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800421 flowMissing(rule);
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700422 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800423 log.debug("Can't add missing flow rule {}", e.getMessage());
424 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800425 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800426 }
alshabib93cb57f2015-02-12 17:43:26 -0800427
alshabib5c370ff2014-09-18 10:12:14 -0700428 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800429
430 @Override
431 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
432 store.batchOperationComplete(FlowRuleBatchEvent.completed(
433 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
434 operation
435 ));
436 }
alshabib57044ba2014-09-16 15:58:01 -0700437 }
438
tomc78acee2014-09-24 15:16:55 -0700439 // Store delegate to re-post events emitted from the store.
440 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800441
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800442
Madan Jampani117aaae2014-10-23 10:04:05 -0700443 // TODO: Right now we only dispatch events at individual flowEntry level.
444 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700445 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700446 public void notify(FlowRuleBatchEvent event) {
447 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700448 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700449 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800450 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800451 request.ops().stream().forEach(
452 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800453 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700454
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800455 case ADD:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700456 post(new FlowRuleEvent(RULE_ADD_REQUESTED,
457 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800458 break;
459 case REMOVE:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700460 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED,
461 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800462 break;
463 case MODIFY:
464 //TODO: do something here when the time comes.
465 break;
466 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800467 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800468 }
469 }
470 );
471
472 DeviceId deviceId = event.deviceId();
473
474 FlowRuleBatchOperation batchOperation =
475 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700476
Thomas Vachuska27bee092015-06-23 19:03:10 -0700477 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
478 if (flowRuleProvider != null) {
479 flowRuleProvider.executeBatch(batchOperation);
480 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800481
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800482 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700483
Madan Jampani117aaae2014-10-23 10:04:05 -0700484 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800485
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800486 FlowOperationsProcessor fops = pendingFlowOperations.remove(
487 event.subject().batchId());
488 if (event.result().isSuccess()) {
489 if (fops != null) {
490 fops.satisfy(event.deviceId());
491 }
492 } else {
493 fops.fail(event.deviceId(), event.result().failedItems());
494 }
495
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700496 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800497
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700498 default:
499 break;
500 }
tomc78acee2014-09-24 15:16:55 -0700501 }
502 }
alshabib902d41b2014-10-07 16:52:05 -0700503
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800504 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700505
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800506 private final List<Set<FlowRuleOperation>> stages;
507 private final FlowRuleOperationsContext context;
508 private final FlowRuleOperations fops;
509 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700510
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800511 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700512
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800513 public FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800514 this.stages = Lists.newArrayList(ops.stages());
515 this.context = ops.callback();
516 this.fops = ops;
517 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700518 }
519
520 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800521 public void run() {
522 if (stages.size() > 0) {
523 process(stages.remove(0));
524 } else if (!hasFailed.get() && context != null) {
525 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700526 }
527 }
528
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800529 private void process(Set<FlowRuleOperation> ops) {
530 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
531 ArrayListMultimap.create();
532
533 FlowRuleBatchEntry fbe;
534 for (FlowRuleOperation flowRuleOperation : ops) {
535 switch (flowRuleOperation.type()) {
536 // FIXME: Brian needs imagination when creating class names.
537 case ADD:
538 fbe = new FlowRuleBatchEntry(
539 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
540 break;
541 case MODIFY:
542 fbe = new FlowRuleBatchEntry(
543 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
544 break;
545 case REMOVE:
546 fbe = new FlowRuleBatchEntry(
547 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
548 break;
549 default:
550 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
551 }
552 pendingDevices.add(flowRuleOperation.rule().deviceId());
553 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
554 }
555
556
557 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700558 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800559 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
560 deviceId, id);
561 pendingFlowOperations.put(id, this);
Sho SHIMIZUf88e5932015-05-27 12:03:51 -0700562 deviceInstallers.submit(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700563 }
564 }
565
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800566 public void satisfy(DeviceId devId) {
567 pendingDevices.remove(devId);
568 if (pendingDevices.isEmpty()) {
569 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700570 }
alshabib193525b2014-10-08 18:58:03 -0700571 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800572
573
574
575 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
576 hasFailed.set(true);
577 pendingDevices.remove(devId);
578 if (pendingDevices.isEmpty()) {
579 operationsService.submit(this);
580 }
581
582 if (context != null) {
583 final FlowRuleOperations.Builder failedOpsBuilder =
584 FlowRuleOperations.builder();
585 failures.stream().forEach(failedOpsBuilder::add);
586
587 context.onError(failedOpsBuilder.build());
588 }
589 }
590
alshabib902d41b2014-10-07 16:52:05 -0700591 }
alshabib57044ba2014-09-16 15:58:01 -0700592}