blob: a2c02993c5bb2fd8c43dd5c0ce89d944438079f7 [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;
48import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.flow.FlowRuleBatchEvent;
50import org.onosproject.net.flow.FlowRuleBatchOperation;
51import org.onosproject.net.flow.FlowRuleBatchRequest;
52import 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 Vachuskac4ee7372016-02-02 16:10:09 -0800104 private final Logger log = getLogger(getClass());
105
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700106 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700107 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
108
109 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
110 label = "Allow flow rules in switch not installed by ONOS")
111 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
112
Charles Chan0c7c43b2016-01-14 17:39:20 -0800113 @Property(name = "purgeOnDisconnection", boolValue = false,
114 label = "Purge entries associated with a device when the device goes offline")
115 private boolean purgeOnDisconnection = false;
116
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800117 private static final int DEFAULT_POLL_FREQUENCY = 30;
118 @Property(name = "fallbackFlowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
119 label = "Frequency (in seconds) for polling flow statistics via fallback provider")
120 private int fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
alshabib57044ba2014-09-16 15:58:01 -0700121
alshabibbb42cad2014-09-25 11:43:05 -0700122 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800123 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700124
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800125 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800126
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800127 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800128 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800129
130 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700131 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800132
133 private IdGenerator idGenerator;
134
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700135 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700136
tombe988312014-09-19 18:38:47 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700139
alshabib57044ba2014-09-16 15:58:01 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700141 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700142
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected CoreService coreService;
145
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800147 protected MastershipService mastershipService;
148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700150 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800151
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
153 protected DriverService driverService;
154
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700155 @Activate
156 public void activate(ComponentContext context) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800157 modified(context);
tomc78acee2014-09-24 15:16:55 -0700158 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700159 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800160 deviceService.addListener(deviceListener);
161 cfgService.registerProperties(getClass());
162 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
alshabib57044ba2014-09-16 15:58:01 -0700163 log.info("Started");
164 }
165
166 @Deactivate
167 public void deactivate() {
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700168 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700169 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800170 deviceInstallers.shutdownNow();
171 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700172 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700173 eventDispatcher.removeSink(FlowRuleEvent.class);
174 log.info("Stopped");
175 }
176
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700177 @Modified
178 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800179 if (context != null) {
180 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700181 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800182 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800183 deviceService, mastershipService, fallbackFlowPollFrequency);
184 }
185
186 @Override
187 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800188 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800189 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700190
Charles Chan0c7c43b2016-01-14 17:39:20 -0800191 /**
192 * Extracts properties from the component configuration context.
193 *
194 * @param context the component context
195 */
196 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700197 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800198 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700199
Jian Lid9b5f552016-03-11 18:15:31 -0800200 flag = Tools.isPropertyEnabled(properties, "allowExtraneousRules");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800201 if (flag == null) {
202 log.info("AllowExtraneousRules is not configured, " +
203 "using current value of {}", allowExtraneousRules);
204 } else {
205 allowExtraneousRules = flag;
206 log.info("Configured. AllowExtraneousRules is {}",
207 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700208 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800209
Jian Lid9b5f552016-03-11 18:15:31 -0800210 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800211 if (flag == null) {
212 log.info("PurgeOnDisconnection is not configured, " +
213 "using current value of {}", purgeOnDisconnection);
214 } else {
215 purgeOnDisconnection = flag;
216 log.info("Configured. PurgeOnDisconnection is {}",
217 purgeOnDisconnection ? "enabled" : "disabled");
218 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800219
220 String s = get(properties, "fallbackFlowPollFrequency");
221 try {
222 fallbackFlowPollFrequency = isNullOrEmpty(s) ? DEFAULT_POLL_FREQUENCY : Integer.parseInt(s);
223 } catch (NumberFormatException e) {
224 fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
225 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800226 }
227
alshabib57044ba2014-09-16 15:58:01 -0700228 @Override
tom9b4030d2014-10-06 10:39:03 -0700229 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900230 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700231 return store.getFlowRuleCount();
232 }
233
234 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700235 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900236 checkPermission(FLOWRULE_READ);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700237 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700238 }
239
240 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700241 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900242 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900243
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800244 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800245 for (FlowRule flowRule : flowRules) {
246 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700247 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800248 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700249 }
250
251 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530252 public void purgeFlowRules(DeviceId deviceId) {
253 checkPermission(FLOWRULE_WRITE);
254 store.purgeFlowRule(deviceId);
255 }
256
257 @Override
alshabib57044ba2014-09-16 15:58:01 -0700258 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900259 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900260
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800261 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800262 for (FlowRule flowRule : flowRules) {
263 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700264 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800265 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700266 }
alshabib57044ba2014-09-16 15:58:01 -0700267
alshabiba68eb962014-09-24 20:34:13 -0700268 @Override
269 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900270 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700271 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700272 }
273
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530274 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700275 @Override
276 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900277 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900278
Madan Jampani6a456162014-10-24 11:36:17 -0700279 Set<FlowRule> flowEntries = Sets.newHashSet();
280 for (Device d : deviceService.getDevices()) {
281 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
282 if (flowEntry.appId() == id.id()) {
283 flowEntries.add(flowEntry);
284 }
285 }
286 }
287 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700288 }
289
290 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530291 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
292 checkPermission(FLOWRULE_READ);
293
294 Set<FlowEntry> flowEntries = Sets.newHashSet();
295 for (Device d : deviceService.getDevices()) {
296 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
297 if (flowEntry.appId() == id.id()) {
298 flowEntries.add(flowEntry);
299 }
300 }
301 }
302 return flowEntries;
303 }
304
305 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800306 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900307 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900308
alshabibaa7e7de2014-11-12 19:20:44 -0800309 Set<FlowRule> matches = Sets.newHashSet();
310 long toLookUp = ((long) appId.id() << 16) | groupId;
311 for (Device d : deviceService.getDevices()) {
312 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
313 if ((flowEntry.id().value() >>> 32) == toLookUp) {
314 matches.add(flowEntry);
315 }
316 }
317 }
318 return matches;
319 }
320
321 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800322 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900323 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700324 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700325 }
326
327 @Override
alshabib57044ba2014-09-16 15:58:01 -0700328 protected FlowRuleProviderService createProviderService(
329 FlowRuleProvider provider) {
330 return new InternalFlowRuleProviderService(provider);
331 }
332
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800333 @Override
334 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
335 log.warn("should not be calling getProvider(ProviderId)");
336 return super.getProvider(pid);
337 }
338
339 /**
340 * {@inheritDoc}
341 * if the Device does not support {@link FlowRuleProgrammable}.
342 */
343 @Override
344 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
345 // if device supports FlowRuleProgrammable,
346 // use FlowRuleProgrammable via FlowRuleDriverProvider
347 return Optional.ofNullable(deviceService.getDevice(deviceId))
348 .filter(dev -> dev.is(FlowRuleProgrammable.class))
349 .<FlowRuleProvider>map(x -> driverProvider)
350 .orElseGet(() -> super.getProvider(deviceId));
351 }
352
alshabib57044ba2014-09-16 15:58:01 -0700353 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700354 extends AbstractProviderService<FlowRuleProvider>
355 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700356
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700357 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700358 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
359
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700360
alshabib57044ba2014-09-16 15:58:01 -0700361 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
362 super(provider);
363 }
364
365 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700366 public void flowRemoved(FlowEntry flowEntry) {
367 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700368 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700369 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700370 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700371 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700372 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800373 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700374 return;
375 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700376 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
377 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
378 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800379 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700380 FlowRuleEvent event = null;
381 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700382 case ADDED:
383 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700384 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700385 break;
386 case PENDING_REMOVE:
387 case REMOVED:
388 event = store.removeFlowRule(stored);
389 break;
390 default:
391 break;
alshabib57044ba2014-09-16 15:58:01 -0700392
alshabiba68eb962014-09-24 20:34:13 -0700393 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700394 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700395 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700396 post(event);
397 }
alshabib57044ba2014-09-16 15:58:01 -0700398 }
399
alshabibba5ac482014-10-02 17:15:20 -0700400
alshabib1c319ff2014-10-04 20:29:09 -0700401 private void flowMissing(FlowEntry flowRule) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700402 checkNotNull(flowRule, FLOW_RULE_NULL);
403 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800404 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700405 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700406 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700407 case PENDING_REMOVE:
408 case REMOVED:
409 event = store.removeFlowRule(flowRule);
tom9b4030d2014-10-06 10:39:03 -0700410 break;
411 case ADDED:
412 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800413 event = store.pendingFlowRule(flowRule);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800414 try {
415 frp.applyFlowRule(flowRule);
416 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700417 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800418 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800419 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800420 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
421 }
422 }
tom9b4030d2014-10-06 10:39:03 -0700423 break;
424 default:
425 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700426 }
427
alshabibbb42cad2014-09-25 11:43:05 -0700428 if (event != null) {
429 log.debug("Flow {} removed", flowRule);
430 post(event);
431 }
alshabib57044ba2014-09-16 15:58:01 -0700432 }
433
alshabibba5ac482014-10-02 17:15:20 -0700434 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700435 checkNotNull(flowRule, FLOW_RULE_NULL);
436 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800437 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700438 FlowRuleProvider frp = getProvider(flowRule.deviceId());
439 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700440 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700441 }
442
alshabib1c319ff2014-10-04 20:29:09 -0700443 private void flowAdded(FlowEntry flowEntry) {
444 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700445 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700446
alshabib1c319ff2014-10-04 20:29:09 -0700447 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700448 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700449 if (event == null) {
450 log.debug("No flow store event generated.");
451 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800452 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700453 post(event);
454 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700455 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800456 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700457 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700458 }
alshabib57044ba2014-09-16 15:58:01 -0700459 }
460
alshabib1c319ff2014-10-04 20:29:09 -0700461 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
462 if (storedRule == null) {
463 return false;
464 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700465 if (storedRule.isPermanent()) {
466 return true;
467 }
468
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700469 final long timeout = storedRule.timeout() * 1000;
470 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700471
472 // Checking flow with hardTimeout
473 if (storedRule.hardTimeout() != 0) {
474 if (!firstSeen.containsKey(storedRule)) {
475 // First time rule adding
476 firstSeen.put(storedRule, currentTime);
477 } else {
478 Long first = firstSeen.get(storedRule);
479 final long hardTimeout = storedRule.hardTimeout() * 1000;
480 if ((currentTime - first) > hardTimeout) {
481 return false;
482 }
483 }
484 }
485
Gamze Abaka8d03f182017-07-20 15:09:24 +0000486 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700487 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700488 return true;
489 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700490 if (!lastSeen.containsKey(storedRule)) {
491 // checking for the first time
492 lastSeen.put(storedRule, storedRule.lastSeen());
493 // Use following if lastSeen attr. was removed.
494 //lastSeen.put(storedRule, currentTime);
495 }
496 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700497
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800498 // concurrently removed? let the liveness check fail
499 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700500 }
501
alshabib5c370ff2014-09-18 10:12:14 -0700502 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700503 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900504 pushFlowMetricsInternal(deviceId, flowEntries, true);
505 }
506
507 @Override
508 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
509 pushFlowMetricsInternal(deviceId, flowEntries, false);
510 }
511
512 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
513 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700514 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
515 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
516
Saurav Dasfa2fa932015-03-03 11:29:48 -0800517 for (FlowEntry rule : flowEntries) {
518 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700519 FlowEntry storedRule = storedRules.remove(rule);
520 if (storedRule != null) {
521 if (storedRule.exactMatch(rule)) {
522 // we both have the rule, let's update some info then.
523 flowAdded(rule);
524 } else {
525 // the two rules are not an exact match - remove the
526 // switch's rule and install our rule
527 extraneousFlow(rule);
528 flowMissing(storedRule);
529 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800530 } else {
531 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700532 if (!allowExtraneousRules) {
533 extraneousFlow(rule);
534 }
alshabib93cb57f2015-02-12 17:43:26 -0800535 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700536 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700537 log.warn("Can't process added or extra rule {} for device {}:{}",
538 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800539 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800540 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900541
542 // DO NOT reinstall
543 if (useMissingFlow) {
544 for (FlowEntry rule : storedRules.keySet()) {
545 try {
546 // there are rules in the store that aren't on the switch
547 log.debug("Adding rule in store, but not on switch {}", rule);
548 flowMissing(rule);
549 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700550 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900551 }
alshabib93cb57f2015-02-12 17:43:26 -0800552 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800553 }
alshabib5c370ff2014-09-18 10:12:14 -0700554 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800555
556 @Override
557 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
558 store.batchOperationComplete(FlowRuleBatchEvent.completed(
559 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
560 operation
561 ));
562 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700563
564 @Override
565 public void pushTableStatistics(DeviceId deviceId,
566 List<TableStatisticsEntry> tableStats) {
567 store.updateTableStatistics(deviceId, tableStats);
568 }
alshabib57044ba2014-09-16 15:58:01 -0700569 }
570
tomc78acee2014-09-24 15:16:55 -0700571 // Store delegate to re-post events emitted from the store.
572 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800573
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800574
Madan Jampani117aaae2014-10-23 10:04:05 -0700575 // TODO: Right now we only dispatch events at individual flowEntry level.
576 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700577 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700578 public void notify(FlowRuleBatchEvent event) {
579 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700580 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700581 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800582 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700583 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800584 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800585 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800586 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700587 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800588 break;
589 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700590 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800591 break;
592 case MODIFY:
593 //TODO: do something here when the time comes.
594 break;
595 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800596 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800597 }
598 }
599 );
600
601 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700602 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800603 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700604 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
605 if (flowRuleProvider != null) {
606 flowRuleProvider.executeBatch(batchOperation);
607 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800608
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800609 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700610
Madan Jampani117aaae2014-10-23 10:04:05 -0700611 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800612
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800613 FlowOperationsProcessor fops = pendingFlowOperations.remove(
614 event.subject().batchId());
615 if (event.result().isSuccess()) {
616 if (fops != null) {
617 fops.satisfy(event.deviceId());
618 }
619 } else {
620 fops.fail(event.deviceId(), event.result().failedItems());
621 }
622
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700623 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800624
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700625 default:
626 break;
627 }
tomc78acee2014-09-24 15:16:55 -0700628 }
629 }
alshabib902d41b2014-10-07 16:52:05 -0700630
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700631 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
632 switch (input) {
633 case ADD:
634 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
635 case MODIFY:
636 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
637 case REMOVE:
638 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
639 default:
640 throw new UnsupportedOperationException("Unknown flow rule type " + input);
641 }
642 }
643
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800644 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700645 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800646 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700647
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700648 // Mutable
649 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700650 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700651 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700652
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700653 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800654 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800655 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700656 }
657
658 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700659 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800660 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800661 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700662 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700663 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700664 }
665 }
666
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800667 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700668 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800669
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700670 for (FlowRuleOperation op : ops) {
671 perDeviceBatches.put(op.rule().deviceId(),
672 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800673 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000674 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800675
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800676 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700677 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800678 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
679 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000680 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700681 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700682 }
683 }
684
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700685 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000686 pendingDevices.remove(devId);
687 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700688 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700689 }
alshabib193525b2014-10-08 18:58:03 -0700690 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800691
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700692 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700693 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000694 pendingDevices.remove(devId);
695 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700696 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800697 }
698
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700699 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
700 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800701
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700702 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800703 }
alshabib902d41b2014-10-07 16:52:05 -0700704 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700705
706 @Override
707 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
708 checkPermission(FLOWRULE_READ);
709 return store.getTableStatistics(deviceId);
710 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800711
Patryk Konopka7e40c012017-06-06 13:38:06 +0200712 @Override
713 public long getActiveFlowRuleCount(DeviceId deviceId) {
714 return store.getActiveFlowRuleCount(deviceId);
715 }
716
Charles Chan0c7c43b2016-01-14 17:39:20 -0800717 private class InternalDeviceListener implements DeviceListener {
718 @Override
719 public void event(DeviceEvent event) {
720 switch (event.type()) {
721 case DEVICE_REMOVED:
722 case DEVICE_AVAILABILITY_CHANGED:
723 DeviceId deviceId = event.subject().id();
724 if (!deviceService.isAvailable(deviceId)) {
725 if (purgeOnDisconnection) {
726 store.purgeFlowRule(deviceId);
727 }
728 }
729 break;
730 default:
731 break;
732 }
733 }
734 }
alshabib57044ba2014-09-16 15:58:01 -0700735}