blob: 9162ff63658a67d937ed55aca958c2eddf7e20a5 [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
Brian O'Connord12267c2015-02-17 18:17:08 -080018import com.google.common.collect.ArrayListMultimap;
19import com.google.common.collect.Iterables;
20import com.google.common.collect.Lists;
21import com.google.common.collect.Maps;
22import com.google.common.collect.Multimap;
23import com.google.common.collect.Sets;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070024
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.onosproject.cfg.ComponentConfigService;
Charles Chan0c7c43b2016-01-14 17:39:20 -080034import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceListener;
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;
Charles Chan0c7c43b2016-01-14 17:39:20 -080078import static com.google.common.base.Strings.isNullOrEmpty;
Brian O'Connord12267c2015-02-17 18:17:08 -080079import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070080import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
81import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090082import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070083import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090084import static org.onosproject.security.AppPermission.Type.*;
85
Changhoon Yoon541ef712015-05-23 17:18:34 +090086
alshabiba7f7ca82014-09-22 11:41:23 -070087
tome4729872014-09-23 00:37:37 -070088/**
89 * Provides implementation of the flow NB & SB APIs.
90 */
Brian O'Connord12267c2015-02-17 18:17:08 -080091@Component(immediate = true, enabled = true)
alshabib57044ba2014-09-16 15:58:01 -070092@Service
tom202175a2014-09-19 19:00:11 -070093public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070094 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
95 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -070096 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070097
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -070098 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -070099 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
100
101 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
102 label = "Allow flow rules in switch not installed by ONOS")
103 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
104
Charles Chan0c7c43b2016-01-14 17:39:20 -0800105 @Property(name = "purgeOnDisconnection", boolValue = false,
106 label = "Purge entries associated with a device when the device goes offline")
107 private boolean purgeOnDisconnection = false;
108
alshabib57044ba2014-09-16 15:58:01 -0700109 private final Logger log = getLogger(getClass());
110
alshabibbb42cad2014-09-25 11:43:05 -0700111 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800112 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700113
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800114 protected ExecutorService deviceInstallers =
Brian O'Connord12267c2015-02-17 18:17:08 -0800115 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800116
117 protected ExecutorService operationsService =
Brian O'Connord12267c2015-02-17 18:17:08 -0800118 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d"));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800119
120 private IdGenerator idGenerator;
121
Brian O'Connord12267c2015-02-17 18:17:08 -0800122 private Map<Long, FlowOperationsProcessor> pendingFlowOperations
123 = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700124
tombe988312014-09-19 18:38:47 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700127
alshabib57044ba2014-09-16 15:58:01 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700129 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700130
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected CoreService coreService;
133
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800136
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700137 @Activate
138 public void activate(ComponentContext context) {
tomc78acee2014-09-24 15:16:55 -0700139 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700140 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800141 deviceService.addListener(deviceListener);
142 cfgService.registerProperties(getClass());
143 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
144 modified(context);
alshabib57044ba2014-09-16 15:58:01 -0700145 log.info("Started");
146 }
147
148 @Deactivate
149 public void deactivate() {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700150 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800151 deviceInstallers.shutdownNow();
152 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700153 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700154 eventDispatcher.removeSink(FlowRuleEvent.class);
155 log.info("Stopped");
156 }
157
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700158 @Modified
159 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800160 if (context != null) {
161 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700162 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800163 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700164
Charles Chan0c7c43b2016-01-14 17:39:20 -0800165 /**
166 * Extracts properties from the component configuration context.
167 *
168 * @param context the component context
169 */
170 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700171 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800172 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700173
Charles Chan0c7c43b2016-01-14 17:39:20 -0800174 flag = isPropertyEnabled(properties, "allowExtraneousRules");
175 if (flag == null) {
176 log.info("AllowExtraneousRules is not configured, " +
177 "using current value of {}", allowExtraneousRules);
178 } else {
179 allowExtraneousRules = flag;
180 log.info("Configured. AllowExtraneousRules is {}",
181 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700182 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800183
184 flag = isPropertyEnabled(properties, "purgeOnDisconnection");
185 if (flag == null) {
186 log.info("PurgeOnDisconnection is not configured, " +
187 "using current value of {}", purgeOnDisconnection);
188 } else {
189 purgeOnDisconnection = flag;
190 log.info("Configured. PurgeOnDisconnection is {}",
191 purgeOnDisconnection ? "enabled" : "disabled");
192 }
193 }
194
195 /**
196 * Check property name is defined and set to true.
197 *
198 * @param properties properties to be looked up
199 * @param propertyName the name of the property to look up
200 * @return value when the propertyName is defined or return null
201 */
202 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
203 String propertyName) {
204 Boolean value = null;
205 try {
206 String s = (String) properties.get(propertyName);
207 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
208 } catch (ClassCastException e) {
209 // No propertyName defined.
210 value = null;
211 }
212 return value;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700213 }
214
alshabib57044ba2014-09-16 15:58:01 -0700215 @Override
tom9b4030d2014-10-06 10:39:03 -0700216 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900217 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700218 return store.getFlowRuleCount();
219 }
220
221 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700222 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900223 checkPermission(FLOWRULE_READ);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700224 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700225 }
226
227 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700228 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900229 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900230
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800231 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700232 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800233 builder.add(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700234 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800235 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700236 }
237
238 @Override
239 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900240 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900241
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800242 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
alshabib57044ba2014-09-16 15:58:01 -0700243 for (int i = 0; i < flowRules.length; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800244 builder.remove(flowRules[i]);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700245 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800246 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700247 }
alshabib57044ba2014-09-16 15:58:01 -0700248
alshabiba68eb962014-09-24 20:34:13 -0700249 @Override
250 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900251 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700252 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700253 }
254
255 @Override
256 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900257 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900258
Madan Jampani6a456162014-10-24 11:36:17 -0700259 Set<FlowRule> flowEntries = Sets.newHashSet();
260 for (Device d : deviceService.getDevices()) {
261 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
262 if (flowEntry.appId() == id.id()) {
263 flowEntries.add(flowEntry);
264 }
265 }
266 }
267 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700268 }
269
270 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800271 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900272 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900273
alshabibaa7e7de2014-11-12 19:20:44 -0800274 Set<FlowRule> matches = Sets.newHashSet();
275 long toLookUp = ((long) appId.id() << 16) | groupId;
276 for (Device d : deviceService.getDevices()) {
277 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
278 if ((flowEntry.id().value() >>> 32) == toLookUp) {
279 matches.add(flowEntry);
280 }
281 }
282 }
283 return matches;
284 }
285
286 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800287 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900288 checkPermission(FLOWRULE_WRITE);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800289 operationsService.submit(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700290 }
291
292 @Override
alshabib57044ba2014-09-16 15:58:01 -0700293 protected FlowRuleProviderService createProviderService(
294 FlowRuleProvider provider) {
295 return new InternalFlowRuleProviderService(provider);
296 }
297
298 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700299 extends AbstractProviderService<FlowRuleProvider>
300 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700301
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700302 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
303
alshabib57044ba2014-09-16 15:58:01 -0700304 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
305 super(provider);
306 }
307
308 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700309 public void flowRemoved(FlowEntry flowEntry) {
310 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700311 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700312 lastSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700313 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700314 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800315 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700316 return;
317 }
alshabib1c319ff2014-10-04 20:29:09 -0700318 Device device = deviceService.getDevice(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700319 FlowRuleProvider frp = getProvider(device.providerId());
320 FlowRuleEvent event = null;
321 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700322 case ADDED:
323 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700324 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700325 break;
326 case PENDING_REMOVE:
327 case REMOVED:
328 event = store.removeFlowRule(stored);
329 break;
330 default:
331 break;
alshabib57044ba2014-09-16 15:58:01 -0700332
alshabiba68eb962014-09-24 20:34:13 -0700333 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700334 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700335 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700336 post(event);
337 }
alshabib57044ba2014-09-16 15:58:01 -0700338 }
339
alshabibba5ac482014-10-02 17:15:20 -0700340
alshabib1c319ff2014-10-04 20:29:09 -0700341 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700342 checkNotNull(flowRule, FLOW_RULE_NULL);
343 checkValidity();
alshabiba68eb962014-09-24 20:34:13 -0700344 Device device = deviceService.getDevice(flowRule.deviceId());
345 FlowRuleProvider frp = getProvider(device.providerId());
alshabibbb42cad2014-09-25 11:43:05 -0700346 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700347 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700348 case PENDING_REMOVE:
349 case REMOVED:
350 event = store.removeFlowRule(flowRule);
tom9b4030d2014-10-06 10:39:03 -0700351 break;
352 case ADDED:
353 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800354 event = store.pendingFlowRule(flowRule);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800355 try {
356 frp.applyFlowRule(flowRule);
357 } catch (UnsupportedOperationException e) {
358 log.warn(e.getMessage());
359 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800360 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800361 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
362 }
363 }
tom9b4030d2014-10-06 10:39:03 -0700364 break;
365 default:
366 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700367 }
368
alshabibbb42cad2014-09-25 11:43:05 -0700369 if (event != null) {
370 log.debug("Flow {} removed", flowRule);
371 post(event);
372 }
alshabib57044ba2014-09-16 15:58:01 -0700373 }
374
alshabibba5ac482014-10-02 17:15:20 -0700375 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700376 checkNotNull(flowRule, FLOW_RULE_NULL);
377 checkValidity();
alshabib2374fc92014-10-22 11:03:23 -0700378 FlowRuleProvider frp = getProvider(flowRule.deviceId());
379 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700380 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700381 }
382
alshabib1c319ff2014-10-04 20:29:09 -0700383 private void flowAdded(FlowEntry flowEntry) {
384 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700385 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700386
alshabib1c319ff2014-10-04 20:29:09 -0700387 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700388 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700389 if (event == null) {
390 log.debug("No flow store event generated.");
391 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800392 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700393 post(event);
394 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700395 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800396 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700397 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700398 }
alshabib57044ba2014-09-16 15:58:01 -0700399 }
400
alshabib1c319ff2014-10-04 20:29:09 -0700401 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
402 if (storedRule == null) {
403 return false;
404 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700405 if (storedRule.isPermanent()) {
406 return true;
407 }
408
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700409 final long timeout = storedRule.timeout() * 1000;
410 final long currentTime = System.currentTimeMillis();
alshabib85c41972014-10-03 13:48:39 -0700411 if (storedRule.packets() != swRule.packets()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700412 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700413 return true;
414 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700415 if (!lastSeen.containsKey(storedRule)) {
416 // checking for the first time
417 lastSeen.put(storedRule, storedRule.lastSeen());
418 // Use following if lastSeen attr. was removed.
419 //lastSeen.put(storedRule, currentTime);
420 }
421 Long last = lastSeen.get(storedRule);
422 if (last == null) {
423 // concurrently removed? let the liveness check fail
424 return false;
425 }
alshabib85c41972014-10-03 13:48:39 -0700426
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700427 if ((currentTime - last) <= timeout) {
alshabibc274c902014-10-03 14:58:27 -0700428 return true;
429 }
430 return false;
alshabibba5ac482014-10-02 17:15:20 -0700431 }
432
alshabib5c370ff2014-09-18 10:12:14 -0700433 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700434 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900435 pushFlowMetricsInternal(deviceId, flowEntries, true);
436 }
437
438 @Override
439 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
440 pushFlowMetricsInternal(deviceId, flowEntries, false);
441 }
442
443 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
444 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700445 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
446 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
447
Saurav Dasfa2fa932015-03-03 11:29:48 -0800448 for (FlowEntry rule : flowEntries) {
449 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700450 FlowEntry storedRule = storedRules.remove(rule);
451 if (storedRule != null) {
452 if (storedRule.exactMatch(rule)) {
453 // we both have the rule, let's update some info then.
454 flowAdded(rule);
455 } else {
456 // the two rules are not an exact match - remove the
457 // switch's rule and install our rule
458 extraneousFlow(rule);
459 flowMissing(storedRule);
460 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800461 } else {
462 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700463 if (!allowExtraneousRules) {
464 extraneousFlow(rule);
465 }
alshabib93cb57f2015-02-12 17:43:26 -0800466 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700467 } catch (Exception e) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800468 log.debug("Can't process added or extra rule {}", e.getMessage());
469 continue;
alshabib93cb57f2015-02-12 17:43:26 -0800470 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800471 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900472
473 // DO NOT reinstall
474 if (useMissingFlow) {
475 for (FlowEntry rule : storedRules.keySet()) {
476 try {
477 // there are rules in the store that aren't on the switch
478 log.debug("Adding rule in store, but not on switch {}", rule);
479 flowMissing(rule);
480 } catch (Exception e) {
Jonathan Hart26a8d952015-12-02 15:16:35 -0800481 log.debug("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900482 continue;
483 }
alshabib93cb57f2015-02-12 17:43:26 -0800484 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800485 }
alshabib5c370ff2014-09-18 10:12:14 -0700486 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800487
488 @Override
489 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
490 store.batchOperationComplete(FlowRuleBatchEvent.completed(
491 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
492 operation
493 ));
494 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700495
496 @Override
497 public void pushTableStatistics(DeviceId deviceId,
498 List<TableStatisticsEntry> tableStats) {
499 store.updateTableStatistics(deviceId, tableStats);
500 }
alshabib57044ba2014-09-16 15:58:01 -0700501 }
502
tomc78acee2014-09-24 15:16:55 -0700503 // Store delegate to re-post events emitted from the store.
504 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800505
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800506
Madan Jampani117aaae2014-10-23 10:04:05 -0700507 // TODO: Right now we only dispatch events at individual flowEntry level.
508 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700509 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700510 public void notify(FlowRuleBatchEvent event) {
511 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700512 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700513 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800514 // Request has been forwarded to MASTER Node, and was
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800515 request.ops().stream().forEach(
516 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800517 switch (op.operator()) {
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700518
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800519 case ADD:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700520 post(new FlowRuleEvent(RULE_ADD_REQUESTED,
521 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800522 break;
523 case REMOVE:
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700524 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED,
525 op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800526 break;
527 case MODIFY:
528 //TODO: do something here when the time comes.
529 break;
530 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800531 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800532 }
533 }
534 );
535
536 DeviceId deviceId = event.deviceId();
537
538 FlowRuleBatchOperation batchOperation =
539 request.asBatchOperation(deviceId);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700540
Thomas Vachuska27bee092015-06-23 19:03:10 -0700541 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
542 if (flowRuleProvider != null) {
543 flowRuleProvider.executeBatch(batchOperation);
544 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800545
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800546 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700547
Madan Jampani117aaae2014-10-23 10:04:05 -0700548 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800549
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800550 FlowOperationsProcessor fops = pendingFlowOperations.remove(
551 event.subject().batchId());
552 if (event.result().isSuccess()) {
553 if (fops != null) {
554 fops.satisfy(event.deviceId());
555 }
556 } else {
557 fops.fail(event.deviceId(), event.result().failedItems());
558 }
559
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700560 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800561
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700562 default:
563 break;
564 }
tomc78acee2014-09-24 15:16:55 -0700565 }
566 }
alshabib902d41b2014-10-07 16:52:05 -0700567
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800568 private class FlowOperationsProcessor implements Runnable {
alshabib902d41b2014-10-07 16:52:05 -0700569
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800570 private final List<Set<FlowRuleOperation>> stages;
571 private final FlowRuleOperationsContext context;
572 private final FlowRuleOperations fops;
573 private final AtomicBoolean hasFailed = new AtomicBoolean(false);
alshabib902d41b2014-10-07 16:52:05 -0700574
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800575 private Set<DeviceId> pendingDevices;
alshabib902d41b2014-10-07 16:52:05 -0700576
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800577 public FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800578 this.stages = Lists.newArrayList(ops.stages());
579 this.context = ops.callback();
580 this.fops = ops;
581 pendingDevices = Sets.newConcurrentHashSet();
alshabib902d41b2014-10-07 16:52:05 -0700582 }
583
584 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800585 public void run() {
586 if (stages.size() > 0) {
587 process(stages.remove(0));
588 } else if (!hasFailed.get() && context != null) {
589 context.onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700590 }
591 }
592
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800593 private void process(Set<FlowRuleOperation> ops) {
594 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches =
595 ArrayListMultimap.create();
596
597 FlowRuleBatchEntry fbe;
598 for (FlowRuleOperation flowRuleOperation : ops) {
599 switch (flowRuleOperation.type()) {
600 // FIXME: Brian needs imagination when creating class names.
601 case ADD:
602 fbe = new FlowRuleBatchEntry(
603 FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule());
604 break;
605 case MODIFY:
606 fbe = new FlowRuleBatchEntry(
607 FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule());
608 break;
609 case REMOVE:
610 fbe = new FlowRuleBatchEntry(
611 FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule());
612 break;
613 default:
614 throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type());
615 }
616 pendingDevices.add(flowRuleOperation.rule().deviceId());
617 perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe);
618 }
619
620
621 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700622 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800623 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
624 deviceId, id);
625 pendingFlowOperations.put(id, this);
Sho SHIMIZUf88e5932015-05-27 12:03:51 -0700626 deviceInstallers.submit(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700627 }
628 }
629
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800630 public void satisfy(DeviceId devId) {
631 pendingDevices.remove(devId);
632 if (pendingDevices.isEmpty()) {
633 operationsService.submit(this);
alshabib193525b2014-10-08 18:58:03 -0700634 }
alshabib193525b2014-10-08 18:58:03 -0700635 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800636
637
638
639 public void fail(DeviceId devId, Set<? extends FlowRule> failures) {
640 hasFailed.set(true);
641 pendingDevices.remove(devId);
642 if (pendingDevices.isEmpty()) {
643 operationsService.submit(this);
644 }
645
646 if (context != null) {
647 final FlowRuleOperations.Builder failedOpsBuilder =
648 FlowRuleOperations.builder();
649 failures.stream().forEach(failedOpsBuilder::add);
650
651 context.onError(failedOpsBuilder.build());
652 }
653 }
654
alshabib902d41b2014-10-07 16:52:05 -0700655 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700656
657 @Override
658 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
659 checkPermission(FLOWRULE_READ);
660 return store.getTableStatistics(deviceId);
661 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800662
663 private class InternalDeviceListener implements DeviceListener {
664 @Override
665 public void event(DeviceEvent event) {
666 switch (event.type()) {
667 case DEVICE_REMOVED:
668 case DEVICE_AVAILABILITY_CHANGED:
669 DeviceId deviceId = event.subject().id();
670 if (!deviceService.isAvailable(deviceId)) {
671 if (purgeOnDisconnection) {
672 store.purgeFlowRule(deviceId);
673 }
674 }
675 break;
676 default:
677 break;
678 }
679 }
680 }
alshabib57044ba2014-09-16 15:58:01 -0700681}