blob: ffcfe0bb23e4ca1973500d326ee711a856728167 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080035import org.onosproject.core.CoreService;
36import org.onosproject.core.IdGenerator;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080037import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080040import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.device.DeviceService;
Jordan Halterman4f3193a2017-07-11 20:32:49 -070043import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.flow.CompletedBatchOperation;
Charles M.C. Chan1229eca2015-05-18 06:27:52 +080045import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.flow.FlowEntry;
47import org.onosproject.net.flow.FlowRule;
Ray Milkey7bf273c2017-09-27 16:15:15 -070048import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
49import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent;
50import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
51import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.flow.FlowRuleEvent;
53import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080054import org.onosproject.net.flow.FlowRuleOperation;
55import org.onosproject.net.flow.FlowRuleOperations;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080056import org.onosproject.net.flow.FlowRuleProgrammable;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.flow.FlowRuleProvider;
58import org.onosproject.net.flow.FlowRuleProviderRegistry;
59import org.onosproject.net.flow.FlowRuleProviderService;
60import org.onosproject.net.flow.FlowRuleService;
61import org.onosproject.net.flow.FlowRuleStore;
62import org.onosproject.net.flow.FlowRuleStoreDelegate;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070063import org.onosproject.net.flow.TableStatisticsEntry;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080064import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080065import org.onosproject.net.provider.AbstractProviderService;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080066import org.onosproject.net.provider.ProviderId;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070067import org.osgi.service.component.ComponentContext;
alshabib57044ba2014-09-16 15:58:01 -070068import org.slf4j.Logger;
69
Brian O'Connord12267c2015-02-17 18:17:08 -080070import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070071import java.util.Dictionary;
Sho SHIMIZUb837cb72016-08-30 14:44:50 -070072import java.util.HashSet;
Brian O'Connord12267c2015-02-17 18:17:08 -080073import java.util.List;
74import java.util.Map;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080075import java.util.Optional;
Brian O'Connord12267c2015-02-17 18:17:08 -080076import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.ExecutorService;
79import java.util.concurrent.Executors;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080080
Thomas Vachuska9b2da212014-11-10 19:30:25 -080081import static com.google.common.base.Preconditions.checkNotNull;
Charles Chan0c7c43b2016-01-14 17:39:20 -080082import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080083import static org.onlab.util.Tools.get;
Brian O'Connord12267c2015-02-17 18:17:08 -080084import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070085import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
86import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090087import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080088import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ;
89import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070090import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090091
Changhoon Yoon541ef712015-05-23 17:18:34 +090092
alshabiba7f7ca82014-09-22 11:41:23 -070093
tome4729872014-09-23 00:37:37 -070094/**
95 * Provides implementation of the flow NB & SB APIs.
96 */
Sho SHIMIZU5c396e32016-08-12 15:19:12 -070097@Component(immediate = true)
alshabib57044ba2014-09-16 15:58:01 -070098@Service
tom202175a2014-09-19 19:00:11 -070099public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700100 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
101 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -0700102 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -0700103
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800104 public static final String DEVICE_ID_NULL = "Device ID cannot be null";
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800105 private final Logger log = getLogger(getClass());
106
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700107 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700108 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
109
110 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
111 label = "Allow flow rules in switch not installed by ONOS")
112 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
113
Charles Chan0c7c43b2016-01-14 17:39:20 -0800114 @Property(name = "purgeOnDisconnection", boolValue = false,
115 label = "Purge entries associated with a device when the device goes offline")
116 private boolean purgeOnDisconnection = false;
117
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800118 private static final int DEFAULT_POLL_FREQUENCY = 30;
119 @Property(name = "fallbackFlowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
120 label = "Frequency (in seconds) for polling flow statistics via fallback provider")
121 private int fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
alshabib57044ba2014-09-16 15:58:01 -0700122
alshabibbb42cad2014-09-25 11:43:05 -0700123 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800124 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700125
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800126 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800127
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800128 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800129 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800130
131 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700132 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800133
134 private IdGenerator idGenerator;
135
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700136 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700137
tombe988312014-09-19 18:38:47 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700140
alshabib57044ba2014-09-16 15:58:01 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700142 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700143
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
145 protected CoreService coreService;
146
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800148 protected MastershipService mastershipService;
149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700151 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800152
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
154 protected DriverService driverService;
155
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700156 @Activate
157 public void activate(ComponentContext context) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800158 modified(context);
tomc78acee2014-09-24 15:16:55 -0700159 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700160 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800161 deviceService.addListener(deviceListener);
162 cfgService.registerProperties(getClass());
163 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
alshabib57044ba2014-09-16 15:58:01 -0700164 log.info("Started");
165 }
166
167 @Deactivate
168 public void deactivate() {
Andrea Campanella5a3c09c2017-12-01 13:57:48 +0100169 driverProvider.terminate();
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700170 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700171 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800172 deviceInstallers.shutdownNow();
173 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700174 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700175 eventDispatcher.removeSink(FlowRuleEvent.class);
176 log.info("Stopped");
177 }
178
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700179 @Modified
180 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800181 if (context != null) {
182 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700183 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800184 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800185 deviceService, mastershipService, fallbackFlowPollFrequency);
186 }
187
188 @Override
189 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800190 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800191 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700192
Charles Chan0c7c43b2016-01-14 17:39:20 -0800193 /**
194 * Extracts properties from the component configuration context.
195 *
196 * @param context the component context
197 */
198 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700199 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800200 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700201
Jian Lid9b5f552016-03-11 18:15:31 -0800202 flag = Tools.isPropertyEnabled(properties, "allowExtraneousRules");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800203 if (flag == null) {
204 log.info("AllowExtraneousRules is not configured, " +
205 "using current value of {}", allowExtraneousRules);
206 } else {
207 allowExtraneousRules = flag;
208 log.info("Configured. AllowExtraneousRules is {}",
209 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700210 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800211
Jian Lid9b5f552016-03-11 18:15:31 -0800212 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800213 if (flag == null) {
214 log.info("PurgeOnDisconnection is not configured, " +
215 "using current value of {}", purgeOnDisconnection);
216 } else {
217 purgeOnDisconnection = flag;
218 log.info("Configured. PurgeOnDisconnection is {}",
219 purgeOnDisconnection ? "enabled" : "disabled");
220 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800221
222 String s = get(properties, "fallbackFlowPollFrequency");
223 try {
224 fallbackFlowPollFrequency = isNullOrEmpty(s) ? DEFAULT_POLL_FREQUENCY : Integer.parseInt(s);
225 } catch (NumberFormatException e) {
226 fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
227 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800228 }
229
alshabib57044ba2014-09-16 15:58:01 -0700230 @Override
tom9b4030d2014-10-06 10:39:03 -0700231 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900232 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700233 return store.getFlowRuleCount();
234 }
235
236 @Override
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800237 public int getFlowRuleCount(DeviceId deviceId) {
238 checkPermission(FLOWRULE_READ);
239 checkNotNull(deviceId, "Device ID cannot be null");
240 return store.getFlowRuleCount(deviceId);
241 }
242
243 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700244 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900245 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800246 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700247 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700248 }
249
250 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700251 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900252 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900253
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800254 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800255 for (FlowRule flowRule : flowRules) {
256 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700257 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800258 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700259 }
260
261 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530262 public void purgeFlowRules(DeviceId deviceId) {
263 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800264 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530265 store.purgeFlowRule(deviceId);
266 }
267
268 @Override
alshabib57044ba2014-09-16 15:58:01 -0700269 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900270 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900271
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800272 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800273 for (FlowRule flowRule : flowRules) {
274 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700275 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800276 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700277 }
alshabib57044ba2014-09-16 15:58:01 -0700278
alshabiba68eb962014-09-24 20:34:13 -0700279 @Override
280 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900281 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700282 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700283 }
284
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530285 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700286 @Override
287 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900288 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900289
Madan Jampani6a456162014-10-24 11:36:17 -0700290 Set<FlowRule> flowEntries = Sets.newHashSet();
291 for (Device d : deviceService.getDevices()) {
292 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
293 if (flowEntry.appId() == id.id()) {
294 flowEntries.add(flowEntry);
295 }
296 }
297 }
298 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700299 }
300
301 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530302 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
303 checkPermission(FLOWRULE_READ);
304
305 Set<FlowEntry> flowEntries = Sets.newHashSet();
306 for (Device d : deviceService.getDevices()) {
307 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
308 if (flowEntry.appId() == id.id()) {
309 flowEntries.add(flowEntry);
310 }
311 }
312 }
313 return flowEntries;
314 }
315
316 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800317 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900318 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900319
alshabibaa7e7de2014-11-12 19:20:44 -0800320 Set<FlowRule> matches = Sets.newHashSet();
321 long toLookUp = ((long) appId.id() << 16) | groupId;
322 for (Device d : deviceService.getDevices()) {
323 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
324 if ((flowEntry.id().value() >>> 32) == toLookUp) {
325 matches.add(flowEntry);
326 }
327 }
328 }
329 return matches;
330 }
331
332 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800333 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900334 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700335 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700336 }
337
338 @Override
alshabib57044ba2014-09-16 15:58:01 -0700339 protected FlowRuleProviderService createProviderService(
340 FlowRuleProvider provider) {
341 return new InternalFlowRuleProviderService(provider);
342 }
343
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800344 @Override
345 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
346 log.warn("should not be calling getProvider(ProviderId)");
347 return super.getProvider(pid);
348 }
349
350 /**
351 * {@inheritDoc}
352 * if the Device does not support {@link FlowRuleProgrammable}.
353 */
354 @Override
355 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800356 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800357 // if device supports FlowRuleProgrammable,
358 // use FlowRuleProgrammable via FlowRuleDriverProvider
359 return Optional.ofNullable(deviceService.getDevice(deviceId))
360 .filter(dev -> dev.is(FlowRuleProgrammable.class))
361 .<FlowRuleProvider>map(x -> driverProvider)
362 .orElseGet(() -> super.getProvider(deviceId));
363 }
364
alshabib57044ba2014-09-16 15:58:01 -0700365 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700366 extends AbstractProviderService<FlowRuleProvider>
367 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700368
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700369 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700370 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
371
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700372
alshabib57044ba2014-09-16 15:58:01 -0700373 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
374 super(provider);
375 }
376
377 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700378 public void flowRemoved(FlowEntry flowEntry) {
379 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700380 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700381 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700382 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700383 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700384 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800385 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700386 return;
387 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700388 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
389 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
390 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800391 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700392 FlowRuleEvent event = null;
393 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700394 case ADDED:
395 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700396 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700397 break;
398 case PENDING_REMOVE:
399 case REMOVED:
400 event = store.removeFlowRule(stored);
401 break;
402 default:
403 break;
alshabib57044ba2014-09-16 15:58:01 -0700404
alshabiba68eb962014-09-24 20:34:13 -0700405 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700406 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700407 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700408 post(event);
409 }
alshabib57044ba2014-09-16 15:58:01 -0700410 }
411
alshabibba5ac482014-10-02 17:15:20 -0700412
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530413 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700414 checkNotNull(flowRule, FLOW_RULE_NULL);
415 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800416 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700417 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700418 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700419 case PENDING_REMOVE:
420 case REMOVED:
421 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530422 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700423 break;
424 case ADDED:
425 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800426 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530427 if (isFlowOnlyInStore) {
428 // Publishing RULE_ADD_REQUESTED event facilitates
429 // preparation of statistics for the concerned rule
430 if (event == null) {
431 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
432 }
433 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800434 try {
435 frp.applyFlowRule(flowRule);
436 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700437 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800438 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800439 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800440 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
441 }
442 }
tom9b4030d2014-10-06 10:39:03 -0700443 break;
444 default:
445 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700446 }
447
alshabibbb42cad2014-09-25 11:43:05 -0700448 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700449 post(event);
450 }
alshabib57044ba2014-09-16 15:58:01 -0700451 }
452
alshabibba5ac482014-10-02 17:15:20 -0700453 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700454 checkNotNull(flowRule, FLOW_RULE_NULL);
455 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800456 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700457 FlowRuleProvider frp = getProvider(flowRule.deviceId());
458 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700459 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700460 }
461
alshabib1c319ff2014-10-04 20:29:09 -0700462 private void flowAdded(FlowEntry flowEntry) {
463 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700464 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700465
alshabib1c319ff2014-10-04 20:29:09 -0700466 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700467 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700468 if (event == null) {
469 log.debug("No flow store event generated.");
470 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800471 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700472 post(event);
473 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700474 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800475 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700476 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700477 }
alshabib57044ba2014-09-16 15:58:01 -0700478 }
479
alshabib1c319ff2014-10-04 20:29:09 -0700480 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
481 if (storedRule == null) {
482 return false;
483 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700484 if (storedRule.isPermanent()) {
485 return true;
486 }
487
Ray Milkey3717e602018-02-01 13:49:47 -0800488 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700489 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700490
491 // Checking flow with hardTimeout
492 if (storedRule.hardTimeout() != 0) {
493 if (!firstSeen.containsKey(storedRule)) {
494 // First time rule adding
495 firstSeen.put(storedRule, currentTime);
496 } else {
497 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800498 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700499 if ((currentTime - first) > hardTimeout) {
500 return false;
501 }
502 }
503 }
504
Gamze Abaka8d03f182017-07-20 15:09:24 +0000505 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700506 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700507 return true;
508 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700509 if (!lastSeen.containsKey(storedRule)) {
510 // checking for the first time
511 lastSeen.put(storedRule, storedRule.lastSeen());
512 // Use following if lastSeen attr. was removed.
513 //lastSeen.put(storedRule, currentTime);
514 }
515 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700516
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800517 // concurrently removed? let the liveness check fail
518 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700519 }
520
alshabib5c370ff2014-09-18 10:12:14 -0700521 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700522 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900523 pushFlowMetricsInternal(deviceId, flowEntries, true);
524 }
525
526 @Override
527 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
528 pushFlowMetricsInternal(deviceId, flowEntries, false);
529 }
530
531 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
532 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700533 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
534 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
535
Saurav Dasfa2fa932015-03-03 11:29:48 -0800536 for (FlowEntry rule : flowEntries) {
537 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700538 FlowEntry storedRule = storedRules.remove(rule);
539 if (storedRule != null) {
540 if (storedRule.exactMatch(rule)) {
541 // we both have the rule, let's update some info then.
542 flowAdded(rule);
543 } else {
544 // the two rules are not an exact match - remove the
545 // switch's rule and install our rule
546 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530547 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700548 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800549 } else {
550 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700551 if (!allowExtraneousRules) {
552 extraneousFlow(rule);
553 }
alshabib93cb57f2015-02-12 17:43:26 -0800554 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700555 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700556 log.warn("Can't process added or extra rule {} for device {}:{}",
557 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800558 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800559 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900560
561 // DO NOT reinstall
562 if (useMissingFlow) {
563 for (FlowEntry rule : storedRules.keySet()) {
564 try {
565 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530566 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
567 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900568 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700569 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900570 }
alshabib93cb57f2015-02-12 17:43:26 -0800571 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800572 }
alshabib5c370ff2014-09-18 10:12:14 -0700573 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800574
575 @Override
576 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
577 store.batchOperationComplete(FlowRuleBatchEvent.completed(
578 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
579 operation
580 ));
581 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700582
583 @Override
584 public void pushTableStatistics(DeviceId deviceId,
585 List<TableStatisticsEntry> tableStats) {
586 store.updateTableStatistics(deviceId, tableStats);
587 }
alshabib57044ba2014-09-16 15:58:01 -0700588 }
589
tomc78acee2014-09-24 15:16:55 -0700590 // Store delegate to re-post events emitted from the store.
591 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800592
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800593
Madan Jampani117aaae2014-10-23 10:04:05 -0700594 // TODO: Right now we only dispatch events at individual flowEntry level.
595 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700596 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700597 public void notify(FlowRuleBatchEvent event) {
598 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700599 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700600 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800601 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700602 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800603 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800604 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800605 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700606 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800607 break;
608 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700609 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800610 break;
611 case MODIFY:
612 //TODO: do something here when the time comes.
613 break;
614 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800615 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800616 }
617 }
618 );
619
620 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700621 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800622 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700623 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
624 if (flowRuleProvider != null) {
625 flowRuleProvider.executeBatch(batchOperation);
626 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800627
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800628 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700629
Madan Jampani117aaae2014-10-23 10:04:05 -0700630 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800631
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800632 FlowOperationsProcessor fops = pendingFlowOperations.remove(
633 event.subject().batchId());
634 if (event.result().isSuccess()) {
635 if (fops != null) {
636 fops.satisfy(event.deviceId());
637 }
638 } else {
639 fops.fail(event.deviceId(), event.result().failedItems());
640 }
641
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700642 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800643
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700644 default:
645 break;
646 }
tomc78acee2014-09-24 15:16:55 -0700647 }
648 }
alshabib902d41b2014-10-07 16:52:05 -0700649
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700650 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
651 switch (input) {
652 case ADD:
653 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
654 case MODIFY:
655 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
656 case REMOVE:
657 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
658 default:
659 throw new UnsupportedOperationException("Unknown flow rule type " + input);
660 }
661 }
662
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800663 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700664 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800665 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700666
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700667 // Mutable
668 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700669 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700670 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700671
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700672 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800673 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800674 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700675 }
676
677 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700678 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800679 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800680 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700681 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700682 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700683 }
684 }
685
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800686 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700687 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800688
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700689 for (FlowRuleOperation op : ops) {
690 perDeviceBatches.put(op.rule().deviceId(),
691 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800692 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000693 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800694
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800695 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700696 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800697 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
698 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000699 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700700 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700701 }
702 }
703
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700704 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000705 pendingDevices.remove(devId);
706 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700707 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700708 }
alshabib193525b2014-10-08 18:58:03 -0700709 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800710
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700711 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700712 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000713 pendingDevices.remove(devId);
714 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700715 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800716 }
717
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700718 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
719 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800720
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700721 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800722 }
alshabib902d41b2014-10-07 16:52:05 -0700723 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700724
725 @Override
726 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
727 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800728 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700729 return store.getTableStatistics(deviceId);
730 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800731
Patryk Konopka7e40c012017-06-06 13:38:06 +0200732 @Override
733 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800734 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200735 return store.getActiveFlowRuleCount(deviceId);
736 }
737
Charles Chan0c7c43b2016-01-14 17:39:20 -0800738 private class InternalDeviceListener implements DeviceListener {
739 @Override
740 public void event(DeviceEvent event) {
741 switch (event.type()) {
742 case DEVICE_REMOVED:
743 case DEVICE_AVAILABILITY_CHANGED:
744 DeviceId deviceId = event.subject().id();
745 if (!deviceService.isAvailable(deviceId)) {
746 if (purgeOnDisconnection) {
747 store.purgeFlowRule(deviceId);
748 }
749 }
750 break;
751 default:
752 break;
753 }
754 }
755 }
alshabib57044ba2014-09-16 15:58:01 -0700756}