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.PathUtil;
20 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Enriches {@link SyncReactorFutureDecorator} with state compression.
30 public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
32 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorFutureZipDecorator.class);
34 @GuardedBy("compressionGuard")
35 private final Map<InstanceIdentifier<FlowCapableNode>, SyncupEntry> compressionQueue = new HashMap<>();
36 private final Semaphore compressionGuard = new Semaphore(1, false);
38 public SyncReactorFutureZipDecorator(SyncReactor delegate, ListeningExecutorService executorService) {
39 super(delegate, executorService);
42 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
43 final SyncupEntry syncupEntry) throws InterruptedException {
44 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
45 LOG.trace("syncup zip decorator: {}", nodeId.getValue());
48 compressionGuard.acquire();
50 final boolean newFutureNecessary = updateCompressionState(flowcapableNodePath, syncupEntry);
51 if (newFutureNecessary) {
52 super.syncup(flowcapableNodePath, syncupEntry);
54 return Futures.immediateFuture(true);
56 compressionGuard.release();
60 protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
61 final SyncupEntry syncupEntry) throws InterruptedException {
62 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
63 LOG.trace("doSyncupInFuture zip decorator: {}", nodeId.getValue());
65 final SyncupEntry lastCompressionState = removeLastCompressionState(flowcapableNodePath);
66 if (lastCompressionState == null) {
67 return Futures.immediateFuture(true);
69 return super.doSyncupInFuture(flowcapableNodePath, lastCompressionState);
74 * If there is config delta in compression queue for the device and new configuration is coming,
75 * update its zip queue entry. Create/replace zip queue entry for the device with operational delta otherwise.
77 private boolean updateCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
78 final SyncupEntry syncupEntry) {
79 final SyncupEntry previousEntry = compressionQueue.get(flowcapableNodePath);
81 if (previousEntry != null && syncupEntry.isOptimizedConfigDelta() && previousEntry.isOptimizedConfigDelta()) {
82 updateOptimizedConfigDelta(flowcapableNodePath, syncupEntry, previousEntry);
84 compressionQueue.put(flowcapableNodePath, syncupEntry);
86 return previousEntry == null;
89 private void updateOptimizedConfigDelta(InstanceIdentifier<FlowCapableNode> flowcapableNodePath, SyncupEntry actual,
90 SyncupEntry previous) {
91 compressionQueue.put(flowcapableNodePath, new SyncupEntry(actual.getAfter(), actual.getDsTypeAfter(),
92 previous.getBefore(), previous.getDsTypeBefore()));
95 private SyncupEntry removeLastCompressionState(
96 final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
99 compressionGuard.acquire();
100 } catch (InterruptedException e) {
104 return compressionQueue.remove(flowcapableNodePath);
106 compressionGuard.release();