blob: 99ee528e01c0d16141196c6f533ce1e6ed9ddc0a [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
alshabib1c319ff2014-10-04 20:29:09 -0700261 public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900262 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800263 checkNotNull(deviceId, DEVICE_ID_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700264 return store.getFlowEntries(deviceId);
alshabib57044ba2014-09-16 15:58:01 -0700265 }
266
267 @Override
alshabib219ebaa2014-09-22 15:41:24 -0700268 public void applyFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900269 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900270
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800271 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800272 for (FlowRule flowRule : flowRules) {
273 builder.add(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700274 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800275 apply(builder.build());
alshabib57044ba2014-09-16 15:58:01 -0700276 }
277
278 @Override
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530279 public void purgeFlowRules(DeviceId deviceId) {
280 checkPermission(FLOWRULE_WRITE);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800281 checkNotNull(deviceId, DEVICE_ID_NULL);
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530282 store.purgeFlowRule(deviceId);
283 }
284
285 @Override
alshabib57044ba2014-09-16 15:58:01 -0700286 public void removeFlowRules(FlowRule... flowRules) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900287 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900288
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800289 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800290 for (FlowRule flowRule : flowRules) {
291 builder.remove(flowRule);
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700292 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800293 apply(builder.build());
alshabiba68eb962014-09-24 20:34:13 -0700294 }
alshabib57044ba2014-09-16 15:58:01 -0700295
alshabiba68eb962014-09-24 20:34:13 -0700296 @Override
297 public void removeFlowRulesById(ApplicationId id) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900298 checkPermission(FLOWRULE_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900299
Madan Jampani6a456162014-10-24 11:36:17 -0700300 Set<FlowRule> flowEntries = Sets.newHashSet();
301 for (Device d : deviceService.getDevices()) {
302 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
303 if (flowEntry.appId() == id.id()) {
304 flowEntries.add(flowEntry);
305 }
306 }
307 }
Ray Milkey4f7e3632019-02-19 15:35:20 -0800308 removeFlowRules(Iterables.toArray(flowEntries, FlowRule.class));
alshabib57044ba2014-09-16 15:58:01 -0700309 }
310
311 @Override
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530312 public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) {
313 checkPermission(FLOWRULE_READ);
314
315 Set<FlowEntry> flowEntries = Sets.newHashSet();
316 for (Device d : deviceService.getDevices()) {
317 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
318 if (flowEntry.appId() == id.id()) {
319 flowEntries.add(flowEntry);
320 }
321 }
322 }
323 return flowEntries;
324 }
325
326 @Override
alshabibaa7e7de2014-11-12 19:20:44 -0800327 public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900328 checkPermission(FLOWRULE_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900329
alshabibaa7e7de2014-11-12 19:20:44 -0800330 Set<FlowRule> matches = Sets.newHashSet();
331 long toLookUp = ((long) appId.id() << 16) | groupId;
332 for (Device d : deviceService.getDevices()) {
333 for (FlowEntry flowEntry : store.getFlowEntries(d.id())) {
334 if ((flowEntry.id().value() >>> 32) == toLookUp) {
335 matches.add(flowEntry);
336 }
337 }
338 }
339 return matches;
340 }
341
342 @Override
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800343 public void apply(FlowRuleOperations ops) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900344 checkPermission(FLOWRULE_WRITE);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700345 operationsService.execute(new FlowOperationsProcessor(ops));
alshabib902d41b2014-10-07 16:52:05 -0700346 }
347
348 @Override
alshabib57044ba2014-09-16 15:58:01 -0700349 protected FlowRuleProviderService createProviderService(
350 FlowRuleProvider provider) {
351 return new InternalFlowRuleProviderService(provider);
352 }
353
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800354 @Override
355 protected synchronized FlowRuleProvider getProvider(ProviderId pid) {
356 log.warn("should not be calling getProvider(ProviderId)");
357 return super.getProvider(pid);
358 }
359
360 /**
361 * {@inheritDoc}
362 * if the Device does not support {@link FlowRuleProgrammable}.
363 */
364 @Override
365 protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800366 checkNotNull(deviceId, DEVICE_ID_NULL);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800367 // if device supports FlowRuleProgrammable,
368 // use FlowRuleProgrammable via FlowRuleDriverProvider
369 return Optional.ofNullable(deviceService.getDevice(deviceId))
370 .filter(dev -> dev.is(FlowRuleProgrammable.class))
371 .<FlowRuleProvider>map(x -> driverProvider)
372 .orElseGet(() -> super.getProvider(deviceId));
373 }
374
alshabib57044ba2014-09-16 15:58:01 -0700375 private class InternalFlowRuleProviderService
tom9b4030d2014-10-06 10:39:03 -0700376 extends AbstractProviderService<FlowRuleProvider>
377 implements FlowRuleProviderService {
alshabib57044ba2014-09-16 15:58:01 -0700378
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700379 final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700380 final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap();
381
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700382
alshabib57044ba2014-09-16 15:58:01 -0700383 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
384 super(provider);
385 }
386
387 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700388 public void flowRemoved(FlowEntry flowEntry) {
389 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700390 checkValidity();
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700391 lastSeen.remove(flowEntry);
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700392 firstSeen.remove(flowEntry);
alshabib1c319ff2014-10-04 20:29:09 -0700393 FlowEntry stored = store.getFlowEntry(flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700394 if (stored == null) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800395 log.debug("Rule already evicted from store: {}", flowEntry);
alshabiba68eb962014-09-24 20:34:13 -0700396 return;
397 }
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700398 if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) {
399 ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED);
400 }
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800401 FlowRuleProvider frp = getProvider(flowEntry.deviceId());
alshabiba68eb962014-09-24 20:34:13 -0700402 FlowRuleEvent event = null;
403 switch (stored.state()) {
tom9b4030d2014-10-06 10:39:03 -0700404 case ADDED:
405 case PENDING_ADD:
alshabib6eb438a2014-10-01 16:39:37 -0700406 frp.applyFlowRule(stored);
tom9b4030d2014-10-06 10:39:03 -0700407 break;
408 case PENDING_REMOVE:
409 case REMOVED:
410 event = store.removeFlowRule(stored);
411 break;
412 default:
413 break;
alshabib57044ba2014-09-16 15:58:01 -0700414
alshabiba68eb962014-09-24 20:34:13 -0700415 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700416 if (event != null) {
alshabib1c319ff2014-10-04 20:29:09 -0700417 log.debug("Flow {} removed", flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700418 post(event);
419 }
alshabib57044ba2014-09-16 15:58:01 -0700420 }
421
alshabibba5ac482014-10-02 17:15:20 -0700422
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530423 private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) {
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700424 checkNotNull(flowRule, FLOW_RULE_NULL);
425 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800426 FlowRuleProvider frp = getProvider(flowRule.deviceId());
alshabibbb42cad2014-09-25 11:43:05 -0700427 FlowRuleEvent event = null;
alshabiba68eb962014-09-24 20:34:13 -0700428 switch (flowRule.state()) {
tom9b4030d2014-10-06 10:39:03 -0700429 case PENDING_REMOVE:
430 case REMOVED:
431 event = store.removeFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530432 log.debug("Flow {} removed", flowRule);
tom9b4030d2014-10-06 10:39:03 -0700433 break;
434 case ADDED:
435 case PENDING_ADD:
Charles Chan93fa7272016-01-26 22:27:02 -0800436 event = store.pendingFlowRule(flowRule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530437 if (isFlowOnlyInStore) {
438 // Publishing RULE_ADD_REQUESTED event facilitates
439 // preparation of statistics for the concerned rule
440 if (event == null) {
441 event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule);
442 }
443 }
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800444 try {
445 frp.applyFlowRule(flowRule);
446 } catch (UnsupportedOperationException e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700447 log.warn("Unsupported operation", e);
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800448 if (flowRule instanceof DefaultFlowEntry) {
Brian O'Connora3e5cd52015-12-05 15:59:19 -0800449 //FIXME modification of "stored" flow entry outside of store
Charles M.C. Chan1229eca2015-05-18 06:27:52 +0800450 ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED);
451 }
452 }
tom9b4030d2014-10-06 10:39:03 -0700453 break;
454 default:
455 log.debug("Flow {} has not been installed.", flowRule);
alshabiba68eb962014-09-24 20:34:13 -0700456 }
457
alshabibbb42cad2014-09-25 11:43:05 -0700458 if (event != null) {
alshabibbb42cad2014-09-25 11:43:05 -0700459 post(event);
460 }
alshabib57044ba2014-09-16 15:58:01 -0700461 }
462
alshabibba5ac482014-10-02 17:15:20 -0700463 private void extraneousFlow(FlowRule flowRule) {
alshabib219ebaa2014-09-22 15:41:24 -0700464 checkNotNull(flowRule, FLOW_RULE_NULL);
465 checkValidity();
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800466 // getProvider is customized to favor driverProvider
alshabib2374fc92014-10-22 11:03:23 -0700467 FlowRuleProvider frp = getProvider(flowRule.deviceId());
468 frp.removeFlowRule(flowRule);
alshabib54ce5892014-09-23 17:50:51 -0700469 log.debug("Flow {} is on switch but not in store.", flowRule);
alshabib219ebaa2014-09-22 15:41:24 -0700470 }
471
alshabib1c319ff2014-10-04 20:29:09 -0700472 private void flowAdded(FlowEntry flowEntry) {
473 checkNotNull(flowEntry, FLOW_RULE_NULL);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700474 checkValidity();
alshabib57044ba2014-09-16 15:58:01 -0700475
alshabib1c319ff2014-10-04 20:29:09 -0700476 if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) {
alshabib1c319ff2014-10-04 20:29:09 -0700477 FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry);
alshabibba5ac482014-10-02 17:15:20 -0700478 if (event == null) {
479 log.debug("No flow store event generated.");
480 } else {
Jonathan Hart58682dd2014-11-24 20:11:16 -0800481 log.trace("Flow {} {}", flowEntry, event.type());
alshabibba5ac482014-10-02 17:15:20 -0700482 post(event);
483 }
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700484 } else {
Thomas Vachuska4830d392014-11-09 17:09:56 -0800485 log.debug("Removing flow rules....");
alshabib1c319ff2014-10-04 20:29:09 -0700486 removeFlowRules(flowEntry);
Ayaka Koshibe08eabaa2014-09-17 14:59:25 -0700487 }
alshabib57044ba2014-09-16 15:58:01 -0700488 }
489
alshabib1c319ff2014-10-04 20:29:09 -0700490 private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) {
491 if (storedRule == null) {
492 return false;
493 }
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700494 if (storedRule.isPermanent()) {
495 return true;
496 }
497
Ray Milkey3717e602018-02-01 13:49:47 -0800498 final long timeout = storedRule.timeout() * 1000L;
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700499 final long currentTime = System.currentTimeMillis();
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700500
501 // Checking flow with hardTimeout
502 if (storedRule.hardTimeout() != 0) {
503 if (!firstSeen.containsKey(storedRule)) {
504 // First time rule adding
505 firstSeen.put(storedRule, currentTime);
506 } else {
507 Long first = firstSeen.get(storedRule);
Ray Milkey3717e602018-02-01 13:49:47 -0800508 final long hardTimeout = storedRule.hardTimeout() * 1000L;
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700509 if ((currentTime - first) > hardTimeout) {
510 return false;
511 }
512 }
513 }
514
Gamze Abaka8d03f182017-07-20 15:09:24 +0000515 if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) {
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700516 lastSeen.put(storedRule, currentTime);
alshabib85c41972014-10-03 13:48:39 -0700517 return true;
518 }
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -0700519 if (!lastSeen.containsKey(storedRule)) {
520 // checking for the first time
521 lastSeen.put(storedRule, storedRule.lastSeen());
522 // Use following if lastSeen attr. was removed.
523 //lastSeen.put(storedRule, currentTime);
524 }
525 Long last = lastSeen.get(storedRule);
alshabib85c41972014-10-03 13:48:39 -0700526
Thomas Vachuskac4ee7372016-02-02 16:10:09 -0800527 // concurrently removed? let the liveness check fail
528 return last != null && (currentTime - last) <= timeout;
alshabibba5ac482014-10-02 17:15:20 -0700529 }
530
alshabib5c370ff2014-09-18 10:12:14 -0700531 @Override
alshabib1c319ff2014-10-04 20:29:09 -0700532 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
ssyoon9030fbcd92015-08-17 10:42:07 +0900533 pushFlowMetricsInternal(deviceId, flowEntries, true);
534 }
535
536 @Override
537 public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) {
538 pushFlowMetricsInternal(deviceId, flowEntries, false);
539 }
540
541 private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries,
542 boolean useMissingFlow) {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700543 Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap();
544 store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f));
545
Saurav Dasfa2fa932015-03-03 11:29:48 -0800546 for (FlowEntry rule : flowEntries) {
547 try {
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700548 FlowEntry storedRule = storedRules.remove(rule);
549 if (storedRule != null) {
550 if (storedRule.exactMatch(rule)) {
551 // we both have the rule, let's update some info then.
552 flowAdded(rule);
553 } else {
554 // the two rules are not an exact match - remove the
555 // switch's rule and install our rule
556 extraneousFlow(rule);
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530557 flowMissing(storedRule, false);
Jonathan Hartf44e42c2015-08-04 09:58:46 -0700558 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800559 } else {
560 // the device has a rule the store does not have
Marc De Leenheerde47caa2015-04-24 11:27:44 -0700561 if (!allowExtraneousRules) {
562 extraneousFlow(rule);
563 }
alshabib93cb57f2015-02-12 17:43:26 -0800564 }
Sho SHIMIZU24a00d92015-05-05 11:11:13 -0700565 } catch (Exception e) {
Jon Hall274cecb2017-08-09 12:15:48 -0700566 log.warn("Can't process added or extra rule {} for device {}:{}",
567 rule, deviceId, e);
alshabib93cb57f2015-02-12 17:43:26 -0800568 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800569 }
ssyoon9030fbcd92015-08-17 10:42:07 +0900570
571 // DO NOT reinstall
572 if (useMissingFlow) {
573 for (FlowEntry rule : storedRules.keySet()) {
574 try {
575 // there are rules in the store that aren't on the switch
Sivachidambaram Subramanian605104e2017-06-21 07:40:04 +0530576 log.debug("Adding the rule that is present in store but not on switch : {}", rule);
577 flowMissing(rule, true);
ssyoon9030fbcd92015-08-17 10:42:07 +0900578 } catch (Exception e) {
Yuta HIGUCHIf76f6d52017-05-15 18:02:09 -0700579 log.warn("Can't add missing flow rule:", e);
ssyoon9030fbcd92015-08-17 10:42:07 +0900580 }
alshabib93cb57f2015-02-12 17:43:26 -0800581 }
Saurav Dasfa2fa932015-03-03 11:29:48 -0800582 }
alshabib5c370ff2014-09-18 10:12:14 -0700583 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800584
585 @Override
586 public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) {
587 store.batchOperationComplete(FlowRuleBatchEvent.completed(
588 new FlowRuleBatchRequest(batchId, Collections.emptySet()),
589 operation
590 ));
591 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700592
593 @Override
594 public void pushTableStatistics(DeviceId deviceId,
595 List<TableStatisticsEntry> tableStats) {
596 store.updateTableStatistics(deviceId, tableStats);
597 }
alshabib57044ba2014-09-16 15:58:01 -0700598 }
599
tomc78acee2014-09-24 15:16:55 -0700600 // Store delegate to re-post events emitted from the store.
601 private class InternalStoreDelegate implements FlowRuleStoreDelegate {
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800602
Thomas Vachuska9b2da212014-11-10 19:30:25 -0800603
Madan Jampani117aaae2014-10-23 10:04:05 -0700604 // TODO: Right now we only dispatch events at individual flowEntry level.
605 // It may be more efficient for also dispatch events as a batch.
tomc78acee2014-09-24 15:16:55 -0700606 @Override
Madan Jampani117aaae2014-10-23 10:04:05 -0700607 public void notify(FlowRuleBatchEvent event) {
608 final FlowRuleBatchRequest request = event.subject();
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700609 switch (event.type()) {
Madan Jampani117aaae2014-10-23 10:04:05 -0700610 case BATCH_OPERATION_REQUESTED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800611 // Request has been forwarded to MASTER Node, and was
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -0700612 request.ops().forEach(
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800613 op -> {
Ray Milkeyf7329c72015-02-17 11:37:01 -0800614 switch (op.operator()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800615 case ADD:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700616 post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800617 break;
618 case REMOVE:
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700619 post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800620 break;
621 case MODIFY:
622 //TODO: do something here when the time comes.
623 break;
624 default:
Ray Milkeyf7329c72015-02-17 11:37:01 -0800625 log.warn("Unknown flow operation operator: {}", op.operator());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800626 }
627 }
628 );
629
630 DeviceId deviceId = event.deviceId();
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700631 FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId);
Yuta HIGUCHI48dcd372017-02-27 22:55:53 -0800632 // getProvider is customized to favor driverProvider
Thomas Vachuska27bee092015-06-23 19:03:10 -0700633 FlowRuleProvider flowRuleProvider = getProvider(deviceId);
634 if (flowRuleProvider != null) {
635 flowRuleProvider.executeBatch(batchOperation);
636 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800637
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800638 break;
Madan Jampani117aaae2014-10-23 10:04:05 -0700639
Madan Jampani117aaae2014-10-23 10:04:05 -0700640 case BATCH_OPERATION_COMPLETED:
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800641
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800642 FlowOperationsProcessor fops = pendingFlowOperations.remove(
643 event.subject().batchId());
644 if (event.result().isSuccess()) {
645 if (fops != null) {
646 fops.satisfy(event.deviceId());
647 }
648 } else {
649 fops.fail(event.deviceId(), event.result().failedItems());
650 }
651
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700652 break;
Yuta HIGUCHI2fcb40c2014-11-03 14:39:10 -0800653
Yuta HIGUCHIf3d51bd2014-10-21 01:05:33 -0700654 default:
655 break;
656 }
tomc78acee2014-09-24 15:16:55 -0700657 }
658 }
alshabib902d41b2014-10-07 16:52:05 -0700659
Sho SHIMIZU5711ce12016-08-31 13:57:12 -0700660 private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) {
661 switch (input) {
662 case ADD:
663 return FlowRuleBatchEntry.FlowRuleOperation.ADD;
664 case MODIFY:
665 return FlowRuleBatchEntry.FlowRuleOperation.MODIFY;
666 case REMOVE:
667 return FlowRuleBatchEntry.FlowRuleOperation.REMOVE;
668 default:
669 throw new UnsupportedOperationException("Unknown flow rule type " + input);
670 }
671 }
672
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800673 private class FlowOperationsProcessor implements Runnable {
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700674 // Immutable
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800675 private final FlowRuleOperations fops;
alshabib902d41b2014-10-07 16:52:05 -0700676
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700677 // Mutable
678 private final List<Set<FlowRuleOperation>> stages;
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700679 private final Set<DeviceId> pendingDevices = new HashSet<>();
Sho SHIMIZU5f709422016-09-07 09:54:46 -0700680 private boolean hasFailed = false;
alshabib902d41b2014-10-07 16:52:05 -0700681
Sho SHIMIZU7c9b73a2016-08-30 14:08:28 -0700682 FlowOperationsProcessor(FlowRuleOperations ops) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800683 this.stages = Lists.newArrayList(ops.stages());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800684 this.fops = ops;
alshabib902d41b2014-10-07 16:52:05 -0700685 }
686
687 @Override
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700688 public synchronized void run() {
Jon Hallcbd1b392017-01-18 20:15:44 -0800689 if (!stages.isEmpty()) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800690 process(stages.remove(0));
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700691 } else if (!hasFailed) {
Sho SHIMIZUc9e4bb02016-09-01 12:43:39 -0700692 fops.callback().onSuccess(fops);
alshabib193525b2014-10-08 18:58:03 -0700693 }
694 }
695
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800696 private void process(Set<FlowRuleOperation> ops) {
Sho SHIMIZU8efc8962016-08-31 15:17:44 -0700697 Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800698
Sho SHIMIZUf4fd3de2016-08-31 15:47:56 -0700699 for (FlowRuleOperation op : ops) {
700 perDeviceBatches.put(op.rule().deviceId(),
701 new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule()));
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800702 }
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000703 pendingDevices.addAll(perDeviceBatches.keySet());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800704
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800705 for (DeviceId deviceId : perDeviceBatches.keySet()) {
Sho SHIMIZU3a704312015-05-27 13:36:01 -0700706 long id = idGenerator.getNewId();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800707 final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId),
708 deviceId, id);
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000709 pendingFlowOperations.put(id, this);
HIGUCHI Yutad9e01052016-04-14 09:31:42 -0700710 deviceInstallers.execute(() -> store.storeBatch(b));
alshabib193525b2014-10-08 18:58:03 -0700711 }
712 }
713
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700714 synchronized void satisfy(DeviceId devId) {
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000715 pendingDevices.remove(devId);
716 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700717 operationsService.execute(this);
alshabib193525b2014-10-08 18:58:03 -0700718 }
alshabib193525b2014-10-08 18:58:03 -0700719 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800720
Sho SHIMIZU2d7c5392016-08-30 14:14:39 -0700721 synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) {
Sho SHIMIZUb9e0ab72016-09-14 16:54:13 -0700722 hasFailed = true;
Brian O'Connorc9b64dc2016-09-13 23:01:07 +0000723 pendingDevices.remove(devId);
724 if (pendingDevices.isEmpty()) {
Sho SHIMIZUc0bfe7c2016-09-14 16:54:30 -0700725 operationsService.execute(this);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800726 }
727
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700728 FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
729 failures.forEach(failedOpsBuilder::add);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800730
Sho SHIMIZUad4f2cd2016-09-01 13:05:56 -0700731 fops.callback().onError(failedOpsBuilder.build());
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800732 }
alshabib902d41b2014-10-07 16:52:05 -0700733 }
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700734
735 @Override
736 public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) {
737 checkPermission(FLOWRULE_READ);
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800738 checkNotNull(deviceId, DEVICE_ID_NULL);
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700739 return store.getTableStatistics(deviceId);
740 }
Charles Chan0c7c43b2016-01-14 17:39:20 -0800741
Patryk Konopka7e40c012017-06-06 13:38:06 +0200742 @Override
743 public long getActiveFlowRuleCount(DeviceId deviceId) {
Thomas Vachuskaa8e74772018-02-26 11:33:35 -0800744 checkNotNull(deviceId, DEVICE_ID_NULL);
Patryk Konopka7e40c012017-06-06 13:38:06 +0200745 return store.getActiveFlowRuleCount(deviceId);
746 }
747
Charles Chan0c7c43b2016-01-14 17:39:20 -0800748 private class InternalDeviceListener implements DeviceListener {
749 @Override
750 public void event(DeviceEvent event) {
751 switch (event.type()) {
752 case DEVICE_REMOVED:
753 case DEVICE_AVAILABILITY_CHANGED:
754 DeviceId deviceId = event.subject().id();
755 if (!deviceService.isAvailable(deviceId)) {
756 if (purgeOnDisconnection) {
757 store.purgeFlowRule(deviceId);
758 }
759 }
760 break;
761 default:
762 break;
763 }
764 }
765 }
alshabib57044ba2014-09-16 15:58:01 -0700766}