blob: 269d6b4de1f972dd05f5ffdb77e3df7a781a0e75 [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) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800314 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800315 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
316 }
317 }
tom9b4030d2014-10-06 10:39:03 -0700318 break;
319 default:
320 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700321 }
322
alshabibbb42cad2014-09-25 11:43:05 -0700323 if (event != null) {
324 log.debug("Flow {} removed", flowRule);
325 post(event);
326 }
alshabib57044ba2014-09-16 15:58:01 -0700327 }
328
alshabibba5ac482014-10-02 17:15:20 -0700329 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
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))) {
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 }
alshabib57044ba2014-09-16 15:58:01 -0700353 }
354
alshabib1c319ff2014-10-04 20:29:09 -0700355 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
356 if (storedRule == null) {
357 return false;
358 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700359 if (storedRule.isPermanent()) {
360 return true;
361 }
362
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700363 final long timeout = storedRule.timeout() * 1000;
364 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700365 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700366 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700367 return true;
368 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700369 if (!lastSeen.containsKey(storedRule)) {
370 // checking for the first time
371 lastSeen.put(storedRule, storedRule.lastSeen());
372 // Use following if lastSeen attr. was removed.
373 //lastSeen.put(storedRule, currentTime);
374 }
375 Long last = lastSeen.get(storedRule);
376 if (last == null) {
377 // concurrently removed? let the liveness check fail
378 return false;
379 }
alshabib85c41972014-10-03 13:48:39 -0700380
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700381 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700382 return true;
383 }
384 return false;
alshabibba5ac482014-10-02 17:15:20 -0700385 }
386
alshabib5c370ff2014-09-18 10:12:14 -0700387 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700388 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900389 pushFlowMetricsInternal(deviceId, flowEntries, true);
390 }
391
392 @Override
393 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
394 pushFlowMetricsInternal(deviceId, flowEntries, false);
395 }
396
397 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
398 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700399 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
400 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
401
Saurav Dasfa2fa932015-03-03 11:29:48 -0800402 for (FlowEntry rule : flowEntries) {
403 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700404 FlowEntry storedRule = storedRules.remove(rule);
405 if (storedRule != null) {
406 if (storedRule.exactMatch(rule)) {
407 // we both have the rule, let's update some info then.
408 flowAdded(rule);
409 } else {
410 // the two rules are not an exact match - remove the
411 // switch's rule and install our rule
412 extraneousFlow(rule);
413 flowMissing(storedRule);
414 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800415 } else {
416 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700417 if (!allowExtraneousRules) {
418 extraneousFlow(rule);
419 }
alshabib93cb57f2015-02-12 17:43:26 -0800420 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700421 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800422 log.debug("Can't process added or extra rule {}", e.getMessage());
423 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800424 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800425 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900426
427 // DO NOT reinstall
428 if (useMissingFlow) {
429 for (FlowEntry rule : storedRules.keySet()) {
430 try {
431 // there are rules in the store that aren't on the switch
432 log.debug("Adding rule in store, but not on switch {}", rule);
433 flowMissing(rule);
434 } catch (Exception e) {
Jonathan Hart26a8d952015-12-02 15:16:35 -0800435 log.debug("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900436 continue;
437 }
alshabib93cb57f2015-02-12 17:43:26 -0800438 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800439 }
alshabib5c370ff2014-09-18 10:12:14 -0700440 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800441
442 @Override
443 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
444 store.batchOperationComplete(FlowRuleBatchEvent.completed(
445 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
446 operation
447 ));
448 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700449
450 @Override
451 public void pushTableStatistics(DeviceId deviceId,
452 List<TableStatisticsEntry> tableStats) {
453 store.updateTableStatistics(deviceId, tableStats);
454 }
alshabib57044ba2014-09-16 15:58:01 -0700455 }
456
tomc78acee2014-09-24 15:16:55 -0700457 // Store delegate to re-post events emitted from the store.
458 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800459
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800460
Madan Jampani117aaae2014-10-23 10:04:05 -0700461 // TODO: Right now we only dispatch events at individual flowEntry level.
462 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700463 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700464 public void notify(FlowRuleBatchEvent event) {
465 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700466 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700467 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800468 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800469 request.ops().stream().forEach(
470 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800471 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700472
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800473 case ADD:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700474 post(new FlowRuleEvent(RULE_ADD_REQUESTED,
475 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800476 break;
477 case REMOVE:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700478 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED,
479 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800480 break;
481 case MODIFY:
482 //TODO: do something here when the time comes.
483 break;
484 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800485 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800486 }
487 }
488 );
489
490 DeviceId deviceId = event.deviceId();
491
492 FlowRuleBatchOperation batchOperation =
493 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700494
Thomas Vachuska27bee092015-06-23 19:03:10 -0700495 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
496 if (flowRuleProvider != null) {
497 flowRuleProvider.executeBatch(batchOperation);
498 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800499
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800500 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700501
Madan Jampani117aaae2014-10-23 10:04:05 -0700502 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800503
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800504 FlowOperationsProcessor fops = pendingFlowOperations.remove(
505 event.subject().batchId());
506 if (event.result().isSuccess()) {
507 if (fops != null) {
508 fops.satisfy(event.deviceId());
509 }
510 } else {
511 fops.fail(event.deviceId(), event.result().failedItems());
512 }
513
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700514 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800515
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700516 default:
517 break;
518 }
tomc78acee2014-09-24 15:16:55 -0700519 }
520 }
alshabib902d41b2014-10-07 16:52:05 -0700521
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800522 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700523
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800524 private final List<Set<FlowRuleOperation>> stages;
525 private final FlowRuleOperationsContext context;
526 private final FlowRuleOperations fops;
527 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700528
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800529 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700530
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800531 public FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800532 this.stages = Lists.newArrayList(ops.stages());
533 this.context = ops.callback();
534 this.fops = ops;
535 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700536 }
537
538 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800539 public void run() {
540 if (stages.size() > 0) {
541 process(stages.remove(0));
542 } else if (!hasFailed.get() && context != null) {
543 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700544 }
545 }
546
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800547 private void process(Set<FlowRuleOperation> ops) {
548 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
549 ArrayListMultimap.create();
550
551 FlowRuleBatchEntry fbe;
552 for (FlowRuleOperation flowRuleOperation : ops) {
553 switch (flowRuleOperation.type()) {
554 // FIXME: Brian needs imagination when creating class names.
555 case ADD:
556 fbe = new FlowRuleBatchEntry(
557 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
558 break;
559 case MODIFY:
560 fbe = new FlowRuleBatchEntry(
561 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
562 break;
563 case REMOVE:
564 fbe = new FlowRuleBatchEntry(
565 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
566 break;
567 default:
568 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
569 }
570 pendingDevices.add(flowRuleOperation.rule().deviceId());
571 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
572 }
573
574
575 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700576 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800577 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
578 deviceId, id);
579 pendingFlowOperations.put(id, this);
Sho SHIMIZUf88e5932015-05-27 12:03:51 -0700580 deviceInstallers.submit(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700581 }
582 }
583
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800584 public void satisfy(DeviceId devId) {
585 pendingDevices.remove(devId);
586 if (pendingDevices.isEmpty()) {
587 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700588 }
alshabib193525b2014-10-08 18:58:03 -0700589 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800590
591
592
593 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
594 hasFailed.set(true);
595 pendingDevices.remove(devId);
596 if (pendingDevices.isEmpty()) {
597 operationsService.submit(this);
598 }
599
600 if (context != null) {
601 final FlowRuleOperations.Builder failedOpsBuilder =
602 FlowRuleOperations.builder();
603 failures.stream().forEach(failedOpsBuilder::add);
604
605 context.onError(failedOpsBuilder.build());
606 }
607 }
608
alshabib902d41b2014-10-07 16:52:05 -0700609 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700610
611 @Override
612 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
613 checkPermission(FLOWRULE_READ);
614 return store.getTableStatistics(deviceId);
615 }
alshabib57044ba2014-09-16 15:58:01 -0700616}