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