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