blob: 9c1a31a52375acbfb509a37105f96dbc98a18d7f [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 Campanella5a3c09c2017-12-01 13:57:48 +0100168 driverProvider.terminate();
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700169 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700170 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800171 deviceInstallers.shutdownNow();
172 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700173 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700174 eventDispatcher.removeSink(FlowRuleEvent.class);
175 log.info("Stopped");
176 }
177
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700178 @Modified
179 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800180 if (context != null) {
181 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700182 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800183 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800184 deviceService, mastershipService, fallbackFlowPollFrequency);
185 }
186
187 @Override
188 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800189 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800190 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700191
Charles Chan0c7c43b2016-01-14 17:39:20 -0800192 /**
193 * Extracts properties from the component configuration context.
194 *
195 * @param context the component context
196 */
197 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700198 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800199 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700200
Jian Lid9b5f552016-03-11 18:15:31 -0800201 flag = Tools.isPropertyEnabled(properties, "allowExtraneousRules");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800202 if (flag == null) {
203 log.info("AllowExtraneousRules is not configured, " +
204 "using current value of {}", allowExtraneousRules);
205 } else {
206 allowExtraneousRules = flag;
207 log.info("Configured. AllowExtraneousRules is {}",
208 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700209 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800210
Jian Lid9b5f552016-03-11 18:15:31 -0800211 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800212 if (flag == null) {
213 log.info("PurgeOnDisconnection is not configured, " +
214 "using current value of {}", purgeOnDisconnection);
215 } else {
216 purgeOnDisconnection = flag;
217 log.info("Configured. PurgeOnDisconnection is {}",
218 purgeOnDisconnection ? "enabled" : "disabled");
219 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800220
221 String s = get(properties, "fallbackFlowPollFrequency");
222 try {
223 fallbackFlowPollFrequency = isNullOrEmpty(s) ? DEFAULT_POLL_FREQUENCY : Integer.parseInt(s);
224 } catch (NumberFormatException e) {
225 fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
226 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800227 }
228
alshabib57044ba2014-09-16 15:58:01 -0700229 @Override
tom9b4030d2014-10-06 10:39:03 -0700230 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900231 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700232 return store.getFlowRuleCount();
233 }
234
235 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700236 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900237 checkPermission(FLOWRULE_READ);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700238 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700239 }
240
241 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700242 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900243 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900244
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800245 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800246 for (FlowRule flowRule : flowRules) {
247 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700248 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800249 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700250 }
251
252 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530253 public void purgeFlowRules(DeviceId deviceId) {
254 checkPermission(FLOWRULE_WRITE);
255 store.purgeFlowRule(deviceId);
256 }
257
258 @Override
alshabib57044ba2014-09-16 15:58:01 -0700259 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900260 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900261
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800262 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800263 for (FlowRule flowRule : flowRules) {
264 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700265 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800266 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700267 }
alshabib57044ba2014-09-16 15:58:01 -0700268
alshabiba68eb962014-09-24 20:34:13 -0700269 @Override
270 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900271 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700272 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700273 }
274
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530275 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700276 @Override
277 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900278 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900279
Madan Jampani6a456162014-10-24 11:36:17 -0700280 Set<FlowRule> flowEntries = Sets.newHashSet();
281 for (Device d : deviceService.getDevices()) {
282 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
283 if (flowEntry.appId() == id.id()) {
284 flowEntries.add(flowEntry);
285 }
286 }
287 }
288 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700289 }
290
291 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530292 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
293 checkPermission(FLOWRULE_READ);
294
295 Set<FlowEntry> flowEntries = Sets.newHashSet();
296 for (Device d : deviceService.getDevices()) {
297 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
298 if (flowEntry.appId() == id.id()) {
299 flowEntries.add(flowEntry);
300 }
301 }
302 }
303 return flowEntries;
304 }
305
306 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800307 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900308 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900309
alshabibaa7e7de2014-11-12 19:20:44 -0800310 Set<FlowRule> matches = Sets.newHashSet();
311 long toLookUp = ((long) appId.id() << 16) | groupId;
312 for (Device d : deviceService.getDevices()) {
313 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
314 if ((flowEntry.id().value() >>> 32) == toLookUp) {
315 matches.add(flowEntry);
316 }
317 }
318 }
319 return matches;
320 }
321
322 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800323 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900324 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700325 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700326 }
327
328 @Override
alshabib57044ba2014-09-16 15:58:01 -0700329 protected FlowRuleProviderService createProviderService(
330 FlowRuleProvider provider) {
331 return new InternalFlowRuleProviderService(provider);
332 }
333
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800334 @Override
335 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
336 log.warn("should not be calling getProvider(ProviderId)");
337 return super.getProvider(pid);
338 }
339
340 /**
341 * {@inheritDoc}
342 * if the Device does not support {@link FlowRuleProgrammable}.
343 */
344 @Override
345 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
346 // if device supports FlowRuleProgrammable,
347 // use FlowRuleProgrammable via FlowRuleDriverProvider
348 return Optional.ofNullable(deviceService.getDevice(deviceId))
349 .filter(dev -> dev.is(FlowRuleProgrammable.class))
350 .<FlowRuleProvider>map(x -> driverProvider)
351 .orElseGet(() -> super.getProvider(deviceId));
352 }
353
alshabib57044ba2014-09-16 15:58:01 -0700354 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700355 extends AbstractProviderService<FlowRuleProvider>
356 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700357
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700358 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700359 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
360
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700361
alshabib57044ba2014-09-16 15:58:01 -0700362 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
363 super(provider);
364 }
365
366 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700367 public void flowRemoved(FlowEntry flowEntry) {
368 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700369 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700370 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700371 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700372 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700373 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800374 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700375 return;
376 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700377 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
378 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
379 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800380 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700381 FlowRuleEvent event = null;
382 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700383 case ADDED:
384 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700385 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700386 break;
387 case PENDING_REMOVE:
388 case REMOVED:
389 event = store.removeFlowRule(stored);
390 break;
391 default:
392 break;
alshabib57044ba2014-09-16 15:58:01 -0700393
alshabiba68eb962014-09-24 20:34:13 -0700394 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700395 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700396 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700397 post(event);
398 }
alshabib57044ba2014-09-16 15:58:01 -0700399 }
400
alshabibba5ac482014-10-02 17:15:20 -0700401
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530402 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700403 checkNotNull(flowRule, FLOW_RULE_NULL);
404 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800405 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700406 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700407 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700408 case PENDING_REMOVE:
409 case REMOVED:
410 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530411 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700412 break;
413 case ADDED:
414 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800415 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530416 if (isFlowOnlyInStore) {
417 // Publishing RULE_ADD_REQUESTED event facilitates
418 // preparation of statistics for the concerned rule
419 if (event == null) {
420 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
421 }
422 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800423 try {
424 frp.applyFlowRule(flowRule);
425 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700426 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800427 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800428 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800429 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
430 }
431 }
tom9b4030d2014-10-06 10:39:03 -0700432 break;
433 default:
434 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700435 }
436
alshabibbb42cad2014-09-25 11:43:05 -0700437 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700438 post(event);
439 }
alshabib57044ba2014-09-16 15:58:01 -0700440 }
441
alshabibba5ac482014-10-02 17:15:20 -0700442 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700443 checkNotNull(flowRule, FLOW_RULE_NULL);
444 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800445 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700446 FlowRuleProvider frp = getProvider(flowRule.deviceId());
447 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700448 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700449 }
450
alshabib1c319ff2014-10-04 20:29:09 -0700451 private void flowAdded(FlowEntry flowEntry) {
452 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700453 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700454
alshabib1c319ff2014-10-04 20:29:09 -0700455 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700456 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700457 if (event == null) {
458 log.debug("No flow store event generated.");
459 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800460 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700461 post(event);
462 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700463 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800464 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700465 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700466 }
alshabib57044ba2014-09-16 15:58:01 -0700467 }
468
alshabib1c319ff2014-10-04 20:29:09 -0700469 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
470 if (storedRule == null) {
471 return false;
472 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700473 if (storedRule.isPermanent()) {
474 return true;
475 }
476
Ray Milkey3717e602018-02-01 13:49:47 -0800477 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700478 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700479
480 // Checking flow with hardTimeout
481 if (storedRule.hardTimeout() != 0) {
482 if (!firstSeen.containsKey(storedRule)) {
483 // First time rule adding
484 firstSeen.put(storedRule, currentTime);
485 } else {
486 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800487 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700488 if ((currentTime - first) > hardTimeout) {
489 return false;
490 }
491 }
492 }
493
Gamze Abaka8d03f182017-07-20 15:09:24 +0000494 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700495 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700496 return true;
497 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700498 if (!lastSeen.containsKey(storedRule)) {
499 // checking for the first time
500 lastSeen.put(storedRule, storedRule.lastSeen());
501 // Use following if lastSeen attr. was removed.
502 //lastSeen.put(storedRule, currentTime);
503 }
504 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700505
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800506 // concurrently removed? let the liveness check fail
507 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700508 }
509
alshabib5c370ff2014-09-18 10:12:14 -0700510 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700511 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900512 pushFlowMetricsInternal(deviceId, flowEntries, true);
513 }
514
515 @Override
516 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
517 pushFlowMetricsInternal(deviceId, flowEntries, false);
518 }
519
520 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
521 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700522 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
523 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
524
Saurav Dasfa2fa932015-03-03 11:29:48 -0800525 for (FlowEntry rule : flowEntries) {
526 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700527 FlowEntry storedRule = storedRules.remove(rule);
528 if (storedRule != null) {
529 if (storedRule.exactMatch(rule)) {
530 // we both have the rule, let's update some info then.
531 flowAdded(rule);
532 } else {
533 // the two rules are not an exact match - remove the
534 // switch's rule and install our rule
535 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530536 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700537 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800538 } else {
539 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700540 if (!allowExtraneousRules) {
541 extraneousFlow(rule);
542 }
alshabib93cb57f2015-02-12 17:43:26 -0800543 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700544 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700545 log.warn("Can't process added or extra rule {} for device {}:{}",
546 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800547 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800548 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900549
550 // DO NOT reinstall
551 if (useMissingFlow) {
552 for (FlowEntry rule : storedRules.keySet()) {
553 try {
554 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530555 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
556 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900557 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700558 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900559 }
alshabib93cb57f2015-02-12 17:43:26 -0800560 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800561 }
alshabib5c370ff2014-09-18 10:12:14 -0700562 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800563
564 @Override
565 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
566 store.batchOperationComplete(FlowRuleBatchEvent.completed(
567 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
568 operation
569 ));
570 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700571
572 @Override
573 public void pushTableStatistics(DeviceId deviceId,
574 List<TableStatisticsEntry> tableStats) {
575 store.updateTableStatistics(deviceId, tableStats);
576 }
alshabib57044ba2014-09-16 15:58:01 -0700577 }
578
tomc78acee2014-09-24 15:16:55 -0700579 // Store delegate to re-post events emitted from the store.
580 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800581
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800582
Madan Jampani117aaae2014-10-23 10:04:05 -0700583 // TODO: Right now we only dispatch events at individual flowEntry level.
584 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700585 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700586 public void notify(FlowRuleBatchEvent event) {
587 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700588 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700589 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800590 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700591 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800592 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800593 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800594 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700595 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800596 break;
597 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700598 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800599 break;
600 case MODIFY:
601 //TODO: do something here when the time comes.
602 break;
603 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800604 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800605 }
606 }
607 );
608
609 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700610 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800611 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700612 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
613 if (flowRuleProvider != null) {
614 flowRuleProvider.executeBatch(batchOperation);
615 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800616
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800617 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700618
Madan Jampani117aaae2014-10-23 10:04:05 -0700619 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800620
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800621 FlowOperationsProcessor fops = pendingFlowOperations.remove(
622 event.subject().batchId());
623 if (event.result().isSuccess()) {
624 if (fops != null) {
625 fops.satisfy(event.deviceId());
626 }
627 } else {
628 fops.fail(event.deviceId(), event.result().failedItems());
629 }
630
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700631 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800632
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700633 default:
634 break;
635 }
tomc78acee2014-09-24 15:16:55 -0700636 }
637 }
alshabib902d41b2014-10-07 16:52:05 -0700638
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700639 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
640 switch (input) {
641 case ADD:
642 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
643 case MODIFY:
644 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
645 case REMOVE:
646 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
647 default:
648 throw new UnsupportedOperationException("Unknown flow rule type " + input);
649 }
650 }
651
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800652 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700653 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800654 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700655
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700656 // Mutable
657 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700658 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700659 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700660
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700661 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800662 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800663 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700664 }
665
666 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700667 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800668 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800669 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700670 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700671 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700672 }
673 }
674
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800675 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700676 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800677
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700678 for (FlowRuleOperation op : ops) {
679 perDeviceBatches.put(op.rule().deviceId(),
680 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800681 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000682 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800683
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800684 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700685 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800686 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
687 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000688 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700689 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700690 }
691 }
692
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700693 synchronized void satisfy(DeviceId devId) {
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);
alshabib193525b2014-10-08 18:58:03 -0700697 }
alshabib193525b2014-10-08 18:58:03 -0700698 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800699
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700700 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700701 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000702 pendingDevices.remove(devId);
703 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700704 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800705 }
706
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700707 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
708 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800709
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700710 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800711 }
alshabib902d41b2014-10-07 16:52:05 -0700712 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700713
714 @Override
715 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
716 checkPermission(FLOWRULE_READ);
717 return store.getTableStatistics(deviceId);
718 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800719
Patryk Konopka7e40c012017-06-06 13:38:06 +0200720 @Override
721 public long getActiveFlowRuleCount(DeviceId deviceId) {
722 return store.getActiveFlowRuleCount(deviceId);
723 }
724
Charles Chan0c7c43b2016-01-14 17:39:20 -0800725 private class InternalDeviceListener implements DeviceListener {
726 @Override
727 public void event(DeviceEvent event) {
728 switch (event.type()) {
729 case DEVICE_REMOVED:
730 case DEVICE_AVAILABILITY_CHANGED:
731 DeviceId deviceId = event.subject().id();
732 if (!deviceService.isAvailable(deviceId)) {
733 if (purgeOnDisconnection) {
734 store.purgeFlowRule(deviceId);
735 }
736 }
737 break;
738 default:
739 break;
740 }
741 }
742 }
alshabib57044ba2014-09-16 15:58:01 -0700743}