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 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.flow.impl; |
| 17 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 19 | import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 20 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 21 | import static org.onlab.onos.store.flow.impl.FlowStoreMessageSubjects.*; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 22 | import static org.onlab.util.Tools.namedThreads; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 23 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 24 | import java.io.IOException; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 27 | import java.util.Collection; |
| 28 | import java.util.Collections; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 29 | import java.util.Map.Entry; |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 30 | import java.util.Set; |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 31 | import java.util.concurrent.ExecutionException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 32 | import java.util.concurrent.ExecutorService; |
| 33 | import java.util.concurrent.Executors; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 34 | import java.util.concurrent.Future; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 35 | import java.util.concurrent.TimeUnit; |
| 36 | import java.util.concurrent.TimeoutException; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 37 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 38 | import java.util.List; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 39 | |
| 40 | import org.apache.felix.scr.annotations.Activate; |
| 41 | import org.apache.felix.scr.annotations.Component; |
| 42 | import org.apache.felix.scr.annotations.Deactivate; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 43 | import org.apache.felix.scr.annotations.Reference; |
| 44 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 45 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 46 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 47 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.Device; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 49 | import org.onlab.onos.net.DeviceId; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 50 | import org.onlab.onos.net.device.DeviceService; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 51 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.flow.DefaultFlowEntry; |
| 53 | import org.onlab.onos.net.flow.FlowEntry; |
| 54 | import org.onlab.onos.net.flow.FlowEntry.FlowEntryState; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 55 | import org.onlab.onos.net.flow.FlowId; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 56 | import org.onlab.onos.net.flow.FlowRule; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 57 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 58 | import org.onlab.onos.net.flow.FlowRuleBatchEvent; |
| 59 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 60 | import org.onlab.onos.net.flow.FlowRuleBatchRequest; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 61 | import org.onlab.onos.net.flow.FlowRuleEvent; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 62 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 63 | import org.onlab.onos.net.flow.FlowRuleEvent.Type; |
| 64 | import org.onlab.onos.net.flow.FlowRuleStore; |
| 65 | import org.onlab.onos.net.flow.FlowRuleStoreDelegate; |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 66 | import org.onlab.onos.net.flow.StoredFlowEntry; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 67 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
| 68 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 69 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 70 | import org.onlab.onos.store.flow.ReplicaInfo; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 71 | import org.onlab.onos.store.flow.ReplicaInfoEvent; |
| 72 | import org.onlab.onos.store.flow.ReplicaInfoEventListener; |
Yuta HIGUCHI | fe280eb | 2014-10-17 12:10:43 -0700 | [diff] [blame] | 73 | import org.onlab.onos.store.flow.ReplicaInfoService; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 74 | import org.onlab.onos.store.hz.AbstractHazelcastStore; |
| 75 | import org.onlab.onos.store.hz.SMap; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 76 | import org.onlab.onos.store.serializers.DistributedStoreSerializers; |
| 77 | import org.onlab.onos.store.serializers.KryoSerializer; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 78 | import org.onlab.util.KryoNamespace; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 79 | import org.slf4j.Logger; |
| 80 | |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 81 | import com.google.common.base.Function; |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 82 | import com.google.common.cache.Cache; |
| 83 | import com.google.common.cache.CacheBuilder; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 84 | import com.google.common.cache.CacheLoader; |
| 85 | import com.google.common.cache.LoadingCache; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 86 | import com.google.common.collect.ArrayListMultimap; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 87 | import com.google.common.collect.ImmutableList; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 88 | import com.google.common.collect.ImmutableSet; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 89 | import com.google.common.collect.Iterables; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 90 | import com.google.common.collect.Multimap; |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 91 | import com.google.common.util.concurrent.Futures; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 92 | import com.google.common.util.concurrent.ListenableFuture; |
| 93 | import com.google.common.util.concurrent.SettableFuture; |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 94 | import com.hazelcast.core.IMap; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 97 | * Manages inventory of flow rules using a distributed state management protocol. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 98 | */ |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 99 | @Component(immediate = true) |
| 100 | @Service |
| 101 | public class DistributedFlowRuleStore |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 102 | extends AbstractHazelcastStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 103 | implements FlowRuleStore { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 104 | |
| 105 | private final Logger log = getLogger(getClass()); |
| 106 | |
| 107 | // store entries as a pile of rules, no info about device tables |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 108 | private final Multimap<DeviceId, StoredFlowEntry> flowEntries = |
| 109 | ArrayListMultimap.<DeviceId, StoredFlowEntry>create(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 110 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 111 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 113 | protected ReplicaInfoService replicaInfoManager; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 116 | protected ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 119 | protected ClusterService clusterService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected DeviceService deviceService; |
| 123 | |
| 124 | private final AtomicInteger localBatchIdGen = new AtomicInteger(); |
| 125 | |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 126 | // TODO: make this configurable |
| 127 | private int pendingFutureTimeoutMinutes = 5; |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 128 | |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 129 | private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures = |
| 130 | CacheBuilder.newBuilder() |
| 131 | .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES) |
| 132 | // TODO Explicitly fail the future if expired? |
| 133 | //.removalListener(listener) |
| 134 | .build(); |
| 135 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 136 | private LoadingCache<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> smaps; |
| 137 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 138 | |
| 139 | private final ExecutorService futureListeners = |
| 140 | Executors.newCachedThreadPool(namedThreads("flowstore-peer-responders")); |
| 141 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 142 | private final ExecutorService backupExecutors = |
| 143 | Executors.newSingleThreadExecutor(namedThreads("async-backups")); |
| 144 | |
| 145 | // TODO make this configurable |
| 146 | private boolean syncBackup = false; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 147 | |
| 148 | protected static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 149 | @Override |
| 150 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 151 | serializerPool = KryoNamespace.newBuilder() |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 152 | .register(DistributedStoreSerializers.COMMON) |
| 153 | .build() |
| 154 | .populate(1); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | // TODO: make this configurable |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 159 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 160 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 161 | private ReplicaInfoEventListener replicaInfoEventListener; |
| 162 | |
| 163 | @Override |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 164 | @Activate |
| 165 | public void activate() { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 166 | |
| 167 | super.serializer = SERIALIZER; |
| 168 | super.theInstance = storeService.getHazelcastInstance(); |
| 169 | |
| 170 | // Cache to create SMap on demand |
| 171 | smaps = CacheBuilder.newBuilder() |
| 172 | .softValues() |
| 173 | .build(new SMapLoader()); |
| 174 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 175 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new ClusterMessageHandler() { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 176 | |
| 177 | @Override |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 178 | public void handle(final ClusterMessage message) { |
| 179 | FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload()); |
| 180 | log.info("received batch request {}", operation); |
| 181 | final ListenableFuture<CompletedBatchOperation> f = storeBatchInternal(operation); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 182 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 183 | f.addListener(new Runnable() { |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 184 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public void run() { |
| 187 | CompletedBatchOperation result = Futures.getUnchecked(f); |
| 188 | try { |
| 189 | message.respond(SERIALIZER.encode(result)); |
| 190 | } catch (IOException e) { |
| 191 | log.error("Failed to respond back", e); |
| 192 | } |
| 193 | } |
| 194 | }, futureListeners); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 195 | } |
| 196 | }); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 197 | |
| 198 | clusterCommunicator.addSubscriber(GET_FLOW_ENTRY, new ClusterMessageHandler() { |
| 199 | |
| 200 | @Override |
| 201 | public void handle(ClusterMessage message) { |
| 202 | FlowRule rule = SERIALIZER.decode(message.payload()); |
| 203 | log.info("received get flow entry request for {}", rule); |
| 204 | FlowEntry flowEntry = getFlowEntryInternal(rule); |
| 205 | try { |
| 206 | message.respond(SERIALIZER.encode(flowEntry)); |
| 207 | } catch (IOException e) { |
| 208 | log.error("Failed to respond back", e); |
| 209 | } |
| 210 | } |
| 211 | }); |
| 212 | |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 213 | clusterCommunicator.addSubscriber(GET_DEVICE_FLOW_ENTRIES, new ClusterMessageHandler() { |
| 214 | |
| 215 | @Override |
| 216 | public void handle(ClusterMessage message) { |
| 217 | DeviceId deviceId = SERIALIZER.decode(message.payload()); |
| 218 | log.info("Received get flow entries request for {} from {}", deviceId, message.sender()); |
| 219 | Set<FlowEntry> flowEntries = getFlowEntriesInternal(deviceId); |
| 220 | try { |
| 221 | message.respond(SERIALIZER.encode(flowEntries)); |
| 222 | } catch (IOException e) { |
| 223 | log.error("Failed to respond to peer's getFlowEntries request", e); |
| 224 | } |
| 225 | } |
| 226 | }); |
| 227 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 228 | replicaInfoEventListener = new InternalReplicaInfoEventListener(); |
| 229 | |
| 230 | replicaInfoManager.addListener(replicaInfoEventListener); |
| 231 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 232 | log.info("Started"); |
| 233 | } |
| 234 | |
| 235 | @Deactivate |
| 236 | public void deactivate() { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 237 | replicaInfoManager.removeListener(replicaInfoEventListener); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 238 | log.info("Stopped"); |
| 239 | } |
| 240 | |
| 241 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 242 | // TODO: This is not a efficient operation on a distributed sharded |
| 243 | // flow store. We need to revisit the need for this operation or at least |
| 244 | // make it device specific. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 245 | @Override |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 246 | public int getFlowRuleCount() { |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 247 | // implementing in-efficient operation for debugging purpose. |
| 248 | int sum = 0; |
| 249 | for (Device device : deviceService.getDevices()) { |
| 250 | final DeviceId did = device.id(); |
| 251 | sum += Iterables.size(getFlowEntries(did)); |
| 252 | } |
| 253 | return sum; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 257 | public synchronized FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 258 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
| 259 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 260 | return getFlowEntryInternal(rule); |
| 261 | } |
| 262 | |
| 263 | log.info("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
| 264 | replicaInfo.master().orNull(), rule.deviceId()); |
| 265 | |
| 266 | ClusterMessage message = new ClusterMessage( |
| 267 | clusterService.getLocalNode().id(), |
| 268 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 269 | SERIALIZER.encode(rule)); |
| 270 | |
| 271 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 272 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 273 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 274 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 275 | // FIXME: throw a FlowStoreException |
| 276 | throw new RuntimeException(e); |
| 277 | } |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private synchronized StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
| 281 | for (StoredFlowEntry f : flowEntries.get(rule.deviceId())) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 282 | if (f.equals(rule)) { |
| 283 | return f; |
| 284 | } |
| 285 | } |
| 286 | return null; |
| 287 | } |
| 288 | |
| 289 | @Override |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 290 | public synchronized Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 291 | |
| 292 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 293 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 294 | return getFlowEntriesInternal(deviceId); |
| 295 | } |
| 296 | |
| 297 | log.info("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
| 298 | replicaInfo.master().orNull(), deviceId); |
| 299 | |
| 300 | ClusterMessage message = new ClusterMessage( |
| 301 | clusterService.getLocalNode().id(), |
| 302 | GET_DEVICE_FLOW_ENTRIES, |
| 303 | SERIALIZER.encode(deviceId)); |
| 304 | |
| 305 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 306 | Future<byte[]> responseFuture = clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 307 | return SERIALIZER.decode(responseFuture.get(FLOW_RULE_STORE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); |
| 308 | } catch (IOException | TimeoutException | ExecutionException | InterruptedException e) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 309 | // FIXME: throw a FlowStoreException |
| 310 | throw new RuntimeException(e); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 315 | Collection<? extends FlowEntry> rules = flowEntries.get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 316 | if (rules == null) { |
Madan Jampani | f5fdef0 | 2014-10-23 21:58:10 -0700 | [diff] [blame] | 317 | return Collections.emptySet(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 318 | } |
| 319 | return ImmutableSet.copyOf(rules); |
| 320 | } |
| 321 | |
| 322 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 323 | public void storeFlowRule(FlowRule rule) { |
| 324 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)))); |
| 325 | } |
| 326 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 327 | // FIXME document that all of the FlowEntries must be about same device |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 328 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 329 | public Future<CompletedBatchOperation> storeBatch(FlowRuleBatchOperation operation) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 330 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 331 | if (operation.getOperations().isEmpty()) { |
| 332 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 333 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 334 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 335 | DeviceId deviceId = operation.getOperations().get(0).getTarget().deviceId(); |
| 336 | |
| 337 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(deviceId); |
| 338 | |
| 339 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
| 340 | return storeBatchInternal(operation); |
| 341 | } |
| 342 | |
| 343 | log.info("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 344 | replicaInfo.master().orNull(), deviceId); |
Yuta HIGUCHI | f3d51bd | 2014-10-21 01:05:33 -0700 | [diff] [blame] | 345 | |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 346 | ClusterMessage message = new ClusterMessage( |
| 347 | clusterService.getLocalNode().id(), |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 348 | APPLY_BATCH_FLOWS, |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 349 | SERIALIZER.encode(operation)); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 350 | |
| 351 | try { |
Madan Jampani | 24f9efb | 2014-10-24 18:56:23 -0700 | [diff] [blame] | 352 | ListenableFuture<byte[]> responseFuture = |
| 353 | clusterCommunicator.sendAndReceive(message, replicaInfo.master().get()); |
| 354 | return Futures.transform(responseFuture, new Function<byte[], CompletedBatchOperation>() { |
| 355 | @Override |
| 356 | public CompletedBatchOperation apply(byte[] input) { |
| 357 | return SERIALIZER.decode(input); |
| 358 | } |
| 359 | }); |
| 360 | } catch (IOException e) { |
| 361 | return Futures.immediateFailedFuture(e); |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 365 | private ListenableFuture<CompletedBatchOperation> storeBatchInternal(FlowRuleBatchOperation operation) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 366 | final List<StoredFlowEntry> toRemove = new ArrayList<>(); |
| 367 | final List<StoredFlowEntry> toAdd = new ArrayList<>(); |
| 368 | DeviceId did = null; |
| 369 | |
| 370 | |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 371 | for (FlowRuleBatchEntry batchEntry : operation.getOperations()) { |
| 372 | FlowRule flowRule = batchEntry.getTarget(); |
| 373 | FlowRuleOperation op = batchEntry.getOperator(); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 374 | if (did == null) { |
| 375 | did = flowRule.deviceId(); |
| 376 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 377 | if (op.equals(FlowRuleOperation.REMOVE)) { |
| 378 | StoredFlowEntry entry = getFlowEntryInternal(flowRule); |
| 379 | if (entry != null) { |
| 380 | entry.setState(FlowEntryState.PENDING_REMOVE); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 381 | toRemove.add(entry); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 382 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 383 | } else if (op.equals(FlowRuleOperation.ADD)) { |
| 384 | StoredFlowEntry flowEntry = new DefaultFlowEntry(flowRule); |
| 385 | DeviceId deviceId = flowRule.deviceId(); |
| 386 | if (!flowEntries.containsEntry(deviceId, flowEntry)) { |
| 387 | flowEntries.put(deviceId, flowEntry); |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 388 | toAdd.add(flowEntry); |
| 389 | } |
| 390 | } |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 391 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 392 | if (toAdd.isEmpty() && toRemove.isEmpty()) { |
| 393 | return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); |
| 394 | } |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 395 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 396 | // create remote backup copies |
| 397 | final DeviceId deviceId = did; |
| 398 | updateBackup(deviceId, toAdd, toRemove); |
| 399 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 400 | SettableFuture<CompletedBatchOperation> r = SettableFuture.create(); |
| 401 | final int batchId = localBatchIdGen.incrementAndGet(); |
| 402 | |
| 403 | pendingFutures.put(batchId, r); |
| 404 | notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove))); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 405 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 406 | return r; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 409 | private void updateBackup(final DeviceId deviceId, |
| 410 | final List<StoredFlowEntry> toAdd, |
| 411 | final List<? extends FlowRule> list) { |
| 412 | |
| 413 | Future<?> submit = backupExecutors.submit(new UpdateBackup(deviceId, toAdd, list)); |
| 414 | |
| 415 | if (syncBackup) { |
| 416 | // wait for backup to complete |
| 417 | try { |
| 418 | submit.get(); |
| 419 | } catch (InterruptedException | ExecutionException e) { |
| 420 | log.error("Failed to create backups", e); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | private void updateBackup(DeviceId deviceId, List<StoredFlowEntry> toAdd) { |
| 426 | updateBackup(deviceId, toAdd, Collections.<FlowEntry>emptyList()); |
| 427 | } |
| 428 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 429 | @Override |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 430 | public void deleteFlowRule(FlowRule rule) { |
| 431 | storeBatch(new FlowRuleBatchOperation(Arrays.asList(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)))); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 435 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
| 436 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 437 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 438 | return addOrUpdateFlowRuleInternal(rule); |
| 439 | } |
| 440 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 441 | log.error("Tried to update FlowRule {} state," |
| 442 | + " while the Node was not the master.", rule); |
| 443 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | private synchronized FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 447 | final DeviceId did = rule.deviceId(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 448 | |
| 449 | // check if this new rule is an update to an existing entry |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 450 | StoredFlowEntry stored = getFlowEntryInternal(rule); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 451 | if (stored != null) { |
| 452 | stored.setBytes(rule.bytes()); |
| 453 | stored.setLife(rule.life()); |
| 454 | stored.setPackets(rule.packets()); |
| 455 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 456 | stored.setState(FlowEntryState.ADDED); |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 457 | // update backup. |
| 458 | updateBackup(did, Arrays.asList(stored)); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 459 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 460 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 461 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 462 | } |
| 463 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 464 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 465 | // TODO: also update backup. |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 466 | flowEntries.put(did, new DefaultFlowEntry(rule)); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 467 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 468 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 472 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 473 | ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(rule.deviceId()); |
Yuta HIGUCHI | 0858b35 | 2014-10-17 11:56:06 -0700 | [diff] [blame] | 474 | if (replicaInfo.master().get().equals(clusterService.getLocalNode().id())) { |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 475 | // bypass and handle it locally |
| 476 | return removeFlowRuleInternal(rule); |
| 477 | } |
| 478 | |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 479 | log.error("Tried to remove FlowRule {}," |
| 480 | + " while the Node was not the master.", rule); |
| 481 | return null; |
Madan Jampani | 38b250d | 2014-10-17 11:02:38 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | private synchronized FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 485 | final DeviceId deviceId = rule.deviceId(); |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 486 | // This is where one could mark a rule as removed and still keep it in the store. |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 487 | final boolean removed = flowEntries.remove(deviceId, rule); |
| 488 | updateBackup(deviceId, Collections.<StoredFlowEntry>emptyList(), Arrays.asList(rule)); |
| 489 | if (removed) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 490 | return new FlowRuleEvent(RULE_REMOVED, rule); |
| 491 | } else { |
| 492 | return null; |
| 493 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 494 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 495 | |
| 496 | @Override |
| 497 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 498 | final Integer batchId = event.subject().batchId(); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 499 | SettableFuture<CompletedBatchOperation> future |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 500 | = pendingFutures.getIfPresent(batchId); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 501 | if (future != null) { |
| 502 | future.set(event.result()); |
Yuta HIGUCHI | bf89c74 | 2014-10-27 15:10:02 -0700 | [diff] [blame] | 503 | pendingFutures.invalidate(batchId); |
Yuta HIGUCHI | 9def047 | 2014-10-23 15:51:10 -0700 | [diff] [blame] | 504 | } |
Madan Jampani | 117aaae | 2014-10-23 10:04:05 -0700 | [diff] [blame] | 505 | notifyDelegate(event); |
| 506 | } |
Yuta HIGUCHI | 92891d1 | 2014-10-27 20:04:38 -0700 | [diff] [blame] | 507 | |
| 508 | private synchronized void loadFromBackup(final DeviceId did) { |
| 509 | // should relax synchronized condition |
| 510 | |
| 511 | try { |
| 512 | log.info("Loading FlowRules for {} from backups", did); |
| 513 | SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(did); |
| 514 | for (Entry<FlowId, ImmutableList<StoredFlowEntry>> e |
| 515 | : backupFlowTable.entrySet()) { |
| 516 | |
| 517 | // TODO: should we be directly updating internal structure or |
| 518 | // should we be triggering event? |
| 519 | log.debug("loading {}", e.getValue()); |
| 520 | for (StoredFlowEntry entry : e.getValue()) { |
| 521 | flowEntries.remove(did, entry); |
| 522 | flowEntries.put(did, entry); |
| 523 | } |
| 524 | } |
| 525 | } catch (ExecutionException e) { |
| 526 | log.error("Failed to load backup flowtable for {}", did, e); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | private synchronized void removeFromPrimary(final DeviceId did) { |
| 531 | Collection<StoredFlowEntry> removed = flowEntries.removeAll(did); |
| 532 | log.debug("removedFromPrimary {}", removed); |
| 533 | } |
| 534 | |
| 535 | private final class SMapLoader |
| 536 | extends CacheLoader<DeviceId, SMap<FlowId, ImmutableList<StoredFlowEntry>>> { |
| 537 | |
| 538 | @Override |
| 539 | public SMap<FlowId, ImmutableList<StoredFlowEntry>> load(DeviceId id) |
| 540 | throws Exception { |
| 541 | IMap<byte[], byte[]> map = theInstance.getMap("flowtable_" + id.toString()); |
| 542 | return new SMap<FlowId, ImmutableList<StoredFlowEntry>>(map, SERIALIZER); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | private final class InternalReplicaInfoEventListener |
| 547 | implements ReplicaInfoEventListener { |
| 548 | |
| 549 | @Override |
| 550 | public void event(ReplicaInfoEvent event) { |
| 551 | final NodeId local = clusterService.getLocalNode().id(); |
| 552 | final DeviceId did = event.subject(); |
| 553 | final ReplicaInfo rInfo = event.replicaInfo(); |
| 554 | |
| 555 | switch (event.type()) { |
| 556 | case MASTER_CHANGED: |
| 557 | if (local.equals(rInfo.master().orNull())) { |
| 558 | // This node is the new master, populate local structure |
| 559 | // from backup |
| 560 | loadFromBackup(did); |
| 561 | } else { |
| 562 | // This node is no longer the master holder, |
| 563 | // clean local structure |
| 564 | removeFromPrimary(did); |
| 565 | // FIXME: probably should stop pending backup activities in |
| 566 | // executors to avoid overwriting with old value |
| 567 | } |
| 568 | break; |
| 569 | default: |
| 570 | break; |
| 571 | |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | // Task to update FlowEntries in backup HZ store |
| 577 | private final class UpdateBackup implements Runnable { |
| 578 | |
| 579 | private final DeviceId deviceId; |
| 580 | private final List<StoredFlowEntry> toAdd; |
| 581 | private final List<? extends FlowRule> toRemove; |
| 582 | |
| 583 | public UpdateBackup(DeviceId deviceId, |
| 584 | List<StoredFlowEntry> toAdd, |
| 585 | List<? extends FlowRule> list) { |
| 586 | this.deviceId = checkNotNull(deviceId); |
| 587 | this.toAdd = checkNotNull(toAdd); |
| 588 | this.toRemove = checkNotNull(list); |
| 589 | } |
| 590 | |
| 591 | @Override |
| 592 | public void run() { |
| 593 | try { |
| 594 | log.debug("update backup {} +{} -{}", deviceId, toAdd, toRemove); |
| 595 | final SMap<FlowId, ImmutableList<StoredFlowEntry>> backupFlowTable = smaps.get(deviceId); |
| 596 | // Following should be rewritten using async APIs |
| 597 | for (StoredFlowEntry entry : toAdd) { |
| 598 | final FlowId id = entry.id(); |
| 599 | ImmutableList<StoredFlowEntry> original = backupFlowTable.get(id); |
| 600 | List<StoredFlowEntry> list = new ArrayList<>(); |
| 601 | if (original != null) { |
| 602 | list.addAll(original); |
| 603 | } |
| 604 | |
| 605 | list.remove(entry); |
| 606 | list.add(entry); |
| 607 | |
| 608 | ImmutableList<StoredFlowEntry> newValue = ImmutableList.copyOf(list); |
| 609 | boolean success; |
| 610 | if (original == null) { |
| 611 | success = (backupFlowTable.putIfAbsent(id, newValue) == null); |
| 612 | } else { |
| 613 | success = backupFlowTable.replace(id, original, newValue); |
| 614 | } |
| 615 | // TODO retry? |
| 616 | if (!success) { |
| 617 | log.error("Updating backup failed."); |
| 618 | } |
| 619 | } |
| 620 | for (FlowRule entry : toRemove) { |
| 621 | final FlowId id = entry.id(); |
| 622 | ImmutableList<StoredFlowEntry> original = backupFlowTable.get(id); |
| 623 | List<StoredFlowEntry> list = new ArrayList<>(); |
| 624 | if (original != null) { |
| 625 | list.addAll(original); |
| 626 | } |
| 627 | |
| 628 | list.remove(entry); |
| 629 | |
| 630 | ImmutableList<StoredFlowEntry> newValue = ImmutableList.copyOf(list); |
| 631 | boolean success; |
| 632 | if (original == null) { |
| 633 | success = (backupFlowTable.putIfAbsent(id, newValue) == null); |
| 634 | } else { |
| 635 | success = backupFlowTable.replace(id, original, newValue); |
| 636 | } |
| 637 | // TODO retry? |
| 638 | if (!success) { |
| 639 | log.error("Updating backup failed."); |
| 640 | } |
| 641 | } |
| 642 | } catch (ExecutionException e) { |
| 643 | log.error("Failed to write to backups", e); |
| 644 | } |
| 645 | |
| 646 | } |
| 647 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 648 | } |