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;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Enriches {@link SyncReactorFutureDecorator} with state compression.
28 public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
30 @GuardedBy("compressionGuard")
31 private final Map<InstanceIdentifier<FlowCapableNode>, SyncupEntry> compressionQueue = new HashMap<>();
32 private final Semaphore compressionGuard = new Semaphore(1, true);
34 public SyncReactorFutureZipDecorator(SyncReactor delegate, ListeningExecutorService executorService) {
35 super(delegate, executorService);
38 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
39 final SyncupEntry syncupEntry) throws InterruptedException {
41 compressionGuard.acquire();
42 final boolean newTaskNecessary = updateCompressionState(flowcapableNodePath, syncupEntry);
43 if (newTaskNecessary) {
44 return super.syncup(flowcapableNodePath, syncupEntry);
46 return Futures.immediateFuture(Boolean.TRUE);
49 compressionGuard.release();
53 protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
54 final SyncupEntry syncupEntry) throws InterruptedException {
55 final SyncupEntry lastCompressionState = removeLastCompressionState(flowcapableNodePath);
56 if (lastCompressionState == null) {
57 return Futures.immediateFuture(Boolean.TRUE);
59 return super.doSyncupInFuture(flowcapableNodePath, lastCompressionState);
64 * If a syncup entry for corresponding the device is present in compression queue and new configuration diff is
65 * coming - update the entry in compression queue (zip). Create new (no entry in queue for device) or replace
66 * entry (config vs. operational is coming) in queue otherwise.
68 private boolean updateCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
69 final SyncupEntry syncupEntry) {
70 final SyncupEntry previousEntry = compressionQueue.get(flowcapableNodePath);
71 if (previousEntry != null && syncupEntry.isOptimizedConfigDelta()) {
72 updateOptimizedConfigDelta(flowcapableNodePath, syncupEntry, previousEntry);
74 compressionQueue.put(flowcapableNodePath, syncupEntry);
76 return previousEntry == null;
79 private void updateOptimizedConfigDelta(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
80 final SyncupEntry actual,
81 final SyncupEntry previous) {
82 final SyncupEntry updatedEntry = new SyncupEntry(actual.getAfter(), actual.getDsTypeAfter(),
83 previous.getBefore(), previous.getDsTypeBefore());
84 compressionQueue.put(flowcapableNodePath, updatedEntry);
87 private SyncupEntry removeLastCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
90 compressionGuard.acquire();
91 } catch (InterruptedException e) {
94 return compressionQueue.remove(flowcapableNodePath);
96 compressionGuard.release();