Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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 | */ |
| 16 | package org.onosproject.store.flow.impl; |
| 17 | |
| 18 | import com.google.common.base.Objects; |
| 19 | import com.google.common.collect.Iterables; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import com.google.common.util.concurrent.Futures; |
| 23 | |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Modified; |
| 28 | import org.apache.felix.scr.annotations.Property; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | import org.onlab.util.KryoNamespace; |
| 33 | import org.onlab.util.NewConcurrentHashMap; |
| 34 | import org.onlab.util.Tools; |
| 35 | import org.onosproject.cfg.ComponentConfigService; |
| 36 | import org.onosproject.cluster.ClusterService; |
| 37 | import org.onosproject.cluster.NodeId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.core.IdGenerator; |
| 40 | import org.onosproject.mastership.MastershipService; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.device.DeviceService; |
| 43 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 44 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 45 | import org.onosproject.net.flow.FlowEntry; |
| 46 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 47 | import org.onosproject.net.flow.FlowId; |
| 48 | import org.onosproject.net.flow.FlowRule; |
| 49 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 50 | import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 51 | import org.onosproject.net.flow.FlowRuleBatchEvent; |
| 52 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 53 | import org.onosproject.net.flow.FlowRuleBatchRequest; |
| 54 | import org.onosproject.net.flow.FlowRuleEvent; |
| 55 | import org.onosproject.net.flow.FlowRuleEvent.Type; |
| 56 | import org.onosproject.net.flow.FlowRuleService; |
| 57 | import org.onosproject.net.flow.FlowRuleStore; |
| 58 | import org.onosproject.net.flow.FlowRuleStoreDelegate; |
| 59 | import org.onosproject.net.flow.StoredFlowEntry; |
| 60 | import org.onosproject.store.AbstractStore; |
| 61 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 62 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 63 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 64 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 65 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 66 | import org.onosproject.store.flow.ReplicaInfoService; |
| 67 | import org.onosproject.store.serializers.KryoSerializer; |
| 68 | import org.onosproject.store.serializers.StoreSerializer; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 69 | import org.onosproject.store.serializers.custom.DistributedStoreSerializers; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 70 | import org.osgi.service.component.ComponentContext; |
| 71 | import org.slf4j.Logger; |
| 72 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 73 | import java.util.Collections; |
| 74 | import java.util.Dictionary; |
| 75 | import java.util.HashSet; |
| 76 | import java.util.List; |
| 77 | import java.util.Map; |
| 78 | import java.util.Set; |
| 79 | import java.util.concurrent.ConcurrentHashMap; |
| 80 | import java.util.concurrent.ConcurrentMap; |
| 81 | import java.util.concurrent.ExecutorService; |
| 82 | import java.util.concurrent.Executors; |
| 83 | import java.util.concurrent.ScheduledExecutorService; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 84 | import java.util.concurrent.ScheduledFuture; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 85 | import java.util.concurrent.TimeUnit; |
| 86 | import java.util.concurrent.atomic.AtomicInteger; |
| 87 | import java.util.stream.Collectors; |
| 88 | |
| 89 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 90 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 91 | import static org.onlab.util.Tools.get; |
| 92 | import static org.onlab.util.Tools.groupedThreads; |
| 93 | import static org.onosproject.net.flow.FlowRuleEvent.Type.RULE_REMOVED; |
| 94 | import static org.onosproject.store.flow.impl.FlowStoreMessageSubjects.*; |
| 95 | import static org.slf4j.LoggerFactory.getLogger; |
| 96 | |
| 97 | /** |
| 98 | * Manages inventory of flow rules using a distributed state management protocol. |
| 99 | */ |
| 100 | @Component(immediate = true, enabled = true) |
| 101 | @Service |
| 102 | public class NewDistributedFlowRuleStore |
| 103 | extends AbstractStore<FlowRuleBatchEvent, FlowRuleStoreDelegate> |
| 104 | implements FlowRuleStore { |
| 105 | |
| 106 | private final Logger log = getLogger(getClass()); |
| 107 | |
| 108 | private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8; |
| 109 | private static final boolean DEFAULT_BACKUP_ENABLED = true; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 110 | private static final int DEFAULT_BACKUP_PERIOD_MILLIS = 2000; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 111 | private static final long FLOW_RULE_STORE_TIMEOUT_MILLIS = 5000; |
| 112 | |
| 113 | @Property(name = "msgHandlerPoolSize", intValue = MESSAGE_HANDLER_THREAD_POOL_SIZE, |
| 114 | label = "Number of threads in the message handler pool") |
| 115 | private int msgHandlerPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 116 | |
| 117 | @Property(name = "backupEnabled", boolValue = DEFAULT_BACKUP_ENABLED, |
| 118 | label = "Indicates whether backups are enabled or not") |
| 119 | private boolean backupEnabled = DEFAULT_BACKUP_ENABLED; |
| 120 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 121 | @Property(name = "backupPeriod", intValue = DEFAULT_BACKUP_PERIOD_MILLIS, |
| 122 | label = "Delay in ms between successive backup runs") |
| 123 | private int backupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS; |
| 124 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 125 | private InternalFlowTable flowTable = new InternalFlowTable(); |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 128 | protected ReplicaInfoService replicaInfoManager; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected ClusterCommunicationService clusterCommunicator; |
| 132 | |
| 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | protected ClusterService clusterService; |
| 135 | |
| 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected DeviceService deviceService; |
| 138 | |
| 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 140 | protected CoreService coreService; |
| 141 | |
| 142 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 143 | protected ComponentConfigService configService; |
| 144 | |
| 145 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 146 | protected MastershipService mastershipService; |
| 147 | |
| 148 | private Map<Long, NodeId> pendingResponses = Maps.newConcurrentMap(); |
| 149 | private ExecutorService messageHandlingExecutor; |
| 150 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 151 | private ScheduledFuture<?> backupTask; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 152 | private final ScheduledExecutorService backupSenderExecutor = |
| 153 | Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/flow", "backup-sender")); |
| 154 | |
| 155 | protected static final StoreSerializer SERIALIZER = new KryoSerializer() { |
| 156 | @Override |
| 157 | protected void setupKryoPool() { |
| 158 | serializerPool = KryoNamespace.newBuilder() |
| 159 | .register(DistributedStoreSerializers.STORE_COMMON) |
| 160 | .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 161 | .build(); |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | private IdGenerator idGenerator; |
| 166 | private NodeId local; |
| 167 | |
| 168 | @Activate |
| 169 | public void activate(ComponentContext context) { |
| 170 | configService.registerProperties(getClass()); |
| 171 | |
| 172 | idGenerator = coreService.getIdGenerator(FlowRuleService.FLOW_OP_TOPIC); |
| 173 | |
| 174 | local = clusterService.getLocalNode().id(); |
| 175 | |
| 176 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 177 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers")); |
| 178 | |
| 179 | registerMessageHandlers(messageHandlingExecutor); |
| 180 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 181 | if (backupEnabled) { |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 182 | replicaInfoManager.addListener(flowTable); |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 183 | backupTask = backupSenderExecutor.scheduleWithFixedDelay( |
| 184 | flowTable::backup, |
| 185 | 0, |
| 186 | backupPeriod, |
| 187 | TimeUnit.MILLISECONDS); |
| 188 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 189 | |
| 190 | logConfig("Started"); |
| 191 | } |
| 192 | |
| 193 | @Deactivate |
| 194 | public void deactivate(ComponentContext context) { |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 195 | if (backupEnabled) { |
| 196 | replicaInfoManager.removeListener(flowTable); |
| 197 | backupTask.cancel(true); |
| 198 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 199 | configService.unregisterProperties(getClass(), false); |
| 200 | unregisterMessageHandlers(); |
| 201 | messageHandlingExecutor.shutdownNow(); |
| 202 | backupSenderExecutor.shutdownNow(); |
| 203 | log.info("Stopped"); |
| 204 | } |
| 205 | |
| 206 | @SuppressWarnings("rawtypes") |
| 207 | @Modified |
| 208 | public void modified(ComponentContext context) { |
| 209 | if (context == null) { |
| 210 | backupEnabled = DEFAULT_BACKUP_ENABLED; |
| 211 | logConfig("Default config"); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | Dictionary properties = context.getProperties(); |
| 216 | int newPoolSize; |
| 217 | boolean newBackupEnabled; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 218 | int newBackupPeriod; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 219 | try { |
| 220 | String s = get(properties, "msgHandlerPoolSize"); |
| 221 | newPoolSize = isNullOrEmpty(s) ? msgHandlerPoolSize : Integer.parseInt(s.trim()); |
| 222 | |
| 223 | s = get(properties, "backupEnabled"); |
| 224 | newBackupEnabled = isNullOrEmpty(s) ? backupEnabled : Boolean.parseBoolean(s.trim()); |
| 225 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 226 | s = get(properties, "backupPeriod"); |
| 227 | newBackupPeriod = isNullOrEmpty(s) ? backupPeriod : Integer.parseInt(s.trim()); |
| 228 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 229 | } catch (NumberFormatException | ClassCastException e) { |
| 230 | newPoolSize = MESSAGE_HANDLER_THREAD_POOL_SIZE; |
| 231 | newBackupEnabled = DEFAULT_BACKUP_ENABLED; |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 232 | newBackupPeriod = DEFAULT_BACKUP_PERIOD_MILLIS; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 235 | boolean restartBackupTask = false; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 236 | if (newBackupEnabled != backupEnabled) { |
| 237 | backupEnabled = newBackupEnabled; |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 238 | if (!backupEnabled) { |
| 239 | replicaInfoManager.removeListener(flowTable); |
| 240 | if (backupTask != null) { |
| 241 | backupTask.cancel(false); |
| 242 | backupTask = null; |
| 243 | } |
| 244 | } else { |
| 245 | replicaInfoManager.addListener(flowTable); |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 246 | } |
| 247 | restartBackupTask = backupEnabled; |
| 248 | } |
| 249 | if (newBackupPeriod != backupPeriod) { |
| 250 | backupPeriod = newBackupPeriod; |
| 251 | restartBackupTask = backupEnabled; |
| 252 | } |
| 253 | if (restartBackupTask) { |
| 254 | if (backupTask != null) { |
| 255 | // cancel previously running task |
| 256 | backupTask.cancel(false); |
| 257 | } |
| 258 | backupTask = backupSenderExecutor.scheduleWithFixedDelay( |
| 259 | flowTable::backup, |
| 260 | 0, |
| 261 | backupPeriod, |
| 262 | TimeUnit.MILLISECONDS); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 263 | } |
| 264 | if (newPoolSize != msgHandlerPoolSize) { |
| 265 | msgHandlerPoolSize = newPoolSize; |
| 266 | ExecutorService oldMsgHandler = messageHandlingExecutor; |
| 267 | messageHandlingExecutor = Executors.newFixedThreadPool( |
| 268 | msgHandlerPoolSize, groupedThreads("onos/store/flow", "message-handlers")); |
| 269 | |
| 270 | // replace previously registered handlers. |
| 271 | registerMessageHandlers(messageHandlingExecutor); |
| 272 | oldMsgHandler.shutdown(); |
| 273 | } |
| 274 | logConfig("Reconfigured"); |
| 275 | } |
| 276 | |
| 277 | private void registerMessageHandlers(ExecutorService executor) { |
| 278 | |
| 279 | clusterCommunicator.addSubscriber(APPLY_BATCH_FLOWS, new OnStoreBatch(), executor); |
| 280 | clusterCommunicator.<FlowRuleBatchEvent>addSubscriber( |
| 281 | REMOTE_APPLY_COMPLETED, SERIALIZER::decode, this::notifyDelegate, executor); |
| 282 | clusterCommunicator.addSubscriber( |
| 283 | GET_FLOW_ENTRY, SERIALIZER::decode, flowTable::getFlowEntry, SERIALIZER::encode, executor); |
| 284 | clusterCommunicator.addSubscriber( |
| 285 | GET_DEVICE_FLOW_ENTRIES, SERIALIZER::decode, flowTable::getFlowEntries, SERIALIZER::encode, executor); |
| 286 | clusterCommunicator.addSubscriber( |
| 287 | REMOVE_FLOW_ENTRY, SERIALIZER::decode, this::removeFlowRuleInternal, SERIALIZER::encode, executor); |
| 288 | clusterCommunicator.addSubscriber( |
| 289 | REMOVE_FLOW_ENTRY, SERIALIZER::decode, this::removeFlowRuleInternal, SERIALIZER::encode, executor); |
| 290 | clusterCommunicator.addSubscriber( |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 291 | FLOW_TABLE_BACKUP, SERIALIZER::decode, flowTable::onBackupReceipt, SERIALIZER::encode, executor); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | private void unregisterMessageHandlers() { |
| 295 | clusterCommunicator.removeSubscriber(REMOVE_FLOW_ENTRY); |
| 296 | clusterCommunicator.removeSubscriber(GET_DEVICE_FLOW_ENTRIES); |
| 297 | clusterCommunicator.removeSubscriber(GET_FLOW_ENTRY); |
| 298 | clusterCommunicator.removeSubscriber(APPLY_BATCH_FLOWS); |
| 299 | clusterCommunicator.removeSubscriber(REMOTE_APPLY_COMPLETED); |
| 300 | clusterCommunicator.removeSubscriber(FLOW_TABLE_BACKUP); |
| 301 | } |
| 302 | |
| 303 | private void logConfig(String prefix) { |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 304 | log.info("{} with msgHandlerPoolSize = {}; backupEnabled = {}, backupPeriod = {}", |
| 305 | prefix, msgHandlerPoolSize, backupEnabled, backupPeriod); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // This is not a efficient operation on a distributed sharded |
| 309 | // flow store. We need to revisit the need for this operation or at least |
| 310 | // make it device specific. |
| 311 | @Override |
| 312 | public int getFlowRuleCount() { |
| 313 | AtomicInteger sum = new AtomicInteger(0); |
| 314 | deviceService.getDevices().forEach(device -> sum.addAndGet(Iterables.size(getFlowEntries(device.id())))); |
| 315 | return sum.get(); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public FlowEntry getFlowEntry(FlowRule rule) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 320 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 321 | |
| 322 | if (master == null) { |
| 323 | log.warn("Failed to getFlowEntry: No master for {}", rule.deviceId()); |
| 324 | return null; |
| 325 | } |
| 326 | |
| 327 | if (Objects.equal(local, master)) { |
| 328 | return flowTable.getFlowEntry(rule); |
| 329 | } |
| 330 | |
| 331 | log.trace("Forwarding getFlowEntry to {}, which is the primary (master) for device {}", |
| 332 | master, rule.deviceId()); |
| 333 | |
| 334 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(rule, |
| 335 | FlowStoreMessageSubjects.GET_FLOW_ENTRY, |
| 336 | SERIALIZER::encode, |
| 337 | SERIALIZER::decode, |
| 338 | master), |
| 339 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 340 | TimeUnit.MILLISECONDS, |
| 341 | null); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 346 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 347 | |
| 348 | if (master == null) { |
| 349 | log.warn("Failed to getFlowEntries: No master for {}", deviceId); |
| 350 | return Collections.emptyList(); |
| 351 | } |
| 352 | |
| 353 | if (Objects.equal(local, master)) { |
| 354 | return flowTable.getFlowEntries(deviceId); |
| 355 | } |
| 356 | |
| 357 | log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", |
| 358 | master, deviceId); |
| 359 | |
| 360 | return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(deviceId, |
| 361 | FlowStoreMessageSubjects.GET_DEVICE_FLOW_ENTRIES, |
| 362 | SERIALIZER::encode, |
| 363 | SERIALIZER::decode, |
| 364 | master), |
| 365 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 366 | TimeUnit.MILLISECONDS, |
| 367 | Collections.emptyList()); |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public void storeFlowRule(FlowRule rule) { |
| 372 | storeBatch(new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 373 | Collections.singletonList(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)), |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 374 | rule.deviceId(), idGenerator.getNewId())); |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public void storeBatch(FlowRuleBatchOperation operation) { |
| 379 | if (operation.getOperations().isEmpty()) { |
| 380 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 381 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 382 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 387 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 388 | |
| 389 | if (master == null) { |
| 390 | log.warn("No master for {} : flows will be marked for removal", deviceId); |
| 391 | |
| 392 | updateStoreInternal(operation); |
| 393 | |
| 394 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 395 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 396 | new CompletedBatchOperation(true, Collections.emptySet(), operation.deviceId()))); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | if (Objects.equal(local, master)) { |
| 401 | storeBatchInternal(operation); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | log.trace("Forwarding storeBatch to {}, which is the primary (master) for device {}", |
| 406 | master, deviceId); |
| 407 | |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 408 | clusterCommunicator.unicast(operation, |
| 409 | APPLY_BATCH_FLOWS, |
| 410 | SERIALIZER::encode, |
| 411 | master) |
| 412 | .whenComplete((result, error) -> { |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 413 | if (error != null) { |
Madan Jampani | 15f1bc4 | 2015-05-28 10:51:52 -0700 | [diff] [blame] | 414 | log.warn("Failed to storeBatch: {} to {}", operation, master, error); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 415 | |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 416 | Set<FlowRule> allFailures = operation.getOperations() |
| 417 | .stream() |
| 418 | .map(op -> op.target()) |
| 419 | .collect(Collectors.toSet()); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 420 | |
Thomas Vachuska | a267ce4 | 2015-05-27 16:14:23 -0700 | [diff] [blame] | 421 | notifyDelegate(FlowRuleBatchEvent.completed( |
| 422 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 423 | new CompletedBatchOperation(false, allFailures, deviceId))); |
| 424 | } |
Madan Jampani | 175e8fd | 2015-05-20 14:10:45 -0700 | [diff] [blame] | 425 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private void storeBatchInternal(FlowRuleBatchOperation operation) { |
| 429 | |
| 430 | final DeviceId did = operation.deviceId(); |
| 431 | //final Collection<FlowEntry> ft = flowTable.getFlowEntries(did); |
| 432 | Set<FlowRuleBatchEntry> currentOps = updateStoreInternal(operation); |
| 433 | if (currentOps.isEmpty()) { |
| 434 | batchOperationComplete(FlowRuleBatchEvent.completed( |
| 435 | new FlowRuleBatchRequest(operation.id(), Collections.emptySet()), |
| 436 | new CompletedBatchOperation(true, Collections.emptySet(), did))); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | notifyDelegate(FlowRuleBatchEvent.requested(new |
| 441 | FlowRuleBatchRequest(operation.id(), |
| 442 | currentOps), operation.deviceId())); |
| 443 | } |
| 444 | |
| 445 | private Set<FlowRuleBatchEntry> updateStoreInternal(FlowRuleBatchOperation operation) { |
| 446 | return operation.getOperations().stream().map( |
| 447 | op -> { |
| 448 | StoredFlowEntry entry; |
| 449 | switch (op.operator()) { |
| 450 | case ADD: |
| 451 | entry = new DefaultFlowEntry(op.target()); |
| 452 | // always add requested FlowRule |
| 453 | // Note: 2 equal FlowEntry may have different treatment |
| 454 | flowTable.remove(entry.deviceId(), entry); |
| 455 | flowTable.add(entry); |
| 456 | |
| 457 | return op; |
| 458 | case REMOVE: |
| 459 | entry = flowTable.getFlowEntry(op.target()); |
| 460 | if (entry != null) { |
| 461 | entry.setState(FlowEntryState.PENDING_REMOVE); |
| 462 | return op; |
| 463 | } |
| 464 | break; |
| 465 | case MODIFY: |
| 466 | //TODO: figure this out at some point |
| 467 | break; |
| 468 | default: |
| 469 | log.warn("Unknown flow operation operator: {}", op.operator()); |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | ).filter(op -> op != null).collect(Collectors.toSet()); |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void deleteFlowRule(FlowRule rule) { |
| 478 | storeBatch( |
| 479 | new FlowRuleBatchOperation( |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 480 | Collections.singletonList( |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 481 | new FlowRuleBatchEntry( |
| 482 | FlowRuleOperation.REMOVE, |
| 483 | rule)), rule.deviceId(), idGenerator.getNewId())); |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 488 | NodeId master = mastershipService.getMasterFor(rule.deviceId()); |
| 489 | if (Objects.equal(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 490 | return addOrUpdateFlowRuleInternal(rule); |
| 491 | } |
| 492 | |
| 493 | log.warn("Tried to update FlowRule {} state," |
| 494 | + " while the Node was not the master.", rule); |
| 495 | return null; |
| 496 | } |
| 497 | |
| 498 | private FlowRuleEvent addOrUpdateFlowRuleInternal(FlowEntry rule) { |
| 499 | // check if this new rule is an update to an existing entry |
| 500 | StoredFlowEntry stored = flowTable.getFlowEntry(rule); |
| 501 | if (stored != null) { |
| 502 | stored.setBytes(rule.bytes()); |
| 503 | stored.setLife(rule.life()); |
| 504 | stored.setPackets(rule.packets()); |
| 505 | if (stored.state() == FlowEntryState.PENDING_ADD) { |
| 506 | stored.setState(FlowEntryState.ADDED); |
| 507 | return new FlowRuleEvent(Type.RULE_ADDED, rule); |
| 508 | } |
| 509 | return new FlowRuleEvent(Type.RULE_UPDATED, rule); |
| 510 | } |
| 511 | |
| 512 | // TODO: Confirm if this behavior is correct. See SimpleFlowRuleStore |
| 513 | // TODO: also update backup if the behavior is correct. |
| 514 | flowTable.add(rule); |
| 515 | return null; |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public FlowRuleEvent removeFlowRule(FlowEntry rule) { |
| 520 | final DeviceId deviceId = rule.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 521 | NodeId master = mastershipService.getMasterFor(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 522 | |
| 523 | if (Objects.equal(local, master)) { |
| 524 | // bypass and handle it locally |
| 525 | return removeFlowRuleInternal(rule); |
| 526 | } |
| 527 | |
| 528 | if (master == null) { |
| 529 | log.warn("Failed to removeFlowRule: No master for {}", deviceId); |
| 530 | // TODO: revisit if this should be null (="no-op") or Exception |
| 531 | return null; |
| 532 | } |
| 533 | |
| 534 | log.trace("Forwarding removeFlowRule to {}, which is the master for device {}", |
| 535 | master, deviceId); |
| 536 | |
| 537 | return Futures.get(clusterCommunicator.sendAndReceive( |
| 538 | rule, |
| 539 | REMOVE_FLOW_ENTRY, |
| 540 | SERIALIZER::encode, |
| 541 | SERIALIZER::decode, |
| 542 | master), |
| 543 | FLOW_RULE_STORE_TIMEOUT_MILLIS, |
| 544 | TimeUnit.MILLISECONDS, |
| 545 | RuntimeException.class); |
| 546 | } |
| 547 | |
| 548 | private FlowRuleEvent removeFlowRuleInternal(FlowEntry rule) { |
| 549 | final DeviceId deviceId = rule.deviceId(); |
| 550 | // This is where one could mark a rule as removed and still keep it in the store. |
| 551 | final boolean removed = flowTable.remove(deviceId, rule); //flowEntries.remove(deviceId, rule); |
| 552 | return removed ? new FlowRuleEvent(RULE_REMOVED, rule) : null; |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public void batchOperationComplete(FlowRuleBatchEvent event) { |
| 557 | //FIXME: need a per device pending response |
| 558 | NodeId nodeId = pendingResponses.remove(event.subject().batchId()); |
| 559 | if (nodeId == null) { |
| 560 | notifyDelegate(event); |
| 561 | } else { |
| 562 | // TODO check unicast return value |
| 563 | clusterCommunicator.unicast(event, REMOTE_APPLY_COMPLETED, SERIALIZER::encode, nodeId); |
| 564 | //error log: log.warn("Failed to respond to peer for batch operation result"); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | private final class OnStoreBatch implements ClusterMessageHandler { |
| 569 | |
| 570 | @Override |
| 571 | public void handle(final ClusterMessage message) { |
| 572 | FlowRuleBatchOperation operation = SERIALIZER.decode(message.payload()); |
| 573 | log.debug("received batch request {}", operation); |
| 574 | |
| 575 | final DeviceId deviceId = operation.deviceId(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 576 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 577 | if (!Objects.equal(local, master)) { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 578 | Set<FlowRule> failures = new HashSet<>(operation.size()); |
| 579 | for (FlowRuleBatchEntry op : operation.getOperations()) { |
| 580 | failures.add(op.target()); |
| 581 | } |
| 582 | CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId); |
| 583 | // This node is no longer the master, respond as all failed. |
| 584 | // TODO: we might want to wrap response in envelope |
| 585 | // to distinguish sw programming failure and hand over |
| 586 | // it make sense in the latter case to retry immediately. |
| 587 | message.respond(SERIALIZER.encode(allFailed)); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | pendingResponses.put(operation.id(), message.sender()); |
| 592 | storeBatchInternal(operation); |
| 593 | } |
| 594 | } |
| 595 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 596 | private class InternalFlowTable implements ReplicaInfoEventListener { |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 597 | |
| 598 | private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, Set<StoredFlowEntry>>> |
| 599 | flowEntries = new ConcurrentHashMap<>(); |
| 600 | |
| 601 | private final Map<DeviceId, Long> lastBackupTimes = Maps.newConcurrentMap(); |
| 602 | private final Map<DeviceId, Long> lastUpdateTimes = Maps.newConcurrentMap(); |
| 603 | private final Map<DeviceId, NodeId> lastBackupNodes = Maps.newConcurrentMap(); |
| 604 | |
| 605 | private NewConcurrentHashMap<FlowId, Set<StoredFlowEntry>> lazyEmptyFlowTable() { |
| 606 | return NewConcurrentHashMap.<FlowId, Set<StoredFlowEntry>>ifNeeded(); |
| 607 | } |
| 608 | |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 609 | @Override |
| 610 | public void event(ReplicaInfoEvent event) { |
| 611 | if (event.type() == ReplicaInfoEvent.Type.BACKUPS_CHANGED) { |
| 612 | DeviceId deviceId = event.subject(); |
Madan Jampani | 6bd2d9f | 2015-05-14 14:10:42 -0700 | [diff] [blame] | 613 | NodeId master = mastershipService.getMasterFor(deviceId); |
| 614 | if (!Objects.equal(local, master)) { |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 615 | // ignore since this event is for a device this node does not manage. |
| 616 | return; |
| 617 | } |
Madan Jampani | 7267c55 | 2015-05-20 22:39:17 -0700 | [diff] [blame] | 618 | NodeId newBackupNode = getBackupNode(deviceId); |
| 619 | NodeId currentBackupNode = lastBackupNodes.get(deviceId); |
| 620 | if (Objects.equal(newBackupNode, currentBackupNode)) { |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 621 | // ignore since backup location hasn't changed. |
| 622 | return; |
| 623 | } |
Madan Jampani | 7267c55 | 2015-05-20 22:39:17 -0700 | [diff] [blame] | 624 | if (currentBackupNode != null && newBackupNode == null) { |
| 625 | // Current backup node is most likely down and no alternate backup node |
| 626 | // has been chosen. Clear current backup location so that we can resume |
| 627 | // backups when either current backup comes online or a different backup node |
| 628 | // is chosen. |
| 629 | log.warn("Lost backup location {} for deviceId {} and no alternate backup node exists. " |
| 630 | + "Flows can be lost if the master goes down", currentBackupNode, deviceId); |
| 631 | lastBackupNodes.remove(deviceId); |
| 632 | lastBackupTimes.remove(deviceId); |
| 633 | return; |
| 634 | // TODO: Pick any available node as backup and ensure hand-off occurs when |
| 635 | // a new master is elected. |
| 636 | } |
| 637 | log.info("Backup location for {} has changed from {} to {}.", |
| 638 | deviceId, currentBackupNode, newBackupNode); |
| 639 | backupSenderExecutor.schedule(() -> backupFlowEntries(newBackupNode, Sets.newHashSet(deviceId)), |
Madan Jampani | 0306268 | 2015-05-19 17:57:51 -0700 | [diff] [blame] | 640 | 0, |
| 641 | TimeUnit.SECONDS); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | private void backupFlowEntries(NodeId nodeId, Set<DeviceId> deviceIds) { |
| 646 | log.debug("Sending flowEntries for devices {} to {} as backup.", deviceIds, nodeId); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 647 | Map<DeviceId, Map<FlowId, Set<StoredFlowEntry>>> deviceFlowEntries = |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 648 | Maps.newConcurrentMap(); |
| 649 | flowEntries.forEach((key, value) -> { |
| 650 | if (deviceIds.contains(key)) { |
| 651 | deviceFlowEntries.put(key, value); |
| 652 | } |
| 653 | }); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 654 | clusterCommunicator.<Map<DeviceId, Map<FlowId, Set<StoredFlowEntry>>>, Set<DeviceId>>sendAndReceive( |
| 655 | deviceFlowEntries, |
| 656 | FLOW_TABLE_BACKUP, |
| 657 | SERIALIZER::encode, |
| 658 | SERIALIZER::decode, |
| 659 | nodeId) |
| 660 | .whenComplete((backedupDevices, error) -> { |
| 661 | Set<DeviceId> devicesNotBackedup = error != null ? |
| 662 | deviceFlowEntries.keySet() : |
| 663 | Sets.difference(deviceFlowEntries.keySet(), backedupDevices); |
| 664 | if (devicesNotBackedup.size() > 0) { |
| 665 | log.warn("Failed to backup devices: {}", devicesNotBackedup, error); |
| 666 | } |
| 667 | if (backedupDevices != null) { |
| 668 | backedupDevices.forEach(id -> { |
| 669 | lastBackupTimes.put(id, System.currentTimeMillis()); |
| 670 | lastBackupNodes.put(id, nodeId); |
| 671 | }); |
| 672 | } |
| 673 | }); |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 676 | /** |
| 677 | * Returns the flow table for specified device. |
| 678 | * |
| 679 | * @param deviceId identifier of the device |
| 680 | * @return Map representing Flow Table of given device. |
| 681 | */ |
| 682 | private ConcurrentMap<FlowId, Set<StoredFlowEntry>> getFlowTable(DeviceId deviceId) { |
| 683 | return createIfAbsentUnchecked(flowEntries, deviceId, lazyEmptyFlowTable()); |
| 684 | } |
| 685 | |
| 686 | private Set<StoredFlowEntry> getFlowEntriesInternal(DeviceId deviceId, FlowId flowId) { |
| 687 | return getFlowTable(deviceId).computeIfAbsent(flowId, id -> Sets.newCopyOnWriteArraySet()); |
| 688 | } |
| 689 | |
| 690 | private StoredFlowEntry getFlowEntryInternal(FlowRule rule) { |
| 691 | Set<StoredFlowEntry> flowEntries = getFlowEntriesInternal(rule.deviceId(), rule.id()); |
| 692 | return flowEntries.stream() |
| 693 | .filter(entry -> Objects.equal(entry, rule)) |
| 694 | .findAny() |
| 695 | .orElse(null); |
| 696 | } |
| 697 | |
| 698 | private Set<FlowEntry> getFlowEntriesInternal(DeviceId deviceId) { |
| 699 | Set<FlowEntry> result = Sets.newHashSet(); |
| 700 | getFlowTable(deviceId).values().forEach(result::addAll); |
| 701 | return result; |
| 702 | } |
| 703 | |
| 704 | public StoredFlowEntry getFlowEntry(FlowRule rule) { |
| 705 | return getFlowEntryInternal(rule); |
| 706 | } |
| 707 | |
| 708 | public Set<FlowEntry> getFlowEntries(DeviceId deviceId) { |
| 709 | return getFlowEntriesInternal(deviceId); |
| 710 | } |
| 711 | |
| 712 | public void add(FlowEntry rule) { |
| 713 | getFlowEntriesInternal(rule.deviceId(), rule.id()).add((StoredFlowEntry) rule); |
| 714 | lastUpdateTimes.put(rule.deviceId(), System.currentTimeMillis()); |
| 715 | } |
| 716 | |
| 717 | public boolean remove(DeviceId deviceId, FlowEntry rule) { |
| 718 | try { |
| 719 | return getFlowEntriesInternal(deviceId, rule.id()).remove(rule); |
| 720 | } finally { |
| 721 | lastUpdateTimes.put(deviceId, System.currentTimeMillis()); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | private NodeId getBackupNode(DeviceId deviceId) { |
| 726 | List<NodeId> deviceStandbys = replicaInfoManager.getReplicaInfoFor(deviceId).backups(); |
| 727 | // pick the standby which is most likely to become next master |
| 728 | return deviceStandbys.isEmpty() ? null : deviceStandbys.get(0); |
| 729 | } |
| 730 | |
| 731 | private void backup() { |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 732 | if (!backupEnabled) { |
| 733 | return; |
| 734 | } |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 735 | try { |
| 736 | // determine the set of devices that we need to backup during this run. |
| 737 | Set<DeviceId> devicesToBackup = mastershipService.getDevicesOf(local) |
| 738 | .stream() |
| 739 | .filter(deviceId -> { |
| 740 | Long lastBackupTime = lastBackupTimes.get(deviceId); |
| 741 | Long lastUpdateTime = lastUpdateTimes.get(deviceId); |
| 742 | NodeId lastBackupNode = lastBackupNodes.get(deviceId); |
Madan Jampani | 7267c55 | 2015-05-20 22:39:17 -0700 | [diff] [blame] | 743 | NodeId newBackupNode = getBackupNode(deviceId); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 744 | return lastBackupTime == null |
Madan Jampani | 7267c55 | 2015-05-20 22:39:17 -0700 | [diff] [blame] | 745 | || !Objects.equal(lastBackupNode, newBackupNode) |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 746 | || (lastUpdateTime != null && lastUpdateTime > lastBackupTime); |
| 747 | }) |
| 748 | .collect(Collectors.toSet()); |
| 749 | |
| 750 | // compute a mapping from node to the set of devices whose flow entries it should backup |
| 751 | Map<NodeId, Set<DeviceId>> devicesToBackupByNode = Maps.newHashMap(); |
| 752 | devicesToBackup.forEach(deviceId -> { |
| 753 | NodeId backupLocation = getBackupNode(deviceId); |
| 754 | if (backupLocation != null) { |
| 755 | devicesToBackupByNode.computeIfAbsent(backupLocation, nodeId -> Sets.newHashSet()) |
| 756 | .add(deviceId); |
| 757 | } |
| 758 | }); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 759 | // send the device flow entries to their respective backup nodes |
Madan Jampani | f7536ab | 2015-05-07 23:23:23 -0700 | [diff] [blame] | 760 | devicesToBackupByNode.forEach(this::backupFlowEntries); |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 761 | } catch (Exception e) { |
| 762 | log.error("Backup failed.", e); |
| 763 | } |
| 764 | } |
| 765 | |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 766 | private Set<DeviceId> onBackupReceipt(Map<DeviceId, Map<FlowId, Set<StoredFlowEntry>>> flowTables) { |
Madan Jampani | 7267c55 | 2015-05-20 22:39:17 -0700 | [diff] [blame] | 767 | log.debug("Received flowEntries for {} to backup", flowTables.keySet()); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 768 | Set<DeviceId> backedupDevices = Sets.newHashSet(); |
| 769 | try { |
| 770 | Set<DeviceId> managedDevices = mastershipService.getDevicesOf(local); |
| 771 | // Only process those devices are that not managed by the local node. |
| 772 | Maps.filterKeys(flowTables, deviceId -> !managedDevices.contains(deviceId)) |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 773 | .forEach((deviceId, flowTable) -> { |
| 774 | Map<FlowId, Set<StoredFlowEntry>> deviceFlowTable = getFlowTable(deviceId); |
| 775 | deviceFlowTable.clear(); |
| 776 | deviceFlowTable.putAll(flowTable); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 777 | backedupDevices.add(deviceId); |
Madan Jampani | 08bf17b | 2015-05-06 16:25:26 -0700 | [diff] [blame] | 778 | }); |
Madan Jampani | 654b58a | 2015-05-22 11:28:11 -0700 | [diff] [blame] | 779 | } catch (Exception e) { |
| 780 | log.warn("Failure processing backup request", e); |
| 781 | } |
| 782 | return backedupDevices; |
Madan Jampani | 86940d9 | 2015-05-06 11:47:57 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |