blob: a5d535e85317d3d937246a3a0593ac732873fcc5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
alshabib57044ba2014-09-16 15:58:01 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070027import org.apache.felix.scr.annotations.Modified;
28import org.apache.felix.scr.annotations.Property;
alshabib57044ba2014-09-16 15:58:01 -070029import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
Jian Lid9b5f552016-03-11 18:15:31 -080032import org.onlab.util.Tools;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070033import org.onosproject.cfg.ComponentConfigService;
pier7aceddf2019-12-19 16:04:23 +010034import org.onosproject.cluster.ClusterService;
35import org.onosproject.cluster.NodeId;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080037import org.onosproject.core.CoreService;
38import org.onosproject.core.IdGenerator;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080039import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080042import org.onosproject.net.device.DeviceEvent;
43import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.device.DeviceService;
Jordan Halterman4f3193a2017-07-11 20:32:49 -070045import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.flow.CompletedBatchOperation;
Charles M.C. Chan1229eca2015-05-18 06:27:52 +080047import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.flow.FlowEntry;
49import org.onosproject.net.flow.FlowRule;
Ray Milkey7bf273c2017-09-27 16:15:15 -070050import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
51import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent;
52import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
53import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.flow.FlowRuleEvent;
55import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080056import org.onosproject.net.flow.FlowRuleOperation;
57import org.onosproject.net.flow.FlowRuleOperations;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080058import org.onosproject.net.flow.FlowRuleProgrammable;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.flow.FlowRuleProvider;
60import org.onosproject.net.flow.FlowRuleProviderRegistry;
61import org.onosproject.net.flow.FlowRuleProviderService;
62import org.onosproject.net.flow.FlowRuleService;
63import org.onosproject.net.flow.FlowRuleStore;
64import org.onosproject.net.flow.FlowRuleStoreDelegate;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070065import org.onosproject.net.flow.TableStatisticsEntry;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080066import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080067import org.onosproject.net.provider.AbstractProviderService;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080068import org.onosproject.net.provider.ProviderId;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070069import org.osgi.service.component.ComponentContext;
alshabib57044ba2014-09-16 15:58:01 -070070import org.slf4j.Logger;
71
Brian O'Connord12267c2015-02-17 18:17:08 -080072import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070073import java.util.Dictionary;
Sho SHIMIZUb837cb72016-08-30 14:44:50 -070074import java.util.HashSet;
Brian O'Connord12267c2015-02-17 18:17:08 -080075import java.util.List;
76import java.util.Map;
pier7aceddf2019-12-19 16:04:23 +010077import java.util.Objects;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080078import java.util.Optional;
Brian O'Connord12267c2015-02-17 18:17:08 -080079import java.util.Set;
80import java.util.concurrent.ConcurrentHashMap;
81import java.util.concurrent.ExecutorService;
82import java.util.concurrent.Executors;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080083
Thomas Vachuska9b2da212014-11-10 19:30:25 -080084import static com.google.common.base.Preconditions.checkNotNull;
Charles Chan0c7c43b2016-01-14 17:39:20 -080085import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080086import static org.onlab.util.Tools.get;
Brian O'Connord12267c2015-02-17 18:17:08 -080087import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070088import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
89import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090090import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080091import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ;
92import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070093import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090094
tome4729872014-09-23 00:37:37 -070095/**
96 * Provides implementation of the flow NB & SB APIs.
97 */
Sho SHIMIZU5c396e32016-08-12 15:19:12 -070098@Component(immediate = true)
alshabib57044ba2014-09-16 15:58:01 -070099@Service
tom202175a2014-09-19 19:00:11 -0700100public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700101 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
102 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -0700103 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -0700104
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800105 private final Logger log = getLogger(getClass());
106
Thomas Vachuska227943d2018-03-19 09:37:28 -0700107 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
108 private static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700109 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
110
111 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
112 label = "Allow flow rules in switch not installed by ONOS")
113 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
114
Charles Chan0c7c43b2016-01-14 17:39:20 -0800115 @Property(name = "purgeOnDisconnection", boolValue = false,
116 label = "Purge entries associated with a device when the device goes offline")
117 private boolean purgeOnDisconnection = false;
118
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800119 private static final int DEFAULT_POLL_FREQUENCY = 30;
120 @Property(name = "fallbackFlowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
121 label = "Frequency (in seconds) for polling flow statistics via fallback provider")
122 private int fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
alshabib57044ba2014-09-16 15:58:01 -0700123
alshabibbb42cad2014-09-25 11:43:05 -0700124 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800125 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700126
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800127 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800128
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800129 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800130 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800131
132 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700133 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800134
135 private IdGenerator idGenerator;
136
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700137 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700138
pier7aceddf2019-12-19 16:04:23 +0100139 private NodeId local;
140
tombe988312014-09-19 18:38:47 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700143
alshabib57044ba2014-09-16 15:58:01 -0700144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700145 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700146
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
148 protected CoreService coreService;
149
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800151 protected MastershipService mastershipService;
152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700154 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800155
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
157 protected DriverService driverService;
158
pier7aceddf2019-12-19 16:04:23 +0100159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
160 protected ClusterService clusterService;
161
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700162 @Activate
163 public void activate(ComponentContext context) {
tomc78acee2014-09-24 15:16:55 -0700164 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700165 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800166 deviceService.addListener(deviceListener);
167 cfgService.registerProperties(getClass());
pierddda5312019-04-16 11:48:08 +0200168 modified(context);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800169 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
pier7aceddf2019-12-19 16:04:23 +0100170 local = clusterService.getLocalNode().id();
alshabib57044ba2014-09-16 15:58:01 -0700171 log.info("Started");
172 }
173
174 @Deactivate
175 public void deactivate() {
Andrea Campanella5a3c09c2017-12-01 13:57:48 +0100176 driverProvider.terminate();
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700177 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700178 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800179 deviceInstallers.shutdownNow();
180 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700181 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700182 eventDispatcher.removeSink(FlowRuleEvent.class);
183 log.info("Stopped");
184 }
185
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700186 @Modified
187 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800188 if (context != null) {
189 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700190 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800191 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800192 deviceService, mastershipService, fallbackFlowPollFrequency);
193 }
194
195 @Override
196 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800197 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800198 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700199
Charles Chan0c7c43b2016-01-14 17:39:20 -0800200 /**
201 * Extracts properties from the component configuration context.
202 *
203 * @param context the component context
204 */
205 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700206 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800207 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700208
Jian Lid9b5f552016-03-11 18:15:31 -0800209 flag = Tools.isPropertyEnabled(properties, "allowExtraneousRules");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800210 if (flag == null) {
211 log.info("AllowExtraneousRules is not configured, " +
212 "using current value of {}", allowExtraneousRules);
213 } else {
214 allowExtraneousRules = flag;
215 log.info("Configured. AllowExtraneousRules is {}",
216 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700217 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800218
Jian Lid9b5f552016-03-11 18:15:31 -0800219 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800220 if (flag == null) {
221 log.info("PurgeOnDisconnection is not configured, " +
222 "using current value of {}", purgeOnDisconnection);
223 } else {
224 purgeOnDisconnection = flag;
225 log.info("Configured. PurgeOnDisconnection is {}",
226 purgeOnDisconnection ? "enabled" : "disabled");
227 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800228
229 String s = get(properties, "fallbackFlowPollFrequency");
230 try {
231 fallbackFlowPollFrequency = isNullOrEmpty(s) ? DEFAULT_POLL_FREQUENCY : Integer.parseInt(s);
232 } catch (NumberFormatException e) {
233 fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
234 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800235 }
236
alshabib57044ba2014-09-16 15:58:01 -0700237 @Override
tom9b4030d2014-10-06 10:39:03 -0700238 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900239 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700240 return store.getFlowRuleCount();
241 }
242
243 @Override
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800244 public int getFlowRuleCount(DeviceId deviceId) {
245 checkPermission(FLOWRULE_READ);
Thomas Vachuska227943d2018-03-19 09:37:28 -0700246 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800247 return store.getFlowRuleCount(deviceId);
248 }
249
250 @Override
Jordan Halterman7a9bfb82019-03-04 18:12:20 -0800251 public int getFlowRuleCount(DeviceId deviceId, FlowEntry.FlowEntryState state) {
252 checkPermission(FLOWRULE_READ);
253 checkNotNull(deviceId, "Device ID cannot be null");
254 return store.getFlowRuleCount(deviceId, state);
255 }
256
257 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700258 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900259 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800260 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700261 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700262 }
263
264 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700265 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900266 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900267
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800268 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800269 for (FlowRule flowRule : flowRules) {
270 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700271 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800272 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700273 }
274
275 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530276 public void purgeFlowRules(DeviceId deviceId) {
277 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800278 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530279 store.purgeFlowRule(deviceId);
280 }
281
282 @Override
alshabib57044ba2014-09-16 15:58:01 -0700283 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900284 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900285
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800286 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800287 for (FlowRule flowRule : flowRules) {
288 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700289 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800290 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700291 }
alshabib57044ba2014-09-16 15:58:01 -0700292
alshabiba68eb962014-09-24 20:34:13 -0700293 @Override
294 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900295 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700296 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700297 }
298
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530299 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700300 @Override
301 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900302 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900303
Madan Jampani6a456162014-10-24 11:36:17 -0700304 Set<FlowRule> flowEntries = Sets.newHashSet();
305 for (Device d : deviceService.getDevices()) {
306 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
307 if (flowEntry.appId() == id.id()) {
308 flowEntries.add(flowEntry);
309 }
310 }
311 }
312 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700313 }
314
315 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530316 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
317 checkPermission(FLOWRULE_READ);
318
319 Set<FlowEntry> flowEntries = Sets.newHashSet();
320 for (Device d : deviceService.getDevices()) {
321 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
322 if (flowEntry.appId() == id.id()) {
323 flowEntries.add(flowEntry);
324 }
325 }
326 }
327 return flowEntries;
328 }
329
330 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800331 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900332 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900333
alshabibaa7e7de2014-11-12 19:20:44 -0800334 Set<FlowRule> matches = Sets.newHashSet();
335 long toLookUp = ((long) appId.id() << 16) | groupId;
336 for (Device d : deviceService.getDevices()) {
337 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
338 if ((flowEntry.id().value() >>> 32) == toLookUp) {
339 matches.add(flowEntry);
340 }
341 }
342 }
343 return matches;
344 }
345
346 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800347 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900348 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700349 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700350 }
351
352 @Override
alshabib57044ba2014-09-16 15:58:01 -0700353 protected FlowRuleProviderService createProviderService(
354 FlowRuleProvider provider) {
355 return new InternalFlowRuleProviderService(provider);
356 }
357
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800358 @Override
359 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
360 log.warn("should not be calling getProvider(ProviderId)");
361 return super.getProvider(pid);
362 }
363
364 /**
365 * {@inheritDoc}
366 * if the Device does not support {@link FlowRuleProgrammable}.
367 */
368 @Override
369 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800370 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800371 // if device supports FlowRuleProgrammable,
372 // use FlowRuleProgrammable via FlowRuleDriverProvider
373 return Optional.ofNullable(deviceService.getDevice(deviceId))
374 .filter(dev -> dev.is(FlowRuleProgrammable.class))
375 .<FlowRuleProvider>map(x -> driverProvider)
376 .orElseGet(() -> super.getProvider(deviceId));
377 }
378
alshabib57044ba2014-09-16 15:58:01 -0700379 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700380 extends AbstractProviderService<FlowRuleProvider>
381 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700382
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700383 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700384 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
385
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700386
alshabib57044ba2014-09-16 15:58:01 -0700387 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
388 super(provider);
389 }
390
391 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700392 public void flowRemoved(FlowEntry flowEntry) {
393 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700394 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700395 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700396 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700397 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700398 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800399 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700400 return;
401 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700402 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
403 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
404 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800405 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700406 FlowRuleEvent event = null;
407 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700408 case ADDED:
409 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700410 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700411 break;
412 case PENDING_REMOVE:
413 case REMOVED:
414 event = store.removeFlowRule(stored);
415 break;
416 default:
417 break;
alshabib57044ba2014-09-16 15:58:01 -0700418
alshabiba68eb962014-09-24 20:34:13 -0700419 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700420 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700421 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700422 post(event);
423 }
alshabib57044ba2014-09-16 15:58:01 -0700424 }
425
alshabibba5ac482014-10-02 17:15:20 -0700426
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530427 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700428 checkNotNull(flowRule, FLOW_RULE_NULL);
429 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800430 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700431 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700432 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700433 case PENDING_REMOVE:
434 case REMOVED:
435 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530436 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700437 break;
438 case ADDED:
439 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800440 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530441 if (isFlowOnlyInStore) {
442 // Publishing RULE_ADD_REQUESTED event facilitates
443 // preparation of statistics for the concerned rule
444 if (event == null) {
445 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
446 }
447 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800448 try {
449 frp.applyFlowRule(flowRule);
450 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700451 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800452 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800453 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800454 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
455 }
456 }
tom9b4030d2014-10-06 10:39:03 -0700457 break;
458 default:
459 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700460 }
461
alshabibbb42cad2014-09-25 11:43:05 -0700462 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700463 post(event);
464 }
alshabib57044ba2014-09-16 15:58:01 -0700465 }
466
alshabibba5ac482014-10-02 17:15:20 -0700467 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700468 checkNotNull(flowRule, FLOW_RULE_NULL);
469 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800470 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700471 FlowRuleProvider frp = getProvider(flowRule.deviceId());
472 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700473 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700474 }
475
pier7aceddf2019-12-19 16:04:23 +0100476 private boolean flowAdded(FlowEntry flowEntry) {
alshabib1c319ff2014-10-04 20:29:09 -0700477 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700478 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700479
alshabib1c319ff2014-10-04 20:29:09 -0700480 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700481 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700482 if (event == null) {
483 log.debug("No flow store event generated.");
pier7aceddf2019-12-19 16:04:23 +0100484 return false;
alshabibba5ac482014-10-02 17:15:20 -0700485 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800486 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700487 post(event);
488 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700489 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800490 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700491 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700492 }
pier7aceddf2019-12-19 16:04:23 +0100493 return true;
alshabib57044ba2014-09-16 15:58:01 -0700494 }
495
alshabib1c319ff2014-10-04 20:29:09 -0700496 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
497 if (storedRule == null) {
498 return false;
499 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700500 if (storedRule.isPermanent()) {
501 return true;
502 }
503
Ray Milkey3717e602018-02-01 13:49:47 -0800504 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700505 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700506
507 // Checking flow with hardTimeout
508 if (storedRule.hardTimeout() != 0) {
509 if (!firstSeen.containsKey(storedRule)) {
510 // First time rule adding
511 firstSeen.put(storedRule, currentTime);
512 } else {
513 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800514 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700515 if ((currentTime - first) > hardTimeout) {
516 return false;
517 }
518 }
519 }
520
Gamze Abaka8d03f182017-07-20 15:09:24 +0000521 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700522 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700523 return true;
524 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700525 if (!lastSeen.containsKey(storedRule)) {
526 // checking for the first time
527 lastSeen.put(storedRule, storedRule.lastSeen());
528 // Use following if lastSeen attr. was removed.
529 //lastSeen.put(storedRule, currentTime);
530 }
531 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700532
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800533 // concurrently removed? let the liveness check fail
534 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700535 }
536
alshabib5c370ff2014-09-18 10:12:14 -0700537 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700538 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900539 pushFlowMetricsInternal(deviceId, flowEntries, true);
540 }
541
542 @Override
543 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
544 pushFlowMetricsInternal(deviceId, flowEntries, false);
545 }
546
547 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
548 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700549 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
550 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
pier7aceddf2019-12-19 16:04:23 +0100551 NodeId master;
552 boolean done;
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700553
pier7aceddf2019-12-19 16:04:23 +0100554 // Processing flow rules
Saurav Dasfa2fa932015-03-03 11:29:48 -0800555 for (FlowEntry rule : flowEntries) {
556 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700557 FlowEntry storedRule = storedRules.remove(rule);
558 if (storedRule != null) {
559 if (storedRule.exactMatch(rule)) {
560 // we both have the rule, let's update some info then.
pier7aceddf2019-12-19 16:04:23 +0100561 done = flowAdded(rule);
562 if (!done) {
563 // Mastership change can occur during this iteration
564 master = mastershipService.getMasterFor(deviceId);
565 if (!Objects.equals(local, master)) {
566 log.warn("Tried to update the flow stats while the node was not the master");
567 return;
568 }
569 }
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700570 } else {
pier7aceddf2019-12-19 16:04:23 +0100571 // Mastership change can occur during this iteration
572 master = mastershipService.getMasterFor(deviceId);
573 if (!Objects.equals(local, master)) {
574 log.warn("Tried to update the flows while the node was not the master");
575 return;
576 }
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700577 // the two rules are not an exact match - remove the
578 // switch's rule and install our rule
579 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530580 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700581 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800582 } else {
583 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700584 if (!allowExtraneousRules) {
pier7aceddf2019-12-19 16:04:23 +0100585 // Mastership change can occur during this iteration
586 master = mastershipService.getMasterFor(deviceId);
587 if (!Objects.equals(local, master)) {
588 log.warn("Tried to remove flows while the node was not the master");
589 return;
590 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700591 extraneousFlow(rule);
592 }
alshabib93cb57f2015-02-12 17:43:26 -0800593 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700594 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700595 log.warn("Can't process added or extra rule {} for device {}:{}",
596 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800597 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800598 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900599
600 // DO NOT reinstall
601 if (useMissingFlow) {
602 for (FlowEntry rule : storedRules.keySet()) {
pier7aceddf2019-12-19 16:04:23 +0100603 // Mastership change can occur during this iteration
604 master = mastershipService.getMasterFor(deviceId);
605 if (!Objects.equals(local, master)) {
606 log.warn("Tried to install missing rules while the node was not the master");
607 return;
608 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900609 try {
610 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530611 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
612 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900613 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700614 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900615 }
alshabib93cb57f2015-02-12 17:43:26 -0800616 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800617 }
alshabib5c370ff2014-09-18 10:12:14 -0700618 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800619
620 @Override
621 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
622 store.batchOperationComplete(FlowRuleBatchEvent.completed(
623 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
624 operation
625 ));
626 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700627
628 @Override
629 public void pushTableStatistics(DeviceId deviceId,
630 List<TableStatisticsEntry> tableStats) {
631 store.updateTableStatistics(deviceId, tableStats);
632 }
alshabib57044ba2014-09-16 15:58:01 -0700633 }
634
tomc78acee2014-09-24 15:16:55 -0700635 // Store delegate to re-post events emitted from the store.
636 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800637
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800638
Madan Jampani117aaae2014-10-23 10:04:05 -0700639 // TODO: Right now we only dispatch events at individual flowEntry level.
640 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700641 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700642 public void notify(FlowRuleBatchEvent event) {
643 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700644 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700645 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800646 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700647 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800648 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800649 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800650 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700651 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800652 break;
653 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700654 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800655 break;
656 case MODIFY:
657 //TODO: do something here when the time comes.
658 break;
659 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800660 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800661 }
662 }
663 );
664
665 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700666 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800667 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700668 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
669 if (flowRuleProvider != null) {
pier93702472019-03-11 15:14:02 -0700670 log.trace("Sending {} flow rules to {}", batchOperation.size(), deviceId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700671 flowRuleProvider.executeBatch(batchOperation);
672 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800673
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800674 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700675
Madan Jampani117aaae2014-10-23 10:04:05 -0700676 case BATCH_OPERATION_COMPLETED:
pier1b408482019-02-19 10:16:54 +0100677 // Operation completed, let's retrieve the processor and trigger the callback
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800678 FlowOperationsProcessor fops = pendingFlowOperations.remove(
679 event.subject().batchId());
pier1b408482019-02-19 10:16:54 +0100680 if (fops != null) {
681 if (event.result().isSuccess()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800682 fops.satisfy(event.deviceId());
pier1b408482019-02-19 10:16:54 +0100683 } else {
684 fops.fail(event.deviceId(), event.result().failedItems());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800685 }
686 } else {
pier1b408482019-02-19 10:16:54 +0100687 log.warn("Unable to find flow operations processor for batch: {}", event.subject().batchId());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800688 }
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700689 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800690
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700691 default:
692 break;
693 }
tomc78acee2014-09-24 15:16:55 -0700694 }
695 }
alshabib902d41b2014-10-07 16:52:05 -0700696
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700697 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
698 switch (input) {
699 case ADD:
700 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
701 case MODIFY:
702 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
703 case REMOVE:
704 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
705 default:
706 throw new UnsupportedOperationException("Unknown flow rule type " + input);
707 }
708 }
709
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800710 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700711 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800712 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700713
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700714 // Mutable
715 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700716 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700717 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700718
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700719 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800720 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800721 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700722 }
723
724 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700725 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800726 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800727 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700728 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700729 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700730 }
731 }
732
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800733 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700734 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800735
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700736 for (FlowRuleOperation op : ops) {
737 perDeviceBatches.put(op.rule().deviceId(),
738 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800739 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000740 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800741
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800742 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700743 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800744 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
745 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000746 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700747 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700748 }
749 }
750
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700751 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000752 pendingDevices.remove(devId);
753 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700754 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700755 }
alshabib193525b2014-10-08 18:58:03 -0700756 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800757
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700758 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700759 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000760 pendingDevices.remove(devId);
761 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700762 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800763 }
764
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700765 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
766 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800767
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700768 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800769 }
alshabib902d41b2014-10-07 16:52:05 -0700770 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700771
772 @Override
773 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
774 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800775 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700776 return store.getTableStatistics(deviceId);
777 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800778
Patryk Konopka7e40c012017-06-06 13:38:06 +0200779 @Override
780 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800781 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200782 return store.getActiveFlowRuleCount(deviceId);
783 }
784
Charles Chan0c7c43b2016-01-14 17:39:20 -0800785 private class InternalDeviceListener implements DeviceListener {
786 @Override
787 public void event(DeviceEvent event) {
788 switch (event.type()) {
789 case DEVICE_REMOVED:
790 case DEVICE_AVAILABILITY_CHANGED:
791 DeviceId deviceId = event.subject().id();
792 if (!deviceService.isAvailable(deviceId)) {
793 if (purgeOnDisconnection) {
794 store.purgeFlowRule(deviceId);
795 }
796 }
797 break;
798 default:
799 break;
800 }
801 }
802 }
alshabib57044ba2014-09-16 15:58:01 -0700803}