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; |
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 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 83 | enum BatchState { STARTED, FINISHED, CANCELLED }; |
| 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() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 106 | futureService = Executors.newCachedThreadPool(namedThreads("provider-future-listeners")); |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 107 | store.setDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 108 | eventDispatcher.addSink(FlowRuleEvent.class, listenerRegistry); |
| 109 | log.info("Started"); |
| 110 | } |
| 111 | |
| 112 | @Deactivate |
| 113 | public void deactivate() { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 114 | futureService.shutdownNow(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 115 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 116 | store.unsetDelegate(delegate); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 117 | eventDispatcher.removeSink(FlowRuleEvent.class); |
| 118 | log.info("Stopped"); |
| 119 | } |
| 120 | |
| 121 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 122 | public int getFlowRuleCount() { |
| 123 | return store.getFlowRuleCount(); |
| 124 | } |
| 125 | |
| 126 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 127 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 128 | return store.getFlowEntries(deviceId); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 132 | public void applyFlowRules(FlowRule... flowRules) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 133 | Set<FlowRuleBatchEntry> toAddBatchEntries = Sets.newHashSet(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 134 | for (int i = 0; i < flowRules.length; i++) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 135 | toAddBatchEntries.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, flowRules[i])); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 136 | } |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 137 | applyBatch(new FlowRuleBatchOperation(toAddBatchEntries)); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void removeFlowRules(FlowRule... flowRules) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 142 | Set<FlowRuleBatchEntry> toRemoveBatchEntries = Sets.newHashSet(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 143 | for (int i = 0; i < flowRules.length; i++) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 144 | toRemoveBatchEntries.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, flowRules[i])); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 145 | } |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 146 | applyBatch(new FlowRuleBatchOperation(toRemoveBatchEntries)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 147 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 148 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public void removeFlowRulesById(ApplicationId id) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 151 | removeFlowRules(Iterables.toArray(getFlowRulesById(id), FlowRule.class)); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Iterable<FlowRule> getFlowRulesById(ApplicationId id) { |
Madan Jampani | 6a45616 | 2014-10-24 11:36:17 -0700 | [diff] [blame] | 156 | Set<FlowRule> flowEntries = Sets.newHashSet(); |
| 157 | for (Device d : deviceService.getDevices()) { |
| 158 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 159 | if (flowEntry.appId() == id.id()) { |
| 160 | flowEntries.add(flowEntry); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return flowEntries; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
alshabib | aa7e7de | 2014-11-12 19:20:44 -0800 | [diff] [blame] | 168 | public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) { |
| 169 | Set<FlowRule> matches = Sets.newHashSet(); |
| 170 | long toLookUp = ((long) appId.id() << 16) | groupId; |
| 171 | for (Device d : deviceService.getDevices()) { |
| 172 | for (FlowEntry flowEntry : store.getFlowEntries(d.id())) { |
| 173 | if ((flowEntry.id().value() >>> 32) == toLookUp) { |
| 174 | matches.add(flowEntry); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | return matches; |
| 179 | } |
| 180 | |
| 181 | @Override |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 182 | public Future<CompletedBatchOperation> applyBatch( |
| 183 | FlowRuleBatchOperation batch) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 184 | Multimap<DeviceId, FlowRuleBatchEntry> perDeviceBatches = |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 185 | ArrayListMultimap.create(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 186 | List<Future<CompletedBatchOperation>> futures = Lists.newArrayList(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 187 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 188 | final FlowRule f = fbe.getTarget(); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 189 | perDeviceBatches.put(f.deviceId(), fbe); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 190 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 191 | |
| 192 | for (DeviceId deviceId : perDeviceBatches.keySet()) { |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 193 | FlowRuleBatchOperation b = |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 194 | new FlowRuleBatchOperation(perDeviceBatches.get(deviceId)); |
| 195 | Future<CompletedBatchOperation> future = store.storeBatch(b); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 196 | futures.add(future); |
| 197 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 198 | return new FlowRuleBatchFuture(futures, perDeviceBatches); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 202 | public void addListener(FlowRuleListener listener) { |
| 203 | listenerRegistry.addListener(listener); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void removeListener(FlowRuleListener listener) { |
| 208 | listenerRegistry.removeListener(listener); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | protected FlowRuleProviderService createProviderService( |
| 213 | FlowRuleProvider provider) { |
| 214 | return new InternalFlowRuleProviderService(provider); |
| 215 | } |
| 216 | |
| 217 | private class InternalFlowRuleProviderService |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 218 | extends AbstractProviderService<FlowRuleProvider> |
| 219 | implements FlowRuleProviderService { |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 220 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 221 | final Map<FlowEntry, Long> lastSeen = Maps.newConcurrentMap(); |
| 222 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 223 | protected InternalFlowRuleProviderService(FlowRuleProvider provider) { |
| 224 | super(provider); |
| 225 | } |
| 226 | |
| 227 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 228 | public void flowRemoved(FlowEntry flowEntry) { |
| 229 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 230 | checkValidity(); |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 231 | lastSeen.remove(flowEntry); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 232 | FlowEntry stored = store.getFlowEntry(flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 233 | if (stored == null) { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 234 | log.debug("Rule already evicted from store: {}", flowEntry); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 235 | return; |
| 236 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 237 | Device device = deviceService.getDevice(flowEntry.deviceId()); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 238 | FlowRuleProvider frp = getProvider(device.providerId()); |
| 239 | FlowRuleEvent event = null; |
| 240 | switch (stored.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 241 | case ADDED: |
| 242 | case PENDING_ADD: |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 243 | frp.applyFlowRule(stored); |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 244 | break; |
| 245 | case PENDING_REMOVE: |
| 246 | case REMOVED: |
| 247 | event = store.removeFlowRule(stored); |
| 248 | break; |
| 249 | default: |
| 250 | break; |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 251 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 252 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 253 | if (event != null) { |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 254 | log.debug("Flow {} removed", flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 255 | post(event); |
| 256 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 257 | } |
| 258 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 259 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 260 | private void flowMissing(FlowEntry flowRule) { |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 261 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 262 | checkValidity(); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 263 | Device device = deviceService.getDevice(flowRule.deviceId()); |
| 264 | FlowRuleProvider frp = getProvider(device.providerId()); |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 265 | FlowRuleEvent event = null; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 266 | switch (flowRule.state()) { |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 267 | case PENDING_REMOVE: |
| 268 | case REMOVED: |
| 269 | event = store.removeFlowRule(flowRule); |
| 270 | frp.removeFlowRule(flowRule); |
| 271 | break; |
| 272 | case ADDED: |
| 273 | case PENDING_ADD: |
| 274 | frp.applyFlowRule(flowRule); |
| 275 | break; |
| 276 | default: |
| 277 | log.debug("Flow {} has not been installed.", flowRule); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 278 | } |
| 279 | |
alshabib | bb42cad | 2014-09-25 11:43:05 -0700 | [diff] [blame] | 280 | if (event != null) { |
| 281 | log.debug("Flow {} removed", flowRule); |
| 282 | post(event); |
| 283 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 284 | |
| 285 | } |
| 286 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 287 | |
| 288 | private void extraneousFlow(FlowRule flowRule) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 289 | checkNotNull(flowRule, FLOW_RULE_NULL); |
| 290 | checkValidity(); |
alshabib | 2374fc9 | 2014-10-22 11:03:23 -0700 | [diff] [blame] | 291 | FlowRuleProvider frp = getProvider(flowRule.deviceId()); |
| 292 | frp.removeFlowRule(flowRule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 293 | log.debug("Flow {} is on switch but not in store.", flowRule); |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 294 | } |
| 295 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 296 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 297 | private void flowAdded(FlowEntry flowEntry) { |
| 298 | checkNotNull(flowEntry, FLOW_RULE_NULL); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 299 | checkValidity(); |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 300 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 301 | if (checkRuleLiveness(flowEntry, store.getFlowEntry(flowEntry))) { |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 302 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 303 | FlowRuleEvent event = store.addOrUpdateFlowRule(flowEntry); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 304 | if (event == null) { |
| 305 | log.debug("No flow store event generated."); |
| 306 | } else { |
Jonathan Hart | 58682dd | 2014-11-24 20:11:16 -0800 | [diff] [blame] | 307 | log.trace("Flow {} {}", flowEntry, event.type()); |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 308 | post(event); |
| 309 | } |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 310 | } else { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 311 | log.debug("Removing flow rules...."); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 312 | removeFlowRules(flowEntry); |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 313 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 314 | |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 315 | } |
| 316 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 317 | private boolean checkRuleLiveness(FlowEntry swRule, FlowEntry storedRule) { |
| 318 | if (storedRule == null) { |
| 319 | return false; |
| 320 | } |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 321 | if (storedRule.isPermanent()) { |
| 322 | return true; |
| 323 | } |
| 324 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 325 | final long timeout = storedRule.timeout() * 1000; |
| 326 | final long currentTime = System.currentTimeMillis(); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 327 | if (storedRule.packets() != swRule.packets()) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 328 | lastSeen.put(storedRule, currentTime); |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 329 | return true; |
| 330 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 331 | if (!lastSeen.containsKey(storedRule)) { |
| 332 | // checking for the first time |
| 333 | lastSeen.put(storedRule, storedRule.lastSeen()); |
| 334 | // Use following if lastSeen attr. was removed. |
| 335 | //lastSeen.put(storedRule, currentTime); |
| 336 | } |
| 337 | Long last = lastSeen.get(storedRule); |
| 338 | if (last == null) { |
| 339 | // concurrently removed? let the liveness check fail |
| 340 | return false; |
| 341 | } |
alshabib | 85c4197 | 2014-10-03 13:48:39 -0700 | [diff] [blame] | 342 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 343 | if ((currentTime - last) <= timeout) { |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | return false; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Ayaka Koshibe | 08eabaa | 2014-09-17 14:59:25 -0700 | [diff] [blame] | 349 | // Posts the specified event to the local event dispatcher. |
| 350 | private void post(FlowRuleEvent event) { |
| 351 | if (event != null) { |
| 352 | eventDispatcher.post(event); |
| 353 | } |
| 354 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 355 | |
| 356 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 357 | public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowEntry> flowEntries) { |
| 358 | List<FlowEntry> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); |
alshabib | bb8b128 | 2014-09-22 17:00:18 -0700 | [diff] [blame] | 359 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 360 | for (FlowEntry rule : flowEntries) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 361 | if (storedRules.remove(rule)) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 362 | // we both have the rule, let's update some info then. |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 363 | flowAdded(rule); |
| 364 | } else { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 365 | // the device has a rule the store does not have |
| 366 | extraneousFlow(rule); |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 369 | for (FlowEntry rule : storedRules) { |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 370 | // there are rules in the store that aren't on the switch |
| 371 | flowMissing(rule); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame] | 372 | |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 373 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 374 | } |
alshabib | 57044ba | 2014-09-16 15:58:01 -0700 | [diff] [blame] | 375 | } |
| 376 | |
tom | c78acee | 2014-09-24 15:16:55 -0700 | [diff] [blame] | 377 | // Store delegate to re-post events emitted from the store. |
| 378 | private class InternalStoreDelegate implements FlowRuleStoreDelegate { |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 379 | |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 380 | // FIXME set appropriate default and make it configurable |
| 381 | private static final int TIMEOUT_PER_OP = 500; // ms |
Thomas Vachuska | 9b2da21 | 2014-11-10 19:30:25 -0800 | [diff] [blame] | 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 { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 410 | res = result.get(TIMEOUT_PER_OP * batchOperation.size(), 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 | } |
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 | } |