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