blob: d9bec9e2af170b1e42132d82023a5636aef75467 [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;
Ray Milkeyd04e2272018-10-16 18:20:18 -070083import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES;
84import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES_DEFAULT;
85import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY;
86import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY_DEFAULT;
87import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION;
88import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION_DEFAULT;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070089import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED;
90import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090091import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac4ee7372016-02-02 16:10:09 -080092import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ;
93import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070094import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090095
tome4729872014-09-23 00:37:37 -070096/**
97 * Provides implementation of the flow NB & SB APIs.
98 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070099@Component(
100 immediate = true,
101 service = {
102 FlowRuleService.class,
103 FlowRuleProviderRegistry.class
104 },
105 property = {
Ray Milkey2d7bca12018-10-17 14:51:52 -0700106 ALLOW_EXTRANEOUS_RULES + ":Boolean=" + ALLOW_EXTRANEOUS_RULES_DEFAULT,
107 PURGE_ON_DISCONNECTION + ":Boolean=" + PURGE_ON_DISCONNECTION_DEFAULT,
108 POLL_FREQUENCY + ":Integer=" + POLL_FREQUENCY_DEFAULT
Ray Milkeyd04e2272018-10-16 18:20:18 -0700109 }
110)
tom202175a2014-09-19 19:00:11 -0700111public class FlowRuleManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700112 extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener,
113 FlowRuleProvider, FlowRuleProviderService>
tom9b4030d2014-10-06 10:39:03 -0700114 implements FlowRuleService, FlowRuleProviderRegistry {
alshabib57044ba2014-09-16 15:58:01 -0700115
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800116 private final Logger log = getLogger(getClass());
117
Thomas Vachuska227943d2018-03-19 09:37:28 -0700118 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
119 private static final String FLOW_RULE_NULL = "FlowRule cannot be null";
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700120
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700121 /** Allow flow rules in switch not installed by ONOS. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700122 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES_DEFAULT;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700123
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700124 /** Purge entries associated with a device when the device goes offline. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700125 private boolean purgeOnDisconnection = PURGE_ON_DISCONNECTION_DEFAULT;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800126
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700127 /** Frequency (in seconds) for polling flow statistics via fallback provider. */
Ray Milkeyd04e2272018-10-16 18:20:18 -0700128 private int fallbackFlowPollFrequency = POLL_FREQUENCY_DEFAULT;
alshabib57044ba2014-09-16 15:58:01 -0700129
alshabibbb42cad2014-09-25 11:43:05 -0700130 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800131 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700132
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800133 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800134
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800135 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800136 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800137
138 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700139 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800140
141 private IdGenerator idGenerator;
142
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700143 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700144
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tombe988312014-09-19 18:38:47 -0700146 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700147
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700149 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700150
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800152 protected CoreService coreService;
153
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700154 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800155 protected MastershipService mastershipService;
156
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700157 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700158 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800159
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700160 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700161 protected DriverService driverService;
162
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700163 @Activate
164 public void activate(ComponentContext context) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800165 modified(context);
tomc78acee2014-09-24 15:16:55 -0700166 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700167 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800168 deviceService.addListener(deviceListener);
169 cfgService.registerProperties(getClass());
170 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
alshabib57044ba2014-09-16 15:58:01 -0700171 log.info("Started");
172 }
173
174 @Deactivate
175 public void deactivate() {
Andrea Campanella5a3c09c2017-12-01 13:57:48 +0100176 driverProvider.terminate();
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700177 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700178 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800179 deviceInstallers.shutdownNow();
180 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700181 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700182 eventDispatcher.removeSink(FlowRuleEvent.class);
183 log.info("Stopped");
184 }
185
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700186 @Modified
187 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800188 if (context != null) {
189 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700190 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800191 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800192 deviceService, mastershipService, fallbackFlowPollFrequency);
193 }
194
195 @Override
196 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800197 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800198 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700199
Charles Chan0c7c43b2016-01-14 17:39:20 -0800200 /**
201 * Extracts properties from the component configuration context.
202 *
203 * @param context the component context
204 */
205 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700206 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800207 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700208
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700209 flag = Tools.isPropertyEnabled(properties, ALLOW_EXTRANEOUS_RULES);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800210 if (flag == null) {
211 log.info("AllowExtraneousRules is not configured, " +
212 "using current value of {}", allowExtraneousRules);
213 } else {
214 allowExtraneousRules = flag;
215 log.info("Configured. AllowExtraneousRules is {}",
216 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700217 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800218
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700219 flag = Tools.isPropertyEnabled(properties, PURGE_ON_DISCONNECTION);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800220 if (flag == null) {
221 log.info("PurgeOnDisconnection is not configured, " +
222 "using current value of {}", purgeOnDisconnection);
223 } else {
224 purgeOnDisconnection = flag;
225 log.info("Configured. PurgeOnDisconnection is {}",
226 purgeOnDisconnection ? "enabled" : "disabled");
227 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800228
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700229 String s = get(properties, POLL_FREQUENCY);
Carmelo Casconee6a97542018-06-20 16:48:00 +0200230 if (isNullOrEmpty(s)) {
231 log.info("fallbackFlowPollFrequency is not configured, " +
232 "using current value of {} seconds",
233 fallbackFlowPollFrequency);
234 } else {
235 try {
236 fallbackFlowPollFrequency = Integer.parseInt(s);
237 log.info("Configured. FallbackFlowPollFrequency is {} seconds",
238 fallbackFlowPollFrequency);
239 } catch (NumberFormatException e) {
240 log.warn("Configured fallbackFlowPollFrequency value '{}' " +
241 "is not a number, using current value of {} seconds",
242 fallbackFlowPollFrequency);
243 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800244 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800245 }
246
alshabib57044ba2014-09-16 15:58:01 -0700247 @Override
tom9b4030d2014-10-06 10:39:03 -0700248 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900249 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700250 return store.getFlowRuleCount();
251 }
252
253 @Override
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800254 public int getFlowRuleCount(DeviceId deviceId) {
255 checkPermission(FLOWRULE_READ);
Thomas Vachuska227943d2018-03-19 09:37:28 -0700256 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800257 return store.getFlowRuleCount(deviceId);
258 }
259
260 @Override
Jordan Haltermanb81fdc12019-03-04 18:12:20 -0800261 public int getFlowRuleCount(DeviceId deviceId, FlowEntry.FlowEntryState state) {
262 checkPermission(FLOWRULE_READ);
263 checkNotNull(deviceId, "Device ID cannot be null");
264 return store.getFlowRuleCount(deviceId, state);
265 }
266
267 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700268 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900269 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800270 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700271 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700272 }
273
274 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700275 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900276 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900277
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800278 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800279 for (FlowRule flowRule : flowRules) {
280 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700281 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800282 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700283 }
284
285 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530286 public void purgeFlowRules(DeviceId deviceId) {
287 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800288 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530289 store.purgeFlowRule(deviceId);
290 }
291
292 @Override
alshabib57044ba2014-09-16 15:58:01 -0700293 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900294 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900295
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800296 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800297 for (FlowRule flowRule : flowRules) {
298 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700299 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800300 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700301 }
alshabib57044ba2014-09-16 15:58:01 -0700302
alshabiba68eb962014-09-24 20:34:13 -0700303 @Override
304 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900305 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900306
Madan Jampani6a456162014-10-24 11:36:17 -0700307 Set<FlowRule> flowEntries = Sets.newHashSet();
308 for (Device d : deviceService.getDevices()) {
309 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
310 if (flowEntry.appId() == id.id()) {
311 flowEntries.add(flowEntry);
312 }
313 }
314 }
Ray Milkey4f7e3632019-02-19 15:35:20 -0800315 removeFlowRules(Iterables.toArray(flowEntries, FlowRule.class));
alshabib57044ba2014-09-16 15:58:01 -0700316 }
317
318 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530319 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
320 checkPermission(FLOWRULE_READ);
321
322 Set<FlowEntry> flowEntries = Sets.newHashSet();
323 for (Device d : deviceService.getDevices()) {
324 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
325 if (flowEntry.appId() == id.id()) {
326 flowEntries.add(flowEntry);
327 }
328 }
329 }
330 return flowEntries;
331 }
332
333 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800334 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900335 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900336
alshabibaa7e7de2014-11-12 19:20:44 -0800337 Set<FlowRule> matches = Sets.newHashSet();
338 long toLookUp = ((long) appId.id() << 16) | groupId;
339 for (Device d : deviceService.getDevices()) {
340 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
341 if ((flowEntry.id().value() >>> 32) == toLookUp) {
342 matches.add(flowEntry);
343 }
344 }
345 }
346 return matches;
347 }
348
349 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800350 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900351 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700352 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700353 }
354
355 @Override
alshabib57044ba2014-09-16 15:58:01 -0700356 protected FlowRuleProviderService createProviderService(
357 FlowRuleProvider provider) {
358 return new InternalFlowRuleProviderService(provider);
359 }
360
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800361 @Override
362 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
363 log.warn("should not be calling getProvider(ProviderId)");
364 return super.getProvider(pid);
365 }
366
367 /**
368 * {@inheritDoc}
369 * if the Device does not support {@link FlowRuleProgrammable}.
370 */
371 @Override
372 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800373 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800374 // if device supports FlowRuleProgrammable,
375 // use FlowRuleProgrammable via FlowRuleDriverProvider
376 return Optional.ofNullable(deviceService.getDevice(deviceId))
377 .filter(dev -> dev.is(FlowRuleProgrammable.class))
378 .<FlowRuleProvider>map(x -> driverProvider)
379 .orElseGet(() -> super.getProvider(deviceId));
380 }
381
alshabib57044ba2014-09-16 15:58:01 -0700382 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700383 extends AbstractProviderService<FlowRuleProvider>
384 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700385
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700386 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700387 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
388
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700389
alshabib57044ba2014-09-16 15:58:01 -0700390 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
391 super(provider);
392 }
393
394 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700395 public void flowRemoved(FlowEntry flowEntry) {
396 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700397 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700398 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700399 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700400 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700401 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800402 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700403 return;
404 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700405 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
406 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
407 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800408 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700409 FlowRuleEvent event = null;
410 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700411 case ADDED:
412 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700413 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700414 break;
415 case PENDING_REMOVE:
416 case REMOVED:
417 event = store.removeFlowRule(stored);
418 break;
419 default:
420 break;
alshabib57044ba2014-09-16 15:58:01 -0700421
alshabiba68eb962014-09-24 20:34:13 -0700422 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700423 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700424 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700425 post(event);
426 }
alshabib57044ba2014-09-16 15:58:01 -0700427 }
428
alshabibba5ac482014-10-02 17:15:20 -0700429
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530430 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700431 checkNotNull(flowRule, FLOW_RULE_NULL);
432 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800433 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700434 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700435 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700436 case PENDING_REMOVE:
437 case REMOVED:
438 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530439 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700440 break;
441 case ADDED:
442 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800443 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530444 if (isFlowOnlyInStore) {
445 // Publishing RULE_ADD_REQUESTED event facilitates
446 // preparation of statistics for the concerned rule
447 if (event == null) {
448 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
449 }
450 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800451 try {
452 frp.applyFlowRule(flowRule);
453 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700454 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800455 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800456 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800457 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
458 }
459 }
tom9b4030d2014-10-06 10:39:03 -0700460 break;
461 default:
462 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700463 }
464
alshabibbb42cad2014-09-25 11:43:05 -0700465 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700466 post(event);
467 }
alshabib57044ba2014-09-16 15:58:01 -0700468 }
469
alshabibba5ac482014-10-02 17:15:20 -0700470 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700471 checkNotNull(flowRule, FLOW_RULE_NULL);
472 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800473 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700474 FlowRuleProvider frp = getProvider(flowRule.deviceId());
475 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700476 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700477 }
478
alshabib1c319ff2014-10-04 20:29:09 -0700479 private void flowAdded(FlowEntry flowEntry) {
480 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700481 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700482
alshabib1c319ff2014-10-04 20:29:09 -0700483 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700484 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700485 if (event == null) {
486 log.debug("No flow store event generated.");
487 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800488 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700489 post(event);
490 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700491 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800492 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700493 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700494 }
alshabib57044ba2014-09-16 15:58:01 -0700495 }
496
alshabib1c319ff2014-10-04 20:29:09 -0700497 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
498 if (storedRule == null) {
499 return false;
500 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700501 if (storedRule.isPermanent()) {
502 return true;
503 }
504
Ray Milkey3717e602018-02-01 13:49:47 -0800505 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700506 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700507
508 // Checking flow with hardTimeout
509 if (storedRule.hardTimeout() != 0) {
510 if (!firstSeen.containsKey(storedRule)) {
511 // First time rule adding
512 firstSeen.put(storedRule, currentTime);
513 } else {
514 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800515 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700516 if ((currentTime - first) > hardTimeout) {
517 return false;
518 }
519 }
520 }
521
Gamze Abaka8d03f182017-07-20 15:09:24 +0000522 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700523 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700524 return true;
525 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700526 if (!lastSeen.containsKey(storedRule)) {
527 // checking for the first time
528 lastSeen.put(storedRule, storedRule.lastSeen());
529 // Use following if lastSeen attr. was removed.
530 //lastSeen.put(storedRule, currentTime);
531 }
532 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700533
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800534 // concurrently removed? let the liveness check fail
535 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700536 }
537
alshabib5c370ff2014-09-18 10:12:14 -0700538 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700539 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900540 pushFlowMetricsInternal(deviceId, flowEntries, true);
541 }
542
543 @Override
544 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
545 pushFlowMetricsInternal(deviceId, flowEntries, false);
546 }
547
548 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
549 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700550 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
551 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
552
Saurav Dasfa2fa932015-03-03 11:29:48 -0800553 for (FlowEntry rule : flowEntries) {
554 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700555 FlowEntry storedRule = storedRules.remove(rule);
556 if (storedRule != null) {
557 if (storedRule.exactMatch(rule)) {
558 // we both have the rule, let's update some info then.
559 flowAdded(rule);
560 } else {
561 // the two rules are not an exact match - remove the
562 // switch's rule and install our rule
563 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530564 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700565 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800566 } else {
567 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700568 if (!allowExtraneousRules) {
569 extraneousFlow(rule);
570 }
alshabib93cb57f2015-02-12 17:43:26 -0800571 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700572 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700573 log.warn("Can't process added or extra rule {} for device {}:{}",
574 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800575 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800576 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900577
578 // DO NOT reinstall
579 if (useMissingFlow) {
580 for (FlowEntry rule : storedRules.keySet()) {
581 try {
582 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530583 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
584 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900585 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700586 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900587 }
alshabib93cb57f2015-02-12 17:43:26 -0800588 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800589 }
alshabib5c370ff2014-09-18 10:12:14 -0700590 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800591
592 @Override
593 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
594 store.batchOperationComplete(FlowRuleBatchEvent.completed(
595 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
596 operation
597 ));
598 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700599
600 @Override
601 public void pushTableStatistics(DeviceId deviceId,
602 List<TableStatisticsEntry> tableStats) {
603 store.updateTableStatistics(deviceId, tableStats);
604 }
alshabib57044ba2014-09-16 15:58:01 -0700605 }
606
tomc78acee2014-09-24 15:16:55 -0700607 // Store delegate to re-post events emitted from the store.
608 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800609
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800610
Madan Jampani117aaae2014-10-23 10:04:05 -0700611 // TODO: Right now we only dispatch events at individual flowEntry level.
612 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700613 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700614 public void notify(FlowRuleBatchEvent event) {
615 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700616 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700617 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800618 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700619 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800620 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800621 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800622 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700623 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800624 break;
625 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700626 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800627 break;
628 case MODIFY:
629 //TODO: do something here when the time comes.
630 break;
631 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800632 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800633 }
634 }
635 );
636
637 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700638 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800639 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700640 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
641 if (flowRuleProvider != null) {
642 flowRuleProvider.executeBatch(batchOperation);
643 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800644
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800645 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700646
Madan Jampani117aaae2014-10-23 10:04:05 -0700647 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800648
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800649 FlowOperationsProcessor fops = pendingFlowOperations.remove(
650 event.subject().batchId());
651 if (event.result().isSuccess()) {
652 if (fops != null) {
653 fops.satisfy(event.deviceId());
654 }
655 } else {
656 fops.fail(event.deviceId(), event.result().failedItems());
657 }
658
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700659 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800660
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700661 default:
662 break;
663 }
tomc78acee2014-09-24 15:16:55 -0700664 }
665 }
alshabib902d41b2014-10-07 16:52:05 -0700666
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700667 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
668 switch (input) {
669 case ADD:
670 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
671 case MODIFY:
672 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
673 case REMOVE:
674 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
675 default:
676 throw new UnsupportedOperationException("Unknown flow rule type " + input);
677 }
678 }
679
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800680 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700681 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800682 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700683
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700684 // Mutable
685 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700686 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700687 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700688
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700689 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800690 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800691 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700692 }
693
694 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700695 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800696 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800697 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700698 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700699 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700700 }
701 }
702
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800703 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700704 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800705
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700706 for (FlowRuleOperation op : ops) {
707 perDeviceBatches.put(op.rule().deviceId(),
708 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800709 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000710 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800711
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800712 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700713 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800714 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
715 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000716 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700717 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700718 }
719 }
720
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700721 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000722 pendingDevices.remove(devId);
723 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700724 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700725 }
alshabib193525b2014-10-08 18:58:03 -0700726 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800727
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700728 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700729 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000730 pendingDevices.remove(devId);
731 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700732 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800733 }
734
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700735 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
736 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800737
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700738 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800739 }
alshabib902d41b2014-10-07 16:52:05 -0700740 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700741
742 @Override
743 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
744 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800745 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700746 return store.getTableStatistics(deviceId);
747 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800748
Patryk Konopka7e40c012017-06-06 13:38:06 +0200749 @Override
750 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800751 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200752 return store.getActiveFlowRuleCount(deviceId);
753 }
754
Charles Chan0c7c43b2016-01-14 17:39:20 -0800755 private class InternalDeviceListener implements DeviceListener {
756 @Override
757 public void event(DeviceEvent event) {
758 switch (event.type()) {
759 case DEVICE_REMOVED:
760 case DEVICE_AVAILABILITY_CHANGED:
761 DeviceId deviceId = event.subject().id();
762 if (!deviceService.isAvailable(deviceId)) {
763 if (purgeOnDisconnection) {
764 store.purgeFlowRule(deviceId);
765 }
766 }
767 break;
768 default:
769 break;
770 }
771 }
772 }
alshabib57044ba2014-09-16 15:58:01 -0700773}