2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import java.util.HashMap;
16 import java.util.concurrent.Semaphore;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
19 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 * Enriches {@link SyncReactorFutureDecorator} with state compression.
26 public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
28 @GuardedBy("compressionGuard")
29 private final Map<InstanceIdentifier<FlowCapableNode>, SyncupEntry> compressionQueue = new HashMap<>();
30 private final Semaphore compressionGuard = new Semaphore(1, true);
32 public SyncReactorFutureZipDecorator(final SyncReactor delegate, final ListeningExecutorService executorService) {
33 super(delegate, executorService);
36 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
37 final SyncupEntry syncupEntry) throws InterruptedException {
39 compressionGuard.acquire();
40 final boolean newTaskNecessary = updateCompressionState(flowcapableNodePath, syncupEntry);
41 if (newTaskNecessary) {
42 super.syncup(flowcapableNodePath, syncupEntry);
44 return Futures.immediateFuture(Boolean.TRUE);
46 compressionGuard.release();
50 protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
51 final SyncupEntry syncupEntry) throws InterruptedException {
52 final SyncupEntry lastCompressionState = removeLastCompressionState(flowcapableNodePath);
53 if (lastCompressionState == null) {
54 return Futures.immediateFuture(Boolean.TRUE);
56 return super.doSyncupInFuture(flowcapableNodePath, lastCompressionState);
61 * If a syncup entry for corresponding the device is present in compression queue and new configuration diff is
62 * coming - update the entry in compression queue (zip). Create new (no entry in queue for device) or replace
63 * entry (config vs. operational is coming) in queue otherwise.
65 private boolean updateCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
66 final SyncupEntry syncupEntry) {
67 final SyncupEntry previousEntry = compressionQueue.get(flowcapableNodePath);
68 if (previousEntry != null && syncupEntry.isOptimizedConfigDelta()) {
69 updateOptimizedConfigDelta(flowcapableNodePath, syncupEntry, previousEntry);
71 compressionQueue.put(flowcapableNodePath, syncupEntry);
73 return previousEntry == null;
76 private void updateOptimizedConfigDelta(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
77 final SyncupEntry actual,
78 final SyncupEntry previous) {
79 final SyncupEntry updatedEntry = new SyncupEntry(actual.getAfter(), actual.getDsTypeAfter(),
80 previous.getBefore(), previous.getDsTypeBefore());
81 compressionQueue.put(flowcapableNodePath, updatedEntry);
84 private SyncupEntry removeLastCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
87 compressionGuard.acquire();
88 } catch (InterruptedException e) {
91 return compressionQueue.remove(flowcapableNodePath);
93 compressionGuard.release();