blob: 63ee03ec43297e76f5c16935c274f01134bcd0d6 [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;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070025
alshabib57044ba2014-09-16 15:58:01 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070029import org.apache.felix.scr.annotations.Modified;
30import org.apache.felix.scr.annotations.Property;
alshabib57044ba2014-09-16 15:58:01 -070031import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070034import org.onlab.util.Tools;
35import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070036import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080038import org.onosproject.core.CoreService;
39import org.onosproject.core.IdGenerator;
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;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070062import org.onosproject.net.flow.TableStatisticsEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import 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 Vachuska42e8cce2015-07-29 19:25:18 -070079import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
80import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090081import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070082import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090083import static org.onosproject.security.AppPermission.Type.*;
84
Changhoon Yoon541ef712015-05-23 17:18:34 +090085
alshabiba7f7ca82014-09-22 11:41:23 -070086
tome4729872014-09-23 00:37:37 -070087/**
88 * Provides implementation of the flow NB & SB APIs.
89 */
Brian O'Connord12267c2015-02-17 18:17:08 -080090@Component(immediate = true, enabled = true)
alshabib57044ba2014-09-16 15:58:01 -070091@Service
tom202175a2014-09-19 19:00:11 -070092public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070093 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
94 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -070095 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070096
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070097 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -070098 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
99
100 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
101 label = "Allow flow rules in switch not installed by ONOS")
102 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
103
alshabib57044ba2014-09-16 15:58:01 -0700104 private final Logger log = getLogger(getClass());
105
alshabibbb42cad2014-09-25 11:43:05 -0700106 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
tomc78acee2014-09-24 15:16:55 -0700107
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800108 protected ExecutorService deviceInstallers =
Brian O'Connord12267c2015-02-17 18:17:08 -0800109 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800110
111 protected ExecutorService operationsService =
Brian O'Connord12267c2015-02-17 18:17:08 -0800112 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800113
114 private IdGenerator idGenerator;
115
Brian O'Connord12267c2015-02-17 18:17:08 -0800116 private Map<Long, FlowOperationsProcessor> pendingFlowOperations
117 = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700118
tombe988312014-09-19 18:38:47 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700121
alshabib57044ba2014-09-16 15:58:01 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700123 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700124
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected CoreService coreService;
127
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800130
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700131 @Activate
132 public void activate(ComponentContext context) {
133 cfgService.registerProperties(getClass());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800134 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
135
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700136 modified(context);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800137
tomc78acee2014-09-24 15:16:55 -0700138 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700139 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
140 log.info("Started");
141 }
142
143 @Deactivate
144 public void deactivate() {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700145 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800146 deviceInstallers.shutdownNow();
147 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700148 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700149 eventDispatcher.removeSink(FlowRuleEvent.class);
150 log.info("Stopped");
151 }
152
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700153 @Modified
154 public void modified(ComponentContext context) {
155 if (context == null) {
156 return;
157 }
158
159 Dictionary<?, ?> properties = context.getProperties();
160
161 String s = Tools.get(properties, "allowExtraneousRules");
162 allowExtraneousRules = Strings.isNullOrEmpty(s) ? ALLOW_EXTRANEOUS_RULES : Boolean.valueOf(s);
163
164 if (allowExtraneousRules) {
165 log.info("Allowing flow rules not installed by ONOS");
166 }
167 }
168
alshabib57044ba2014-09-16 15:58:01 -0700169 @Override
tom9b4030d2014-10-06 10:39:03 -0700170 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900171 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700172 return store.getFlowRuleCount();
173 }
174
175 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700176 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900177 checkPermission(FLOWRULE_READ);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700178 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700179 }
180
181 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700182 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900183 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900184
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800185 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700186 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800187 builder.add(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700188 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800189 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700190 }
191
192 @Override
193 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900194 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900195
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800196 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700197 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800198 builder.remove(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700199 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800200 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700201 }
alshabib57044ba2014-09-16 15:58:01 -0700202
alshabiba68eb962014-09-24 20:34:13 -0700203 @Override
204 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900205 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700206 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700207 }
208
209 @Override
210 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900211 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900212
Madan Jampani6a456162014-10-24 11:36:17 -0700213 Set<FlowRule> flowEntries = Sets.newHashSet();
214 for (Device d : deviceService.getDevices()) {
215 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
216 if (flowEntry.appId() == id.id()) {
217 flowEntries.add(flowEntry);
218 }
219 }
220 }
221 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700222 }
223
224 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800225 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900226 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900227
alshabibaa7e7de2014-11-12 19:20:44 -0800228 Set<FlowRule> matches = Sets.newHashSet();
229 long toLookUp = ((long) appId.id() << 16) | groupId;
230 for (Device d : deviceService.getDevices()) {
231 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
232 if ((flowEntry.id().value() >>> 32) == toLookUp) {
233 matches.add(flowEntry);
234 }
235 }
236 }
237 return matches;
238 }
239
240 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800241 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900242 checkPermission(FLOWRULE_WRITE);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800243 operationsService.submit(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700244 }
245
246 @Override
alshabib57044ba2014-09-16 15:58:01 -0700247 protected FlowRuleProviderService createProviderService(
248 FlowRuleProvider provider) {
249 return new InternalFlowRuleProviderService(provider);
250 }
251
252 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700253 extends AbstractProviderService<FlowRuleProvider>
254 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700255
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700256 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
257
alshabib57044ba2014-09-16 15:58:01 -0700258 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
259 super(provider);
260 }
261
262 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700263 public void flowRemoved(FlowEntry flowEntry) {
264 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700265 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700266 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700267 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700268 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800269 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700270 return;
271 }
alshabib1c319ff2014-10-04 20:29:09 -0700272 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700273 FlowRuleProvider frp = getProvider(device.providerId());
274 FlowRuleEvent event = null;
275 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700276 case ADDED:
277 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700278 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700279 break;
280 case PENDING_REMOVE:
281 case REMOVED:
282 event = store.removeFlowRule(stored);
283 break;
284 default:
285 break;
alshabib57044ba2014-09-16 15:58:01 -0700286
alshabiba68eb962014-09-24 20:34:13 -0700287 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700288 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700289 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700290 post(event);
291 }
alshabib57044ba2014-09-16 15:58:01 -0700292 }
293
alshabibba5ac482014-10-02 17:15:20 -0700294
alshabib1c319ff2014-10-04 20:29:09 -0700295 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700296 checkNotNull(flowRule, FLOW_RULE_NULL);
297 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700298 Device device = deviceService.getDevice(flowRule.deviceId());
299 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700300 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700301 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700302 case PENDING_REMOVE:
303 case REMOVED:
304 event = store.removeFlowRule(flowRule);
305 frp.removeFlowRule(flowRule);
306 break;
307 case ADDED:
308 case PENDING_ADD:
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800309 try {
310 frp.applyFlowRule(flowRule);
311 } catch (UnsupportedOperationException e) {
312 log.warn(e.getMessage());
313 if (flowRule instanceof DefaultFlowEntry) {
314 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
315 }
316 }
tom9b4030d2014-10-06 10:39:03 -0700317 break;
318 default:
319 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700320 }
321
alshabibbb42cad2014-09-25 11:43:05 -0700322 if (event != null) {
323 log.debug("Flow {} removed", flowRule);
324 post(event);
325 }
alshabib57044ba2014-09-16 15:58:01 -0700326
327 }
328
alshabibba5ac482014-10-02 17:15:20 -0700329
330 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700331 checkNotNull(flowRule, FLOW_RULE_NULL);
332 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700333 FlowRuleProvider frp = getProvider(flowRule.deviceId());
334 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700335 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700336 }
337
alshabibba5ac482014-10-02 17:15:20 -0700338
alshabib1c319ff2014-10-04 20:29:09 -0700339 private void flowAdded(FlowEntry flowEntry) {
340 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700341 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700342
alshabib1c319ff2014-10-04 20:29:09 -0700343 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabibba5ac482014-10-02 17:15:20 -0700344
alshabib1c319ff2014-10-04 20:29:09 -0700345 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700346 if (event == null) {
347 log.debug("No flow store event generated.");
348 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800349 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700350 post(event);
351 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700352 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800353 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700354 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700355 }
alshabib219ebaa2014-09-22 15:41:24 -0700356
alshabib57044ba2014-09-16 15:58:01 -0700357 }
358
alshabib1c319ff2014-10-04 20:29:09 -0700359 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
360 if (storedRule == null) {
361 return false;
362 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700363 if (storedRule.isPermanent()) {
364 return true;
365 }
366
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700367 final long timeout = storedRule.timeout() * 1000;
368 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700369 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700370 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700371 return true;
372 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700373 if (!lastSeen.containsKey(storedRule)) {
374 // checking for the first time
375 lastSeen.put(storedRule, storedRule.lastSeen());
376 // Use following if lastSeen attr. was removed.
377 //lastSeen.put(storedRule, currentTime);
378 }
379 Long last = lastSeen.get(storedRule);
380 if (last == null) {
381 // concurrently removed? let the liveness check fail
382 return false;
383 }
alshabib85c41972014-10-03 13:48:39 -0700384
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700385 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700386 return true;
387 }
388 return false;
alshabibba5ac482014-10-02 17:15:20 -0700389 }
390
alshabib5c370ff2014-09-18 10:12:14 -0700391 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700392 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900393 pushFlowMetricsInternal(deviceId, flowEntries, true);
394 }
395
396 @Override
397 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
398 pushFlowMetricsInternal(deviceId, flowEntries, false);
399 }
400
401 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
402 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700403 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
404 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
405
Saurav Dasfa2fa932015-03-03 11:29:48 -0800406 for (FlowEntry rule : flowEntries) {
407 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700408 FlowEntry storedRule = storedRules.remove(rule);
409 if (storedRule != null) {
410 if (storedRule.exactMatch(rule)) {
411 // we both have the rule, let's update some info then.
412 flowAdded(rule);
413 } else {
414 // the two rules are not an exact match - remove the
415 // switch's rule and install our rule
416 extraneousFlow(rule);
417 flowMissing(storedRule);
418 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800419 } else {
420 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700421 if (!allowExtraneousRules) {
422 extraneousFlow(rule);
423 }
alshabib93cb57f2015-02-12 17:43:26 -0800424 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700425 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800426 log.debug("Can't process added or extra rule {}", e.getMessage());
427 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800428 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800429 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900430
431 // DO NOT reinstall
432 if (useMissingFlow) {
433 for (FlowEntry rule : storedRules.keySet()) {
434 try {
435 // there are rules in the store that aren't on the switch
436 log.debug("Adding rule in store, but not on switch {}", rule);
437 flowMissing(rule);
438 } catch (Exception e) {
Jonathan Hart26a8d952015-12-02 15:16:35 -0800439 log.debug("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900440 continue;
441 }
alshabib93cb57f2015-02-12 17:43:26 -0800442 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800443 }
alshabib5c370ff2014-09-18 10:12:14 -0700444 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800445
446 @Override
447 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
448 store.batchOperationComplete(FlowRuleBatchEvent.completed(
449 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
450 operation
451 ));
452 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700453
454 @Override
455 public void pushTableStatistics(DeviceId deviceId,
456 List<TableStatisticsEntry> tableStats) {
457 store.updateTableStatistics(deviceId, tableStats);
458 }
alshabib57044ba2014-09-16 15:58:01 -0700459 }
460
tomc78acee2014-09-24 15:16:55 -0700461 // Store delegate to re-post events emitted from the store.
462 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800463
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800464
Madan Jampani117aaae2014-10-23 10:04:05 -0700465 // TODO: Right now we only dispatch events at individual flowEntry level.
466 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700467 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700468 public void notify(FlowRuleBatchEvent event) {
469 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700470 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700471 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800472 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800473 request.ops().stream().forEach(
474 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800475 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700476
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800477 case ADD:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700478 post(new FlowRuleEvent(RULE_ADD_REQUESTED,
479 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800480 break;
481 case REMOVE:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700482 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED,
483 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800484 break;
485 case MODIFY:
486 //TODO: do something here when the time comes.
487 break;
488 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800489 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800490 }
491 }
492 );
493
494 DeviceId deviceId = event.deviceId();
495
496 FlowRuleBatchOperation batchOperation =
497 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700498
Thomas Vachuska27bee092015-06-23 19:03:10 -0700499 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
500 if (flowRuleProvider != null) {
501 flowRuleProvider.executeBatch(batchOperation);
502 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800503
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800504 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700505
Madan Jampani117aaae2014-10-23 10:04:05 -0700506 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800507
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800508 FlowOperationsProcessor fops = pendingFlowOperations.remove(
509 event.subject().batchId());
510 if (event.result().isSuccess()) {
511 if (fops != null) {
512 fops.satisfy(event.deviceId());
513 }
514 } else {
515 fops.fail(event.deviceId(), event.result().failedItems());
516 }
517
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700518 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800519
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700520 default:
521 break;
522 }
tomc78acee2014-09-24 15:16:55 -0700523 }
524 }
alshabib902d41b2014-10-07 16:52:05 -0700525
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800526 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700527
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800528 private final List<Set<FlowRuleOperation>> stages;
529 private final FlowRuleOperationsContext context;
530 private final FlowRuleOperations fops;
531 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700532
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800533 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700534
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800535 public FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800536 this.stages = Lists.newArrayList(ops.stages());
537 this.context = ops.callback();
538 this.fops = ops;
539 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700540 }
541
542 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800543 public void run() {
544 if (stages.size() > 0) {
545 process(stages.remove(0));
546 } else if (!hasFailed.get() && context != null) {
547 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700548 }
549 }
550
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800551 private void process(Set<FlowRuleOperation> ops) {
552 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
553 ArrayListMultimap.create();
554
555 FlowRuleBatchEntry fbe;
556 for (FlowRuleOperation flowRuleOperation : ops) {
557 switch (flowRuleOperation.type()) {
558 // FIXME: Brian needs imagination when creating class names.
559 case ADD:
560 fbe = new FlowRuleBatchEntry(
561 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
562 break;
563 case MODIFY:
564 fbe = new FlowRuleBatchEntry(
565 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
566 break;
567 case REMOVE:
568 fbe = new FlowRuleBatchEntry(
569 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
570 break;
571 default:
572 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
573 }
574 pendingDevices.add(flowRuleOperation.rule().deviceId());
575 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
576 }
577
578
579 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700580 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800581 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
582 deviceId, id);
583 pendingFlowOperations.put(id, this);
Sho SHIMIZUf88e5932015-05-27 12:03:51 -0700584 deviceInstallers.submit(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700585 }
586 }
587
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800588 public void satisfy(DeviceId devId) {
589 pendingDevices.remove(devId);
590 if (pendingDevices.isEmpty()) {
591 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700592 }
alshabib193525b2014-10-08 18:58:03 -0700593 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800594
595
596
597 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
598 hasFailed.set(true);
599 pendingDevices.remove(devId);
600 if (pendingDevices.isEmpty()) {
601 operationsService.submit(this);
602 }
603
604 if (context != null) {
605 final FlowRuleOperations.Builder failedOpsBuilder =
606 FlowRuleOperations.builder();
607 failures.stream().forEach(failedOpsBuilder::add);
608
609 context.onError(failedOpsBuilder.build());
610 }
611 }
612
alshabib902d41b2014-10-07 16:52:05 -0700613 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700614
615 @Override
616 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
617 checkPermission(FLOWRULE_READ);
618 return store.getTableStatistics(deviceId);
619 }
alshabib57044ba2014-09-16 15:58:01 -0700620}