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 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Enriches {@link SyncReactorFutureDecorator} with state compression.
27  */
28 public class SyncReactorFutureZipDecorator extends SyncReactorFutureDecorator {
29
30     @GuardedBy("compressionGuard")
31     private final Map<InstanceIdentifier<FlowCapableNode>, SyncupEntry> compressionQueue = new HashMap<>();
32     private final Semaphore compressionGuard = new Semaphore(1, false);
33
34     public SyncReactorFutureZipDecorator(SyncReactor delegate, ListeningExecutorService executorService) {
35         super(delegate, executorService);
36     }
37
38     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
39                                             final SyncupEntry syncupEntry) throws InterruptedException {
40         try {
41             compressionGuard.acquire();
42             final boolean newTaskNecessary = updateCompressionState(flowcapableNodePath, syncupEntry);
43             if (newTaskNecessary) {
44                 return super.syncup(flowcapableNodePath, syncupEntry);
45             } else {
46                 return Futures.immediateFuture(Boolean.TRUE);
47             }
48         } finally {
49             compressionGuard.release();
50         }
51     }
52
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);
58         } else {
59             return super.doSyncupInFuture(flowcapableNodePath, lastCompressionState);
60         }
61     }
62
63     /**
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.
67      */
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);
73         } else {
74             compressionQueue.put(flowcapableNodePath, syncupEntry);
75         }
76         return previousEntry == null;
77     }
78
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);
85     }
86
87     private SyncupEntry removeLastCompressionState(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath) {
88         try {
89             try {
90                 compressionGuard.acquire();
91             } catch (InterruptedException e) {
92                 return null;
93             }
94             return compressionQueue.remove(flowcapableNodePath);
95         } finally {
96             compressionGuard.release();
97         }
98     }
99 }