blob: a1d046c555174e027ba7154c21db1782618f7872 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.device.DeviceService;
42import org.onosproject.net.flow.CompletedBatchOperation;
Charles M.C. Chan1229eca2015-05-18 06:27:52 +080043import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.flow.FlowEntry;
45import org.onosproject.net.flow.FlowRule;
46import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.flow.FlowRuleBatchEvent;
48import org.onosproject.net.flow.FlowRuleBatchOperation;
49import org.onosproject.net.flow.FlowRuleBatchRequest;
50import org.onosproject.net.flow.FlowRuleEvent;
51import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080052import org.onosproject.net.flow.FlowRuleOperation;
53import org.onosproject.net.flow.FlowRuleOperations;
54import org.onosproject.net.flow.FlowRuleOperationsContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.flow.FlowRuleProvider;
56import org.onosproject.net.flow.FlowRuleProviderRegistry;
57import org.onosproject.net.flow.FlowRuleProviderService;
58import org.onosproject.net.flow.FlowRuleService;
59import org.onosproject.net.flow.FlowRuleStore;
60import org.onosproject.net.flow.FlowRuleStoreDelegate;
Brian O'Connorabafb502014-12-02 22:26:20 -080061import org.onosproject.net.provider.AbstractProviderService;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070062import org.osgi.service.component.ComponentContext;
alshabib57044ba2014-09-16 15:58:01 -070063import org.slf4j.Logger;
64
Brian O'Connord12267c2015-02-17 18:17:08 -080065import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070066import java.util.Dictionary;
Brian O'Connord12267c2015-02-17 18:17:08 -080067import java.util.List;
68import java.util.Map;
69import java.util.Set;
70import java.util.concurrent.ConcurrentHashMap;
71import java.util.concurrent.ExecutorService;
72import java.util.concurrent.Executors;
73import java.util.concurrent.atomic.AtomicBoolean;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080074
Thomas Vachuska9b2da212014-11-10 19:30:25 -080075import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connord12267c2015-02-17 18:17:08 -080076import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070077import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
78import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090079import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070080import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090081import static org.onosproject.security.AppPermission.Type.*;
82
Changhoon Yoon541ef712015-05-23 17:18:34 +090083
alshabiba7f7ca82014-09-22 11:41:23 -070084
tome4729872014-09-23 00:37:37 -070085/**
86 * Provides implementation of the flow NB & SB APIs.
87 */
Brian O'Connord12267c2015-02-17 18:17:08 -080088@Component(immediate = true, enabled = true)
alshabib57044ba2014-09-16 15:58:01 -070089@Service
tom202175a2014-09-19 19:00:11 -070090public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070091 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
92 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -070093 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070094
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070095 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -070096 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
97
98 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
99 label = "Allow flow rules in switch not installed by ONOS")
100 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
101
alshabib57044ba2014-09-16 15:58:01 -0700102 private final Logger log = getLogger(getClass());
103
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 DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700122
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected CoreService coreService;
125
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
127 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800128
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700129 @Activate
130 public void activate(ComponentContext context) {
131 cfgService.registerProperties(getClass());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800132 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
133
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700134 modified(context);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800135
tomc78acee2014-09-24 15:16:55 -0700136 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700137 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
138 log.info("Started");
139 }
140
141 @Deactivate
142 public void deactivate() {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700143 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800144 deviceInstallers.shutdownNow();
145 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700146 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700147 eventDispatcher.removeSink(FlowRuleEvent.class);
148 log.info("Stopped");
149 }
150
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700151 @Modified
152 public void modified(ComponentContext context) {
153 if (context == null) {
154 return;
155 }
156
157 Dictionary<?, ?> properties = context.getProperties();
158
159 String s = Tools.get(properties, "allowExtraneousRules");
160 allowExtraneousRules = Strings.isNullOrEmpty(s) ? ALLOW_EXTRANEOUS_RULES : Boolean.valueOf(s);
161
162 if (allowExtraneousRules) {
163 log.info("Allowing flow rules not installed by ONOS");
164 }
165 }
166
alshabib57044ba2014-09-16 15:58:01 -0700167 @Override
tom9b4030d2014-10-06 10:39:03 -0700168 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900169 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700170 return store.getFlowRuleCount();
171 }
172
173 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700174 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900175 checkPermission(FLOWRULE_READ);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700176 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700177 }
178
179 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700180 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900181 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900182
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800183 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700184 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800185 builder.add(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700186 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800187 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700188 }
189
190 @Override
191 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900192 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900193
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800194 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700195 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800196 builder.remove(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700197 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800198 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700199 }
alshabib57044ba2014-09-16 15:58:01 -0700200
alshabiba68eb962014-09-24 20:34:13 -0700201 @Override
202 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900203 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700204 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700205 }
206
207 @Override
208 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900209 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900210
Madan Jampani6a456162014-10-24 11:36:17 -0700211 Set<FlowRule> flowEntries = Sets.newHashSet();
212 for (Device d : deviceService.getDevices()) {
213 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
214 if (flowEntry.appId() == id.id()) {
215 flowEntries.add(flowEntry);
216 }
217 }
218 }
219 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700220 }
221
222 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800223 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900224 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900225
alshabibaa7e7de2014-11-12 19:20:44 -0800226 Set<FlowRule> matches = Sets.newHashSet();
227 long toLookUp = ((long) appId.id() << 16) | groupId;
228 for (Device d : deviceService.getDevices()) {
229 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
230 if ((flowEntry.id().value() >>> 32) == toLookUp) {
231 matches.add(flowEntry);
232 }
233 }
234 }
235 return matches;
236 }
237
238 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800239 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900240 checkPermission(FLOWRULE_WRITE);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800241 operationsService.submit(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700242 }
243
244 @Override
alshabib57044ba2014-09-16 15:58:01 -0700245 protected FlowRuleProviderService createProviderService(
246 FlowRuleProvider provider) {
247 return new InternalFlowRuleProviderService(provider);
248 }
249
250 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700251 extends AbstractProviderService<FlowRuleProvider>
252 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700253
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700254 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
255
alshabib57044ba2014-09-16 15:58:01 -0700256 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
257 super(provider);
258 }
259
260 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700261 public void flowRemoved(FlowEntry flowEntry) {
262 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700263 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700264 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700265 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700266 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800267 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700268 return;
269 }
alshabib1c319ff2014-10-04 20:29:09 -0700270 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700271 FlowRuleProvider frp = getProvider(device.providerId());
272 FlowRuleEvent event = null;
273 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700274 case ADDED:
275 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700276 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700277 break;
278 case PENDING_REMOVE:
279 case REMOVED:
280 event = store.removeFlowRule(stored);
281 break;
282 default:
283 break;
alshabib57044ba2014-09-16 15:58:01 -0700284
alshabiba68eb962014-09-24 20:34:13 -0700285 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700286 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700287 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700288 post(event);
289 }
alshabib57044ba2014-09-16 15:58:01 -0700290 }
291
alshabibba5ac482014-10-02 17:15:20 -0700292
alshabib1c319ff2014-10-04 20:29:09 -0700293 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700294 checkNotNull(flowRule, FLOW_RULE_NULL);
295 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700296 Device device = deviceService.getDevice(flowRule.deviceId());
297 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700298 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700299 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700300 case PENDING_REMOVE:
301 case REMOVED:
302 event = store.removeFlowRule(flowRule);
303 frp.removeFlowRule(flowRule);
304 break;
305 case ADDED:
306 case PENDING_ADD:
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800307 try {
308 frp.applyFlowRule(flowRule);
309 } catch (UnsupportedOperationException e) {
310 log.warn(e.getMessage());
311 if (flowRule instanceof DefaultFlowEntry) {
312 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
313 }
314 }
tom9b4030d2014-10-06 10:39:03 -0700315 break;
316 default:
317 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700318 }
319
alshabibbb42cad2014-09-25 11:43:05 -0700320 if (event != null) {
321 log.debug("Flow {} removed", flowRule);
322 post(event);
323 }
alshabib57044ba2014-09-16 15:58:01 -0700324
325 }
326
alshabibba5ac482014-10-02 17:15:20 -0700327
328 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700329 checkNotNull(flowRule, FLOW_RULE_NULL);
330 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700331 FlowRuleProvider frp = getProvider(flowRule.deviceId());
332 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700333 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700334 }
335
alshabibba5ac482014-10-02 17:15:20 -0700336
alshabib1c319ff2014-10-04 20:29:09 -0700337 private void flowAdded(FlowEntry flowEntry) {
338 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700339 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700340
alshabib1c319ff2014-10-04 20:29:09 -0700341 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700342
alshabib1c319ff2014-10-04 20:29:09 -0700343 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700344 if (event == null) {
345 log.debug("No flow store event generated.");
346 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800347 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700348 post(event);
349 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700350 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800351 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700352 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700353 }
alshabib219ebaa2014-09-22 15:41:24 -0700354
alshabib57044ba2014-09-16 15:58:01 -0700355 }
356
alshabib1c319ff2014-10-04 20:29:09 -0700357 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
358 if (storedRule == null) {
359 return false;
360 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700361 if (storedRule.isPermanent()) {
362 return true;
363 }
364
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700365 final long timeout = storedRule.timeout() * 1000;
366 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700367 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700368 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700369 return true;
370 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700371 if (!lastSeen.containsKey(storedRule)) {
372 // checking for the first time
373 lastSeen.put(storedRule, storedRule.lastSeen());
374 // Use following if lastSeen attr. was removed.
375 //lastSeen.put(storedRule, currentTime);
376 }
377 Long last = lastSeen.get(storedRule);
378 if (last == null) {
379 // concurrently removed? let the liveness check fail
380 return false;
381 }
alshabib85c41972014-10-03 13:48:39 -0700382
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700383 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700384 return true;
385 }
386 return false;
alshabibba5ac482014-10-02 17:15:20 -0700387 }
388
alshabib5c370ff2014-09-18 10:12:14 -0700389 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700390 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700391 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
392 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
393
Saurav Dasfa2fa932015-03-03 11:29:48 -0800394 for (FlowEntry rule : flowEntries) {
395 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700396 FlowEntry storedRule = storedRules.remove(rule);
397 if (storedRule != null) {
398 if (storedRule.exactMatch(rule)) {
399 // we both have the rule, let's update some info then.
400 flowAdded(rule);
401 } else {
402 // the two rules are not an exact match - remove the
403 // switch's rule and install our rule
404 extraneousFlow(rule);
405 flowMissing(storedRule);
406 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800407 } else {
408 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700409 if (!allowExtraneousRules) {
410 extraneousFlow(rule);
411 }
alshabib93cb57f2015-02-12 17:43:26 -0800412 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700413 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800414 log.debug("Can't process added or extra rule {}", e.getMessage());
415 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800416 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800417 }
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700418 for (FlowEntry rule : storedRules.keySet()) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800419 try {
420 // there are rules in the store that aren't on the switch
Saurav Das6c44a632015-05-30 22:05:22 -0700421 log.debug("Adding rule in store, but not on switch {}", rule);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800422 flowMissing(rule);
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700423 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800424 log.debug("Can't add missing flow rule {}", e.getMessage());
425 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800426 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800427 }
alshabib93cb57f2015-02-12 17:43:26 -0800428
alshabib5c370ff2014-09-18 10:12:14 -0700429 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800430
431 @Override
432 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
433 store.batchOperationComplete(FlowRuleBatchEvent.completed(
434 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
435 operation
436 ));
437 }
alshabib57044ba2014-09-16 15:58:01 -0700438 }
439
tomc78acee2014-09-24 15:16:55 -0700440 // Store delegate to re-post events emitted from the store.
441 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800442
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800443
Madan Jampani117aaae2014-10-23 10:04:05 -0700444 // TODO: Right now we only dispatch events at individual flowEntry level.
445 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700446 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700447 public void notify(FlowRuleBatchEvent event) {
448 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700449 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700450 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800451 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800452 request.ops().stream().forEach(
453 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800454 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700455
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800456 case ADD:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700457 post(new FlowRuleEvent(RULE_ADD_REQUESTED,
458 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800459 break;
460 case REMOVE:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700461 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED,
462 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800463 break;
464 case MODIFY:
465 //TODO: do something here when the time comes.
466 break;
467 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800468 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800469 }
470 }
471 );
472
473 DeviceId deviceId = event.deviceId();
474
475 FlowRuleBatchOperation batchOperation =
476 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700477
Thomas Vachuska27bee092015-06-23 19:03:10 -0700478 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
479 if (flowRuleProvider != null) {
480 flowRuleProvider.executeBatch(batchOperation);
481 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800482
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800483 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700484
Madan Jampani117aaae2014-10-23 10:04:05 -0700485 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800486
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800487 FlowOperationsProcessor fops = pendingFlowOperations.remove(
488 event.subject().batchId());
489 if (event.result().isSuccess()) {
490 if (fops != null) {
491 fops.satisfy(event.deviceId());
492 }
493 } else {
494 fops.fail(event.deviceId(), event.result().failedItems());
495 }
496
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700497 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800498
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700499 default:
500 break;
501 }
tomc78acee2014-09-24 15:16:55 -0700502 }
503 }
alshabib902d41b2014-10-07 16:52:05 -0700504
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800505 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700506
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800507 private final List<Set<FlowRuleOperation>> stages;
508 private final FlowRuleOperationsContext context;
509 private final FlowRuleOperations fops;
510 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700511
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800512 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700513
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800514 public FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800515 this.stages = Lists.newArrayList(ops.stages());
516 this.context = ops.callback();
517 this.fops = ops;
518 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700519 }
520
521 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800522 public void run() {
523 if (stages.size() > 0) {
524 process(stages.remove(0));
525 } else if (!hasFailed.get() && context != null) {
526 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700527 }
528 }
529
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800530 private void process(Set<FlowRuleOperation> ops) {
531 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
532 ArrayListMultimap.create();
533
534 FlowRuleBatchEntry fbe;
535 for (FlowRuleOperation flowRuleOperation : ops) {
536 switch (flowRuleOperation.type()) {
537 // FIXME: Brian needs imagination when creating class names.
538 case ADD:
539 fbe = new FlowRuleBatchEntry(
540 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
541 break;
542 case MODIFY:
543 fbe = new FlowRuleBatchEntry(
544 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
545 break;
546 case REMOVE:
547 fbe = new FlowRuleBatchEntry(
548 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
549 break;
550 default:
551 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
552 }
553 pendingDevices.add(flowRuleOperation.rule().deviceId());
554 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
555 }
556
557
558 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700559 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800560 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
561 deviceId, id);
562 pendingFlowOperations.put(id, this);
Sho SHIMIZUf88e5932015-05-27 12:03:51 -0700563 deviceInstallers.submit(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700564 }
565 }
566
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800567 public void satisfy(DeviceId devId) {
568 pendingDevices.remove(devId);
569 if (pendingDevices.isEmpty()) {
570 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700571 }
alshabib193525b2014-10-08 18:58:03 -0700572 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800573
574
575
576 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
577 hasFailed.set(true);
578 pendingDevices.remove(devId);
579 if (pendingDevices.isEmpty()) {
580 operationsService.submit(this);
581 }
582
583 if (context != null) {
584 final FlowRuleOperations.Builder failedOpsBuilder =
585 FlowRuleOperations.builder();
586 failures.stream().forEach(failedOpsBuilder::add);
587
588 context.onError(failedOpsBuilder.build());
589 }
590 }
591
alshabib902d41b2014-10-07 16:52:05 -0700592 }
alshabib57044ba2014-09-16 15:58:01 -0700593}