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; |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 31 | import org.onosproject.mastership.MastershipService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 32a9c0b | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 34 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 35 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceService; |
Jordan Halterman | 4f3193a | 2017-07-11 20:32:49 -0700 | [diff] [blame] | 39 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.CompletedBatchOperation; |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.DefaultFlowEntry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flow.FlowEntry; |
| 43 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.flow.FlowRuleEvent; |
| 45 | import org.onosproject.net.flow.FlowRuleListener; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 46 | import org.onosproject.net.flow.FlowRuleOperation; |
| 47 | import org.onosproject.net.flow.FlowRuleOperations; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 48 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.FlowRuleProvider; |
| 50 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 51 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 52 | import org.onosproject.net.flow.FlowRuleService; |
| 53 | import org.onosproject.net.flow.FlowRuleStore; |
| 54 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 55 | import org.onosproject.net.flow.TableStatisticsEntry; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEntry; |
| 57 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchEvent; |
| 58 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchOperation; |
| 59 | import org.onosproject.net.flow.oldbatch.FlowRuleBatchRequest; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 60 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.provider.AbstractProviderService; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 62 | import org.onosproject.net.provider.ProviderId; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 63 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 64 | import org.osgi.service.component.annotations.Activate; |
| 65 | import org.osgi.service.component.annotations.Component; |
| 66 | import org.osgi.service.component.annotations.Deactivate; |
| 67 | import org.osgi.service.component.annotations.Modified; |
| 68 | import org.osgi.service.component.annotations.Reference; |
| 69 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 70 | import org.slf4j.Logger; |
| 71 | |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 72 | import java.util.Collections; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 73 | import java.util.Dictionary; |
Sho SHIMIZU | b837cb7 | 2016-08-30 14:44:50 -0700 | [diff] [blame] | 74 | import java.util.HashSet; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 75 | import java.util.List; |
| 76 | import java.util.Map; |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 77 | import java.util.Objects; |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 78 | import java.util.Optional; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 79 | import java.util.Set; |
| 80 | import java.util.concurrent.ConcurrentHashMap; |
| 81 | import java.util.concurrent.ExecutorService; |
| 82 | import java.util.concurrent.Executors; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 83 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 84 | import static com.google.common.base.Preconditions.checkNotNull; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 85 | import static com.google.common.base.Strings.isNullOrEmpty; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 86 | import static org.onlab.util.Tools.get; |
Brian O'Connor | d12267c | 2015-02-17 18:17:08 -0800 | [diff] [blame] | 87 | import static org.onlab.util.Tools.groupedThreads; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 88 | import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES; |
| 89 | import static org.onosproject.net.OsgiPropertyConstants.ALLOW_EXTRANEOUS_RULES_DEFAULT; |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 90 | import static org.onosproject.net.OsgiPropertyConstants.IMPORT_EXTRANEOUS_RULES; |
| 91 | import static org.onosproject.net.OsgiPropertyConstants.IMPORT_EXTRANEOUS_RULES_DEFAULT; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 92 | import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY; |
| 93 | import static org.onosproject.net.OsgiPropertyConstants.POLL_FREQUENCY_DEFAULT; |
| 94 | import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION; |
| 95 | import static org.onosproject.net.OsgiPropertyConstants.PURGE_ON_DISCONNECTION_DEFAULT; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 96 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_ADD_REQUESTED; |
| 97 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVE_REQUESTED; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 98 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 99 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_READ; |
| 100 | import static org.onosproject.security.AppPermission.Type.FLOWRULE_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 101 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 102 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Provides implementation of the flow NB & SB APIs. |
| 105 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 106 | @Component( |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 107 | immediate = true, |
| 108 | service = { |
| 109 | FlowRuleService.class, |
| 110 | FlowRuleProviderRegistry.class |
| 111 | }, |
| 112 | property = { |
| 113 | ALLOW_EXTRANEOUS_RULES + ":Boolean=" + ALLOW_EXTRANEOUS_RULES_DEFAULT, |
| 114 | IMPORT_EXTRANEOUS_RULES + ":Boolean=" + IMPORT_EXTRANEOUS_RULES_DEFAULT, |
| 115 | PURGE_ON_DISCONNECTION + ":Boolean=" + PURGE_ON_DISCONNECTION_DEFAULT, |
| 116 | POLL_FREQUENCY + ":Integer=" + POLL_FREQUENCY_DEFAULT |
| 117 | } |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 118 | ) |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 119 | public class FlowRuleManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 120 | extends AbstractListenerProviderRegistry<FlowRuleEvent, FlowRuleListener, |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 121 | FlowRuleProvider, FlowRuleProviderService> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 122 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 123 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 124 | private final Logger log = getLogger(getClass()); |
| 125 | |
Thomas Vachuska | 227943d | 2018-03-19 09:37:28 -0700 | [diff] [blame] | 126 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 127 | private static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 128 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 129 | /** Allow flow rules in switch not installed by ONOS. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 130 | private boolean allowExtraneousRules = ALLOW_EXTRANEOUS_RULES_DEFAULT; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 131 | |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 132 | /** Allow to import flow rules in switch not installed by ONOS. */ |
| 133 | private boolean importExtraneousRules = IMPORT_EXTRANEOUS_RULES_DEFAULT; |
| 134 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 135 | /** Purge entries associated with a device when the device goes offline. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 136 | private boolean purgeOnDisconnection = PURGE_ON_DISCONNECTION_DEFAULT; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 137 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 138 | /** Frequency (in seconds) for polling flow statistics via fallback provider. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 139 | private int fallbackFlowPollFrequency = POLL_FREQUENCY_DEFAULT; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 140 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 141 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 142 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 143 | |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 144 | private final FlowRuleDriverProvider driverProvider = new FlowRuleDriverProvider(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 145 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 146 | protected ExecutorService deviceInstallers = |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 147 | Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "device-installer-%d", log)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 148 | |
| 149 | protected ExecutorService operationsService = |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 150 | Executors.newFixedThreadPool(32, groupedThreads("onos/flowservice", "operations-%d", log)); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 151 | |
| 152 | private IdGenerator idGenerator; |
| 153 | |
Sho SHIMIZU | 9f95074 | 2016-09-01 09:55:38 -0700 | [diff] [blame] | 154 | private final Map<Long, FlowOperationsProcessor> pendingFlowOperations = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 155 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 156 | private NodeId local; |
| 157 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 158 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 159 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 160 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 161 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 162 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 163 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 164 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 165 | protected CoreService coreService; |
| 166 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 167 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 168 | protected MastershipService mastershipService; |
| 169 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 170 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 171 | protected ComponentConfigService cfgService; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 172 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 173 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 4f3193a | 2017-07-11 20:32:49 -0700 | [diff] [blame] | 174 | protected DriverService driverService; |
| 175 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 176 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 177 | protected ClusterService clusterService; |
| 178 | |
Andrea Campanella | 32a9c0b | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 179 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 180 | protected NetworkConfigRegistry netCfgService; |
| 181 | |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 182 | @Activate |
| 183 | public void activate(ComponentContext context) { |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 184 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 185 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 186 | deviceService.addListener(deviceListener); |
| 187 | cfgService.registerProperties(getClass()); |
pier | ed2a0a4 | 2019-04-16 11:48:08 +0200 | [diff] [blame] | 188 | modified(context); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 189 | idGenerator = coreService.getIdGenerator(FLOW_OP_TOPIC); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 190 | local = clusterService.getLocalNode().id(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 191 | log.info("Started"); |
| 192 | } |
| 193 | |
| 194 | @Deactivate |
| 195 | public void deactivate() { |
Andrea Campanella | 5a3c09c | 2017-12-01 13:57:48 +0100 | [diff] [blame] | 196 | driverProvider.terminate(); |
Andrea Campanella | 3f1c61e | 2016-04-01 17:30:12 -0700 | [diff] [blame] | 197 | deviceService.removeListener(deviceListener); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 198 | cfgService.unregisterProperties(getClass(), false); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 199 | deviceInstallers.shutdownNow(); |
| 200 | operationsService.shutdownNow(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 201 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 202 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 203 | log.info("Stopped"); |
| 204 | } |
| 205 | |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 206 | @Modified |
| 207 | public void modified(ComponentContext context) { |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 208 | if (context != null) { |
| 209 | readComponentConfiguration(context); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 210 | } |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 211 | driverProvider.init(new InternalFlowRuleProviderService(driverProvider), |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 212 | deviceService, mastershipService, fallbackFlowPollFrequency); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | protected FlowRuleProvider defaultProvider() { |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 217 | return driverProvider; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 218 | } |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 219 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 220 | /** |
| 221 | * Extracts properties from the component configuration context. |
| 222 | * |
| 223 | * @param context the component context |
| 224 | */ |
| 225 | private void readComponentConfiguration(ComponentContext context) { |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 226 | Dictionary<?, ?> properties = context.getProperties(); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 227 | Boolean flag; |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 228 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 229 | flag = Tools.isPropertyEnabled(properties, ALLOW_EXTRANEOUS_RULES); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 230 | if (flag == null) { |
| 231 | log.info("AllowExtraneousRules is not configured, " + |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 232 | "using current value of {}", allowExtraneousRules); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 233 | } else { |
| 234 | allowExtraneousRules = flag; |
| 235 | log.info("Configured. AllowExtraneousRules is {}", |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 236 | allowExtraneousRules ? "enabled" : "disabled"); |
| 237 | } |
| 238 | |
| 239 | flag = Tools.isPropertyEnabled(properties, IMPORT_EXTRANEOUS_RULES); |
| 240 | if (flag == null) { |
| 241 | log.info("ImportExtraneousRules is not configured, " + |
| 242 | "using current value of {}", importExtraneousRules); |
| 243 | } else { |
| 244 | importExtraneousRules = flag; |
| 245 | log.info("Configured. importExtraneousRules is {}", |
| 246 | importExtraneousRules ? "enabled" : "disabled"); |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 247 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 248 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 249 | flag = Tools.isPropertyEnabled(properties, PURGE_ON_DISCONNECTION); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 250 | if (flag == null) { |
| 251 | log.info("PurgeOnDisconnection is not configured, " + |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 252 | "using current value of {}", purgeOnDisconnection); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 253 | } else { |
| 254 | purgeOnDisconnection = flag; |
| 255 | log.info("Configured. PurgeOnDisconnection is {}", |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 256 | purgeOnDisconnection ? "enabled" : "disabled"); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 257 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 258 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 259 | String s = get(properties, POLL_FREQUENCY); |
Carmelo Cascone | e6a9754 | 2018-06-20 16:48:00 +0200 | [diff] [blame] | 260 | if (isNullOrEmpty(s)) { |
| 261 | log.info("fallbackFlowPollFrequency is not configured, " + |
| 262 | "using current value of {} seconds", |
| 263 | fallbackFlowPollFrequency); |
| 264 | } else { |
| 265 | try { |
| 266 | fallbackFlowPollFrequency = Integer.parseInt(s); |
| 267 | log.info("Configured. FallbackFlowPollFrequency is {} seconds", |
| 268 | fallbackFlowPollFrequency); |
| 269 | } catch (NumberFormatException e) { |
| 270 | log.warn("Configured fallbackFlowPollFrequency value '{}' " + |
| 271 | "is not a number, using current value of {} seconds", |
| 272 | fallbackFlowPollFrequency); |
| 273 | } |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 274 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 275 | } |
| 276 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 277 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 278 | public int getFlowRuleCount() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 279 | checkPermission(FLOWRULE_READ); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 280 | return store.getFlowRuleCount(); |
| 281 | } |
| 282 | |
| 283 | @Override |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 284 | public int getFlowRuleCount(DeviceId deviceId) { |
| 285 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | 227943d | 2018-03-19 09:37:28 -0700 | [diff] [blame] | 286 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 287 | return store.getFlowRuleCount(deviceId); |
| 288 | } |
| 289 | |
| 290 | @Override |
Jordan Halterman | b81fdc1 | 2019-03-04 18:12:20 -0800 | [diff] [blame] | 291 | public int getFlowRuleCount(DeviceId deviceId, FlowEntry.FlowEntryState state) { |
| 292 | checkPermission(FLOWRULE_READ); |
| 293 | checkNotNull(deviceId, "Device ID cannot be null"); |
| 294 | return store.getFlowRuleCount(deviceId, state); |
| 295 | } |
| 296 | |
| 297 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 298 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 299 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 300 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 301 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 305 | public void applyFlowRules(FlowRule... flowRules) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 306 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 307 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 308 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 309 | for (FlowRule flowRule : flowRules) { |
| 310 | builder.add(flowRule); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 311 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 312 | apply(builder.build()); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Override |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 316 | public void purgeFlowRules(DeviceId deviceId) { |
| 317 | checkPermission(FLOWRULE_WRITE); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 318 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 319 | store.purgeFlowRule(deviceId); |
| 320 | } |
| 321 | |
| 322 | @Override |
Daniele Moro | 43ac289 | 2021-07-15 17:02:59 +0200 | [diff] [blame^] | 323 | public void purgeFlowRules(DeviceId deviceId, ApplicationId appId) { |
| 324 | checkPermission(FLOWRULE_WRITE); |
| 325 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 326 | checkNotNull(appId, "Application ID cannot be null!"); |
| 327 | |
| 328 | store.purgeFlowRules(deviceId, appId); |
| 329 | } |
| 330 | |
| 331 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 332 | public void removeFlowRules(FlowRule... flowRules) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 333 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 334 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 335 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 336 | for (FlowRule flowRule : flowRules) { |
| 337 | builder.remove(flowRule); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 338 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 339 | apply(builder.build()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 340 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 341 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 342 | @Override |
| 343 | public void removeFlowRulesById(ApplicationId id) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 344 | checkPermission(FLOWRULE_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 345 | |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 346 | Set<FlowRule> flowEntries = Sets.newHashSet(); |
| 347 | for (Device d : deviceService.getDevices()) { |
| 348 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 349 | if (flowEntry.appId() == id.id()) { |
| 350 | flowEntries.add(flowEntry); |
| 351 | } |
| 352 | } |
| 353 | } |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame] | 354 | removeFlowRules(Iterables.toArray(flowEntries, FlowRule.class)); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | @Override |
Bharath Thiruveedula | 99849dc | 2016-11-17 22:04:38 +0530 | [diff] [blame] | 358 | public Iterable<FlowEntry> getFlowEntriesById(ApplicationId id) { |
| 359 | checkPermission(FLOWRULE_READ); |
| 360 | |
| 361 | Set<FlowEntry> flowEntries = Sets.newHashSet(); |
| 362 | for (Device d : deviceService.getDevices()) { |
| 363 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 364 | if (flowEntry.appId() == id.id()) { |
| 365 | flowEntries.add(flowEntry); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | return flowEntries; |
| 370 | } |
| 371 | |
| 372 | @Override |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 373 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 374 | checkPermission(FLOWRULE_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 375 | |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 376 | Set<FlowRule> matches = Sets.newHashSet(); |
| 377 | long toLookUp = ((long) appId.id() << 16) | groupId; |
| 378 | for (Device d : deviceService.getDevices()) { |
| 379 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 380 | if ((flowEntry.id().value() >>> 32) == toLookUp) { |
| 381 | matches.add(flowEntry); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | return matches; |
| 386 | } |
| 387 | |
| 388 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 389 | public void apply(FlowRuleOperations ops) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 390 | checkPermission(FLOWRULE_WRITE); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 391 | operationsService.execute(new FlowOperationsProcessor(ops)); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 395 | protected FlowRuleProviderService createProviderService( |
| 396 | FlowRuleProvider provider) { |
| 397 | return new InternalFlowRuleProviderService(provider); |
| 398 | } |
| 399 | |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 400 | @Override |
| 401 | protected synchronized FlowRuleProvider getProvider(ProviderId pid) { |
| 402 | log.warn("should not be calling getProvider(ProviderId)"); |
| 403 | return super.getProvider(pid); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * {@inheritDoc} |
| 408 | * if the Device does not support {@link FlowRuleProgrammable}. |
| 409 | */ |
| 410 | @Override |
| 411 | protected synchronized FlowRuleProvider getProvider(DeviceId deviceId) { |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 412 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 413 | // if device supports FlowRuleProgrammable, |
| 414 | // use FlowRuleProgrammable via FlowRuleDriverProvider |
| 415 | return Optional.ofNullable(deviceService.getDevice(deviceId)) |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 416 | .filter(dev -> dev.is(FlowRuleProgrammable.class)) |
| 417 | .<FlowRuleProvider>map(x -> driverProvider) |
| 418 | .orElseGet(() -> super.getProvider(deviceId)); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 419 | } |
| 420 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 421 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 422 | extends AbstractProviderService<FlowRuleProvider> |
| 423 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 424 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 425 | final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 426 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 427 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 428 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 429 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 430 | super(provider); |
| 431 | } |
| 432 | |
| 433 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 434 | public void flowRemoved(FlowEntry flowEntry) { |
| 435 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 436 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 437 | lastSeen.remove(flowEntry); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 438 | firstSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 439 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 440 | if (stored == null) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 441 | log.debug("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 442 | return; |
| 443 | } |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 444 | if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) { |
| 445 | ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED); |
| 446 | } |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 447 | FlowRuleProvider frp = getProvider(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 448 | FlowRuleEvent event = null; |
| 449 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 450 | case ADDED: |
| 451 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 452 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 453 | break; |
| 454 | case PENDING_REMOVE: |
| 455 | case REMOVED: |
| 456 | event = store.removeFlowRule(stored); |
| 457 | break; |
| 458 | default: |
| 459 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 460 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 461 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 462 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 463 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 464 | post(event); |
| 465 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 466 | } |
| 467 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 468 | |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 469 | private void flowMissing(FlowEntry flowRule, boolean isFlowOnlyInStore) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 470 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 471 | checkValidity(); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 472 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 473 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 474 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 475 | case PENDING_REMOVE: |
| 476 | case REMOVED: |
| 477 | event = store.removeFlowRule(flowRule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 478 | log.debug("Flow {} removed", flowRule); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 479 | break; |
| 480 | case ADDED: |
| 481 | case PENDING_ADD: |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 482 | event = store.pendingFlowRule(flowRule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 483 | if (isFlowOnlyInStore) { |
| 484 | // Publishing RULE_ADD_REQUESTED event facilitates |
| 485 | // preparation of statistics for the concerned rule |
| 486 | if (event == null) { |
| 487 | event = new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, flowRule); |
| 488 | } |
| 489 | } |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 490 | try { |
| 491 | frp.applyFlowRule(flowRule); |
| 492 | } catch (UnsupportedOperationException e) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 493 | log.warn("Unsupported operation", e); |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 494 | if (flowRule instanceof DefaultFlowEntry) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 495 | //FIXME modification of "stored" flow entry outside of store |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 496 | ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED); |
| 497 | } |
| 498 | } |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 499 | break; |
| 500 | default: |
| 501 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 502 | } |
| 503 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 504 | if (event != null) { |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 505 | post(event); |
| 506 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 507 | } |
| 508 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 509 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 510 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 511 | checkValidity(); |
Yuta HIGUCHI | 48dcd37 | 2017-02-27 22:55:53 -0800 | [diff] [blame] | 512 | // getProvider is customized to favor driverProvider |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 513 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 514 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 515 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 518 | private boolean flowAdded(FlowEntry flowEntry) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 519 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 520 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 521 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 522 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 523 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 524 | if (event == null) { |
| 525 | log.debug("No flow store event generated."); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 526 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 527 | } else { |
Jonathan Hart | 58682dd | 2014-11-24 20:11:16 -0800 | [diff] [blame] | 528 | log.trace("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 529 | post(event); |
| 530 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 531 | } else { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 532 | log.debug("Removing flow rules...."); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 533 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 534 | } |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 535 | return true; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 536 | } |
| 537 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 538 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 539 | if (storedRule == null) { |
| 540 | return false; |
| 541 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 542 | if (storedRule.isPermanent()) { |
| 543 | return true; |
| 544 | } |
| 545 | |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 546 | final long timeout = storedRule.timeout() * 1000L; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 547 | final long currentTime = System.currentTimeMillis(); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 548 | |
| 549 | // Checking flow with hardTimeout |
| 550 | if (storedRule.hardTimeout() != 0) { |
| 551 | if (!firstSeen.containsKey(storedRule)) { |
| 552 | // First time rule adding |
| 553 | firstSeen.put(storedRule, currentTime); |
| 554 | } else { |
| 555 | Long first = firstSeen.get(storedRule); |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 556 | final long hardTimeout = storedRule.hardTimeout() * 1000L; |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame] | 557 | if ((currentTime - first) > hardTimeout) { |
| 558 | return false; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
Gamze Abaka | 8d03f18 | 2017-07-20 15:09:24 +0000 | [diff] [blame] | 563 | if (storedRule.packets() != swRule.packets() || storedRule.bytes() != swRule.bytes()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 564 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 565 | return true; |
| 566 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 567 | if (!lastSeen.containsKey(storedRule)) { |
| 568 | // checking for the first time |
| 569 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 570 | // Use following if lastSeen attr. was removed. |
| 571 | //lastSeen.put(storedRule, currentTime); |
| 572 | } |
| 573 | Long last = lastSeen.get(storedRule); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 574 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 575 | // concurrently removed? let the liveness check fail |
| 576 | return last != null && (currentTime - last) <= timeout; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 577 | } |
| 578 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 579 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 580 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 581 | pushFlowMetricsInternal(deviceId, flowEntries, true); |
| 582 | } |
| 583 | |
| 584 | @Override |
| 585 | public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 586 | pushFlowMetricsInternal(deviceId, flowEntries, false); |
| 587 | } |
| 588 | |
| 589 | private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries, |
| 590 | boolean useMissingFlow) { |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 591 | Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap(); |
| 592 | store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f)); |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 593 | NodeId master; |
| 594 | boolean done; |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 595 | |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 596 | // Processing flow rules |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 597 | for (FlowEntry rule : flowEntries) { |
| 598 | try { |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 599 | FlowEntry storedRule = storedRules.remove(rule); |
| 600 | if (storedRule != null) { |
| 601 | if (storedRule.exactMatch(rule)) { |
| 602 | // we both have the rule, let's update some info then. |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 603 | done = flowAdded(rule); |
| 604 | if (!done) { |
| 605 | // Mastership change can occur during this iteration |
| 606 | master = mastershipService.getMasterFor(deviceId); |
| 607 | if (!Objects.equals(local, master)) { |
| 608 | log.warn("Tried to update the flow stats while the node was not the master"); |
| 609 | return; |
| 610 | } |
| 611 | } |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 612 | } else { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 613 | // Mastership change can occur during this iteration |
| 614 | master = mastershipService.getMasterFor(deviceId); |
| 615 | if (!Objects.equals(local, master)) { |
| 616 | log.warn("Tried to update the flows while the node was not the master"); |
| 617 | return; |
| 618 | } |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 619 | // the two rules are not an exact match - remove the |
| 620 | // switch's rule and install our rule |
| 621 | extraneousFlow(rule); |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 622 | flowMissing(storedRule, false); |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 623 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 624 | } else { |
| 625 | // the device has a rule the store does not have |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 626 | if (!allowExtraneousRules) { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 627 | // Mastership change can occur during this iteration |
| 628 | master = mastershipService.getMasterFor(deviceId); |
| 629 | if (!Objects.equals(local, master)) { |
| 630 | log.warn("Tried to remove flows while the node was not the master"); |
| 631 | return; |
| 632 | } |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 633 | extraneousFlow(rule); |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 634 | } else if (importExtraneousRules) { // Stores the rule, if so is indicated |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 635 | FlowRuleEvent flowRuleEvent = store.addOrUpdateFlowRule(rule); |
| 636 | if (flowRuleEvent == null) { |
| 637 | // Mastership change can occur during this iteration |
| 638 | master = mastershipService.getMasterFor(deviceId); |
| 639 | if (!Objects.equals(local, master)) { |
| 640 | log.warn("Tried to import flows while the node was not the master"); |
| 641 | return; |
| 642 | } |
| 643 | } |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 644 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 645 | } |
Sho SHIMIZU | 24a00d9 | 2015-05-05 11:11:13 -0700 | [diff] [blame] | 646 | } catch (Exception e) { |
Jon Hall | 274cecb | 2017-08-09 12:15:48 -0700 | [diff] [blame] | 647 | log.warn("Can't process added or extra rule {} for device {}:{}", |
| 648 | rule, deviceId, e); |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 649 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 650 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 651 | |
| 652 | // DO NOT reinstall |
| 653 | if (useMissingFlow) { |
| 654 | for (FlowEntry rule : storedRules.keySet()) { |
Pier Luigi Ventre | d8a923c | 2020-02-20 11:25:31 +0000 | [diff] [blame] | 655 | // Mastership change can occur during this iteration |
| 656 | master = mastershipService.getMasterFor(deviceId); |
| 657 | if (!Objects.equals(local, master)) { |
| 658 | log.warn("Tried to install missing rules while the node was not the master"); |
| 659 | return; |
| 660 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 661 | try { |
| 662 | // there are rules in the store that aren't on the switch |
Sivachidambaram Subramanian | 605104e | 2017-06-21 07:40:04 +0530 | [diff] [blame] | 663 | log.debug("Adding the rule that is present in store but not on switch : {}", rule); |
| 664 | flowMissing(rule, true); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 665 | } catch (Exception e) { |
Yuta HIGUCHI | f76f6d5 | 2017-05-15 18:02:09 -0700 | [diff] [blame] | 666 | log.warn("Can't add missing flow rule:", e); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 667 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 668 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 669 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 670 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 671 | |
| 672 | @Override |
| 673 | public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) { |
| 674 | store.batchOperationComplete(FlowRuleBatchEvent.completed( |
| 675 | new FlowRuleBatchRequest(batchId, Collections.emptySet()), |
| 676 | operation |
| 677 | )); |
| 678 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 679 | |
| 680 | @Override |
| 681 | public void pushTableStatistics(DeviceId deviceId, |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 682 | List<TableStatisticsEntry> tableStats) { |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 683 | store.updateTableStatistics(deviceId, tableStats); |
| 684 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 685 | } |
| 686 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 687 | // Store delegate to re-post events emitted from the store. |
| 688 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 689 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 690 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 691 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 692 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 693 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 694 | public void notify(FlowRuleBatchEvent event) { |
| 695 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 696 | switch (event.type()) { |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 697 | case BATCH_OPERATION_REQUESTED: |
| 698 | // Request has been forwarded to MASTER Node, and was |
| 699 | request.ops().forEach( |
| 700 | op -> { |
| 701 | switch (op.operator()) { |
| 702 | case ADD: |
| 703 | post(new FlowRuleEvent(RULE_ADD_REQUESTED, op.target())); |
| 704 | break; |
| 705 | case REMOVE: |
| 706 | post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, op.target())); |
| 707 | break; |
| 708 | case MODIFY: |
| 709 | //TODO: do something here when the time comes. |
| 710 | break; |
| 711 | default: |
| 712 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 713 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 714 | } |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 715 | ); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 716 | |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 717 | DeviceId deviceId = event.deviceId(); |
| 718 | FlowRuleBatchOperation batchOperation = request.asBatchOperation(deviceId); |
| 719 | // getProvider is customized to favor driverProvider |
| 720 | FlowRuleProvider flowRuleProvider = getProvider(deviceId); |
| 721 | if (flowRuleProvider != null) { |
| 722 | log.trace("Sending {} flow rules to {}", batchOperation.size(), deviceId); |
| 723 | flowRuleProvider.executeBatch(batchOperation); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 724 | } |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 725 | |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 726 | break; |
| 727 | |
| 728 | case BATCH_OPERATION_COMPLETED: |
| 729 | // Operation completed, let's retrieve the processor and trigger the callback |
| 730 | FlowOperationsProcessor fops = pendingFlowOperations.remove( |
| 731 | event.subject().batchId()); |
| 732 | if (fops != null) { |
| 733 | if (event.result().isSuccess()) { |
| 734 | fops.satisfy(event.deviceId()); |
| 735 | } else { |
| 736 | fops.fail(event.deviceId(), event.result().failedItems()); |
| 737 | } |
| 738 | } else { |
| 739 | log.warn("Unable to find flow operations processor for batch: {}", event.subject().batchId()); |
| 740 | } |
| 741 | break; |
| 742 | |
| 743 | default: |
| 744 | break; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 745 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 748 | |
Sho SHIMIZU | 5711ce1 | 2016-08-31 13:57:12 -0700 | [diff] [blame] | 749 | private static FlowRuleBatchEntry.FlowRuleOperation mapOperationType(FlowRuleOperation.Type input) { |
| 750 | switch (input) { |
| 751 | case ADD: |
| 752 | return FlowRuleBatchEntry.FlowRuleOperation.ADD; |
| 753 | case MODIFY: |
| 754 | return FlowRuleBatchEntry.FlowRuleOperation.MODIFY; |
| 755 | case REMOVE: |
| 756 | return FlowRuleBatchEntry.FlowRuleOperation.REMOVE; |
| 757 | default: |
| 758 | throw new UnsupportedOperationException("Unknown flow rule type " + input); |
| 759 | } |
| 760 | } |
| 761 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 762 | private class FlowOperationsProcessor implements Runnable { |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 763 | // Immutable |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 764 | private final FlowRuleOperations fops; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 765 | |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 766 | // Mutable |
| 767 | private final List<Set<FlowRuleOperation>> stages; |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 768 | private final Set<DeviceId> pendingDevices = new HashSet<>(); |
Sho SHIMIZU | 5f70942 | 2016-09-07 09:54:46 -0700 | [diff] [blame] | 769 | private boolean hasFailed = false; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 770 | |
Sho SHIMIZU | 7c9b73a | 2016-08-30 14:08:28 -0700 | [diff] [blame] | 771 | FlowOperationsProcessor(FlowRuleOperations ops) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 772 | this.stages = Lists.newArrayList(ops.stages()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 773 | this.fops = ops; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | @Override |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 777 | public synchronized void run() { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 778 | if (!stages.isEmpty()) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 779 | process(stages.remove(0)); |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 780 | } else if (!hasFailed) { |
Sho SHIMIZU | c9e4bb0 | 2016-09-01 12:43:39 -0700 | [diff] [blame] | 781 | fops.callback().onSuccess(fops); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 785 | private void process(Set<FlowRuleOperation> ops) { |
Sho SHIMIZU | 8efc896 | 2016-08-31 15:17:44 -0700 | [diff] [blame] | 786 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = ArrayListMultimap.create(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 787 | |
Sho SHIMIZU | f4fd3de | 2016-08-31 15:47:56 -0700 | [diff] [blame] | 788 | for (FlowRuleOperation op : ops) { |
| 789 | perDeviceBatches.put(op.rule().deviceId(), |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 790 | new FlowRuleBatchEntry(mapOperationType(op.type()), op.rule())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 791 | } |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 792 | pendingDevices.addAll(perDeviceBatches.keySet()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 793 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 794 | for (DeviceId deviceId : perDeviceBatches.keySet()) { |
Sho SHIMIZU | 3a70431 | 2015-05-27 13:36:01 -0700 | [diff] [blame] | 795 | long id = idGenerator.getNewId(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 796 | final FlowRuleBatchOperation b = new FlowRuleBatchOperation(perDeviceBatches.get(deviceId), |
Ivan Eroshkin | 3f56eb1 | 2019-11-25 10:38:56 +0100 | [diff] [blame] | 797 | deviceId, id); |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 798 | pendingFlowOperations.put(id, this); |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 799 | deviceInstallers.execute(() -> store.storeBatch(b)); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 803 | synchronized void satisfy(DeviceId devId) { |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 804 | pendingDevices.remove(devId); |
| 805 | if (pendingDevices.isEmpty()) { |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 806 | operationsService.execute(this); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 807 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 808 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 809 | |
Sho SHIMIZU | 2d7c539 | 2016-08-30 14:14:39 -0700 | [diff] [blame] | 810 | synchronized void fail(DeviceId devId, Set<? extends FlowRule> failures) { |
Sho SHIMIZU | b9e0ab7 | 2016-09-14 16:54:13 -0700 | [diff] [blame] | 811 | hasFailed = true; |
Brian O'Connor | c9b64dc | 2016-09-13 23:01:07 +0000 | [diff] [blame] | 812 | pendingDevices.remove(devId); |
| 813 | if (pendingDevices.isEmpty()) { |
Sho SHIMIZU | c0bfe7c | 2016-09-14 16:54:30 -0700 | [diff] [blame] | 814 | operationsService.execute(this); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 817 | FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder(); |
| 818 | failures.forEach(failedOpsBuilder::add); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 819 | |
Sho SHIMIZU | ad4f2cd | 2016-09-01 13:05:56 -0700 | [diff] [blame] | 820 | fops.callback().onError(failedOpsBuilder.build()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 821 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 822 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 823 | |
| 824 | @Override |
| 825 | public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) { |
| 826 | checkPermission(FLOWRULE_READ); |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 827 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 828 | return store.getTableStatistics(deviceId); |
| 829 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 830 | |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 831 | @Override |
| 832 | public long getActiveFlowRuleCount(DeviceId deviceId) { |
Thomas Vachuska | a8e7477 | 2018-02-26 11:33:35 -0800 | [diff] [blame] | 833 | checkNotNull(deviceId, DEVICE_ID_NULL); |
Patryk Konopka | 7e40c01 | 2017-06-06 13:38:06 +0200 | [diff] [blame] | 834 | return store.getActiveFlowRuleCount(deviceId); |
| 835 | } |
| 836 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 837 | private class InternalDeviceListener implements DeviceListener { |
| 838 | @Override |
| 839 | public void event(DeviceEvent event) { |
| 840 | switch (event.type()) { |
| 841 | case DEVICE_REMOVED: |
| 842 | case DEVICE_AVAILABILITY_CHANGED: |
| 843 | DeviceId deviceId = event.subject().id(); |
| 844 | if (!deviceService.isAvailable(deviceId)) { |
Andrea Campanella | 32a9c0b | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 845 | BasicDeviceConfig cfg = netCfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 846 | //if purgeOnDisconnection is set for the device or it's a global configuration |
| 847 | // lets remove the flows. Priority is given to the per device flag |
| 848 | boolean purge = cfg != null && cfg.isPurgeOnDisconnectionConfigured() ? |
| 849 | cfg.purgeOnDisconnection() : purgeOnDisconnection; |
| 850 | if (purge) { |
| 851 | log.info("PurgeOnDisconnection is requested for device {}, " + |
| 852 | "removing flows", deviceId); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 853 | store.purgeFlowRule(deviceId); |
| 854 | } |
| 855 | } |
| 856 | break; |
| 857 | default: |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 862 | } |