Merge "Revert "Bug 6745 Do not ignore syncup return value""
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorFutureZipDecorator.java
1 /**
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
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;
15 import java.util.Map;
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
23 /**
24  * Enriches {@link SyncReactorFutureDecorator} with state compression.
25  */
26 public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
27
28     @GuardedBy("compressionGuard")
29     private final Map<InstanceIdentifier<FlowCapableNode>, SyncupEntry> compressionQueue = new HashMap<>();
30     private final Semaphore compressionGuard = new Semaphore(1, true);
31
32     public SyncReactorFutureZipDecorator(final SyncReactor delegate, final ListeningExecutorService executorService) {
33         super(delegate, executorService);
34     }
35
36     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
37                                             final SyncupEntry syncupEntry) throws InterruptedException {
38         try {
39             compressionGuard.acquire();
40             final boolean newTaskNecessary = updateCompressionState(flowcapableNodePath, syncupEntry);
41             if (newTaskNecessary) {
42                 super.syncup(flowcapableNodePath, syncupEntry);
43             }
44             return Futures.immediateFuture(Boolean.TRUE);
45         } finally {
46             compressionGuard.release();
47         }
48     }
49
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);
55         } else {
56             return super.doSyncupInFuture(flowcapableNodePath, lastCompressionState);
57         }
58     }
59
60     /**
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.
64      */
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);
70         } else {
71             compressionQueue.put(flowcapableNodePath, syncupEntry);
72         }
73         return previousEntry == null;
74     }
75
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);
82     }
83
84     private SyncupEntry removeLastCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
85         try {
86             try {
87                 compressionGuard.acquire();
88             } catch (InterruptedException e) {
89                 return null;
90             }
91             return compressionQueue.remove(flowcapableNodePath);
92         } finally {
93             compressionGuard.release();
94         }
95     }
96 }