blob: 65cbd99c17d585c76f7bf6643fc44f4644d18836 [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;
Jian Lid9b5f552016-03-11 18:15:31 -080024import org.onlab.util.Tools;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070025import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.ApplicationId;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080027import org.onosproject.core.CoreService;
28import org.onosproject.core.IdGenerator;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080029import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080032import org.onosproject.net.device.DeviceEvent;
33import org.onosproject.net.device.DeviceListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.device.DeviceService;
Jordan Halterman4f3193a2017-07-11 20:32:49 -070035import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.CompletedBatchOperation;
Charles M.C. Chan1229eca2015-05-18 06:27:52 +080037import org.onosproject.net.flow.DefaultFlowEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.FlowEntry;
39import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.flow.FlowRuleEvent;
41import org.onosproject.net.flow.FlowRuleListener;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080042import org.onosproject.net.flow.FlowRuleOperation;
43import org.onosproject.net.flow.FlowRuleOperations;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080044import org.onosproject.net.flow.FlowRuleProgrammable;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.flow.FlowRuleProvider;
46import org.onosproject.net.flow.FlowRuleProviderRegistry;
47import org.onosproject.net.flow.FlowRuleProviderService;
48import org.onosproject.net.flow.FlowRuleService;
49import org.onosproject.net.flow.FlowRuleStore;
50import org.onosproject.net.flow.FlowRuleStoreDelegate;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070051import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry;
53import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent;
54import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation;
55import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080056import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.provider.AbstractProviderService;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080058import org.onosproject.net.provider.ProviderId;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070059import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060import org.osgi.service.component.annotations.Activate;
61import org.osgi.service.component.annotations.Component;
62import org.osgi.service.component.annotations.Deactivate;
63import org.osgi.service.component.annotations.Modified;
64import org.osgi.service.component.annotations.Reference;
65import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib57044ba2014-09-16 15:58:01 -070066import org.slf4j.Logger;
67
Brian O'Connord12267c2015-02-17 18:17:08 -080068import java.util.Collections;
Marc De Leenheerde47caa2015-04-24 11:27:44 -070069import java.util.Dictionary;
Sho SHIMIZUb837cb72016-08-30 14:44:50 -070070import java.util.HashSet;
Brian O'Connord12267c2015-02-17 18:17:08 -080071import java.util.List;
72import java.util.Map;
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -080073import java.util.Optional;
Brian O'Connord12267c2015-02-17 18:17:08 -080074import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
76import java.util.concurrent.ExecutorService;
77import java.util.concurrent.Executors;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080078
Thomas Vachuska9b2da212014-11-10 19:30:25 -080079import static com.google.common.base.Preconditions.checkNotNull;
Charles Chan0c7c43b2016-01-14 17:39:20 -080080import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080081import static org.onlab.util.Tools.get;
Brian O'Connord12267c2015-02-17 18:17:08 -080082import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070083import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
84import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090085import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080086import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ;
87import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070088import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090089
tome4729872014-09-23 00:37:37 -070090/**
91 * Provides implementation of the flow NB & SB APIs.
92 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070093@Component(immediate = true, service = { FlowRuleService.class, FlowRuleProviderRegistry.class })
tom202175a2014-09-19 19:00:11 -070094public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070095 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
96 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -070097 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -070098
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080099 private final Logger log = getLogger(getClass());
100
Thomas Vachuska227943d2018-03-19 09:37:28 -0700101 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
102 private static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700103 private static final boolean ALLOW_EXTRANEOUS_RULES = false;
104
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700105 //@Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
106 // label = "Allow flow rules in switch not installed by ONOS")
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700107 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES;
108
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700109 //@Property(name = "purgeOnDisconnection", boolValue = false,
110 // label = "Purge entries associated with a device when the device goes offline")
Charles Chan0c7c43b2016-01-14 17:39:20 -0800111 private boolean purgeOnDisconnection = false;
112
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800113 private static final int DEFAULT_POLL_FREQUENCY = 30;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700114 //@Property(name = "fallbackFlowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
115 // label = "Frequency (in seconds) for polling flow statistics via fallback provider")
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800116 private int fallbackFlowPollFrequency = DEFAULT_POLL_FREQUENCY;
alshabib57044ba2014-09-16 15:58:01 -0700117
alshabibbb42cad2014-09-25 11:43:05 -0700118 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800119 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700120
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800121 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800122
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800123 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800124 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800125
126 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700127 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800128
129 private IdGenerator idGenerator;
130
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700131 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700132
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tombe988312014-09-19 18:38:47 -0700134 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700135
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700137 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700138
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800140 protected CoreService coreService;
141
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800143 protected MastershipService mastershipService;
144
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700146 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800147
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700149 protected DriverService driverService;
150
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700151 @Activate
152 public void activate(ComponentContext context) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800153 modified(context);
tomc78acee2014-09-24 15:16:55 -0700154 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700155 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800156 deviceService.addListener(deviceListener);
157 cfgService.registerProperties(getClass());
158 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
alshabib57044ba2014-09-16 15:58:01 -0700159 log.info("Started");
160 }
161
162 @Deactivate
163 public void deactivate() {
Andrea Campanella5a3c09c2017-12-01 13:57:48 +0100164 driverProvider.terminate();
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700165 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700166 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800167 deviceInstallers.shutdownNow();
168 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700169 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700170 eventDispatcher.removeSink(FlowRuleEvent.class);
171 log.info("Stopped");
172 }
173
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700174 @Modified
175 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800176 if (context != null) {
177 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700178 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800179 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800180 deviceService, mastershipService, fallbackFlowPollFrequency);
181 }
182
183 @Override
184 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800185 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800186 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700187
Charles Chan0c7c43b2016-01-14 17:39:20 -0800188 /**
189 * Extracts properties from the component configuration context.
190 *
191 * @param context the component context
192 */
193 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700194 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800195 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700196
Jian Lid9b5f552016-03-11 18:15:31 -0800197 flag = Tools.isPropertyEnabled(properties, "allowExtraneousRules");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800198 if (flag == null) {
199 log.info("AllowExtraneousRules is not configured, " +
200 "using current value of {}", allowExtraneousRules);
201 } else {
202 allowExtraneousRules = flag;
203 log.info("Configured. AllowExtraneousRules is {}",
204 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700205 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800206
Jian Lid9b5f552016-03-11 18:15:31 -0800207 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800208 if (flag == null) {
209 log.info("PurgeOnDisconnection is not configured, " +
210 "using current value of {}", purgeOnDisconnection);
211 } else {
212 purgeOnDisconnection = flag;
213 log.info("Configured. PurgeOnDisconnection is {}",
214 purgeOnDisconnection ? "enabled" : "disabled");
215 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800216
217 String s = get(properties, "fallbackFlowPollFrequency");
Carmelo Casconee6a97542018-06-20 16:48:00 +0200218 if (isNullOrEmpty(s)) {
219 log.info("fallbackFlowPollFrequency is not configured, " +
220 "using current value of {} seconds",
221 fallbackFlowPollFrequency);
222 } else {
223 try {
224 fallbackFlowPollFrequency = Integer.parseInt(s);
225 log.info("Configured. FallbackFlowPollFrequency is {} seconds",
226 fallbackFlowPollFrequency);
227 } catch (NumberFormatException e) {
228 log.warn("Configured fallbackFlowPollFrequency value '{}' " +
229 "is not a number, using current value of {} seconds",
230 fallbackFlowPollFrequency);
231 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800232 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800233 }
234
alshabib57044ba2014-09-16 15:58:01 -0700235 @Override
tom9b4030d2014-10-06 10:39:03 -0700236 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900237 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700238 return store.getFlowRuleCount();
239 }
240
241 @Override
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800242 public int getFlowRuleCount(DeviceId deviceId) {
243 checkPermission(FLOWRULE_READ);
Thomas Vachuska227943d2018-03-19 09:37:28 -0700244 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800245 return store.getFlowRuleCount(deviceId);
246 }
247
248 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700249 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900250 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800251 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700252 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700253 }
254
255 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700256 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900257 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900258
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800259 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800260 for (FlowRule flowRule : flowRules) {
261 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700262 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800263 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700264 }
265
266 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530267 public void purgeFlowRules(DeviceId deviceId) {
268 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800269 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530270 store.purgeFlowRule(deviceId);
271 }
272
273 @Override
alshabib57044ba2014-09-16 15:58:01 -0700274 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900275 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900276
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800277 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800278 for (FlowRule flowRule : flowRules) {
279 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700280 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800281 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700282 }
alshabib57044ba2014-09-16 15:58:01 -0700283
alshabiba68eb962014-09-24 20:34:13 -0700284 @Override
285 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900286 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700287 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700288 }
289
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530290 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700291 @Override
292 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900293 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900294
Madan Jampani6a456162014-10-24 11:36:17 -0700295 Set<FlowRule> 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;
alshabib57044ba2014-09-16 15:58:01 -0700304 }
305
306 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530307 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
308 checkPermission(FLOWRULE_READ);
309
310 Set<FlowEntry> flowEntries = Sets.newHashSet();
311 for (Device d : deviceService.getDevices()) {
312 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
313 if (flowEntry.appId() == id.id()) {
314 flowEntries.add(flowEntry);
315 }
316 }
317 }
318 return flowEntries;
319 }
320
321 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800322 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900323 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900324
alshabibaa7e7de2014-11-12 19:20:44 -0800325 Set<FlowRule> matches = Sets.newHashSet();
326 long toLookUp = ((long) appId.id() << 16) | groupId;
327 for (Device d : deviceService.getDevices()) {
328 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
329 if ((flowEntry.id().value() >>> 32) == toLookUp) {
330 matches.add(flowEntry);
331 }
332 }
333 }
334 return matches;
335 }
336
337 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800338 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900339 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700340 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700341 }
342
343 @Override
alshabib57044ba2014-09-16 15:58:01 -0700344 protected FlowRuleProviderService createProviderService(
345 FlowRuleProvider provider) {
346 return new InternalFlowRuleProviderService(provider);
347 }
348
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800349 @Override
350 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
351 log.warn("should not be calling getProvider(ProviderId)");
352 return super.getProvider(pid);
353 }
354
355 /**
356 * {@inheritDoc}
357 * if the Device does not support {@link FlowRuleProgrammable}.
358 */
359 @Override
360 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800361 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800362 // if device supports FlowRuleProgrammable,
363 // use FlowRuleProgrammable via FlowRuleDriverProvider
364 return Optional.ofNullable(deviceService.getDevice(deviceId))
365 .filter(dev -> dev.is(FlowRuleProgrammable.class))
366 .<FlowRuleProvider>map(x -> driverProvider)
367 .orElseGet(() -> super.getProvider(deviceId));
368 }
369
alshabib57044ba2014-09-16 15:58:01 -0700370 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700371 extends AbstractProviderService<FlowRuleProvider>
372 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700373
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700374 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700375 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
376
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700377
alshabib57044ba2014-09-16 15:58:01 -0700378 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
379 super(provider);
380 }
381
382 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700383 public void flowRemoved(FlowEntry flowEntry) {
384 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700385 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700386 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700387 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700388 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700389 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800390 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700391 return;
392 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700393 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
394 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
395 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800396 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700397 FlowRuleEvent event = null;
398 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700399 case ADDED:
400 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700401 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700402 break;
403 case PENDING_REMOVE:
404 case REMOVED:
405 event = store.removeFlowRule(stored);
406 break;
407 default:
408 break;
alshabib57044ba2014-09-16 15:58:01 -0700409
alshabiba68eb962014-09-24 20:34:13 -0700410 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700411 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700412 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700413 post(event);
414 }
alshabib57044ba2014-09-16 15:58:01 -0700415 }
416
alshabibba5ac482014-10-02 17:15:20 -0700417
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530418 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700419 checkNotNull(flowRule, FLOW_RULE_NULL);
420 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800421 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700422 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700423 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700424 case PENDING_REMOVE:
425 case REMOVED:
426 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530427 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700428 break;
429 case ADDED:
430 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800431 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530432 if (isFlowOnlyInStore) {
433 // Publishing RULE_ADD_REQUESTED event facilitates
434 // preparation of statistics for the concerned rule
435 if (event == null) {
436 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
437 }
438 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800439 try {
440 frp.applyFlowRule(flowRule);
441 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700442 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800443 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800444 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800445 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
446 }
447 }
tom9b4030d2014-10-06 10:39:03 -0700448 break;
449 default:
450 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700451 }
452
alshabibbb42cad2014-09-25 11:43:05 -0700453 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700454 post(event);
455 }
alshabib57044ba2014-09-16 15:58:01 -0700456 }
457
alshabibba5ac482014-10-02 17:15:20 -0700458 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700459 checkNotNull(flowRule, FLOW_RULE_NULL);
460 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800461 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700462 FlowRuleProvider frp = getProvider(flowRule.deviceId());
463 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700464 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700465 }
466
alshabib1c319ff2014-10-04 20:29:09 -0700467 private void flowAdded(FlowEntry flowEntry) {
468 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700469 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700470
alshabib1c319ff2014-10-04 20:29:09 -0700471 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700472 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700473 if (event == null) {
474 log.debug("No flow store event generated.");
475 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800476 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700477 post(event);
478 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700479 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800480 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700481 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700482 }
alshabib57044ba2014-09-16 15:58:01 -0700483 }
484
alshabib1c319ff2014-10-04 20:29:09 -0700485 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
486 if (storedRule == null) {
487 return false;
488 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700489 if (storedRule.isPermanent()) {
490 return true;
491 }
492
Ray Milkey3717e602018-02-01 13:49:47 -0800493 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700494 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700495
496 // Checking flow with hardTimeout
497 if (storedRule.hardTimeout() != 0) {
498 if (!firstSeen.containsKey(storedRule)) {
499 // First time rule adding
500 firstSeen.put(storedRule, currentTime);
501 } else {
502 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800503 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700504 if ((currentTime - first) > hardTimeout) {
505 return false;
506 }
507 }
508 }
509
Gamze Abaka8d03f182017-07-20 15:09:24 +0000510 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700511 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700512 return true;
513 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700514 if (!lastSeen.containsKey(storedRule)) {
515 // checking for the first time
516 lastSeen.put(storedRule, storedRule.lastSeen());
517 // Use following if lastSeen attr. was removed.
518 //lastSeen.put(storedRule, currentTime);
519 }
520 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700521
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800522 // concurrently removed? let the liveness check fail
523 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700524 }
525
alshabib5c370ff2014-09-18 10:12:14 -0700526 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700527 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900528 pushFlowMetricsInternal(deviceId, flowEntries, true);
529 }
530
531 @Override
532 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
533 pushFlowMetricsInternal(deviceId, flowEntries, false);
534 }
535
536 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
537 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700538 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
539 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
540
Saurav Dasfa2fa932015-03-03 11:29:48 -0800541 for (FlowEntry rule : flowEntries) {
542 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700543 FlowEntry storedRule = storedRules.remove(rule);
544 if (storedRule != null) {
545 if (storedRule.exactMatch(rule)) {
546 // we both have the rule, let's update some info then.
547 flowAdded(rule);
548 } else {
549 // the two rules are not an exact match - remove the
550 // switch's rule and install our rule
551 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530552 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700553 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800554 } else {
555 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700556 if (!allowExtraneousRules) {
557 extraneousFlow(rule);
558 }
alshabib93cb57f2015-02-12 17:43:26 -0800559 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700560 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700561 log.warn("Can't process added or extra rule {} for device {}:{}",
562 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800563 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800564 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900565
566 // DO NOT reinstall
567 if (useMissingFlow) {
568 for (FlowEntry rule : storedRules.keySet()) {
569 try {
570 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530571 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
572 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900573 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700574 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900575 }
alshabib93cb57f2015-02-12 17:43:26 -0800576 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800577 }
alshabib5c370ff2014-09-18 10:12:14 -0700578 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800579
580 @Override
581 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
582 store.batchOperationComplete(FlowRuleBatchEvent.completed(
583 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
584 operation
585 ));
586 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700587
588 @Override
589 public void pushTableStatistics(DeviceId deviceId,
590 List<TableStatisticsEntry> tableStats) {
591 store.updateTableStatistics(deviceId, tableStats);
592 }
alshabib57044ba2014-09-16 15:58:01 -0700593 }
594
tomc78acee2014-09-24 15:16:55 -0700595 // Store delegate to re-post events emitted from the store.
596 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800597
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800598
Madan Jampani117aaae2014-10-23 10:04:05 -0700599 // TODO: Right now we only dispatch events at individual flowEntry level.
600 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700601 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700602 public void notify(FlowRuleBatchEvent event) {
603 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700604 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700605 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800606 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700607 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800608 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800609 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800610 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700611 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800612 break;
613 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700614 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800615 break;
616 case MODIFY:
617 //TODO: do something here when the time comes.
618 break;
619 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800620 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800621 }
622 }
623 );
624
625 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700626 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800627 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700628 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
629 if (flowRuleProvider != null) {
630 flowRuleProvider.executeBatch(batchOperation);
631 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800632
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800633 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700634
Madan Jampani117aaae2014-10-23 10:04:05 -0700635 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800636
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800637 FlowOperationsProcessor fops = pendingFlowOperations.remove(
638 event.subject().batchId());
639 if (event.result().isSuccess()) {
640 if (fops != null) {
641 fops.satisfy(event.deviceId());
642 }
643 } else {
644 fops.fail(event.deviceId(), event.result().failedItems());
645 }
646
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700647 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800648
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700649 default:
650 break;
651 }
tomc78acee2014-09-24 15:16:55 -0700652 }
653 }
alshabib902d41b2014-10-07 16:52:05 -0700654
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700655 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
656 switch (input) {
657 case ADD:
658 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
659 case MODIFY:
660 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
661 case REMOVE:
662 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
663 default:
664 throw new UnsupportedOperationException("Unknown flow rule type " + input);
665 }
666 }
667
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800668 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700669 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800670 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700671
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700672 // Mutable
673 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700674 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700675 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700676
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700677 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800678 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800679 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700680 }
681
682 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700683 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800684 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800685 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700686 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700687 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700688 }
689 }
690
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800691 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700692 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800693
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700694 for (FlowRuleOperation op : ops) {
695 perDeviceBatches.put(op.rule().deviceId(),
696 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800697 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000698 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800699
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800700 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700701 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800702 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
703 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000704 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700705 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700706 }
707 }
708
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700709 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000710 pendingDevices.remove(devId);
711 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700712 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700713 }
alshabib193525b2014-10-08 18:58:03 -0700714 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800715
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700716 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700717 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000718 pendingDevices.remove(devId);
719 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700720 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800721 }
722
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700723 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
724 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800725
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700726 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800727 }
alshabib902d41b2014-10-07 16:52:05 -0700728 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700729
730 @Override
731 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
732 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800733 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700734 return store.getTableStatistics(deviceId);
735 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800736
Patryk Konopka7e40c012017-06-06 13:38:06 +0200737 @Override
738 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800739 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200740 return store.getActiveFlowRuleCount(deviceId);
741 }
742
Charles Chan0c7c43b2016-01-14 17:39:20 -0800743 private class InternalDeviceListener implements DeviceListener {
744 @Override
745 public void event(DeviceEvent event) {
746 switch (event.type()) {
747 case DEVICE_REMOVED:
748 case DEVICE_AVAILABILITY_CHANGED:
749 DeviceId deviceId = event.subject().id();
750 if (!deviceService.isAvailable(deviceId)) {
751 if (purgeOnDisconnection) {
752 store.purgeFlowRule(deviceId);
753 }
754 }
755 break;
756 default:
757 break;
758 }
759 }
760 }
alshabib57044ba2014-09-16 15:58:01 -0700761}