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