blob: 9285ebc80885a10295328146eb1d1df0f27c7351 [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 = {
106 ALLOW_EXTRANEOUS_RULES + "=" + ALLOW_EXTRANEOUS_RULES_DEFAULT,
107 PURGE_ON_DISCONNECTION + "=" + PURGE_ON_DISCONNECTION_DEFAULT,
108 POLL_FREQUENCY + "=" + POLL_FREQUENCY_DEFAULT
109 }
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700121 //@Property(name = "allowExtraneousRules", boolValue = ALLOW_EXTRANEOUS_RULES,
122 // label = "Allow flow rules in switch not installed by ONOS")
Ray Milkeyd04e2272018-10-16 18:20:18 -0700123 private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES_DEFAULT;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700124
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700125 //@Property(name = "purgeOnDisconnection", boolValue = false,
126 // label = "Purge entries associated with a device when the device goes offline")
Ray Milkeyd04e2272018-10-16 18:20:18 -0700127 private boolean purgeOnDisconnection = PURGE_ON_DISCONNECTION_DEFAULT;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800128
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700129 //@Property(name = "fallbackFlowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
130 // label = "Frequency (in seconds) for polling flow statistics via fallback provider")
Ray Milkeyd04e2272018-10-16 18:20:18 -0700131 private int fallbackFlowPollFrequency = POLL_FREQUENCY_DEFAULT;
alshabib57044ba2014-09-16 15:58:01 -0700132
alshabibbb42cad2014-09-25 11:43:05 -0700133 private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800134 private final DeviceListener deviceListener = new InternalDeviceListener();
tomc78acee2014-09-24 15:16:55 -0700135
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800136 private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800137
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800138 protected ExecutorService deviceInstallers =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800139 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800140
141 protected ExecutorService operationsService =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700142 Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800143
144 private IdGenerator idGenerator;
145
Sho SHIMIZU9f950742016-09-01 09:55:38 -0700146 private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>();
Yuta HIGUCHI9def0472014-10-23 15:51:10 -0700147
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
tombe988312014-09-19 18:38:47 -0700149 protected FlowRuleStore store;
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700150
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ayaka Koshibeb55524f2014-09-18 09:59:24 -0700152 protected DeviceService deviceService;
alshabib57044ba2014-09-16 15:58:01 -0700153
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700154 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800155 protected CoreService coreService;
156
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700157 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800158 protected MastershipService mastershipService;
159
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700160 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700161 protected ComponentConfigService cfgService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800162
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700163 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman4f3193a2017-07-11 20:32:49 -0700164 protected DriverService driverService;
165
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700166 @Activate
167 public void activate(ComponentContext context) {
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800168 modified(context);
tomc78acee2014-09-24 15:16:55 -0700169 store.setDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700170 eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800171 deviceService.addListener(deviceListener);
172 cfgService.registerProperties(getClass());
173 idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC);
alshabib57044ba2014-09-16 15:58:01 -0700174 log.info("Started");
175 }
176
177 @Deactivate
178 public void deactivate() {
Andrea Campanella5a3c09c2017-12-01 13:57:48 +0100179 driverProvider.terminate();
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700180 deviceService.removeListener(deviceListener);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700181 cfgService.unregisterProperties(getClass(), false);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800182 deviceInstallers.shutdownNow();
183 operationsService.shutdownNow();
tomc78acee2014-09-24 15:16:55 -0700184 store.unsetDelegate(delegate);
alshabib57044ba2014-09-16 15:58:01 -0700185 eventDispatcher.removeSink(FlowRuleEvent.class);
186 log.info("Stopped");
187 }
188
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700189 @Modified
190 public void modified(ComponentContext context) {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800191 if (context != null) {
192 readComponentConfiguration(context);
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700193 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800194 driverProvider.init(new InternalFlowRuleProviderService(driverProvider),
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800195 deviceService, mastershipService, fallbackFlowPollFrequency);
196 }
197
198 @Override
199 protected FlowRuleProvider defaultProvider() {
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800200 return driverProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800201 }
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700202
Charles Chan0c7c43b2016-01-14 17:39:20 -0800203 /**
204 * Extracts properties from the component configuration context.
205 *
206 * @param context the component context
207 */
208 private void readComponentConfiguration(ComponentContext context) {
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700209 Dictionary<?, ?> properties = context.getProperties();
Charles Chan0c7c43b2016-01-14 17:39:20 -0800210 Boolean flag;
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700211
Jian Lid9b5f552016-03-11 18:15:31 -0800212 flag = Tools.isPropertyEnabled(properties, "allowExtraneousRules");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800213 if (flag == null) {
214 log.info("AllowExtraneousRules is not configured, " +
215 "using current value of {}", allowExtraneousRules);
216 } else {
217 allowExtraneousRules = flag;
218 log.info("Configured. AllowExtraneousRules is {}",
219 allowExtraneousRules ? "enabled" : "disabled");
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700220 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800221
Jian Lid9b5f552016-03-11 18:15:31 -0800222 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800223 if (flag == null) {
224 log.info("PurgeOnDisconnection is not configured, " +
225 "using current value of {}", purgeOnDisconnection);
226 } else {
227 purgeOnDisconnection = flag;
228 log.info("Configured. PurgeOnDisconnection is {}",
229 purgeOnDisconnection ? "enabled" : "disabled");
230 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800231
232 String s = get(properties, "fallbackFlowPollFrequency");
Carmelo Casconee6a97542018-06-20 16:48:00 +0200233 if (isNullOrEmpty(s)) {
234 log.info("fallbackFlowPollFrequency is not configured, " +
235 "using current value of {} seconds",
236 fallbackFlowPollFrequency);
237 } else {
238 try {
239 fallbackFlowPollFrequency = Integer.parseInt(s);
240 log.info("Configured. FallbackFlowPollFrequency is {} seconds",
241 fallbackFlowPollFrequency);
242 } catch (NumberFormatException e) {
243 log.warn("Configured fallbackFlowPollFrequency value '{}' " +
244 "is not a number, using current value of {} seconds",
245 fallbackFlowPollFrequency);
246 }
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800247 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800248 }
249
alshabib57044ba2014-09-16 15:58:01 -0700250 @Override
tom9b4030d2014-10-06 10:39:03 -0700251 public int getFlowRuleCount() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900252 checkPermission(FLOWRULE_READ);
tom9b4030d2014-10-06 10:39:03 -0700253 return store.getFlowRuleCount();
254 }
255
256 @Override
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800257 public int getFlowRuleCount(DeviceId deviceId) {
258 checkPermission(FLOWRULE_READ);
Thomas Vachuska227943d2018-03-19 09:37:28 -0700259 checkNotNull(deviceId, DEVICE_ID_NULL);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800260 return store.getFlowRuleCount(deviceId);
261 }
262
263 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700264 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900265 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800266 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700267 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700268 }
269
270 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700271 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900272 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900273
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800274 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800275 for (FlowRule flowRule : flowRules) {
276 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700277 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800278 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700279 }
280
281 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530282 public void purgeFlowRules(DeviceId deviceId) {
283 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800284 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530285 store.purgeFlowRule(deviceId);
286 }
287
288 @Override
alshabib57044ba2014-09-16 15:58:01 -0700289 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900290 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900291
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800292 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800293 for (FlowRule flowRule : flowRules) {
294 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700295 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800296 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700297 }
alshabib57044ba2014-09-16 15:58:01 -0700298
alshabiba68eb962014-09-24 20:34:13 -0700299 @Override
300 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900301 checkPermission(FLOWRULE_WRITE);
Madan Jampani6a456162014-10-24 11:36:17 -0700302 removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class));
alshabiba68eb962014-09-24 20:34:13 -0700303 }
304
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530305 @Deprecated
alshabiba68eb962014-09-24 20:34:13 -0700306 @Override
307 public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900308 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900309
Madan Jampani6a456162014-10-24 11:36:17 -0700310 Set<FlowRule> 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;
alshabib57044ba2014-09-16 15:58:01 -0700319 }
320
321 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530322 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
323 checkPermission(FLOWRULE_READ);
324
325 Set<FlowEntry> flowEntries = Sets.newHashSet();
326 for (Device d : deviceService.getDevices()) {
327 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
328 if (flowEntry.appId() == id.id()) {
329 flowEntries.add(flowEntry);
330 }
331 }
332 }
333 return flowEntries;
334 }
335
336 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800337 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900338 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900339
alshabibaa7e7de2014-11-12 19:20:44 -0800340 Set<FlowRule> matches = Sets.newHashSet();
341 long toLookUp = ((long) appId.id() << 16) | groupId;
342 for (Device d : deviceService.getDevices()) {
343 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
344 if ((flowEntry.id().value() >>> 32) == toLookUp) {
345 matches.add(flowEntry);
346 }
347 }
348 }
349 return matches;
350 }
351
352 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800353 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900354 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700355 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700356 }
357
358 @Override
alshabib57044ba2014-09-16 15:58:01 -0700359 protected FlowRuleProviderService createProviderService(
360 FlowRuleProvider provider) {
361 return new InternalFlowRuleProviderService(provider);
362 }
363
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800364 @Override
365 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
366 log.warn("should not be calling getProvider(ProviderId)");
367 return super.getProvider(pid);
368 }
369
370 /**
371 * {@inheritDoc}
372 * if the Device does not support {@link FlowRuleProgrammable}.
373 */
374 @Override
375 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800376 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800377 // if device supports FlowRuleProgrammable,
378 // use FlowRuleProgrammable via FlowRuleDriverProvider
379 return Optional.ofNullable(deviceService.getDevice(deviceId))
380 .filter(dev -> dev.is(FlowRuleProgrammable.class))
381 .<FlowRuleProvider>map(x -> driverProvider)
382 .orElseGet(() -> super.getProvider(deviceId));
383 }
384
alshabib57044ba2014-09-16 15:58:01 -0700385 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700386 extends AbstractProviderService<FlowRuleProvider>
387 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700388
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700389 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700390 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
391
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700392
alshabib57044ba2014-09-16 15:58:01 -0700393 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
394 super(provider);
395 }
396
397 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700398 public void flowRemoved(FlowEntry flowEntry) {
399 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700400 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700401 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700402 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700403 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700404 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800405 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700406 return;
407 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700408 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
409 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
410 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800411 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700412 FlowRuleEvent event = null;
413 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700414 case ADDED:
415 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700416 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700417 break;
418 case PENDING_REMOVE:
419 case REMOVED:
420 event = store.removeFlowRule(stored);
421 break;
422 default:
423 break;
alshabib57044ba2014-09-16 15:58:01 -0700424
alshabiba68eb962014-09-24 20:34:13 -0700425 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700426 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700427 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700428 post(event);
429 }
alshabib57044ba2014-09-16 15:58:01 -0700430 }
431
alshabibba5ac482014-10-02 17:15:20 -0700432
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530433 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700434 checkNotNull(flowRule, FLOW_RULE_NULL);
435 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800436 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700437 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700438 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700439 case PENDING_REMOVE:
440 case REMOVED:
441 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530442 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700443 break;
444 case ADDED:
445 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800446 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530447 if (isFlowOnlyInStore) {
448 // Publishing RULE_ADD_REQUESTED event facilitates
449 // preparation of statistics for the concerned rule
450 if (event == null) {
451 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
452 }
453 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800454 try {
455 frp.applyFlowRule(flowRule);
456 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700457 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800458 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800459 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800460 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
461 }
462 }
tom9b4030d2014-10-06 10:39:03 -0700463 break;
464 default:
465 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700466 }
467
alshabibbb42cad2014-09-25 11:43:05 -0700468 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700469 post(event);
470 }
alshabib57044ba2014-09-16 15:58:01 -0700471 }
472
alshabibba5ac482014-10-02 17:15:20 -0700473 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700474 checkNotNull(flowRule, FLOW_RULE_NULL);
475 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800476 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700477 FlowRuleProvider frp = getProvider(flowRule.deviceId());
478 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700479 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700480 }
481
alshabib1c319ff2014-10-04 20:29:09 -0700482 private void flowAdded(FlowEntry flowEntry) {
483 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700484 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700485
alshabib1c319ff2014-10-04 20:29:09 -0700486 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700487 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700488 if (event == null) {
489 log.debug("No flow store event generated.");
490 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800491 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700492 post(event);
493 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700494 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800495 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700496 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700497 }
alshabib57044ba2014-09-16 15:58:01 -0700498 }
499
alshabib1c319ff2014-10-04 20:29:09 -0700500 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
501 if (storedRule == null) {
502 return false;
503 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700504 if (storedRule.isPermanent()) {
505 return true;
506 }
507
Ray Milkey3717e602018-02-01 13:49:47 -0800508 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700509 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700510
511 // Checking flow with hardTimeout
512 if (storedRule.hardTimeout() != 0) {
513 if (!firstSeen.containsKey(storedRule)) {
514 // First time rule adding
515 firstSeen.put(storedRule, currentTime);
516 } else {
517 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800518 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700519 if ((currentTime - first) > hardTimeout) {
520 return false;
521 }
522 }
523 }
524
Gamze Abaka8d03f182017-07-20 15:09:24 +0000525 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700526 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700527 return true;
528 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700529 if (!lastSeen.containsKey(storedRule)) {
530 // checking for the first time
531 lastSeen.put(storedRule, storedRule.lastSeen());
532 // Use following if lastSeen attr. was removed.
533 //lastSeen.put(storedRule, currentTime);
534 }
535 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700536
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800537 // concurrently removed? let the liveness check fail
538 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700539 }
540
alshabib5c370ff2014-09-18 10:12:14 -0700541 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700542 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900543 pushFlowMetricsInternal(deviceId, flowEntries, true);
544 }
545
546 @Override
547 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
548 pushFlowMetricsInternal(deviceId, flowEntries, false);
549 }
550
551 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
552 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700553 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
554 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
555
Saurav Dasfa2fa932015-03-03 11:29:48 -0800556 for (FlowEntry rule : flowEntries) {
557 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700558 FlowEntry storedRule = storedRules.remove(rule);
559 if (storedRule != null) {
560 if (storedRule.exactMatch(rule)) {
561 // we both have the rule, let's update some info then.
562 flowAdded(rule);
563 } else {
564 // the two rules are not an exact match - remove the
565 // switch's rule and install our rule
566 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530567 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700568 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800569 } else {
570 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700571 if (!allowExtraneousRules) {
572 extraneousFlow(rule);
573 }
alshabib93cb57f2015-02-12 17:43:26 -0800574 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700575 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700576 log.warn("Can't process added or extra rule {} for device {}:{}",
577 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800578 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800579 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900580
581 // DO NOT reinstall
582 if (useMissingFlow) {
583 for (FlowEntry rule : storedRules.keySet()) {
584 try {
585 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530586 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
587 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900588 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700589 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900590 }
alshabib93cb57f2015-02-12 17:43:26 -0800591 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800592 }
alshabib5c370ff2014-09-18 10:12:14 -0700593 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800594
595 @Override
596 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
597 store.batchOperationComplete(FlowRuleBatchEvent.completed(
598 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
599 operation
600 ));
601 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700602
603 @Override
604 public void pushTableStatistics(DeviceId deviceId,
605 List<TableStatisticsEntry> tableStats) {
606 store.updateTableStatistics(deviceId, tableStats);
607 }
alshabib57044ba2014-09-16 15:58:01 -0700608 }
609
tomc78acee2014-09-24 15:16:55 -0700610 // Store delegate to re-post events emitted from the store.
611 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800612
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800613
Madan Jampani117aaae2014-10-23 10:04:05 -0700614 // TODO: Right now we only dispatch events at individual flowEntry level.
615 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700616 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700617 public void notify(FlowRuleBatchEvent event) {
618 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700619 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700620 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800621 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700622 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800623 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800624 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800625 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700626 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800627 break;
628 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700629 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800630 break;
631 case MODIFY:
632 //TODO: do something here when the time comes.
633 break;
634 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800635 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800636 }
637 }
638 );
639
640 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700641 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800642 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700643 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
644 if (flowRuleProvider != null) {
645 flowRuleProvider.executeBatch(batchOperation);
646 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800647
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800648 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700649
Madan Jampani117aaae2014-10-23 10:04:05 -0700650 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800651
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800652 FlowOperationsProcessor fops = pendingFlowOperations.remove(
653 event.subject().batchId());
654 if (event.result().isSuccess()) {
655 if (fops != null) {
656 fops.satisfy(event.deviceId());
657 }
658 } else {
659 fops.fail(event.deviceId(), event.result().failedItems());
660 }
661
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700662 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800663
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700664 default:
665 break;
666 }
tomc78acee2014-09-24 15:16:55 -0700667 }
668 }
alshabib902d41b2014-10-07 16:52:05 -0700669
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700670 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
671 switch (input) {
672 case ADD:
673 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
674 case MODIFY:
675 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
676 case REMOVE:
677 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
678 default:
679 throw new UnsupportedOperationException("Unknown flow rule type " + input);
680 }
681 }
682
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800683 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700684 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800685 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700686
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700687 // Mutable
688 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700689 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700690 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700691
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700692 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800693 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800694 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700695 }
696
697 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700698 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800699 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800700 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700701 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700702 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700703 }
704 }
705
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800706 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700707 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800708
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700709 for (FlowRuleOperation op : ops) {
710 perDeviceBatches.put(op.rule().deviceId(),
711 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800712 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000713 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800714
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800715 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700716 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800717 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
718 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000719 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700720 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700721 }
722 }
723
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700724 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000725 pendingDevices.remove(devId);
726 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700727 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700728 }
alshabib193525b2014-10-08 18:58:03 -0700729 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800730
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700731 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700732 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000733 pendingDevices.remove(devId);
734 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700735 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800736 }
737
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700738 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
739 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800740
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700741 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800742 }
alshabib902d41b2014-10-07 16:52:05 -0700743 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700744
745 @Override
746 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
747 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800748 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700749 return store.getTableStatistics(deviceId);
750 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800751
Patryk Konopka7e40c012017-06-06 13:38:06 +0200752 @Override
753 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800754 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200755 return store.getActiveFlowRuleCount(deviceId);
756 }
757
Charles Chan0c7c43b2016-01-14 17:39:20 -0800758 private class InternalDeviceListener implements DeviceListener {
759 @Override
760 public void event(DeviceEvent event) {
761 switch (event.type()) {
762 case DEVICE_REMOVED:
763 case DEVICE_AVAILABILITY_CHANGED:
764 DeviceId deviceId = event.subject().id();
765 if (!deviceService.isAvailable(deviceId)) {
766 if (purgeOnDisconnection) {
767 store.purgeFlowRule(deviceId);
768 }
769 }
770 break;
771 default:
772 break;
773 }
774 }
775 }
alshabib57044ba2014-09-16 15:58:01 -0700776}