Merge "Bug 6088 - Threads problem on gathering statistics"
[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.base.Function;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
16 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
17 import org.opendaylight.openflowplugin.applications.frsync.util.RetryRegistry;
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
31     private final SyncReactor delegate;
32     private final RetryRegistry retryRegistry;
33
34     public SyncReactorRetryDecorator (final SyncReactor delegate, RetryRegistry retryRegistry) {
35         this.delegate = delegate;
36         this.retryRegistry = retryRegistry;
37     }
38
39     public ListenableFuture<Boolean> syncup (final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
40                                              final FlowCapableNode configTree, final FlowCapableNode operationalTree,
41                                              final LogicalDatastoreType dsType) throws InterruptedException {
42
43         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
44         LOG.trace("syncup retry {}", nodeId.getValue());
45
46         if (dsType == LogicalDatastoreType.CONFIGURATION && retryRegistry.isRegistered(nodeId)) {
47             LOG.trace("Config change ignored because device is in retry [{}]", nodeId);
48             return Futures.immediateFuture(Boolean.FALSE);
49         }
50
51         ListenableFuture<Boolean> syncupResult = delegate.syncup(flowcapableNodePath, configTree, operationalTree, dsType);
52
53         return Futures.transform(syncupResult, new Function<Boolean, Boolean>() {
54             @Override
55             public Boolean apply(Boolean result) {
56                 LOG.trace("syncup ret in retry {}", result);
57                 if (result) {
58                     retryRegistry.unregisterIfRegistered(nodeId);
59                     return true;
60                 } else {
61                     retryRegistry.register(nodeId);
62                     // TODO  elicit statistics gathering if not running actually
63                     // triggerStatisticsGathering(nodeId);
64                     return false;
65                 }
66             }
67         });
68     }
69 }