Cleaning and preparation before bug 6170
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorFutureDecorator.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.ListenableFuture;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
17 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
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  * Decorator for running delegate syncup in Future.
26  */
27 public class SyncReactorFutureDecorator implements SyncReactor {
28
29     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorFutureDecorator.class);
30     public static final String FRM_RPC_CLIENT_PREFIX = "FRM-RPC-client-";
31     private final SyncReactor delegate;
32     private final ListeningExecutorService executorService;
33
34     public SyncReactorFutureDecorator(SyncReactor delegate, ListeningExecutorService executorService) {
35         this.delegate = delegate;
36         this.executorService = executorService;
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         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
43         LOG.trace("syncup future {}", nodeId.getValue());
44
45         final ListenableFuture<Boolean> syncup = executorService.submit(() -> {
46             final String oldThreadName = updateThreadName(nodeId);
47
48             try {
49                 final Boolean ret = doSyncupInFuture(flowcapableNodePath, configTree, operationalTree, dsType)
50                         .get(10000, TimeUnit.MILLISECONDS);
51                 LOG.trace("ret {} {}", nodeId.getValue(), ret);
52                 return true;
53             } catch (TimeoutException e) {
54                 LOG.error("doSyncupInFuture timeout occured {}", nodeId.getValue(), e);
55                 return false;
56             } finally {
57                 updateThreadName(oldThreadName);
58             }
59         });
60
61         return syncup;
62     }
63
64     protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
65                                                          final FlowCapableNode configTree, final FlowCapableNode operationalTree,
66                                                          final LogicalDatastoreType dsType) throws InterruptedException {
67         final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
68         LOG.trace("doSyncupInFuture future {}", nodeId.getValue());
69
70         return delegate.syncup(flowcapableNodePath, configTree, operationalTree, dsType);
71     }
72
73     private String updateThreadName(NodeId nodeId) {
74         final Thread currentThread = Thread.currentThread();
75         final String oldName = currentThread.getName();
76         try {
77             if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) {
78                 currentThread.setName(oldName + "@" + nodeId.getValue());
79             } else {
80                 LOG.warn("try to update foreign thread name {} {}", nodeId, oldName);
81             }
82         } catch (Exception e) {
83             LOG.error("failed updating threadName {}", nodeId, e);
84         }
85         return oldName;
86     }
87
88     private void updateThreadName(String name) {
89         final Thread currentThread = Thread.currentThread();
90         final String oldName = currentThread.getName();
91         try {
92             if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) {
93                 currentThread.setName(name);
94             } else {
95                 LOG.warn("try to update foreign thread name {} {}", oldName, name);
96             }
97         } catch (Exception e) {
98             LOG.error("failed updating threadName {}", name, e);
99         }
100     }
101 }