Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow.impl; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Multimap; |
| 23 | import com.google.common.collect.Sets; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 24 | import org.onlab.util.Tools; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 25 | import org.onosproject.cfg.ComponentConfigService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 29 | import org.onosproject.mastership.MastershipService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceEvent; |
| 33 | import org.onosproject.net.device.DeviceListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceService; |
Jordan Halterman | 4f3193a | 2017-07-11 20:32:49 -0700 | [diff] [blame] | 35 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.CompletedBatchOperation; |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.DefaultFlowEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowEntry; |
| 39 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRuleEvent; |
| 41 | import org.onosproject.net.flow.FlowRuleListener; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.FlowRuleOperation; |
| 43 | import org.onosproject.net.flow.FlowRuleOperations; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.FlowRuleProvider; |
| 46 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 47 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 48 | import org.onosproject.net.flow.FlowRuleService; |
| 49 | import org.onosproject.net.flow.FlowRuleStore; |
| 50 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flow.TableStatisticsEntry; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 53 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent; |
| 54 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 55 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 56 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.net.provider.AbstractProviderService; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 58 | import org.onosproject.net.provider.ProviderId; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 59 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 60 | import org.osgi.service.component.annotations.Activate; |
| 61 | import org.osgi.service.component.annotations.Component; |
| 62 | import org.osgi.service.component.annotations.Deactivate; |
| 63 | import org.osgi.service.component.annotations.Modified; |
| 64 | import org.osgi.service.component.annotations.Reference; |
| 65 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 68 | import java.util.Collections; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 69 | import java.util.Dictionary; |
Sho SHIMIZU | b837cb7 | 2016-08-30 14:44:50 -0700 | [diff] [blame] | 70 | import java.util.HashSet; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 71 | import java.util.List; |
| 72 | import java.util.Map; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 73 | import java.util.Optional; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 74 | import java.util.Set; |
| 75 | import java.util.concurrent.ConcurrentHashMap; |
| 76 | import java.util.concurrent.ExecutorService; |
| 77 | import java.util.concurrent.Executors; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 78 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 79 | import static com.google.common.base.Preconditions.checkNotNull; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 80 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 81 | import static org.onlab.util.Tools.get; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 82 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 83 | import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES; |
| 84 | import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES_DEFAULT; |
| 85 | import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY; |
| 86 | import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY_DEFAULT; |
| 87 | import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION; |
| 88 | import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION_DEFAULT; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 89 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED; |
| 90 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 91 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 92 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ; |
| 93 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 94 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 95 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Provides implementation of the flow NB & SB APIs. |
| 98 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 99 | @Component( |
| 100 | immediate = true, |
| 101 | service = { |
| 102 | FlowRuleService.class, |
| 103 | FlowRuleProviderRegistry.class |
| 104 | }, |
| 105 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 106 | 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 Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 109 | } |
| 110 | ) |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 111 | public class FlowRuleManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 112 | extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener, |
| 113 | FlowRuleProvider, FlowRuleProviderService> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 114 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 115 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 116 | private final Logger log = getLogger(getClass()); |
| 117 | |
Thomas Vachuska | 227943d | 2018-03-19 09:37:28 -0700 | [diff] [blame] | 118 | 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 Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 120 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 121 | /** Allow flow rules in switch not installed by ONOS. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 122 | private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES_DEFAULT; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 123 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 124 | /** Purge entries associated with a device when the device goes offline. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 125 | private boolean purgeOnDisconnection = PURGE_ON_DISCONNECTION_DEFAULT; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 126 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 127 | /** Frequency (in seconds) for polling flow statistics via fallback provider. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 128 | private int fallbackFlowPollFrequency = POLL_FREQUENCY_DEFAULT; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 129 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 130 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 131 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 132 | |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 133 | private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 134 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 135 | protected ExecutorService deviceInstallers = |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 136 | Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 137 | |
| 138 | protected ExecutorService operationsService = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 139 | Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 140 | |
| 141 | private IdGenerator idGenerator; |
| 142 | |
Sho SHIMIZU | 9f95074 | 2016-09-01 09:55:38 -0700 | [diff] [blame] | 143 | private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 144 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 146 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 147 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 148 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 149 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 150 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 151 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 152 | protected CoreService coreService; |
| 153 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 155 | protected MastershipService mastershipService; |
| 156 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 158 | protected ComponentConfigService cfgService; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 159 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 4f3193a | 2017-07-11 20:32:49 -0700 | [diff] [blame] | 161 | protected DriverService driverService; |
| 162 | |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 163 | @Activate |
| 164 | public void activate(ComponentContext context) { |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 165 | modified(context); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 166 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 167 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 168 | deviceService.addListener(deviceListener); |
| 169 | cfgService.registerProperties(getClass()); |
| 170 | idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 171 | log.info("Started"); |
| 172 | } |
| 173 | |
| 174 | @Deactivate |
| 175 | public void deactivate() { |
Andrea Campanella | 5a3c09c | 2017-12-01 13:57:48 +0100 | [diff] [blame] | 176 | driverProvider.terminate(); |
Andrea Campanella | 3f1c61e | 2016-04-01 17:30:12 -0700 | [diff] [blame] | 177 | deviceService.removeListener(deviceListener); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 178 | cfgService.unregisterProperties(getClass(), false); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 179 | deviceInstallers.shutdownNow(); |
| 180 | operationsService.shutdownNow(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 181 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 182 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 183 | log.info("Stopped"); |
| 184 | } |
| 185 | |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 186 | @Modified |
| 187 | public void modified(ComponentContext context) { |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 188 | if (context != null) { |
| 189 | readComponentConfiguration(context); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 190 | } |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 191 | driverProvider.init(new InternalFlowRuleProviderService(driverProvider), |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 192 | deviceService, mastershipService, fallbackFlowPollFrequency); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | protected FlowRuleProvider defaultProvider() { |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 197 | return driverProvider; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 198 | } |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 199 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 200 | /** |
| 201 | * Extracts properties from the component configuration context. |
| 202 | * |
| 203 | * @param context the component context |
| 204 | */ |
| 205 | private void readComponentConfiguration(ComponentContext context) { |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 206 | Dictionary<?, ?> properties = context.getProperties(); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 207 | Boolean flag; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 208 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 209 | flag = Tools.isPropertyEnabled(properties, ALLOW_EXTRANEOUS_RULES); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 210 | 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 Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 217 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 218 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 219 | flag = Tools.isPropertyEnabled(properties, PURGE_ON_DISCONNECTION); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 220 | 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 Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 228 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 229 | String s = get(properties, POLL_FREQUENCY); |
Carmelo Cascone | e6a9754 | 2018-06-20 16:48:00 +0200 | [diff] [blame] | 230 | 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 Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 244 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 245 | } |
| 246 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 247 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 248 | public int getFlowRuleCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 249 | checkPermission(FLOWRULE_READ); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 250 | return store.getFlowRuleCount(); |
| 251 | } |
| 252 | |
| 253 | @Override |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 254 | public int getFlowRuleCount(DeviceId deviceId) { |
| 255 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | 227943d | 2018-03-19 09:37:28 -0700 | [diff] [blame] | 256 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 257 | return store.getFlowRuleCount(deviceId); |
| 258 | } |
| 259 | |
| 260 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 261 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 262 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 263 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 264 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 268 | public void applyFlowRules(FlowRule... flowRules) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 269 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 270 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 271 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 272 | for (FlowRule flowRule : flowRules) { |
| 273 | builder.add(flowRule); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 274 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 275 | apply(builder.build()); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 279 | public void purgeFlowRules(DeviceId deviceId) { |
| 280 | checkPermission(FLOWRULE_WRITE); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 281 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 282 | store.purgeFlowRule(deviceId); |
| 283 | } |
| 284 | |
| 285 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 286 | public void removeFlowRules(FlowRule... flowRules) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 287 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 288 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 289 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 290 | for (FlowRule flowRule : flowRules) { |
| 291 | builder.remove(flowRule); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 292 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 293 | apply(builder.build()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 294 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 295 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 296 | @Override |
| 297 | public void removeFlowRulesById(ApplicationId id) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 298 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 299 | |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 300 | 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 Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame^] | 308 | removeFlowRules(Iterables.toArray(flowEntries, FlowRule.class)); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 312 | 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 |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 327 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 328 | checkPermission(FLOWRULE_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 329 | |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 330 | 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'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 343 | public void apply(FlowRuleOperations ops) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 344 | checkPermission(FLOWRULE_WRITE); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 345 | operationsService.execute(new FlowOperationsProcessor(ops)); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 349 | protected FlowRuleProviderService createProviderService( |
| 350 | FlowRuleProvider provider) { |
| 351 | return new InternalFlowRuleProviderService(provider); |
| 352 | } |
| 353 | |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 354 | @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 Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 366 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 367 | // 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 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 375 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 376 | extends AbstractProviderService<FlowRuleProvider> |
| 377 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 378 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 379 | final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 380 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 381 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 382 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 383 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 384 | super(provider); |
| 385 | } |
| 386 | |
| 387 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 388 | public void flowRemoved(FlowEntry flowEntry) { |
| 389 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 390 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 391 | lastSeen.remove(flowEntry); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 392 | firstSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 393 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 394 | if (stored == null) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 395 | log.debug("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 396 | return; |
| 397 | } |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 398 | if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) { |
| 399 | ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED); |
| 400 | } |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 401 | FlowRuleProvider frp = getProvider(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 402 | FlowRuleEvent event = null; |
| 403 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 404 | case ADDED: |
| 405 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 406 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 407 | break; |
| 408 | case PENDING_REMOVE: |
| 409 | case REMOVED: |
| 410 | event = store.removeFlowRule(stored); |
| 411 | break; |
| 412 | default: |
| 413 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 414 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 415 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 416 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 417 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 418 | post(event); |
| 419 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 420 | } |
| 421 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 422 | |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 423 | private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 424 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 425 | checkValidity(); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 426 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 427 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 428 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 429 | case PENDING_REMOVE: |
| 430 | case REMOVED: |
| 431 | event = store.removeFlowRule(flowRule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 432 | log.debug("Flow {} removed", flowRule); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 433 | break; |
| 434 | case ADDED: |
| 435 | case PENDING_ADD: |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 436 | event = store.pendingFlowRule(flowRule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 437 | 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. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 444 | try { |
| 445 | frp.applyFlowRule(flowRule); |
| 446 | } catch (UnsupportedOperationException e) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 447 | log.warn("Unsupported operation", e); |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 448 | if (flowRule instanceof DefaultFlowEntry) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 449 | //FIXME modification of "stored" flow entry outside of store |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 450 | ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED); |
| 451 | } |
| 452 | } |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 453 | break; |
| 454 | default: |
| 455 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 456 | } |
| 457 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 458 | if (event != null) { |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 459 | post(event); |
| 460 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 461 | } |
| 462 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 463 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 464 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 465 | checkValidity(); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 466 | // getProvider is customized to favor driverProvider |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 467 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 468 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 469 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 470 | } |
| 471 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 472 | private void flowAdded(FlowEntry flowEntry) { |
| 473 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 474 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 475 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 476 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 477 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 478 | if (event == null) { |
| 479 | log.debug("No flow store event generated."); |
| 480 | } else { |
Jonathan Hart | 58682dd | 2014-11-24 20:11:16 -0800 | [diff] [blame] | 481 | log.trace("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 482 | post(event); |
| 483 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 484 | } else { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 485 | log.debug("Removing flow rules...."); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 486 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 487 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 488 | } |
| 489 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 490 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 491 | if (storedRule == null) { |
| 492 | return false; |
| 493 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 494 | if (storedRule.isPermanent()) { |
| 495 | return true; |
| 496 | } |
| 497 | |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 498 | final long timeout = storedRule.timeout() * 1000L; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 499 | final long currentTime = System.currentTimeMillis(); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 500 | |
| 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 Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 508 | final long hardTimeout = storedRule.hardTimeout() * 1000L; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 509 | if ((currentTime - first) > hardTimeout) { |
| 510 | return false; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Gamze Abaka | 8d03f18 | 2017-07-20 15:09:24 +0000 | [diff] [blame] | 515 | if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 516 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 517 | return true; |
| 518 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 519 | 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); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 526 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 527 | // concurrently removed? let the liveness check fail |
| 528 | return last != null && (currentTime - last) <= timeout; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 529 | } |
| 530 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 531 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 532 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 533 | 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 Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 543 | Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap(); |
| 544 | store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f)); |
| 545 | |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 546 | for (FlowEntry rule : flowEntries) { |
| 547 | try { |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 548 | 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 Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 557 | flowMissing(storedRule, false); |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 558 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 559 | } else { |
| 560 | // the device has a rule the store does not have |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 561 | if (!allowExtraneousRules) { |
| 562 | extraneousFlow(rule); |
| 563 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 564 | } |
Sho SHIMIZU | 24a00d9 | 2015-05-05 11:11:13 -0700 | [diff] [blame] | 565 | } catch (Exception e) { |
Jon Hall | 274cecb | 2017-08-09 12:15:48 -0700 | [diff] [blame] | 566 | log.warn("Can't process added or extra rule {} for device {}:{}", |
| 567 | rule, deviceId, e); |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 568 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 569 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 570 | |
| 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 Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 576 | log.debug("Adding the rule that is present in store but not on switch : {}", rule); |
| 577 | flowMissing(rule, true); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 578 | } catch (Exception e) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 579 | log.warn("Can't add missing flow rule:", e); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 580 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 581 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 582 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 583 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 584 | |
| 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 Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 592 | |
| 593 | @Override |
| 594 | public void pushTableStatistics(DeviceId deviceId, |
| 595 | List<TableStatisticsEntry> tableStats) { |
| 596 | store.updateTableStatistics(deviceId, tableStats); |
| 597 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 598 | } |
| 599 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 600 | // Store delegate to re-post events emitted from the store. |
| 601 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 602 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 603 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 604 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 605 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 606 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 607 | public void notify(FlowRuleBatchEvent event) { |
| 608 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 609 | switch (event.type()) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 610 | case BATCH_OPERATION_REQUESTED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 611 | // Request has been forwarded to MASTER Node, and was |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 612 | request.ops().forEach( |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 613 | op -> { |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 614 | switch (op.operator()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 615 | case ADD: |
Sho SHIMIZU | 8efc896 | 2016-08-31 15:17:44 -0700 | [diff] [blame] | 616 | post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 617 | break; |
| 618 | case REMOVE: |
Sho SHIMIZU | 8efc896 | 2016-08-31 15:17:44 -0700 | [diff] [blame] | 619 | post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 620 | break; |
| 621 | case MODIFY: |
| 622 | //TODO: do something here when the time comes. |
| 623 | break; |
| 624 | default: |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 625 | log.warn("Unknown flow operation operator: {}", op.operator()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | ); |
| 629 | |
| 630 | DeviceId deviceId = event.deviceId(); |
Sho SHIMIZU | 8efc896 | 2016-08-31 15:17:44 -0700 | [diff] [blame] | 631 | FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 632 | // getProvider is customized to favor driverProvider |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 633 | FlowRuleProvider flowRuleProvider = getProvider(deviceId); |
| 634 | if (flowRuleProvider != null) { |
| 635 | flowRuleProvider.executeBatch(batchOperation); |
| 636 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 637 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 638 | break; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 639 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 640 | case BATCH_OPERATION_COMPLETED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 641 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 642 | 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 HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 652 | break; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 653 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 654 | default: |
| 655 | break; |
| 656 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 659 | |
Sho SHIMIZU | 5711ce1 | 2016-08-31 13:57:12 -0700 | [diff] [blame] | 660 | 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'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 673 | private class FlowOperationsProcessor implements Runnable { |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 674 | // Immutable |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 675 | private final FlowRuleOperations fops; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 676 | |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 677 | // Mutable |
| 678 | private final List<Set<FlowRuleOperation>> stages; |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 679 | private final Set<DeviceId> pendingDevices = new HashSet<>(); |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 680 | private boolean hasFailed = false; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 681 | |
Sho SHIMIZU | 7c9b73a | 2016-08-30 14:08:28 -0700 | [diff] [blame] | 682 | FlowOperationsProcessor(FlowRuleOperations ops) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 683 | this.stages = Lists.newArrayList(ops.stages()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 684 | this.fops = ops; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | @Override |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 688 | public synchronized void run() { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 689 | if (!stages.isEmpty()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 690 | process(stages.remove(0)); |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 691 | } else if (!hasFailed) { |
Sho SHIMIZU | c9e4bb0 | 2016-09-01 12:43:39 -0700 | [diff] [blame] | 692 | fops.callback().onSuccess(fops); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 696 | private void process(Set<FlowRuleOperation> ops) { |
Sho SHIMIZU | 8efc896 | 2016-08-31 15:17:44 -0700 | [diff] [blame] | 697 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 698 | |
Sho SHIMIZU | f4fd3de | 2016-08-31 15:47:56 -0700 | [diff] [blame] | 699 | for (FlowRuleOperation op : ops) { |
| 700 | perDeviceBatches.put(op.rule().deviceId(), |
| 701 | new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 702 | } |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 703 | pendingDevices.addAll(perDeviceBatches.keySet()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 704 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 705 | for (DeviceId deviceId : perDeviceBatches.keySet()) { |
Sho SHIMIZU | 3a70431 | 2015-05-27 13:36:01 -0700 | [diff] [blame] | 706 | long id = idGenerator.getNewId(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 707 | final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId), |
| 708 | deviceId, id); |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 709 | pendingFlowOperations.put(id, this); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 710 | deviceInstallers.execute(() -> store.storeBatch(b)); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 714 | synchronized void satisfy(DeviceId devId) { |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 715 | pendingDevices.remove(devId); |
| 716 | if (pendingDevices.isEmpty()) { |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 717 | operationsService.execute(this); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 718 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 719 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 720 | |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 721 | synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) { |
Sho SHIMIZU | b9e0ab7 | 2016-09-14 16:54:13 -0700 | [diff] [blame] | 722 | hasFailed = true; |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 723 | pendingDevices.remove(devId); |
| 724 | if (pendingDevices.isEmpty()) { |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 725 | operationsService.execute(this); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 728 | FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder(); |
| 729 | failures.forEach(failedOpsBuilder::add); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 730 | |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 731 | fops.callback().onError(failedOpsBuilder.build()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 732 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 733 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 734 | |
| 735 | @Override |
| 736 | public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) { |
| 737 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 738 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 739 | return store.getTableStatistics(deviceId); |
| 740 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 741 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 742 | @Override |
| 743 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 744 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 745 | return store.getActiveFlowRuleCount(deviceId); |
| 746 | } |
| 747 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 748 | 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 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 766 | } |