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 | */ |
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 | |
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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.event.AbstractListenerRegistry; |
| 33 | import org.onosproject.event.EventDeliveryService; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 38 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 39 | import org.onosproject.net.flow.FlowEntry; |
| 40 | import org.onosproject.net.flow.FlowRule; |
| 41 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 42 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 43 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 44 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 45 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 46 | import org.onosproject.net.flow.FlowRuleEvent; |
| 47 | import org.onosproject.net.flow.FlowRuleListener; |
| 48 | import org.onosproject.net.flow.FlowRuleProvider; |
| 49 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 50 | import org.onosproject.net.flow.FlowRuleProviderService; |
| 51 | import org.onosproject.net.flow.FlowRuleService; |
| 52 | import org.onosproject.net.flow.FlowRuleStore; |
| 53 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 54 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 55 | import org.onosproject.net.provider.AbstractProviderService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 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; |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 70 | import static com.google.common.base.Preconditions.checkNotNull; |
| 71 | import static org.onlab.util.Tools.namedThreads; |
| 72 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 73 | |
tom | e472987 | 2014-09-23 00:37:37 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Provides implementation of the flow NB & SB APIs. |
| 76 | */ |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 77 | @Component(immediate = true) |
| 78 | @Service |
tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 79 | public class FlowRuleManager |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 80 | extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> |
| 81 | implements FlowRuleService, FlowRuleProviderRegistry { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 82 | |
Sho SHIMIZU | 183b12fd | 2015-01-20 14:56:39 -0800 | [diff] [blame] | 83 | enum BatchState { STARTED, FINISHED, CANCELLED } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 84 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 85 | public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 86 | private final Logger log = getLogger(getClass()); |
| 87 | |
| 88 | private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 89 | listenerRegistry = new AbstractListenerRegistry<>(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 90 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 91 | private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 92 | |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 93 | private ExecutorService futureService; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 94 | |
tom | be98831 | 2014-09-19 18:38:47 -0700 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected FlowRuleStore store; |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 97 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 99 | protected EventDeliveryService eventDispatcher; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | b55524f | 2014-09-18 09:59:24 -0700 | [diff] [blame] | 102 | protected DeviceService deviceService; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 103 | |
| 104 | @Activate |
| 105 | public void activate() { |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 106 | futureService = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 107 | Executors.newFixedThreadPool(32, namedThreads("onos-provider-future-listeners-%d")); |
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) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 235 | log.debug("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 { |
Jonathan Hart | 58682dd | 2014-11-24 20:11:16 -0800 | [diff] [blame] | 308 | log.trace("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) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 359 | Set<FlowEntry> storedRules = Sets.newHashSet(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 | |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 381 | // FIXME set appropriate default and make it configurable |
| 382 | private static final int TIMEOUT_PER_OP = 500; // ms |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 383 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 384 | // TODO: Right now we only dispatch events at individual flowEntry level. |
| 385 | // It may be more efficient for also dispatch events as a batch. |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 386 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 387 | public void notify(FlowRuleBatchEvent event) { |
| 388 | final FlowRuleBatchRequest request = event.subject(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 389 | switch (event.type()) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 390 | case BATCH_OPERATION_REQUESTED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 391 | // Request has been forwarded to MASTER Node, and was |
| 392 | for (FlowRule entry : request.toAdd()) { |
Madan Jampani | 31961c1 | 2014-10-23 12:06:58 -0700 | [diff] [blame] | 393 | eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, entry)); |
| 394 | } |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 395 | for (FlowRule entry : request.toRemove()) { |
Madan Jampani | 31961c1 | 2014-10-23 12:06:58 -0700 | [diff] [blame] | 396 | eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVE_REQUESTED, entry)); |
| 397 | } |
| 398 | // FIXME: what about op.equals(FlowRuleOperation.MODIFY) ? |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 399 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 400 | FlowRuleBatchOperation batchOperation = request.asBatchOperation(); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 401 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 402 | FlowRuleProvider flowRuleProvider = |
| 403 | getProvider(batchOperation.getOperations().get(0).getTarget().deviceId()); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 404 | final Future<CompletedBatchOperation> result = |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 405 | flowRuleProvider.executeBatch(batchOperation); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 406 | futureService.submit(new Runnable() { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 407 | @Override |
| 408 | public void run() { |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 409 | CompletedBatchOperation res; |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 410 | try { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 411 | res = result.get(TIMEOUT_PER_OP * batchOperation.size(), TimeUnit.MILLISECONDS); |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 412 | store.batchOperationComplete(FlowRuleBatchEvent.completed(request, res)); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 413 | } catch (TimeoutException | InterruptedException | ExecutionException e) { |
| 414 | log.warn("Something went wrong with the batch operation {}", |
Yuta HIGUCHI | f1ccee8 | 2014-11-11 20:39:58 -0800 | [diff] [blame] | 415 | request.batchId(), e); |
| 416 | |
| 417 | Set<FlowRule> failures = new HashSet<>(batchOperation.size()); |
| 418 | for (FlowRuleBatchEntry op : batchOperation.getOperations()) { |
| 419 | failures.add(op.getTarget()); |
| 420 | } |
| 421 | res = new CompletedBatchOperation(false, failures); |
| 422 | store.batchOperationComplete(FlowRuleBatchEvent.completed(request, res)); |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 423 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 424 | } |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 425 | }); |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 426 | break; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 427 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 428 | case BATCH_OPERATION_COMPLETED: |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 429 | // MASTER Node has pushed the batch down to the Device |
| 430 | |
| 431 | // Note: RULE_ADDED will be posted |
| 432 | // when Flow was actually confirmed by stats reply. |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 433 | break; |
Yuta HIGUCHI | 2fcb40c | 2014-11-03 14:39:10 -0800 | [diff] [blame] | 434 | |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 435 | default: |
| 436 | break; |
| 437 | } |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 440 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 441 | private class FlowRuleBatchFuture implements Future<CompletedBatchOperation> { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 442 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 443 | private final List<Future<CompletedBatchOperation>> futures; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 444 | private final Multimap<DeviceId, FlowRuleBatchEntry> batches; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 445 | private final AtomicReference<BatchState> state; |
| 446 | private CompletedBatchOperation overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 447 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 448 | public FlowRuleBatchFuture(List<Future<CompletedBatchOperation>> futures, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 449 | Multimap<DeviceId, FlowRuleBatchEntry> batches) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 450 | this.futures = futures; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 451 | this.batches = batches; |
Sho SHIMIZU | d58c5b7 | 2015-01-20 19:25:03 -0800 | [diff] [blame] | 452 | this.state = new AtomicReference<>(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 | } |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 460 | if (log.isDebugEnabled()) { |
| 461 | log.debug("Cancelling FlowRuleBatchFuture", |
| 462 | new RuntimeException("Just printing backtrace")); |
| 463 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 464 | if (!state.compareAndSet(BatchState.STARTED, BatchState.CANCELLED)) { |
| 465 | return false; |
| 466 | } |
| 467 | cleanUpBatch(); |
| 468 | for (Future<CompletedBatchOperation> f : futures) { |
| 469 | f.cancel(mayInterruptIfRunning); |
| 470 | } |
| 471 | return true; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public boolean isCancelled() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 476 | return state.get() == BatchState.CANCELLED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public boolean isDone() { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 481 | return state.get() == BatchState.FINISHED; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 482 | } |
| 483 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 484 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 485 | @Override |
| 486 | public CompletedBatchOperation get() throws InterruptedException, |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 487 | ExecutionException { |
| 488 | |
| 489 | if (isDone()) { |
| 490 | return overall; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 491 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 492 | |
| 493 | boolean success = true; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 494 | Set<FlowRule> failed = Sets.newHashSet(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 495 | Set<Long> failedIds = Sets.newHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 496 | CompletedBatchOperation completed; |
| 497 | for (Future<CompletedBatchOperation> future : futures) { |
| 498 | completed = future.get(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 499 | success = validateBatchOperation(failed, failedIds, completed); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 502 | return finalizeBatchOperation(success, failed, failedIds); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 503 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public CompletedBatchOperation get(long timeout, TimeUnit unit) |
| 508 | throws InterruptedException, ExecutionException, |
| 509 | TimeoutException { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 510 | |
| 511 | if (isDone()) { |
| 512 | return overall; |
| 513 | } |
| 514 | boolean success = true; |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 515 | Set<FlowRule> failed = Sets.newHashSet(); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 516 | Set<Long> failedIds = Sets.newHashSet(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 517 | CompletedBatchOperation completed; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 518 | for (Future<CompletedBatchOperation> future : futures) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 519 | completed = future.get(timeout, unit); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 520 | success = validateBatchOperation(failed, failedIds, completed); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 521 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 522 | return finalizeBatchOperation(success, failed, failedIds); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Yuta HIGUCHI | 4b52444 | 2014-10-28 22:23:57 -0700 | [diff] [blame] | 525 | private boolean validateBatchOperation(Set<FlowRule> failed, |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 526 | Set<Long> failedIds, |
| 527 | CompletedBatchOperation completed) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 528 | |
| 529 | if (isCancelled()) { |
| 530 | throw new CancellationException(); |
| 531 | } |
| 532 | if (!completed.isSuccess()) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 533 | log.warn("FlowRuleBatch failed: {}", completed); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 534 | failed.addAll(completed.failedItems()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 535 | failedIds.addAll(completed.failedIds()); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 536 | cleanUpBatch(); |
| 537 | cancelAllSubBatches(); |
| 538 | return false; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | private void cancelAllSubBatches() { |
| 544 | for (Future<CompletedBatchOperation> f : futures) { |
| 545 | f.cancel(true); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | private CompletedBatchOperation finalizeBatchOperation(boolean success, |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 550 | Set<FlowRule> failed, |
| 551 | Set<Long> failedIds) { |
alshabib | 2683458 | 2014-10-08 20:15:46 -0700 | [diff] [blame] | 552 | synchronized (this) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 553 | if (!state.compareAndSet(BatchState.STARTED, BatchState.FINISHED)) { |
| 554 | if (state.get() == BatchState.FINISHED) { |
| 555 | return overall; |
| 556 | } |
| 557 | throw new CancellationException(); |
| 558 | } |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 559 | overall = new CompletedBatchOperation(success, failed, failedIds); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 560 | return overall; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | private void cleanUpBatch() { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 565 | log.debug("cleaning up batch"); |
| 566 | // TODO convert these into a batch? |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 567 | for (FlowRuleBatchEntry fbe : batches.values()) { |
| 568 | if (fbe.getOperator() == FlowRuleOperation.ADD || |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 569 | fbe.getOperator() == FlowRuleOperation.MODIFY) { |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 570 | store.deleteFlowRule(fbe.getTarget()); |
| 571 | } else if (fbe.getOperator() == FlowRuleOperation.REMOVE) { |
alshabib | cf36991 | 2014-10-13 14:16:42 -0700 | [diff] [blame] | 572 | store.removeFlowRule(new DefaultFlowEntry(fbe.getTarget())); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 573 | store.storeFlowRule(fbe.getTarget()); |
| 574 | } |
| 575 | } |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 576 | } |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 577 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 578 | } |