blob: bdc48acfa95ba314c9cacb0434558fcb0746d916 [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;
Jian Lid9b5f552016-03-11 18:15:31 -080024import org.onlab.util.Tools;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070025import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080027import org.onosproject.core.CoreService;
28import org.onosproject.core.IdGenerator;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080029import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080032import org.onosproject.net.device.DeviceEvent;
33import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.device.DeviceService;
Jordan Halterman4f3193a2017-07-11 20:32:49 -070035import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.CompletedBatchOperation;
Charles M.C. Chan1229eca2015-05-18 06:27:52 +080037import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.FlowEntry;
39import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.flow.FlowRuleEvent;
41import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080042import org.onosproject.net.flow.FlowRuleOperation;
43import org.onosproject.net.flow.FlowRuleOperations;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080044import org.onosproject.net.flow.FlowRuleProgrammable;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.flow.FlowRuleProvider;
46import org.onosproject.net.flow.FlowRuleProviderRegistry;
47import org.onosproject.net.flow.FlowRuleProviderService;
48import org.onosproject.net.flow.FlowRuleService;
49import org.onosproject.net.flow.FlowRuleStore;
50import org.onosproject.net.flow.FlowRuleStoreDelegate;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070051import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
53import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent;
54import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
55import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080056import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.provider.AbstractProviderService;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080058import org.onosproject.net.provider.ProviderId;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070059import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060import org.osgi.service.component.annotations.Activate;
61import org.osgi.service.component.annotations.Component;
62import org.osgi.service.component.annotations.Deactivate;
63import org.osgi.service.component.annotations.Modified;
64import org.osgi.service.component.annotations.Reference;
65import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib57044ba2014-09-16 15:58:01 -070066import org.slf4j.Logger;
67
Brian O'Connord12267c2015-02-17 18:17:08 -080068import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070069import java.util.Dictionary;
Sho SHIMIZUb837cb72016-08-30 14:44:50 -070070import java.util.HashSet;
Brian O'Connord12267c2015-02-17 18:17:08 -080071import java.util.List;
72import java.util.Map;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080073import java.util.Optional;
Brian O'Connord12267c2015-02-17 18:17:08 -080074import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
76import java.util.concurrent.ExecutorService;
77import java.util.concurrent.Executors;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080078
Thomas Vachuska9b2da212014-11-10 19:30:25 -080079import static com.google.common.base.Preconditions.checkNotNull;
Charles Chan0c7c43b2016-01-14 17:39:20 -080080import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080081import static org.onlab.util.Tools.get;
Brian O'Connord12267c2015-02-17 18:17:08 -080082import static org.onlab.util.Tools.groupedThreads;
Ray Milkeyd04e2272018-10-16 18:20:18 -070083import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES;
84import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES_DEFAULT;
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +010085import static org.onosproject.net.OsgiPropertyConstants.IMPORT_EXTRANEOUS_RULES;
86import static org.onosproject.net.OsgiPropertyConstants.IMPORT_EXTRANEOUS_RULES_DEFAULT;
Ray Milkeyd04e2272018-10-16 18:20:18 -070087import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY;
88import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY_DEFAULT;
89import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION;
90import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION_DEFAULT;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070091import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
92import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090093import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080094import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ;
95import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070096import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090097
tome4729872014-09-23 00:37:37 -070098/**
99 * Provides implementation of the flow NB & SB APIs.
100 */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700101@Component(
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100102 immediate = true,
103 service = {
104 FlowRuleService.class,
105 FlowRuleProviderRegistry.class
106 },
107 property = {
108 ALLOW_EXTRANEOUS_RULES + ":Boolean=" + ALLOW_EXTRANEOUS_RULES_DEFAULT,
109 IMPORT_EXTRANEOUS_RULES + ":Boolean=" + IMPORT_EXTRANEOUS_RULES_DEFAULT,
110 PURGE_ON_DISCONNECTION + ":Boolean=" + PURGE_ON_DISCONNECTION_DEFAULT,
111 POLL_FREQUENCY + ":Integer=" + POLL_FREQUENCY_DEFAULT
112 }
Ray Milkeyd04e2272018-10-16 18:20:18 -0700113)
tom202175a2014-09-19 19:00:11 -0700114public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700115 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100116 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -0700117 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -0700118
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800119 private final Logger log = getLogger(getClass());
120
Thomas Vachuska227943d2018-03-19 09:37:28 -0700121 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
122 private static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700123
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700124 /** Allow flow rules in switch not installed by ONOS. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700125 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES_DEFAULT;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700126
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100127 /** Allow to import flow rules in switch not installed by ONOS. */
128 private boolean importExtraneousRules = IMPORT_EXTRANEOUS_RULES_DEFAULT;
129
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700130 /** Purge entries associated with a device when the device goes offline. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700131 private boolean purgeOnDisconnection = PURGE_ON_DISCONNECTION_DEFAULT;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800132
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700133 /** Frequency (in seconds) for polling flow statistics via fallback provider. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700134 private int fallbackFlowPollFrequency = POLL_FREQUENCY_DEFAULT;
alshabib57044ba2014-09-16 15:58:01 -0700135
alshabibbb42cad2014-09-25 11:43:05 -0700136 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800137 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700138
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800139 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800140
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800141 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800142 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800143
144 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700145 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800146
147 private IdGenerator idGenerator;
148
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700149 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700150
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tombe988312014-09-19 18:38:47 -0700152 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700153
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700154 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700155 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700156
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700157 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800158 protected CoreService coreService;
159
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700160 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800161 protected MastershipService mastershipService;
162
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700163 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700164 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800165
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700166 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700167 protected DriverService driverService;
168
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700169 @Activate
170 public void activate(ComponentContext context) {
tomc78acee2014-09-24 15:16:55 -0700171 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700172 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800173 deviceService.addListener(deviceListener);
174 cfgService.registerProperties(getClass());
piered2a0a42019-04-16 11:48:08 +0200175 modified(context);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800176 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
alshabib57044ba2014-09-16 15:58:01 -0700177 log.info("Started");
178 }
179
180 @Deactivate
181 public void deactivate() {
Andrea Campanella5a3c09c2017-12-01 13:57:48 +0100182 driverProvider.terminate();
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700183 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700184 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800185 deviceInstallers.shutdownNow();
186 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700187 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700188 eventDispatcher.removeSink(FlowRuleEvent.class);
189 log.info("Stopped");
190 }
191
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700192 @Modified
193 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800194 if (context != null) {
195 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700196 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800197 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100198 deviceService, mastershipService, fallbackFlowPollFrequency);
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800199 }
200
201 @Override
202 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800203 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800204 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700205
Charles Chan0c7c43b2016-01-14 17:39:20 -0800206 /**
207 * Extracts properties from the component configuration context.
208 *
209 * @param context the component context
210 */
211 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700212 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800213 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700214
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700215 flag = Tools.isPropertyEnabled(properties, ALLOW_EXTRANEOUS_RULES);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800216 if (flag == null) {
217 log.info("AllowExtraneousRules is not configured, " +
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100218 "using current value of {}", allowExtraneousRules);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800219 } else {
220 allowExtraneousRules = flag;
221 log.info("Configured. AllowExtraneousRules is {}",
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100222 allowExtraneousRules ? "enabled" : "disabled");
223 }
224
225 flag = Tools.isPropertyEnabled(properties, IMPORT_EXTRANEOUS_RULES);
226 if (flag == null) {
227 log.info("ImportExtraneousRules is not configured, " +
228 "using current value of {}", importExtraneousRules);
229 } else {
230 importExtraneousRules = flag;
231 log.info("Configured. importExtraneousRules is {}",
232 importExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700233 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800234
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700235 flag = Tools.isPropertyEnabled(properties, PURGE_ON_DISCONNECTION);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800236 if (flag == null) {
237 log.info("PurgeOnDisconnection is not configured, " +
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100238 "using current value of {}", purgeOnDisconnection);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800239 } else {
240 purgeOnDisconnection = flag;
241 log.info("Configured. PurgeOnDisconnection is {}",
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100242 purgeOnDisconnection ? "enabled" : "disabled");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800243 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800244
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700245 String s = get(properties, POLL_FREQUENCY);
Carmelo Casconee6a97542018-06-20 16:48:00 +0200246 if (isNullOrEmpty(s)) {
247 log.info("fallbackFlowPollFrequency is not configured, " +
248 "using current value of {} seconds",
249 fallbackFlowPollFrequency);
250 } else {
251 try {
252 fallbackFlowPollFrequency = Integer.parseInt(s);
253 log.info("Configured. FallbackFlowPollFrequency is {} seconds",
254 fallbackFlowPollFrequency);
255 } catch (NumberFormatException e) {
256 log.warn("Configured fallbackFlowPollFrequency value '{}' " +
257 "is not a number, using current value of {} seconds",
258 fallbackFlowPollFrequency);
259 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800260 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800261 }
262
alshabib57044ba2014-09-16 15:58:01 -0700263 @Override
tom9b4030d2014-10-06 10:39:03 -0700264 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900265 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700266 return store.getFlowRuleCount();
267 }
268
269 @Override
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800270 public int getFlowRuleCount(DeviceId deviceId) {
271 checkPermission(FLOWRULE_READ);
Thomas Vachuska227943d2018-03-19 09:37:28 -0700272 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800273 return store.getFlowRuleCount(deviceId);
274 }
275
276 @Override
Jordan Haltermanb81fdc12019-03-04 18:12:20 -0800277 public int getFlowRuleCount(DeviceId deviceId, FlowEntry.FlowEntryState state) {
278 checkPermission(FLOWRULE_READ);
279 checkNotNull(deviceId, "Device ID cannot be null");
280 return store.getFlowRuleCount(deviceId, state);
281 }
282
283 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700284 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900285 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800286 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700287 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700288 }
289
290 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700291 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900292 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900293
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800294 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800295 for (FlowRule flowRule : flowRules) {
296 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700297 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800298 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700299 }
300
301 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530302 public void purgeFlowRules(DeviceId deviceId) {
303 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800304 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530305 store.purgeFlowRule(deviceId);
306 }
307
308 @Override
alshabib57044ba2014-09-16 15:58:01 -0700309 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900310 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900311
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800312 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800313 for (FlowRule flowRule : flowRules) {
314 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700315 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800316 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700317 }
alshabib57044ba2014-09-16 15:58:01 -0700318
alshabiba68eb962014-09-24 20:34:13 -0700319 @Override
320 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900321 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900322
Madan Jampani6a456162014-10-24 11:36:17 -0700323 Set<FlowRule> flowEntries = Sets.newHashSet();
324 for (Device d : deviceService.getDevices()) {
325 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
326 if (flowEntry.appId() == id.id()) {
327 flowEntries.add(flowEntry);
328 }
329 }
330 }
Ray Milkey4f7e3632019-02-19 15:35:20 -0800331 removeFlowRules(Iterables.toArray(flowEntries, FlowRule.class));
alshabib57044ba2014-09-16 15:58:01 -0700332 }
333
334 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530335 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
336 checkPermission(FLOWRULE_READ);
337
338 Set<FlowEntry> flowEntries = Sets.newHashSet();
339 for (Device d : deviceService.getDevices()) {
340 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
341 if (flowEntry.appId() == id.id()) {
342 flowEntries.add(flowEntry);
343 }
344 }
345 }
346 return flowEntries;
347 }
348
349 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800350 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900351 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900352
alshabibaa7e7de2014-11-12 19:20:44 -0800353 Set<FlowRule> matches = Sets.newHashSet();
354 long toLookUp = ((long) appId.id() << 16) | groupId;
355 for (Device d : deviceService.getDevices()) {
356 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
357 if ((flowEntry.id().value() >>> 32) == toLookUp) {
358 matches.add(flowEntry);
359 }
360 }
361 }
362 return matches;
363 }
364
365 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800366 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900367 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700368 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700369 }
370
371 @Override
alshabib57044ba2014-09-16 15:58:01 -0700372 protected FlowRuleProviderService createProviderService(
373 FlowRuleProvider provider) {
374 return new InternalFlowRuleProviderService(provider);
375 }
376
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800377 @Override
378 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
379 log.warn("should not be calling getProvider(ProviderId)");
380 return super.getProvider(pid);
381 }
382
383 /**
384 * {@inheritDoc}
385 * if the Device does not support {@link FlowRuleProgrammable}.
386 */
387 @Override
388 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800389 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800390 // if device supports FlowRuleProgrammable,
391 // use FlowRuleProgrammable via FlowRuleDriverProvider
392 return Optional.ofNullable(deviceService.getDevice(deviceId))
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100393 .filter(dev -> dev.is(FlowRuleProgrammable.class))
394 .<FlowRuleProvider>map(x -> driverProvider)
395 .orElseGet(() -> super.getProvider(deviceId));
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800396 }
397
alshabib57044ba2014-09-16 15:58:01 -0700398 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700399 extends AbstractProviderService<FlowRuleProvider>
400 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700401
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700402 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700403 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
404
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700405
alshabib57044ba2014-09-16 15:58:01 -0700406 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
407 super(provider);
408 }
409
410 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700411 public void flowRemoved(FlowEntry flowEntry) {
412 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700413 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700414 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700415 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700416 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700417 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800418 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700419 return;
420 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700421 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
422 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
423 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800424 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700425 FlowRuleEvent event = null;
426 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700427 case ADDED:
428 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700429 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700430 break;
431 case PENDING_REMOVE:
432 case REMOVED:
433 event = store.removeFlowRule(stored);
434 break;
435 default:
436 break;
alshabib57044ba2014-09-16 15:58:01 -0700437
alshabiba68eb962014-09-24 20:34:13 -0700438 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700439 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700440 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700441 post(event);
442 }
alshabib57044ba2014-09-16 15:58:01 -0700443 }
444
alshabibba5ac482014-10-02 17:15:20 -0700445
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530446 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700447 checkNotNull(flowRule, FLOW_RULE_NULL);
448 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800449 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700450 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700451 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700452 case PENDING_REMOVE:
453 case REMOVED:
454 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530455 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700456 break;
457 case ADDED:
458 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800459 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530460 if (isFlowOnlyInStore) {
461 // Publishing RULE_ADD_REQUESTED event facilitates
462 // preparation of statistics for the concerned rule
463 if (event == null) {
464 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
465 }
466 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800467 try {
468 frp.applyFlowRule(flowRule);
469 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700470 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800471 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800472 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800473 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
474 }
475 }
tom9b4030d2014-10-06 10:39:03 -0700476 break;
477 default:
478 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700479 }
480
alshabibbb42cad2014-09-25 11:43:05 -0700481 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700482 post(event);
483 }
alshabib57044ba2014-09-16 15:58:01 -0700484 }
485
alshabibba5ac482014-10-02 17:15:20 -0700486 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700487 checkNotNull(flowRule, FLOW_RULE_NULL);
488 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800489 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700490 FlowRuleProvider frp = getProvider(flowRule.deviceId());
491 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700492 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700493 }
494
alshabib1c319ff2014-10-04 20:29:09 -0700495 private void flowAdded(FlowEntry flowEntry) {
496 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700497 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700498
alshabib1c319ff2014-10-04 20:29:09 -0700499 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700500 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700501 if (event == null) {
502 log.debug("No flow store event generated.");
503 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800504 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700505 post(event);
506 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700507 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800508 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700509 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700510 }
alshabib57044ba2014-09-16 15:58:01 -0700511 }
512
alshabib1c319ff2014-10-04 20:29:09 -0700513 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
514 if (storedRule == null) {
515 return false;
516 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700517 if (storedRule.isPermanent()) {
518 return true;
519 }
520
Ray Milkey3717e602018-02-01 13:49:47 -0800521 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700522 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700523
524 // Checking flow with hardTimeout
525 if (storedRule.hardTimeout() != 0) {
526 if (!firstSeen.containsKey(storedRule)) {
527 // First time rule adding
528 firstSeen.put(storedRule, currentTime);
529 } else {
530 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800531 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700532 if ((currentTime - first) > hardTimeout) {
533 return false;
534 }
535 }
536 }
537
Gamze Abaka8d03f182017-07-20 15:09:24 +0000538 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700539 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700540 return true;
541 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700542 if (!lastSeen.containsKey(storedRule)) {
543 // checking for the first time
544 lastSeen.put(storedRule, storedRule.lastSeen());
545 // Use following if lastSeen attr. was removed.
546 //lastSeen.put(storedRule, currentTime);
547 }
548 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700549
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800550 // concurrently removed? let the liveness check fail
551 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700552 }
553
alshabib5c370ff2014-09-18 10:12:14 -0700554 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700555 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900556 pushFlowMetricsInternal(deviceId, flowEntries, true);
557 }
558
559 @Override
560 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
561 pushFlowMetricsInternal(deviceId, flowEntries, false);
562 }
563
564 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
565 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700566 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
567 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
568
Saurav Dasfa2fa932015-03-03 11:29:48 -0800569 for (FlowEntry rule : flowEntries) {
570 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700571 FlowEntry storedRule = storedRules.remove(rule);
572 if (storedRule != null) {
573 if (storedRule.exactMatch(rule)) {
574 // we both have the rule, let's update some info then.
575 flowAdded(rule);
576 } else {
577 // 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) {
585 extraneousFlow(rule);
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100586 } else if (importExtraneousRules) { // Stores the rule, if so is indicated
587 store.addOrUpdateFlowRule(rule);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700588 }
alshabib93cb57f2015-02-12 17:43:26 -0800589 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700590 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700591 log.warn("Can't process added or extra rule {} for device {}:{}",
592 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800593 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800594 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900595
596 // DO NOT reinstall
597 if (useMissingFlow) {
598 for (FlowEntry rule : storedRules.keySet()) {
599 try {
600 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530601 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
602 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900603 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700604 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900605 }
alshabib93cb57f2015-02-12 17:43:26 -0800606 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800607 }
alshabib5c370ff2014-09-18 10:12:14 -0700608 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800609
610 @Override
611 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
612 store.batchOperationComplete(FlowRuleBatchEvent.completed(
613 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
614 operation
615 ));
616 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700617
618 @Override
619 public void pushTableStatistics(DeviceId deviceId,
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100620 List<TableStatisticsEntry> tableStats) {
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700621 store.updateTableStatistics(deviceId, tableStats);
622 }
alshabib57044ba2014-09-16 15:58:01 -0700623 }
624
tomc78acee2014-09-24 15:16:55 -0700625 // Store delegate to re-post events emitted from the store.
626 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800627
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800628
Madan Jampani117aaae2014-10-23 10:04:05 -0700629 // TODO: Right now we only dispatch events at individual flowEntry level.
630 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700631 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700632 public void notify(FlowRuleBatchEvent event) {
633 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700634 switch (event.type()) {
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100635 case BATCH_OPERATION_REQUESTED:
636 // Request has been forwarded to MASTER Node, and was
637 request.ops().forEach(
638 op -> {
639 switch (op.operator()) {
640 case ADD:
641 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
642 break;
643 case REMOVE:
644 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
645 break;
646 case MODIFY:
647 //TODO: do something here when the time comes.
648 break;
649 default:
650 log.warn("Unknown flow operation operator: {}", op.operator());
651 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800652 }
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100653 );
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800654
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100655 DeviceId deviceId = event.deviceId();
656 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
657 // getProvider is customized to favor driverProvider
658 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
659 if (flowRuleProvider != null) {
660 log.trace("Sending {} flow rules to {}", batchOperation.size(), deviceId);
661 flowRuleProvider.executeBatch(batchOperation);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800662 }
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800663
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100664 break;
665
666 case BATCH_OPERATION_COMPLETED:
667 // Operation completed, let's retrieve the processor and trigger the callback
668 FlowOperationsProcessor fops = pendingFlowOperations.remove(
669 event.subject().batchId());
670 if (fops != null) {
671 if (event.result().isSuccess()) {
672 fops.satisfy(event.deviceId());
673 } else {
674 fops.fail(event.deviceId(), event.result().failedItems());
675 }
676 } else {
677 log.warn("Unable to find flow operations processor for batch: {}", event.subject().batchId());
678 }
679 break;
680
681 default:
682 break;
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700683 }
tomc78acee2014-09-24 15:16:55 -0700684 }
685 }
alshabib902d41b2014-10-07 16:52:05 -0700686
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700687 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
688 switch (input) {
689 case ADD:
690 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
691 case MODIFY:
692 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
693 case REMOVE:
694 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
695 default:
696 throw new UnsupportedOperationException("Unknown flow rule type " + input);
697 }
698 }
699
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800700 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700701 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800702 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700703
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700704 // Mutable
705 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700706 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700707 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700708
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700709 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800710 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800711 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700712 }
713
714 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700715 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800716 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800717 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700718 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700719 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700720 }
721 }
722
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800723 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700724 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800725
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700726 for (FlowRuleOperation op : ops) {
727 perDeviceBatches.put(op.rule().deviceId(),
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100728 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800729 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000730 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800731
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800732 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700733 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800734 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
Ivan Eroshkin3f56eb12019-11-25 10:38:56 +0100735 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000736 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700737 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700738 }
739 }
740
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700741 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000742 pendingDevices.remove(devId);
743 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700744 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700745 }
alshabib193525b2014-10-08 18:58:03 -0700746 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800747
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700748 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700749 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000750 pendingDevices.remove(devId);
751 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700752 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800753 }
754
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700755 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
756 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800757
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700758 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800759 }
alshabib902d41b2014-10-07 16:52:05 -0700760 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700761
762 @Override
763 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
764 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800765 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700766 return store.getTableStatistics(deviceId);
767 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800768
Patryk Konopka7e40c012017-06-06 13:38:06 +0200769 @Override
770 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800771 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200772 return store.getActiveFlowRuleCount(deviceId);
773 }
774
Charles Chan0c7c43b2016-01-14 17:39:20 -0800775 private class InternalDeviceListener implements DeviceListener {
776 @Override
777 public void event(DeviceEvent event) {
778 switch (event.type()) {
779 case DEVICE_REMOVED:
780 case DEVICE_AVAILABILITY_CHANGED:
781 DeviceId deviceId = event.subject().id();
782 if (!deviceService.isAvailable(deviceId)) {
783 if (purgeOnDisconnection) {
784 store.purgeFlowRule(deviceId);
785 }
786 }
787 break;
788 default:
789 break;
790 }
791 }
792 }
alshabib57044ba2014-09-16 15:58:01 -0700793}