blob: 56a54e4562c368a5813919d60cdedfa76104c52a [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");
Carmelo Casconee6a97542018-06-20 16:48:00 +0200221 if (isNullOrEmpty(s)) {
222 log.info("fallbackFlowPollFrequency is not configured, " +
223 "using current value of {} seconds",
224 fallbackFlowPollFrequency);
225 } else {
226 try {
227 fallbackFlowPollFrequency = Integer.parseInt(s);
228 log.info("Configured. FallbackFlowPollFrequency is {} seconds",
229 fallbackFlowPollFrequency);
230 } catch (NumberFormatException e) {
231 log.warn("Configured fallbackFlowPollFrequency value '{}' " +
232 "is not a number, using current value of {} seconds",
233 fallbackFlowPollFrequency);
234 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800235 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800236 }
237
alshabib57044ba2014-09-16 15:58:01 -0700238 @Override
tom9b4030d2014-10-06 10:39:03 -0700239 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900240 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700241 return store.getFlowRuleCount();
242 }
243
244 @Override
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800245 public int getFlowRuleCount(DeviceId deviceId) {
246 checkPermission(FLOWRULE_READ);
Thomas Vachuska227943d2018-03-19 09:37:28 -0700247 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800248 return store.getFlowRuleCount(deviceId);
249 }
250
251 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700252 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900253 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800254 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700255 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700256 }
257
258 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700259 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900260 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900261
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800262 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800263 for (FlowRule flowRule : flowRules) {
264 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700265 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800266 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700267 }
268
269 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530270 public void purgeFlowRules(DeviceId deviceId) {
271 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800272 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530273 store.purgeFlowRule(deviceId);
274 }
275
276 @Override
alshabib57044ba2014-09-16 15:58:01 -0700277 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900278 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900279
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800280 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800281 for (FlowRule flowRule : flowRules) {
282 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700283 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800284 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700285 }
alshabib57044ba2014-09-16 15:58:01 -0700286
alshabiba68eb962014-09-24 20:34:13 -0700287 @Override
288 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900289 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700290 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700291 }
292
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530293 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700294 @Override
295 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900296 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900297
Madan Jampani6a456162014-10-24 11:36:17 -0700298 Set<FlowRule> flowEntries = Sets.newHashSet();
299 for (Device d : deviceService.getDevices()) {
300 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
301 if (flowEntry.appId() == id.id()) {
302 flowEntries.add(flowEntry);
303 }
304 }
305 }
306 return flowEntries;
alshabib57044ba2014-09-16 15:58:01 -0700307 }
308
309 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530310 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
311 checkPermission(FLOWRULE_READ);
312
313 Set<FlowEntry> flowEntries = Sets.newHashSet();
314 for (Device d : deviceService.getDevices()) {
315 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
316 if (flowEntry.appId() == id.id()) {
317 flowEntries.add(flowEntry);
318 }
319 }
320 }
321 return flowEntries;
322 }
323
324 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800325 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900326 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900327
alshabibaa7e7de2014-11-12 19:20:44 -0800328 Set<FlowRule> matches = Sets.newHashSet();
329 long toLookUp = ((long) appId.id() << 16) | groupId;
330 for (Device d : deviceService.getDevices()) {
331 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
332 if ((flowEntry.id().value() >>> 32) == toLookUp) {
333 matches.add(flowEntry);
334 }
335 }
336 }
337 return matches;
338 }
339
340 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800341 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900342 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700343 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700344 }
345
346 @Override
alshabib57044ba2014-09-16 15:58:01 -0700347 protected FlowRuleProviderService createProviderService(
348 FlowRuleProvider provider) {
349 return new InternalFlowRuleProviderService(provider);
350 }
351
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800352 @Override
353 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
354 log.warn("should not be calling getProvider(ProviderId)");
355 return super.getProvider(pid);
356 }
357
358 /**
359 * {@inheritDoc}
360 * if the Device does not support {@link FlowRuleProgrammable}.
361 */
362 @Override
363 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800364 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800365 // if device supports FlowRuleProgrammable,
366 // use FlowRuleProgrammable via FlowRuleDriverProvider
367 return Optional.ofNullable(deviceService.getDevice(deviceId))
368 .filter(dev -> dev.is(FlowRuleProgrammable.class))
369 .<FlowRuleProvider>map(x -> driverProvider)
370 .orElseGet(() -> super.getProvider(deviceId));
371 }
372
alshabib57044ba2014-09-16 15:58:01 -0700373 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700374 extends AbstractProviderService<FlowRuleProvider>
375 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700376
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700377 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700378 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
379
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700380
alshabib57044ba2014-09-16 15:58:01 -0700381 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
382 super(provider);
383 }
384
385 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700386 public void flowRemoved(FlowEntry flowEntry) {
387 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700388 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700389 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700390 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700391 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700392 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800393 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700394 return;
395 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700396 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
397 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
398 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800399 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700400 FlowRuleEvent event = null;
401 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700402 case ADDED:
403 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700404 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700405 break;
406 case PENDING_REMOVE:
407 case REMOVED:
408 event = store.removeFlowRule(stored);
409 break;
410 default:
411 break;
alshabib57044ba2014-09-16 15:58:01 -0700412
alshabiba68eb962014-09-24 20:34:13 -0700413 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700414 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700415 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700416 post(event);
417 }
alshabib57044ba2014-09-16 15:58:01 -0700418 }
419
alshabibba5ac482014-10-02 17:15:20 -0700420
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530421 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700422 checkNotNull(flowRule, FLOW_RULE_NULL);
423 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800424 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700425 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700426 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700427 case PENDING_REMOVE:
428 case REMOVED:
429 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530430 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700431 break;
432 case ADDED:
433 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800434 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530435 if (isFlowOnlyInStore) {
436 // Publishing RULE_ADD_REQUESTED event facilitates
437 // preparation of statistics for the concerned rule
438 if (event == null) {
439 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
440 }
441 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800442 try {
443 frp.applyFlowRule(flowRule);
444 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700445 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800446 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800447 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800448 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
449 }
450 }
tom9b4030d2014-10-06 10:39:03 -0700451 break;
452 default:
453 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700454 }
455
alshabibbb42cad2014-09-25 11:43:05 -0700456 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700457 post(event);
458 }
alshabib57044ba2014-09-16 15:58:01 -0700459 }
460
alshabibba5ac482014-10-02 17:15:20 -0700461 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700462 checkNotNull(flowRule, FLOW_RULE_NULL);
463 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800464 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700465 FlowRuleProvider frp = getProvider(flowRule.deviceId());
466 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700467 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700468 }
469
alshabib1c319ff2014-10-04 20:29:09 -0700470 private void flowAdded(FlowEntry flowEntry) {
471 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700472 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700473
alshabib1c319ff2014-10-04 20:29:09 -0700474 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700475 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700476 if (event == null) {
477 log.debug("No flow store event generated.");
478 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800479 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700480 post(event);
481 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700482 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800483 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700484 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700485 }
alshabib57044ba2014-09-16 15:58:01 -0700486 }
487
alshabib1c319ff2014-10-04 20:29:09 -0700488 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
489 if (storedRule == null) {
490 return false;
491 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700492 if (storedRule.isPermanent()) {
493 return true;
494 }
495
Ray Milkey3717e602018-02-01 13:49:47 -0800496 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700497 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700498
499 // Checking flow with hardTimeout
500 if (storedRule.hardTimeout() != 0) {
501 if (!firstSeen.containsKey(storedRule)) {
502 // First time rule adding
503 firstSeen.put(storedRule, currentTime);
504 } else {
505 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800506 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700507 if ((currentTime - first) > hardTimeout) {
508 return false;
509 }
510 }
511 }
512
Gamze Abaka8d03f182017-07-20 15:09:24 +0000513 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700514 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700515 return true;
516 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700517 if (!lastSeen.containsKey(storedRule)) {
518 // checking for the first time
519 lastSeen.put(storedRule, storedRule.lastSeen());
520 // Use following if lastSeen attr. was removed.
521 //lastSeen.put(storedRule, currentTime);
522 }
523 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700524
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800525 // concurrently removed? let the liveness check fail
526 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700527 }
528
alshabib5c370ff2014-09-18 10:12:14 -0700529 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700530 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900531 pushFlowMetricsInternal(deviceId, flowEntries, true);
532 }
533
534 @Override
535 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
536 pushFlowMetricsInternal(deviceId, flowEntries, false);
537 }
538
539 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
540 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700541 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
542 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
543
Saurav Dasfa2fa932015-03-03 11:29:48 -0800544 for (FlowEntry rule : flowEntries) {
545 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700546 FlowEntry storedRule = storedRules.remove(rule);
547 if (storedRule != null) {
548 if (storedRule.exactMatch(rule)) {
549 // we both have the rule, let's update some info then.
550 flowAdded(rule);
551 } else {
552 // the two rules are not an exact match - remove the
553 // switch's rule and install our rule
554 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530555 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700556 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800557 } else {
558 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700559 if (!allowExtraneousRules) {
560 extraneousFlow(rule);
561 }
alshabib93cb57f2015-02-12 17:43:26 -0800562 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700563 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700564 log.warn("Can't process added or extra rule {} for device {}:{}",
565 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800566 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800567 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900568
569 // DO NOT reinstall
570 if (useMissingFlow) {
571 for (FlowEntry rule : storedRules.keySet()) {
572 try {
573 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530574 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
575 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900576 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700577 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900578 }
alshabib93cb57f2015-02-12 17:43:26 -0800579 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800580 }
alshabib5c370ff2014-09-18 10:12:14 -0700581 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800582
583 @Override
584 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
585 store.batchOperationComplete(FlowRuleBatchEvent.completed(
586 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
587 operation
588 ));
589 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700590
591 @Override
592 public void pushTableStatistics(DeviceId deviceId,
593 List<TableStatisticsEntry> tableStats) {
594 store.updateTableStatistics(deviceId, tableStats);
595 }
alshabib57044ba2014-09-16 15:58:01 -0700596 }
597
tomc78acee2014-09-24 15:16:55 -0700598 // Store delegate to re-post events emitted from the store.
599 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800600
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800601
Madan Jampani117aaae2014-10-23 10:04:05 -0700602 // TODO: Right now we only dispatch events at individual flowEntry level.
603 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700604 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700605 public void notify(FlowRuleBatchEvent event) {
606 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700607 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700608 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800609 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700610 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800611 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800612 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800613 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700614 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800615 break;
616 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700617 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800618 break;
619 case MODIFY:
620 //TODO: do something here when the time comes.
621 break;
622 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800623 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800624 }
625 }
626 );
627
628 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700629 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800630 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700631 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
632 if (flowRuleProvider != null) {
633 flowRuleProvider.executeBatch(batchOperation);
634 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800635
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800636 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700637
Madan Jampani117aaae2014-10-23 10:04:05 -0700638 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800639
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800640 FlowOperationsProcessor fops = pendingFlowOperations.remove(
641 event.subject().batchId());
642 if (event.result().isSuccess()) {
643 if (fops != null) {
644 fops.satisfy(event.deviceId());
645 }
646 } else {
647 fops.fail(event.deviceId(), event.result().failedItems());
648 }
649
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700650 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800651
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700652 default:
653 break;
654 }
tomc78acee2014-09-24 15:16:55 -0700655 }
656 }
alshabib902d41b2014-10-07 16:52:05 -0700657
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700658 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
659 switch (input) {
660 case ADD:
661 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
662 case MODIFY:
663 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
664 case REMOVE:
665 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
666 default:
667 throw new UnsupportedOperationException("Unknown flow rule type " + input);
668 }
669 }
670
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800671 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700672 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800673 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700674
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700675 // Mutable
676 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700677 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700678 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700679
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700680 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800681 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800682 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700683 }
684
685 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700686 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800687 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800688 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700689 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700690 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700691 }
692 }
693
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800694 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700695 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800696
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700697 for (FlowRuleOperation op : ops) {
698 perDeviceBatches.put(op.rule().deviceId(),
699 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800700 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000701 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800702
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800703 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700704 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800705 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
706 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000707 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700708 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700709 }
710 }
711
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700712 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000713 pendingDevices.remove(devId);
714 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700715 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700716 }
alshabib193525b2014-10-08 18:58:03 -0700717 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800718
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700719 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700720 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000721 pendingDevices.remove(devId);
722 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700723 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800724 }
725
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700726 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
727 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800728
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700729 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800730 }
alshabib902d41b2014-10-07 16:52:05 -0700731 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700732
733 @Override
734 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
735 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800736 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700737 return store.getTableStatistics(deviceId);
738 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800739
Patryk Konopka7e40c012017-06-06 13:38:06 +0200740 @Override
741 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800742 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200743 return store.getActiveFlowRuleCount(deviceId);
744 }
745
Charles Chan0c7c43b2016-01-14 17:39:20 -0800746 private class InternalDeviceListener implements DeviceListener {
747 @Override
748 public void event(DeviceEvent event) {
749 switch (event.type()) {
750 case DEVICE_REMOVED:
751 case DEVICE_AVAILABILITY_CHANGED:
752 DeviceId deviceId = event.subject().id();
753 if (!deviceService.isAvailable(deviceId)) {
754 if (purgeOnDisconnection) {
755 store.purgeFlowRule(deviceId);
756 }
757 }
758 break;
759 default:
760 break;
761 }
762 }
763 }
alshabib57044ba2014-09-16 15:58:01 -0700764}