blob: 7c79a57a2449f79824e6211d02610ef8dbcfe93f [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
tome4729872014-09-23 00:37:37 -070092/**
93 * Provides implementation of the flow NB & SB APIs.
94 */
Sho SHIMIZU5c396e32016-08-12 15:19:12 -070095@Component(immediate = true)
alshabib57044ba2014-09-16 15:58:01 -070096@Service
tom202175a2014-09-19 19:00:11 -070097public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070098 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
99 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -0700100 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -0700101
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800102 private final Logger log = getLogger(getClass());
103
Thomas Vachuska62a379c2018-03-05 18:03:40 -0800104 public static final String DEVICE_ID_NULL = "Device ID cannot be null";
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700105 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700106 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
107
108 @Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
109 label = "Allow flow rules in switch not installed by ONOS")
110 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
111
Charles Chan0c7c43b2016-01-14 17:39:20 -0800112 @Property(name = "purgeOnDisconnection", boolValue = false,
113 label = "Purge entries associated with a device when the device goes offline")
114 private boolean purgeOnDisconnection = false;
115
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800116 private static final int DEFAULT_POLL_FREQUENCY = 30;
117 @Property(name = "fallbackFlowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
118 label = "Frequency (in seconds) for polling flow statistics via fallback provider")
119 private int fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
alshabib57044ba2014-09-16 15:58:01 -0700120
alshabibbb42cad2014-09-25 11:43:05 -0700121 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800122 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700123
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800124 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800125
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800126 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800127 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800128
129 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700130 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800131
132 private IdGenerator idGenerator;
133
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700134 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700135
tombe988312014-09-19 18:38:47 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700138
alshabib57044ba2014-09-16 15:58:01 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700140 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700141
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
143 protected CoreService coreService;
144
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800146 protected MastershipService mastershipService;
147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700149 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800150
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
152 protected DriverService driverService;
153
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700154 @Activate
155 public void activate(ComponentContext context) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800156 modified(context);
tomc78acee2014-09-24 15:16:55 -0700157 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700158 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800159 deviceService.addListener(deviceListener);
160 cfgService.registerProperties(getClass());
161 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
alshabib57044ba2014-09-16 15:58:01 -0700162 log.info("Started");
163 }
164
165 @Deactivate
166 public void deactivate() {
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700167 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700168 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800169 deviceInstallers.shutdownNow();
170 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700171 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700172 eventDispatcher.removeSink(FlowRuleEvent.class);
173 log.info("Stopped");
174 }
175
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700176 @Modified
177 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800178 if (context != null) {
179 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700180 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800181 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800182 deviceService, mastershipService, fallbackFlowPollFrequency);
183 }
184
185 @Override
186 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800187 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800188 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700189
Charles Chan0c7c43b2016-01-14 17:39:20 -0800190 /**
191 * Extracts properties from the component configuration context.
192 *
193 * @param context the component context
194 */
195 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700196 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800197 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700198
Jian Lid9b5f552016-03-11 18:15:31 -0800199 flag = Tools.isPropertyEnabled(properties, "allowExtraneousRules");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800200 if (flag == null) {
201 log.info("AllowExtraneousRules is not configured, " +
202 "using current value of {}", allowExtraneousRules);
203 } else {
204 allowExtraneousRules = flag;
205 log.info("Configured. AllowExtraneousRules is {}",
206 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700207 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800208
Jian Lid9b5f552016-03-11 18:15:31 -0800209 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800210 if (flag == null) {
211 log.info("PurgeOnDisconnection is not configured, " +
212 "using current value of {}", purgeOnDisconnection);
213 } else {
214 purgeOnDisconnection = flag;
215 log.info("Configured. PurgeOnDisconnection is {}",
216 purgeOnDisconnection ? "enabled" : "disabled");
217 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800218
219 String s = get(properties, "fallbackFlowPollFrequency");
220 try {
221 fallbackFlowPollFrequency = isNullOrEmpty(s) ? DEFAULT_POLL_FREQUENCY : Integer.parseInt(s);
222 } catch (NumberFormatException e) {
223 fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
224 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800225 }
226
alshabib57044ba2014-09-16 15:58:01 -0700227 @Override
tom9b4030d2014-10-06 10:39:03 -0700228 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900229 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700230 return store.getFlowRuleCount();
231 }
232
233 @Override
Thomas Vachuska62a379c2018-03-05 18:03:40 -0800234 public int getFlowRuleCount(DeviceId deviceId) {
235 checkPermission(FLOWRULE_READ);
236 checkNotNull(deviceId, DEVICE_ID_NULL);
237 return store.getFlowRuleCount(deviceId);
238 }
239
240 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700241 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900242 checkPermission(FLOWRULE_READ);
Thomas Vachuska62a379c2018-03-05 18:03:40 -0800243 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700244 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700245 }
246
247 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700248 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900249 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900250
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800251 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800252 for (FlowRule flowRule : flowRules) {
253 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700254 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800255 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700256 }
257
258 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530259 public void purgeFlowRules(DeviceId deviceId) {
260 checkPermission(FLOWRULE_WRITE);
Thomas Vachuska62a379c2018-03-05 18:03:40 -0800261 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530262 store.purgeFlowRule(deviceId);
263 }
264
265 @Override
alshabib57044ba2014-09-16 15:58:01 -0700266 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900267 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900268
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800269 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800270 for (FlowRule flowRule : flowRules) {
271 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700272 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800273 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700274 }
alshabib57044ba2014-09-16 15:58:01 -0700275
alshabiba68eb962014-09-24 20:34:13 -0700276 @Override
277 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900278 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700279 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700280 }
281
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530282 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700283 @Override
284 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900285 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900286
Madan Jampani6a456162014-10-24 11:36:17 -0700287 Set<FlowRule> flowEntries = Sets.newHashSet();
288 for (Device d : deviceService.getDevices()) {
289 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
290 if (flowEntry.appId() == id.id()) {
291 flowEntries.add(flowEntry);
292 }
293 }
294 }
295 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700296 }
297
298 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530299 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
300 checkPermission(FLOWRULE_READ);
301
302 Set<FlowEntry> flowEntries = Sets.newHashSet();
303 for (Device d : deviceService.getDevices()) {
304 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
305 if (flowEntry.appId() == id.id()) {
306 flowEntries.add(flowEntry);
307 }
308 }
309 }
310 return flowEntries;
311 }
312
313 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800314 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900315 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900316
alshabibaa7e7de2014-11-12 19:20:44 -0800317 Set<FlowRule> matches = Sets.newHashSet();
318 long toLookUp = ((long) appId.id() << 16) | groupId;
319 for (Device d : deviceService.getDevices()) {
320 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
321 if ((flowEntry.id().value() >>> 32) == toLookUp) {
322 matches.add(flowEntry);
323 }
324 }
325 }
326 return matches;
327 }
328
329 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800330 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900331 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700332 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700333 }
334
335 @Override
alshabib57044ba2014-09-16 15:58:01 -0700336 protected FlowRuleProviderService createProviderService(
337 FlowRuleProvider provider) {
338 return new InternalFlowRuleProviderService(provider);
339 }
340
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800341 @Override
342 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
343 log.warn("should not be calling getProvider(ProviderId)");
344 return super.getProvider(pid);
345 }
346
347 /**
348 * {@inheritDoc}
349 * if the Device does not support {@link FlowRuleProgrammable}.
350 */
351 @Override
352 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
353 // if device supports FlowRuleProgrammable,
354 // use FlowRuleProgrammable via FlowRuleDriverProvider
355 return Optional.ofNullable(deviceService.getDevice(deviceId))
356 .filter(dev -> dev.is(FlowRuleProgrammable.class))
357 .<FlowRuleProvider>map(x -> driverProvider)
358 .orElseGet(() -> super.getProvider(deviceId));
359 }
360
alshabib57044ba2014-09-16 15:58:01 -0700361 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700362 extends AbstractProviderService<FlowRuleProvider>
363 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700364
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700365 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700366 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
367
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700368
alshabib57044ba2014-09-16 15:58:01 -0700369 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
370 super(provider);
371 }
372
373 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700374 public void flowRemoved(FlowEntry flowEntry) {
375 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700376 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700377 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700378 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700379 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700380 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800381 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700382 return;
383 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700384 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
385 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
386 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800387 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700388 FlowRuleEvent event = null;
389 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700390 case ADDED:
391 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700392 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700393 break;
394 case PENDING_REMOVE:
395 case REMOVED:
396 event = store.removeFlowRule(stored);
397 break;
398 default:
399 break;
alshabib57044ba2014-09-16 15:58:01 -0700400
alshabiba68eb962014-09-24 20:34:13 -0700401 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700402 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700403 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700404 post(event);
405 }
alshabib57044ba2014-09-16 15:58:01 -0700406 }
407
alshabibba5ac482014-10-02 17:15:20 -0700408
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530409 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700410 checkNotNull(flowRule, FLOW_RULE_NULL);
411 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800412 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700413 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700414 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700415 case PENDING_REMOVE:
416 case REMOVED:
417 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530418 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700419 break;
420 case ADDED:
421 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800422 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530423 if (isFlowOnlyInStore) {
424 // Publishing RULE_ADD_REQUESTED event facilitates
425 // preparation of statistics for the concerned rule
426 if (event == null) {
427 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
428 }
429 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800430 try {
431 frp.applyFlowRule(flowRule);
432 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700433 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800434 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800435 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800436 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
437 }
438 }
tom9b4030d2014-10-06 10:39:03 -0700439 break;
440 default:
441 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700442 }
443
alshabibbb42cad2014-09-25 11:43:05 -0700444 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700445 post(event);
446 }
alshabib57044ba2014-09-16 15:58:01 -0700447 }
448
alshabibba5ac482014-10-02 17:15:20 -0700449 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700450 checkNotNull(flowRule, FLOW_RULE_NULL);
451 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800452 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700453 FlowRuleProvider frp = getProvider(flowRule.deviceId());
454 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700455 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700456 }
457
alshabib1c319ff2014-10-04 20:29:09 -0700458 private void flowAdded(FlowEntry flowEntry) {
459 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700460 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700461
alshabib1c319ff2014-10-04 20:29:09 -0700462 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700463 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700464 if (event == null) {
465 log.debug("No flow store event generated.");
466 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800467 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700468 post(event);
469 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700470 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800471 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700472 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700473 }
alshabib57044ba2014-09-16 15:58:01 -0700474 }
475
alshabib1c319ff2014-10-04 20:29:09 -0700476 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
477 if (storedRule == null) {
478 return false;
479 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700480 if (storedRule.isPermanent()) {
481 return true;
482 }
483
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700484 final long timeout = storedRule.timeout() * 1000;
485 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700486
487 // Checking flow with hardTimeout
488 if (storedRule.hardTimeout() != 0) {
489 if (!firstSeen.containsKey(storedRule)) {
490 // First time rule adding
491 firstSeen.put(storedRule, currentTime);
492 } else {
493 Long first = firstSeen.get(storedRule);
494 final long hardTimeout = storedRule.hardTimeout() * 1000;
495 if ((currentTime - first) > hardTimeout) {
496 return false;
497 }
498 }
499 }
500
Gamze Abaka8d03f182017-07-20 15:09:24 +0000501 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700502 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700503 return true;
504 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700505 if (!lastSeen.containsKey(storedRule)) {
506 // checking for the first time
507 lastSeen.put(storedRule, storedRule.lastSeen());
508 // Use following if lastSeen attr. was removed.
509 //lastSeen.put(storedRule, currentTime);
510 }
511 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700512
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800513 // concurrently removed? let the liveness check fail
514 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700515 }
516
alshabib5c370ff2014-09-18 10:12:14 -0700517 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700518 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900519 pushFlowMetricsInternal(deviceId, flowEntries, true);
520 }
521
522 @Override
523 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
524 pushFlowMetricsInternal(deviceId, flowEntries, false);
525 }
526
527 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
528 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700529 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
530 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
531
Saurav Dasfa2fa932015-03-03 11:29:48 -0800532 for (FlowEntry rule : flowEntries) {
533 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700534 FlowEntry storedRule = storedRules.remove(rule);
535 if (storedRule != null) {
536 if (storedRule.exactMatch(rule)) {
537 // we both have the rule, let's update some info then.
538 flowAdded(rule);
539 } else {
540 // the two rules are not an exact match - remove the
541 // switch's rule and install our rule
542 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530543 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700544 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800545 } else {
546 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700547 if (!allowExtraneousRules) {
548 extraneousFlow(rule);
549 }
alshabib93cb57f2015-02-12 17:43:26 -0800550 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700551 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700552 log.warn("Can't process added or extra rule {} for device {}:{}",
553 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800554 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800555 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900556
557 // DO NOT reinstall
558 if (useMissingFlow) {
559 for (FlowEntry rule : storedRules.keySet()) {
560 try {
561 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530562 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
563 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900564 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700565 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900566 }
alshabib93cb57f2015-02-12 17:43:26 -0800567 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800568 }
alshabib5c370ff2014-09-18 10:12:14 -0700569 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800570
571 @Override
572 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
573 store.batchOperationComplete(FlowRuleBatchEvent.completed(
574 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
575 operation
576 ));
577 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700578
579 @Override
580 public void pushTableStatistics(DeviceId deviceId,
581 List<TableStatisticsEntry> tableStats) {
582 store.updateTableStatistics(deviceId, tableStats);
583 }
alshabib57044ba2014-09-16 15:58:01 -0700584 }
585
tomc78acee2014-09-24 15:16:55 -0700586 // Store delegate to re-post events emitted from the store.
587 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800588
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800589
Madan Jampani117aaae2014-10-23 10:04:05 -0700590 // TODO: Right now we only dispatch events at individual flowEntry level.
591 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700592 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700593 public void notify(FlowRuleBatchEvent event) {
594 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700595 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700596 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800597 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700598 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800599 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800600 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800601 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700602 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800603 break;
604 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700605 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800606 break;
607 case MODIFY:
608 //TODO: do something here when the time comes.
609 break;
610 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800611 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800612 }
613 }
614 );
615
616 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700617 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800618 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700619 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
620 if (flowRuleProvider != null) {
621 flowRuleProvider.executeBatch(batchOperation);
622 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800623
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800624 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700625
Madan Jampani117aaae2014-10-23 10:04:05 -0700626 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800627
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800628 FlowOperationsProcessor fops = pendingFlowOperations.remove(
629 event.subject().batchId());
630 if (event.result().isSuccess()) {
631 if (fops != null) {
632 fops.satisfy(event.deviceId());
633 }
634 } else {
635 fops.fail(event.deviceId(), event.result().failedItems());
636 }
637
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700638 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800639
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700640 default:
641 break;
642 }
tomc78acee2014-09-24 15:16:55 -0700643 }
644 }
alshabib902d41b2014-10-07 16:52:05 -0700645
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700646 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
647 switch (input) {
648 case ADD:
649 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
650 case MODIFY:
651 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
652 case REMOVE:
653 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
654 default:
655 throw new UnsupportedOperationException("Unknown flow rule type " + input);
656 }
657 }
658
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800659 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700660 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800661 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700662
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700663 // Mutable
664 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700665 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700666 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700667
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700668 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800669 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800670 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700671 }
672
673 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700674 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800675 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800676 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700677 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700678 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700679 }
680 }
681
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800682 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700683 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800684
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700685 for (FlowRuleOperation op : ops) {
686 perDeviceBatches.put(op.rule().deviceId(),
687 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800688 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000689 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800690
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800691 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700692 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800693 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
694 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000695 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700696 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700697 }
698 }
699
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700700 synchronized void satisfy(DeviceId devId) {
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);
alshabib193525b2014-10-08 18:58:03 -0700704 }
alshabib193525b2014-10-08 18:58:03 -0700705 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800706
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700707 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700708 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000709 pendingDevices.remove(devId);
710 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700711 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800712 }
713
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700714 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
715 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800716
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700717 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800718 }
alshabib902d41b2014-10-07 16:52:05 -0700719 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700720
721 @Override
722 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
723 checkPermission(FLOWRULE_READ);
Thomas Vachuska62a379c2018-03-05 18:03:40 -0800724 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700725 return store.getTableStatistics(deviceId);
726 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800727
Patryk Konopka7e40c012017-06-06 13:38:06 +0200728 @Override
729 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuska62a379c2018-03-05 18:03:40 -0800730 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200731 return store.getActiveFlowRuleCount(deviceId);
732 }
733
Charles Chan0c7c43b2016-01-14 17:39:20 -0800734 private class InternalDeviceListener implements DeviceListener {
735 @Override
736 public void event(DeviceEvent event) {
737 switch (event.type()) {
738 case DEVICE_REMOVED:
739 case DEVICE_AVAILABILITY_CHANGED:
740 DeviceId deviceId = event.subject().id();
741 if (!deviceService.isAvailable(deviceId)) {
742 if (purgeOnDisconnection) {
743 store.purgeFlowRule(deviceId);
744 }
745 }
746 break;
747 default:
748 break;
749 }
750 }
751 }
alshabib57044ba2014-09-16 15:58:01 -0700752}