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