Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.flow.impl; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -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; |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 24 | |
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; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 31 | import org.onlab.onos.core.ApplicationId; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 32 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 33 | import org.onlab.onos.event.EventDeliveryService; |
| 34 | import org.onlab.onos.net.Device; |
| 35 | import org.onlab.onos.net.DeviceId; |
| 36 | import org.onlab.onos.net.device.DeviceService; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.flow.FlowEntry; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.flow.FlowRule; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 44 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.flow.FlowRuleEvent; |
| 47 | import org.onlab.onos.net.flow.FlowRuleListener; |
| 48 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 49 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 50 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 51 | import org.onlab.onos.net.flow.FlowRuleService; |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.flow.FlowRuleStore; |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 53 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 54 | import org.onlab.onos.net.provider.AbstractProviderRegistry; |
| 55 | import org.onlab.onos.net.provider.AbstractProviderService; |
| 56 | import org.slf4j.Logger; |
| 57 | |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 58 | import java.util.HashSet; |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 59 | import java.util.List; |
| 60 | import java.util.Map; |
| 61 | import java.util.Set; |
| 62 | import java.util.concurrent.CancellationException; |
| 63 | import java.util.concurrent.ExecutionException; |
| 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Executors; |
| 66 | import java.util.concurrent.Future; |
| 67 | import java.util.concurrent.TimeUnit; |
| 68 | import java.util.concurrent.TimeoutException; |
| 69 | import java.util.concurrent.atomic.AtomicReference; |
| 70 | |
| 71 | import static com.google.common.base.Preconditions.checkNotNull; |
| 72 | import static org.onlab.util.Tools.namedThreads; |
| 73 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 74 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Provides implementation of the flow NB & SB APIs. |
| 77 | */ |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 78 | @Component(immediate = true) |
| 79 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 80 | public class FlowRuleManager |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 81 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 82 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 83 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 84 | enum BatchState { STARTED, FINISHED, CANCELLED }; |
| 85 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 86 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 87 | private final Logger log = getLogger(getClass()); |
| 88 | |
| 89 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 90 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 91 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 92 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 93 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 94 | private ExecutorService futureService; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 95 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 98 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 100 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 101 | |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 103 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 104 | |
| 105 | @Activate |
| 106 | public void activate() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 107 | futureService = Executors.newCachedThreadPool(namedThreads("provider-future-listeners")); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 108 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 109 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 110 | log.info("Started"); |
| 111 | } |
| 112 | |
| 113 | @Deactivate |
| 114 | public void deactivate() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 115 | futureService.shutdownNow(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 116 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 117 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 118 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 119 | log.info("Stopped"); |
| 120 | } |
| 121 | |
| 122 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 123 | public int getFlowRuleCount() { |
| 124 | return store.getFlowRuleCount(); |
| 125 | } |
| 126 | |
| 127 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 128 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 129 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 133 | public void applyFlowRules(FlowRule... flowRules) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 134 | Set<FlowRuleBatchEntry> toAddBatchEntries = Sets.newHashSet(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 135 | for (int i = 0; i < flowRules.length; i++) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 136 | toAddBatchEntries.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, flowRules[i])); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 137 | } |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 138 | applyBatch(new FlowRuleBatchOperation(toAddBatchEntries)); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void removeFlowRules(FlowRule... flowRules) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 143 | Set<FlowRuleBatchEntry> toRemoveBatchEntries = Sets.newHashSet(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 144 | for (int i = 0; i < flowRules.length; i++) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 145 | toRemoveBatchEntries.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, flowRules[i])); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 146 | } |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 147 | applyBatch(new FlowRuleBatchOperation(toRemoveBatchEntries)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 148 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 149 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public void removeFlowRulesById(ApplicationId id) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 152 | removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 157 | Set<FlowRule> flowEntries = Sets.newHashSet(); |
| 158 | for (Device d : deviceService.getDevices()) { |
| 159 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 160 | if (flowEntry.appId() == id.id()) { |
| 161 | flowEntries.add(flowEntry); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | return flowEntries; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame^] | 169 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
| 170 | Set<FlowRule> matches = Sets.newHashSet(); |
| 171 | long toLookUp = ((long) appId.id() << 16) | groupId; |
| 172 | for (Device d : deviceService.getDevices()) { |
| 173 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 174 | if ((flowEntry.id().value() >>> 32) == toLookUp) { |
| 175 | matches.add(flowEntry); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | return matches; |
| 180 | } |
| 181 | |
| 182 | @Override |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 183 | public Future<CompletedBatchOperation> applyBatch( |
| 184 | FlowRuleBatchOperation batch) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 185 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 186 | ArrayListMultimap.create(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 187 | List<Future<CompletedBatchOperation>> futures = Lists.newArrayList(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 188 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 189 | final FlowRule f = fbe.getTarget(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 190 | perDeviceBatches.put(f.deviceId(), fbe); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 191 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 192 | |
| 193 | for (DeviceId deviceId : perDeviceBatches.keySet()) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 194 | FlowRuleBatchOperation b = |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 195 | new FlowRuleBatchOperation(perDeviceBatches.get(deviceId)); |
| 196 | Future<CompletedBatchOperation> future = store.storeBatch(b); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 197 | futures.add(future); |
| 198 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 199 | return new FlowRuleBatchFuture(futures, perDeviceBatches); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 203 | public void addListener(FlowRuleListener listener) { |
| 204 | listenerRegistry.addListener(listener); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void removeListener(FlowRuleListener listener) { |
| 209 | listenerRegistry.removeListener(listener); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected FlowRuleProviderService createProviderService( |
| 214 | FlowRuleProvider provider) { |
| 215 | return new InternalFlowRuleProviderService(provider); |
| 216 | } |
| 217 | |
| 218 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 219 | extends AbstractProviderService<FlowRuleProvider> |
| 220 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 221 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 222 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 223 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 224 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 225 | super(provider); |
| 226 | } |
| 227 | |
| 228 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 229 | public void flowRemoved(FlowEntry flowEntry) { |
| 230 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 231 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 232 | lastSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 233 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 234 | if (stored == null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 235 | log.info("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 236 | return; |
| 237 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 238 | Device device = deviceService.getDevice(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 239 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 240 | FlowRuleEvent event = null; |
| 241 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 242 | case ADDED: |
| 243 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 244 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 245 | break; |
| 246 | case PENDING_REMOVE: |
| 247 | case REMOVED: |
| 248 | event = store.removeFlowRule(stored); |
| 249 | break; |
| 250 | default: |
| 251 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 252 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 253 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 254 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 255 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 256 | post(event); |
| 257 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 258 | } |
| 259 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 260 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 261 | private void flowMissing(FlowEntry flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 262 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 263 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 264 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 265 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 266 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 267 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 268 | case PENDING_REMOVE: |
| 269 | case REMOVED: |
| 270 | event = store.removeFlowRule(flowRule); |
| 271 | frp.removeFlowRule(flowRule); |
| 272 | break; |
| 273 | case ADDED: |
| 274 | case PENDING_ADD: |
| 275 | frp.applyFlowRule(flowRule); |
| 276 | break; |
| 277 | default: |
| 278 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 279 | } |
| 280 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 281 | if (event != null) { |
| 282 | log.debug("Flow {} removed", flowRule); |
| 283 | post(event); |
| 284 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 285 | |
| 286 | } |
| 287 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 288 | |
| 289 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 290 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 291 | checkValidity(); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 292 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 293 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 294 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 295 | } |
| 296 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 297 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 298 | private void flowAdded(FlowEntry flowEntry) { |
| 299 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 300 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 301 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 302 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 303 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 304 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 305 | if (event == null) { |
| 306 | log.debug("No flow store event generated."); |
| 307 | } else { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 308 | log.debug("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 309 | post(event); |
| 310 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 311 | } else { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 312 | log.debug("Removing flow rules...."); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 313 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 314 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 315 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 316 | } |
| 317 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 318 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 319 | if (storedRule == null) { |
| 320 | return false; |
| 321 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 322 | if (storedRule.isPermanent()) { |
| 323 | return true; |
| 324 | } |
| 325 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 326 | final long timeout = storedRule.timeout() * 1000; |
| 327 | final long currentTime = System.currentTimeMillis(); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 328 | if (storedRule.packets() != swRule.packets()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 329 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 330 | return true; |
| 331 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 332 | if (!lastSeen.containsKey(storedRule)) { |
| 333 | // checking for the first time |
| 334 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 335 | // Use following if lastSeen attr. was removed. |
| 336 | //lastSeen.put(storedRule, currentTime); |
| 337 | } |
| 338 | Long last = lastSeen.get(storedRule); |
| 339 | if (last == null) { |
| 340 | // concurrently removed? let the liveness check fail |
| 341 | return false; |
| 342 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 343 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 344 | if ((currentTime - last) <= timeout) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 350 | // Posts the specified event to the local event dispatcher. |
| 351 | private void post(FlowRuleEvent event) { |
| 352 | if (event != null) { |
| 353 | eventDispatcher.post(event); |
| 354 | } |
| 355 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 356 | |
| 357 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 358 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 359 | List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 360 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 361 | for (FlowEntry rule : flowEntries) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 362 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 363 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 364 | flowAdded(rule); |
| 365 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 366 | // the device has a rule the store does not have |
| 367 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 370 | for (FlowEntry rule : storedRules) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 371 | // there are rules in the store that aren't on the switch |
| 372 | flowMissing(rule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 373 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 374 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 375 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 376 | } |
| 377 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 378 | // Store delegate to re-post events emitted from the store. |
| 379 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 380 | |
| 381 | private static final int TIMEOUT = 5000; // ms |
| 382 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 383 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 384 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 385 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 386 | public void notify(FlowRuleBatchEvent event) { |
| 387 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 388 | switch (event.type()) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 389 | case BATCH_OPERATION_REQUESTED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 390 | // Request has been forwarded to MASTER Node, and was |
| 391 | for (FlowRule entry : request.toAdd()) { |
Madan Jampani | 31961c1 | 2014-10-23 12:06:58 -0700 | [diff] [blame] | 392 | eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, entry)); |
| 393 | } |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 394 | for (FlowRule entry : request.toRemove()) { |
Madan Jampani | 31961c1 | 2014-10-23 12:06:58 -0700 | [diff] [blame] | 395 | eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVE_REQUESTED, entry)); |
| 396 | } |
| 397 | // FIXME: what about op.equals(FlowRuleOperation.MODIFY) ? |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 398 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 399 | FlowRuleBatchOperation batchOperation = request.asBatchOperation(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 400 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 401 | FlowRuleProvider flowRuleProvider = |
| 402 | getProvider(batchOperation.getOperations().get(0).getTarget().deviceId()); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 403 | final Future<CompletedBatchOperation> result = |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 404 | flowRuleProvider.executeBatch(batchOperation); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 405 | futureService.submit(new Runnable() { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 406 | @Override |
| 407 | public void run() { |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 408 | CompletedBatchOperation res; |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 409 | try { |
| 410 | res = result.get(TIMEOUT, TimeUnit.MILLISECONDS); |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 411 | store.batchOperationComplete(FlowRuleBatchEvent.completed(request, res)); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 412 | } catch (TimeoutException | InterruptedException | ExecutionException e) { |
| 413 | log.warn("Something went wrong with the batch operation {}", |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 414 | request.batchId(), e); |
| 415 | |
| 416 | Set<FlowRule> failures = new HashSet<>(batchOperation.size()); |
| 417 | for (FlowRuleBatchEntry op : batchOperation.getOperations()) { |
| 418 | failures.add(op.getTarget()); |
| 419 | } |
| 420 | res = new CompletedBatchOperation(false, failures); |
| 421 | store.batchOperationComplete(FlowRuleBatchEvent.completed(request, res)); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 422 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 423 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 424 | }); |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 425 | break; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 426 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 427 | case BATCH_OPERATION_COMPLETED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 428 | // MASTER Node has pushed the batch down to the Device |
| 429 | |
| 430 | // Note: RULE_ADDED will be posted |
| 431 | // when Flow was actually confirmed by stats reply. |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 432 | break; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 433 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 434 | default: |
| 435 | break; |
| 436 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 439 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 440 | private class FlowRuleBatchFuture implements Future<CompletedBatchOperation> { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 441 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 442 | private final List<Future<CompletedBatchOperation>> futures; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 443 | private final Multimap<DeviceId, FlowRuleBatchEntry> batches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 444 | private final AtomicReference<BatchState> state; |
| 445 | private CompletedBatchOperation overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 446 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 447 | public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 448 | Multimap<DeviceId, FlowRuleBatchEntry> batches) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 449 | this.futures = futures; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 450 | this.batches = batches; |
| 451 | state = new AtomicReference<FlowRuleManager.BatchState>(); |
| 452 | state.set(BatchState.STARTED); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public boolean cancel(boolean mayInterruptIfRunning) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 457 | if (state.get() == BatchState.FINISHED) { |
| 458 | return false; |
| 459 | } |
| 460 | if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) { |
| 461 | return false; |
| 462 | } |
| 463 | cleanUpBatch(); |
| 464 | for (Future<CompletedBatchOperation> f : futures) { |
| 465 | f.cancel(mayInterruptIfRunning); |
| 466 | } |
| 467 | return true; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 472 | return state.get() == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public boolean isDone() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 477 | return state.get() == BatchState.FINISHED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 478 | } |
| 479 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 480 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 481 | @Override |
| 482 | public CompletedBatchOperation get() throws InterruptedException, |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 483 | ExecutionException { |
| 484 | |
| 485 | if (isDone()) { |
| 486 | return overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 487 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 488 | |
| 489 | boolean success = true; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 490 | Set<FlowRule> failed = Sets.newHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 491 | CompletedBatchOperation completed; |
| 492 | for (Future<CompletedBatchOperation> future : futures) { |
| 493 | completed = future.get(); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 494 | success = validateBatchOperation(failed, completed); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | return finalizeBatchOperation(success, failed); |
| 498 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 503 | throws InterruptedException, ExecutionException, |
| 504 | TimeoutException { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 505 | |
| 506 | if (isDone()) { |
| 507 | return overall; |
| 508 | } |
| 509 | boolean success = true; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 510 | Set<FlowRule> failed = Sets.newHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 511 | CompletedBatchOperation completed; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 512 | long start = System.nanoTime(); |
| 513 | long end = start + unit.toNanos(timeout); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 514 | |
| 515 | for (Future<CompletedBatchOperation> future : futures) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 516 | long now = System.nanoTime(); |
| 517 | long thisTimeout = end - now; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 518 | completed = future.get(thisTimeout, TimeUnit.NANOSECONDS); |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 519 | success = validateBatchOperation(failed, completed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 520 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 521 | return finalizeBatchOperation(success, failed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 524 | private boolean validateBatchOperation(Set<FlowRule> failed, |
alshabib | 3effd04 | 2014-10-17 12:00:31 -0700 | [diff] [blame] | 525 | CompletedBatchOperation completed) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 526 | |
| 527 | if (isCancelled()) { |
| 528 | throw new CancellationException(); |
| 529 | } |
| 530 | if (!completed.isSuccess()) { |
| 531 | failed.addAll(completed.failedItems()); |
| 532 | cleanUpBatch(); |
| 533 | cancelAllSubBatches(); |
| 534 | return false; |
| 535 | } |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | private void cancelAllSubBatches() { |
| 540 | for (Future<CompletedBatchOperation> f : futures) { |
| 541 | f.cancel(true); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | private CompletedBatchOperation finalizeBatchOperation(boolean success, |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 546 | Set<FlowRule> failed) { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 547 | synchronized (this) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 548 | if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) { |
| 549 | if (state.get() == BatchState.FINISHED) { |
| 550 | return overall; |
| 551 | } |
| 552 | throw new CancellationException(); |
| 553 | } |
| 554 | overall = new CompletedBatchOperation(success, failed); |
| 555 | return overall; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | private void cleanUpBatch() { |
| 560 | for (FlowRuleBatchEntry fbe : batches.values()) { |
| 561 | if (fbe.getOperator() == FlowRuleOperation.ADD || |
| 562 | fbe.getOperator() == FlowRuleOperation.MODIFY) { |
| 563 | store.deleteFlowRule(fbe.getTarget()); |
| 564 | } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 565 | store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget())); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 566 | store.storeFlowRule(fbe.getTarget()); |
| 567 | } |
| 568 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 569 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 570 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 571 | } |