Fix findbugs violations in applications
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorRetryDecorator.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.MoreExecutors;
14 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
15 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
16 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
17 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Adding retry mechanism in case of unsuccessful syncup.
26  */
27 public class SyncReactorRetryDecorator implements SyncReactor {
28
29     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorRetryDecorator.class);
30     private final SyncReactor delegate;
31     private final ReconciliationRegistry reconciliationRegistry;
32
33     public SyncReactorRetryDecorator(final SyncReactor delegate, final ReconciliationRegistry reconciliationRegistry) {
34         this.delegate = delegate;
35         this.reconciliationRegistry = reconciliationRegistry;
36     }
37
38     @Override
39     public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
40                                             final SyncupEntry syncupEntry) {
41
42         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
43         if (syncupEntry.isOptimizedConfigDelta() && reconciliationRegistry.isRegistered(nodeId)) {
44             LOG.debug("Config change ignored because {} is in reconcile.", nodeId.getValue());
45             return Futures.immediateFuture(Boolean.TRUE);
46         }
47
48         ListenableFuture<Boolean> syncupResult = delegate.syncup(flowcapableNodePath,syncupEntry);
49
50         return Futures.transform(syncupResult, result -> {
51             if (result) {
52                 reconciliationRegistry.unregisterIfRegistered(nodeId);
53             } else {
54                 reconciliationRegistry.register(nodeId);
55             }
56
57             return result;
58         }, MoreExecutors.directExecutor());
59     }
60 }