blob: 47623a7b63375b7d9e242cb1293abc76b01fe70d [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 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
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530401 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
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);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530410 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700411 break;
412 case ADDED:
413 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800414 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530415 if (isFlowOnlyInStore) {
416 // Publishing RULE_ADD_REQUESTED event facilitates
417 // preparation of statistics for the concerned rule
418 if (event == null) {
419 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
420 }
421 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800422 try {
423 frp.applyFlowRule(flowRule);
424 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700425 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800426 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800427 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800428 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
429 }
430 }
tom9b4030d2014-10-06 10:39:03 -0700431 break;
432 default:
433 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700434 }
435
alshabibbb42cad2014-09-25 11:43:05 -0700436 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700437 post(event);
438 }
alshabib57044ba2014-09-16 15:58:01 -0700439 }
440
alshabibba5ac482014-10-02 17:15:20 -0700441 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700442 checkNotNull(flowRule, FLOW_RULE_NULL);
443 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800444 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700445 FlowRuleProvider frp = getProvider(flowRule.deviceId());
446 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700447 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700448 }
449
alshabib1c319ff2014-10-04 20:29:09 -0700450 private void flowAdded(FlowEntry flowEntry) {
451 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700452 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700453
alshabib1c319ff2014-10-04 20:29:09 -0700454 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700455 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700456 if (event == null) {
457 log.debug("No flow store event generated.");
458 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800459 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700460 post(event);
461 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700462 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800463 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700464 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700465 }
alshabib57044ba2014-09-16 15:58:01 -0700466 }
467
alshabib1c319ff2014-10-04 20:29:09 -0700468 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
469 if (storedRule == null) {
470 return false;
471 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700472 if (storedRule.isPermanent()) {
473 return true;
474 }
475
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700476 final long timeout = storedRule.timeout() * 1000;
477 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700478
479 // Checking flow with hardTimeout
480 if (storedRule.hardTimeout() != 0) {
481 if (!firstSeen.containsKey(storedRule)) {
482 // First time rule adding
483 firstSeen.put(storedRule, currentTime);
484 } else {
485 Long first = firstSeen.get(storedRule);
486 final long hardTimeout = storedRule.hardTimeout() * 1000;
487 if ((currentTime - first) > hardTimeout) {
488 return false;
489 }
490 }
491 }
492
Gamze Abaka8d03f182017-07-20 15:09:24 +0000493 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700494 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700495 return true;
496 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700497 if (!lastSeen.containsKey(storedRule)) {
498 // checking for the first time
499 lastSeen.put(storedRule, storedRule.lastSeen());
500 // Use following if lastSeen attr. was removed.
501 //lastSeen.put(storedRule, currentTime);
502 }
503 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700504
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800505 // concurrently removed? let the liveness check fail
506 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700507 }
508
alshabib5c370ff2014-09-18 10:12:14 -0700509 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700510 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900511 pushFlowMetricsInternal(deviceId, flowEntries, true);
512 }
513
514 @Override
515 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
516 pushFlowMetricsInternal(deviceId, flowEntries, false);
517 }
518
519 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
520 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700521 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
522 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
523
Saurav Dasfa2fa932015-03-03 11:29:48 -0800524 for (FlowEntry rule : flowEntries) {
525 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700526 FlowEntry storedRule = storedRules.remove(rule);
527 if (storedRule != null) {
528 if (storedRule.exactMatch(rule)) {
529 // we both have the rule, let's update some info then.
530 flowAdded(rule);
531 } else {
532 // the two rules are not an exact match - remove the
533 // switch's rule and install our rule
534 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530535 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700536 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800537 } else {
538 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700539 if (!allowExtraneousRules) {
540 extraneousFlow(rule);
541 }
alshabib93cb57f2015-02-12 17:43:26 -0800542 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700543 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700544 log.warn("Can't process added or extra rule {} for device {}:{}",
545 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800546 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800547 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900548
549 // DO NOT reinstall
550 if (useMissingFlow) {
551 for (FlowEntry rule : storedRules.keySet()) {
552 try {
553 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530554 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
555 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900556 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700557 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900558 }
alshabib93cb57f2015-02-12 17:43:26 -0800559 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800560 }
alshabib5c370ff2014-09-18 10:12:14 -0700561 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800562
563 @Override
564 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
565 store.batchOperationComplete(FlowRuleBatchEvent.completed(
566 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
567 operation
568 ));
569 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700570
571 @Override
572 public void pushTableStatistics(DeviceId deviceId,
573 List<TableStatisticsEntry> tableStats) {
574 store.updateTableStatistics(deviceId, tableStats);
575 }
alshabib57044ba2014-09-16 15:58:01 -0700576 }
577
tomc78acee2014-09-24 15:16:55 -0700578 // Store delegate to re-post events emitted from the store.
579 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800580
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800581
Madan Jampani117aaae2014-10-23 10:04:05 -0700582 // TODO: Right now we only dispatch events at individual flowEntry level.
583 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700584 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700585 public void notify(FlowRuleBatchEvent event) {
586 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700587 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700588 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800589 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700590 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800591 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800592 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800593 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700594 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800595 break;
596 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700597 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800598 break;
599 case MODIFY:
600 //TODO: do something here when the time comes.
601 break;
602 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800603 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800604 }
605 }
606 );
607
608 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700609 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800610 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700611 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
612 if (flowRuleProvider != null) {
613 flowRuleProvider.executeBatch(batchOperation);
614 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800615
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800616 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700617
Madan Jampani117aaae2014-10-23 10:04:05 -0700618 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800619
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800620 FlowOperationsProcessor fops = pendingFlowOperations.remove(
621 event.subject().batchId());
622 if (event.result().isSuccess()) {
623 if (fops != null) {
624 fops.satisfy(event.deviceId());
625 }
626 } else {
627 fops.fail(event.deviceId(), event.result().failedItems());
628 }
629
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700630 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800631
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700632 default:
633 break;
634 }
tomc78acee2014-09-24 15:16:55 -0700635 }
636 }
alshabib902d41b2014-10-07 16:52:05 -0700637
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700638 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
639 switch (input) {
640 case ADD:
641 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
642 case MODIFY:
643 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
644 case REMOVE:
645 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
646 default:
647 throw new UnsupportedOperationException("Unknown flow rule type " + input);
648 }
649 }
650
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800651 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700652 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800653 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700654
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700655 // Mutable
656 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700657 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700658 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700659
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700660 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800661 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800662 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700663 }
664
665 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700666 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800667 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800668 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700669 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700670 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700671 }
672 }
673
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800674 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700675 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800676
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700677 for (FlowRuleOperation op : ops) {
678 perDeviceBatches.put(op.rule().deviceId(),
679 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800680 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000681 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800682
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800683 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700684 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800685 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
686 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000687 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700688 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700689 }
690 }
691
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700692 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000693 pendingDevices.remove(devId);
694 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700695 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700696 }
alshabib193525b2014-10-08 18:58:03 -0700697 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800698
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700699 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700700 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000701 pendingDevices.remove(devId);
702 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700703 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800704 }
705
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700706 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
707 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800708
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700709 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800710 }
alshabib902d41b2014-10-07 16:52:05 -0700711 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700712
713 @Override
714 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
715 checkPermission(FLOWRULE_READ);
716 return store.getTableStatistics(deviceId);
717 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800718
Patryk Konopka7e40c012017-06-06 13:38:06 +0200719 @Override
720 public long getActiveFlowRuleCount(DeviceId deviceId) {
721 return store.getActiveFlowRuleCount(deviceId);
722 }
723
Charles Chan0c7c43b2016-01-14 17:39:20 -0800724 private class InternalDeviceListener implements DeviceListener {
725 @Override
726 public void event(DeviceEvent event) {
727 switch (event.type()) {
728 case DEVICE_REMOVED:
729 case DEVICE_AVAILABILITY_CHANGED:
730 DeviceId deviceId = event.subject().id();
731 if (!deviceService.isAvailable(deviceId)) {
732 if (purgeOnDisconnection) {
733 store.purgeFlowRule(deviceId);
734 }
735 }
736 break;
737 default:
738 break;
739 }
740 }
741 }
alshabib57044ba2014-09-16 15:58:01 -0700742}