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 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame^] | 316 | final Map<FlowEntry, Long> firstSeen = Maps.newConcurrentMap(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 317 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 318 | |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame^] | 319 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 320 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 321 | super(provider); |
| 322 | } |
| 323 | |
| 324 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 325 | public void flowRemoved(FlowEntry flowEntry) { |
| 326 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 327 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 328 | lastSeen.remove(flowEntry); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame^] | 329 | firstSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 330 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 331 | if (stored == null) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 332 | log.debug("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 333 | return; |
| 334 | } |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame^] | 335 | if (flowEntry.reason() == FlowEntry.FlowRemoveReason.HARD_TIMEOUT) { |
| 336 | ((DefaultFlowEntry) stored).setState(FlowEntry.FlowEntryState.REMOVED); |
| 337 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 338 | Device device = deviceService.getDevice(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 339 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 340 | FlowRuleEvent event = null; |
| 341 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 342 | case ADDED: |
| 343 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 344 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 345 | break; |
| 346 | case PENDING_REMOVE: |
| 347 | case REMOVED: |
| 348 | event = store.removeFlowRule(stored); |
| 349 | break; |
| 350 | default: |
| 351 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 352 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 353 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 354 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 355 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 356 | post(event); |
| 357 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 358 | } |
| 359 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 360 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 361 | private void flowMissing(FlowEntry flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 362 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 363 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 364 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 365 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 366 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 367 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 368 | case PENDING_REMOVE: |
| 369 | case REMOVED: |
| 370 | event = store.removeFlowRule(flowRule); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 371 | break; |
| 372 | case ADDED: |
| 373 | case PENDING_ADD: |
Charles Chan | 93fa727 | 2016-01-26 22:27:02 -0800 | [diff] [blame] | 374 | event = store.pendingFlowRule(flowRule); |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 375 | try { |
| 376 | frp.applyFlowRule(flowRule); |
| 377 | } catch (UnsupportedOperationException e) { |
| 378 | log.warn(e.getMessage()); |
| 379 | if (flowRule instanceof DefaultFlowEntry) { |
Brian O'Connor | a3e5cd5 | 2015-12-05 15:59:19 -0800 | [diff] [blame] | 380 | //FIXME modification of "stored" flow entry outside of store |
Charles M.C. Chan | 1229eca | 2015-05-18 06:27:52 +0800 | [diff] [blame] | 381 | ((DefaultFlowEntry) flowRule).setState(FlowEntry.FlowEntryState.FAILED); |
| 382 | } |
| 383 | } |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 384 | break; |
| 385 | default: |
| 386 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 387 | } |
| 388 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 389 | if (event != null) { |
| 390 | log.debug("Flow {} removed", flowRule); |
| 391 | post(event); |
| 392 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 393 | } |
| 394 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 395 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 396 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 397 | checkValidity(); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 398 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 399 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 400 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 401 | } |
| 402 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 403 | private void flowAdded(FlowEntry flowEntry) { |
| 404 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 405 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 406 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 407 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 408 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 409 | if (event == null) { |
| 410 | log.debug("No flow store event generated."); |
| 411 | } else { |
Jonathan Hart | 58682dd | 2014-11-24 20:11:16 -0800 | [diff] [blame] | 412 | log.trace("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 413 | post(event); |
| 414 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 415 | } else { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 416 | log.debug("Removing flow rules...."); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 417 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 418 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 419 | } |
| 420 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 421 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 422 | if (storedRule == null) { |
| 423 | return false; |
| 424 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 425 | if (storedRule.isPermanent()) { |
| 426 | return true; |
| 427 | } |
| 428 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 429 | final long timeout = storedRule.timeout() * 1000; |
| 430 | final long currentTime = System.currentTimeMillis(); |
Murat Parlakisik | c6759e8 | 2016-06-29 03:22:22 -0700 | [diff] [blame^] | 431 | |
| 432 | // Checking flow with hardTimeout |
| 433 | if (storedRule.hardTimeout() != 0) { |
| 434 | if (!firstSeen.containsKey(storedRule)) { |
| 435 | // First time rule adding |
| 436 | firstSeen.put(storedRule, currentTime); |
| 437 | } else { |
| 438 | Long first = firstSeen.get(storedRule); |
| 439 | final long hardTimeout = storedRule.hardTimeout() * 1000; |
| 440 | if ((currentTime - first) > hardTimeout) { |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 446 | if (storedRule.packets() != swRule.packets()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 447 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 448 | return true; |
| 449 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 450 | if (!lastSeen.containsKey(storedRule)) { |
| 451 | // checking for the first time |
| 452 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 453 | // Use following if lastSeen attr. was removed. |
| 454 | //lastSeen.put(storedRule, currentTime); |
| 455 | } |
| 456 | Long last = lastSeen.get(storedRule); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 457 | |
Thomas Vachuska | c4ee737 | 2016-02-02 16:10:09 -0800 | [diff] [blame] | 458 | // concurrently removed? let the liveness check fail |
| 459 | return last != null && (currentTime - last) <= timeout; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 460 | } |
| 461 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 462 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 463 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 464 | pushFlowMetricsInternal(deviceId, flowEntries, true); |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 469 | pushFlowMetricsInternal(deviceId, flowEntries, false); |
| 470 | } |
| 471 | |
| 472 | private void pushFlowMetricsInternal(DeviceId deviceId, Iterable<FlowEntry> flowEntries, |
| 473 | boolean useMissingFlow) { |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 474 | Map<FlowEntry, FlowEntry> storedRules = Maps.newHashMap(); |
| 475 | store.getFlowEntries(deviceId).forEach(f -> storedRules.put(f, f)); |
| 476 | |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 477 | for (FlowEntry rule : flowEntries) { |
| 478 | try { |
Jonathan Hart | f44e42c | 2015-08-04 09:58:46 -0700 | [diff] [blame] | 479 | FlowEntry storedRule = storedRules.remove(rule); |
| 480 | if (storedRule != null) { |
| 481 | if (storedRule.exactMatch(rule)) { |
| 482 | // we both have the rule, let's update some info then. |
| 483 | flowAdded(rule); |
| 484 | } else { |
| 485 | // the two rules are not an exact match - remove the |
| 486 | // switch's rule and install our rule |
| 487 | extraneousFlow(rule); |
| 488 | flowMissing(storedRule); |
| 489 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 490 | } else { |
| 491 | // the device has a rule the store does not have |
Marc De Leenheer | de47caa | 2015-04-24 11:27:44 -0700 | [diff] [blame] | 492 | if (!allowExtraneousRules) { |
| 493 | extraneousFlow(rule); |
| 494 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 495 | } |
Sho SHIMIZU | 24a00d9 | 2015-05-05 11:11:13 -0700 | [diff] [blame] | 496 | } catch (Exception e) { |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 497 | log.debug("Can't process added or extra rule {}", e.getMessage()); |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 498 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 499 | } |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 500 | |
| 501 | // DO NOT reinstall |
| 502 | if (useMissingFlow) { |
| 503 | for (FlowEntry rule : storedRules.keySet()) { |
| 504 | try { |
| 505 | // there are rules in the store that aren't on the switch |
| 506 | log.debug("Adding rule in store, but not on switch {}", rule); |
| 507 | flowMissing(rule); |
| 508 | } catch (Exception e) { |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame] | 509 | log.debug("Can't add missing flow rule:", e); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 510 | } |
alshabib | 93cb57f | 2015-02-12 17:43:26 -0800 | [diff] [blame] | 511 | } |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 512 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 513 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 514 | |
| 515 | @Override |
| 516 | public void batchOperationCompleted(long batchId, CompletedBatchOperation operation) { |
| 517 | store.batchOperationComplete(FlowRuleBatchEvent.completed( |
| 518 | new FlowRuleBatchRequest(batchId, Collections.emptySet()), |
| 519 | operation |
| 520 | )); |
| 521 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 522 | |
| 523 | @Override |
| 524 | public void pushTableStatistics(DeviceId deviceId, |
| 525 | List<TableStatisticsEntry> tableStats) { |
| 526 | store.updateTableStatistics(deviceId, tableStats); |
| 527 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 528 | } |
| 529 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 530 | // Store delegate to re-post events emitted from the store. |
| 531 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 532 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 533 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 534 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 535 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 536 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 537 | public void notify(FlowRuleBatchEvent event) { |
| 538 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 539 | switch (event.type()) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 540 | case BATCH_OPERATION_REQUESTED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 541 | // Request has been forwarded to MASTER Node, and was |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 542 | request.ops().forEach( |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 543 | op -> { |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 544 | switch (op.operator()) { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 545 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 546 | case ADD: |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 547 | post(new FlowRuleEvent(RULE_ADD_REQUESTED, |
| 548 | op.target())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 549 | break; |
| 550 | case REMOVE: |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 551 | post(new FlowRuleEvent(RULE_REMOVE_REQUESTED, |
| 552 | op.target())); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 553 | break; |
| 554 | case MODIFY: |
| 555 | //TODO: do something here when the time comes. |
| 556 | break; |
| 557 | default: |
Ray Milkey | f7329c7 | 2015-02-17 11:37:01 -0800 | [diff] [blame] | 558 | log.warn("Unknown flow operation operator: {}", op.operator()); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | ); |
| 562 | |
| 563 | DeviceId deviceId = event.deviceId(); |
| 564 | |
| 565 | FlowRuleBatchOperation batchOperation = |
| 566 | request.asBatchOperation(deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 567 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 568 | FlowRuleProvider flowRuleProvider = getProvider(deviceId); |
| 569 | if (flowRuleProvider != null) { |
| 570 | flowRuleProvider.executeBatch(batchOperation); |
| 571 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 572 | |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 573 | break; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 574 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 575 | case BATCH_OPERATION_COMPLETED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 576 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 577 | FlowOperationsProcessor fops = pendingFlowOperations.remove( |
| 578 | event.subject().batchId()); |
| 579 | if (event.result().isSuccess()) { |
| 580 | if (fops != null) { |
| 581 | fops.satisfy(event.deviceId()); |
| 582 | } |
| 583 | } else { |
| 584 | fops.fail(event.deviceId(), event.result().failedItems()); |
| 585 | } |
| 586 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 587 | break; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 588 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 589 | default: |
| 590 | break; |
| 591 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 594 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 595 | private class FlowOperationsProcessor implements Runnable { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 596 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 597 | private final List<Set<FlowRuleOperation>> stages; |
| 598 | private final FlowRuleOperationsContext context; |
| 599 | private final FlowRuleOperations fops; |
| 600 | private final AtomicBoolean hasFailed = new AtomicBoolean(false); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 601 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 602 | private Set<DeviceId> pendingDevices; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 603 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 604 | public FlowOperationsProcessor(FlowRuleOperations ops) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 605 | this.stages = Lists.newArrayList(ops.stages()); |
| 606 | this.context = ops.callback(); |
| 607 | this.fops = ops; |
| 608 | pendingDevices = Sets.newConcurrentHashSet(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | @Override |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 612 | public void run() { |
| 613 | if (stages.size() > 0) { |
| 614 | process(stages.remove(0)); |
| 615 | } else if (!hasFailed.get() && context != null) { |
| 616 | context.onSuccess(fops); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 620 | private void process(Set<FlowRuleOperation> ops) { |
| 621 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = |
| 622 | ArrayListMultimap.create(); |
| 623 | |
| 624 | FlowRuleBatchEntry fbe; |
| 625 | for (FlowRuleOperation flowRuleOperation : ops) { |
| 626 | switch (flowRuleOperation.type()) { |
| 627 | // FIXME: Brian needs imagination when creating class names. |
| 628 | case ADD: |
| 629 | fbe = new FlowRuleBatchEntry( |
| 630 | FlowRuleBatchEntry.FlowRuleOperation.ADD, flowRuleOperation.rule()); |
| 631 | break; |
| 632 | case MODIFY: |
| 633 | fbe = new FlowRuleBatchEntry( |
| 634 | FlowRuleBatchEntry.FlowRuleOperation.MODIFY, flowRuleOperation.rule()); |
| 635 | break; |
| 636 | case REMOVE: |
| 637 | fbe = new FlowRuleBatchEntry( |
| 638 | FlowRuleBatchEntry.FlowRuleOperation.REMOVE, flowRuleOperation.rule()); |
| 639 | break; |
| 640 | default: |
| 641 | throw new UnsupportedOperationException("Unknown flow rule type " + flowRuleOperation.type()); |
| 642 | } |
| 643 | pendingDevices.add(flowRuleOperation.rule().deviceId()); |
| 644 | perDeviceBatches.put(flowRuleOperation.rule().deviceId(), fbe); |
| 645 | } |
| 646 | |
| 647 | |
| 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); |
| 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 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 657 | public void satisfy(DeviceId devId) { |
| 658 | pendingDevices.remove(devId); |
| 659 | if (pendingDevices.isEmpty()) { |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -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 | |
| 664 | |
| 665 | |
| 666 | public void fail(DeviceId devId, Set<? extends FlowRule> failures) { |
| 667 | hasFailed.set(true); |
| 668 | pendingDevices.remove(devId); |
| 669 | if (pendingDevices.isEmpty()) { |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 670 | operationsService.execute(this); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | if (context != null) { |
| 674 | final FlowRuleOperations.Builder failedOpsBuilder = |
| 675 | FlowRuleOperations.builder(); |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 676 | failures.forEach(failedOpsBuilder::add); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 677 | |
| 678 | context.onError(failedOpsBuilder.build()); |
| 679 | } |
| 680 | } |
| 681 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 682 | } |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 683 | |
| 684 | @Override |
| 685 | public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) { |
| 686 | checkPermission(FLOWRULE_READ); |
| 687 | return store.getTableStatistics(deviceId); |
| 688 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 689 | |
| 690 | private class InternalDeviceListener implements DeviceListener { |
| 691 | @Override |
| 692 | public void event(DeviceEvent event) { |
| 693 | switch (event.type()) { |
| 694 | case DEVICE_REMOVED: |
| 695 | case DEVICE_AVAILABILITY_CHANGED: |
| 696 | DeviceId deviceId = event.subject().id(); |
| 697 | if (!deviceService.isAvailable(deviceId)) { |
| 698 | if (purgeOnDisconnection) { |
| 699 | store.purgeFlowRule(deviceId); |
| 700 | } |
| 701 | } |
| 702 | break; |
| 703 | default: |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 708 | } |