blob: 504784164af5a5736769d6c9407876c0ab78b521 [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
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 Vachuska227943d2018-03-19 09:37:28 -0700104 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
105 private 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 Campanella5a3c09c2017-12-01 13:57:48 +0100167 driverProvider.terminate();
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
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800235 public int getFlowRuleCount(DeviceId deviceId) {
236 checkPermission(FLOWRULE_READ);
Thomas Vachuska227943d2018-03-19 09:37:28 -0700237 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800238 return store.getFlowRuleCount(deviceId);
239 }
240
241 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700242 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900243 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800244 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700245 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700246 }
247
248 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700249 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900250 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900251
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800252 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800253 for (FlowRule flowRule : flowRules) {
254 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700255 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800256 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700257 }
258
259 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530260 public void purgeFlowRules(DeviceId deviceId) {
261 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800262 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530263 store.purgeFlowRule(deviceId);
264 }
265
266 @Override
alshabib57044ba2014-09-16 15:58:01 -0700267 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900268 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900269
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800270 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800271 for (FlowRule flowRule : flowRules) {
272 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700273 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800274 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700275 }
alshabib57044ba2014-09-16 15:58:01 -0700276
alshabiba68eb962014-09-24 20:34:13 -0700277 @Override
278 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900279 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700280 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700281 }
282
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530283 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700284 @Override
285 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900286 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900287
Madan Jampani6a456162014-10-24 11:36:17 -0700288 Set<FlowRule> flowEntries = Sets.newHashSet();
289 for (Device d : deviceService.getDevices()) {
290 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
291 if (flowEntry.appId() == id.id()) {
292 flowEntries.add(flowEntry);
293 }
294 }
295 }
296 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700297 }
298
299 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530300 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
301 checkPermission(FLOWRULE_READ);
302
303 Set<FlowEntry> flowEntries = Sets.newHashSet();
304 for (Device d : deviceService.getDevices()) {
305 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
306 if (flowEntry.appId() == id.id()) {
307 flowEntries.add(flowEntry);
308 }
309 }
310 }
311 return flowEntries;
312 }
313
314 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800315 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900316 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900317
alshabibaa7e7de2014-11-12 19:20:44 -0800318 Set<FlowRule> matches = Sets.newHashSet();
319 long toLookUp = ((long) appId.id() << 16) | groupId;
320 for (Device d : deviceService.getDevices()) {
321 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
322 if ((flowEntry.id().value() >>> 32) == toLookUp) {
323 matches.add(flowEntry);
324 }
325 }
326 }
327 return matches;
328 }
329
330 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800331 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900332 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700333 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700334 }
335
336 @Override
alshabib57044ba2014-09-16 15:58:01 -0700337 protected FlowRuleProviderService createProviderService(
338 FlowRuleProvider provider) {
339 return new InternalFlowRuleProviderService(provider);
340 }
341
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800342 @Override
343 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
344 log.warn("should not be calling getProvider(ProviderId)");
345 return super.getProvider(pid);
346 }
347
348 /**
349 * {@inheritDoc}
350 * if the Device does not support {@link FlowRuleProgrammable}.
351 */
352 @Override
353 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800354 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800355 // if device supports FlowRuleProgrammable,
356 // use FlowRuleProgrammable via FlowRuleDriverProvider
357 return Optional.ofNullable(deviceService.getDevice(deviceId))
358 .filter(dev -> dev.is(FlowRuleProgrammable.class))
359 .<FlowRuleProvider>map(x -> driverProvider)
360 .orElseGet(() -> super.getProvider(deviceId));
361 }
362
alshabib57044ba2014-09-16 15:58:01 -0700363 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700364 extends AbstractProviderService<FlowRuleProvider>
365 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700366
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700367 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700368 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
369
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700370
alshabib57044ba2014-09-16 15:58:01 -0700371 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
372 super(provider);
373 }
374
375 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700376 public void flowRemoved(FlowEntry flowEntry) {
377 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700378 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700379 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700380 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700381 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700382 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800383 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700384 return;
385 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700386 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
387 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
388 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800389 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700390 FlowRuleEvent event = null;
391 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700392 case ADDED:
393 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700394 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700395 break;
396 case PENDING_REMOVE:
397 case REMOVED:
398 event = store.removeFlowRule(stored);
399 break;
400 default:
401 break;
alshabib57044ba2014-09-16 15:58:01 -0700402
alshabiba68eb962014-09-24 20:34:13 -0700403 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700404 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700405 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700406 post(event);
407 }
alshabib57044ba2014-09-16 15:58:01 -0700408 }
409
alshabibba5ac482014-10-02 17:15:20 -0700410
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530411 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700412 checkNotNull(flowRule, FLOW_RULE_NULL);
413 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800414 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700415 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700416 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700417 case PENDING_REMOVE:
418 case REMOVED:
419 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530420 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700421 break;
422 case ADDED:
423 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800424 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530425 if (isFlowOnlyInStore) {
426 // Publishing RULE_ADD_REQUESTED event facilitates
427 // preparation of statistics for the concerned rule
428 if (event == null) {
429 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
430 }
431 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800432 try {
433 frp.applyFlowRule(flowRule);
434 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700435 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800436 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800437 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800438 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
439 }
440 }
tom9b4030d2014-10-06 10:39:03 -0700441 break;
442 default:
443 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700444 }
445
alshabibbb42cad2014-09-25 11:43:05 -0700446 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700447 post(event);
448 }
alshabib57044ba2014-09-16 15:58:01 -0700449 }
450
alshabibba5ac482014-10-02 17:15:20 -0700451 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700452 checkNotNull(flowRule, FLOW_RULE_NULL);
453 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800454 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700455 FlowRuleProvider frp = getProvider(flowRule.deviceId());
456 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700457 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700458 }
459
alshabib1c319ff2014-10-04 20:29:09 -0700460 private void flowAdded(FlowEntry flowEntry) {
461 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700462 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700463
alshabib1c319ff2014-10-04 20:29:09 -0700464 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700465 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700466 if (event == null) {
467 log.debug("No flow store event generated.");
468 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800469 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700470 post(event);
471 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700472 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800473 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700474 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700475 }
alshabib57044ba2014-09-16 15:58:01 -0700476 }
477
alshabib1c319ff2014-10-04 20:29:09 -0700478 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
479 if (storedRule == null) {
480 return false;
481 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700482 if (storedRule.isPermanent()) {
483 return true;
484 }
485
Ray Milkey3717e602018-02-01 13:49:47 -0800486 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700487 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700488
489 // Checking flow with hardTimeout
490 if (storedRule.hardTimeout() != 0) {
491 if (!firstSeen.containsKey(storedRule)) {
492 // First time rule adding
493 firstSeen.put(storedRule, currentTime);
494 } else {
495 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800496 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700497 if ((currentTime - first) > hardTimeout) {
498 return false;
499 }
500 }
501 }
502
Gamze Abaka8d03f182017-07-20 15:09:24 +0000503 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700504 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700505 return true;
506 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700507 if (!lastSeen.containsKey(storedRule)) {
508 // checking for the first time
509 lastSeen.put(storedRule, storedRule.lastSeen());
510 // Use following if lastSeen attr. was removed.
511 //lastSeen.put(storedRule, currentTime);
512 }
513 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700514
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800515 // concurrently removed? let the liveness check fail
516 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700517 }
518
alshabib5c370ff2014-09-18 10:12:14 -0700519 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700520 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900521 pushFlowMetricsInternal(deviceId, flowEntries, true);
522 }
523
524 @Override
525 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
526 pushFlowMetricsInternal(deviceId, flowEntries, false);
527 }
528
529 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
530 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700531 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
532 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
533
Saurav Dasfa2fa932015-03-03 11:29:48 -0800534 for (FlowEntry rule : flowEntries) {
535 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700536 FlowEntry storedRule = storedRules.remove(rule);
537 if (storedRule != null) {
538 if (storedRule.exactMatch(rule)) {
539 // we both have the rule, let's update some info then.
540 flowAdded(rule);
541 } else {
542 // the two rules are not an exact match - remove the
543 // switch's rule and install our rule
544 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530545 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700546 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800547 } else {
548 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700549 if (!allowExtraneousRules) {
550 extraneousFlow(rule);
551 }
alshabib93cb57f2015-02-12 17:43:26 -0800552 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700553 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700554 log.warn("Can't process added or extra rule {} for device {}:{}",
555 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800556 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800557 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900558
559 // DO NOT reinstall
560 if (useMissingFlow) {
561 for (FlowEntry rule : storedRules.keySet()) {
562 try {
563 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530564 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
565 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900566 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700567 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900568 }
alshabib93cb57f2015-02-12 17:43:26 -0800569 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800570 }
alshabib5c370ff2014-09-18 10:12:14 -0700571 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800572
573 @Override
574 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
575 store.batchOperationComplete(FlowRuleBatchEvent.completed(
576 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
577 operation
578 ));
579 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700580
581 @Override
582 public void pushTableStatistics(DeviceId deviceId,
583 List<TableStatisticsEntry> tableStats) {
584 store.updateTableStatistics(deviceId, tableStats);
585 }
alshabib57044ba2014-09-16 15:58:01 -0700586 }
587
tomc78acee2014-09-24 15:16:55 -0700588 // Store delegate to re-post events emitted from the store.
589 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800590
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800591
Madan Jampani117aaae2014-10-23 10:04:05 -0700592 // TODO: Right now we only dispatch events at individual flowEntry level.
593 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700594 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700595 public void notify(FlowRuleBatchEvent event) {
596 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700597 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700598 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800599 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700600 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800601 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800602 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800603 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700604 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800605 break;
606 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700607 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800608 break;
609 case MODIFY:
610 //TODO: do something here when the time comes.
611 break;
612 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800613 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800614 }
615 }
616 );
617
618 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700619 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800620 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700621 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
622 if (flowRuleProvider != null) {
623 flowRuleProvider.executeBatch(batchOperation);
624 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800625
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800626 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700627
Madan Jampani117aaae2014-10-23 10:04:05 -0700628 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800629
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800630 FlowOperationsProcessor fops = pendingFlowOperations.remove(
631 event.subject().batchId());
632 if (event.result().isSuccess()) {
633 if (fops != null) {
634 fops.satisfy(event.deviceId());
635 }
636 } else {
637 fops.fail(event.deviceId(), event.result().failedItems());
638 }
639
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700640 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800641
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700642 default:
643 break;
644 }
tomc78acee2014-09-24 15:16:55 -0700645 }
646 }
alshabib902d41b2014-10-07 16:52:05 -0700647
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700648 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
649 switch (input) {
650 case ADD:
651 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
652 case MODIFY:
653 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
654 case REMOVE:
655 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
656 default:
657 throw new UnsupportedOperationException("Unknown flow rule type " + input);
658 }
659 }
660
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800661 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700662 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800663 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700664
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700665 // Mutable
666 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700667 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700668 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700669
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700670 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800671 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800672 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700673 }
674
675 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700676 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800677 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800678 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700679 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700680 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700681 }
682 }
683
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800684 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700685 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800686
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700687 for (FlowRuleOperation op : ops) {
688 perDeviceBatches.put(op.rule().deviceId(),
689 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800690 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000691 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800692
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800693 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700694 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800695 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
696 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000697 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700698 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700699 }
700 }
701
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700702 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000703 pendingDevices.remove(devId);
704 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700705 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700706 }
alshabib193525b2014-10-08 18:58:03 -0700707 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800708
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700709 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700710 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000711 pendingDevices.remove(devId);
712 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700713 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800714 }
715
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700716 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
717 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800718
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700719 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800720 }
alshabib902d41b2014-10-07 16:52:05 -0700721 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700722
723 @Override
724 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
725 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800726 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700727 return store.getTableStatistics(deviceId);
728 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800729
Patryk Konopka7e40c012017-06-06 13:38:06 +0200730 @Override
731 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800732 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200733 return store.getActiveFlowRuleCount(deviceId);
734 }
735
Charles Chan0c7c43b2016-01-14 17:39:20 -0800736 private class InternalDeviceListener implements DeviceListener {
737 @Override
738 public void event(DeviceEvent event) {
739 switch (event.type()) {
740 case DEVICE_REMOVED:
741 case DEVICE_AVAILABILITY_CHANGED:
742 DeviceId deviceId = event.subject().id();
743 if (!deviceService.isAvailable(deviceId)) {
744 if (purgeOnDisconnection) {
745 store.purgeFlowRule(deviceId);
746 }
747 }
748 break;
749 default:
750 break;
751 }
752 }
753 }
alshabib57044ba2014-09-16 15:58:01 -0700754}