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