2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
\r
11 import com.google.common.util.concurrent.ListenableFuture;
\r
12 import com.google.common.util.concurrent.ListeningExecutorService;
\r
13 import java.util.concurrent.Callable;
\r
14 import java.util.concurrent.TimeUnit;
\r
15 import java.util.concurrent.TimeoutException;
\r
16 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
\r
17 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
\r
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
21 import org.slf4j.Logger;
\r
22 import org.slf4j.LoggerFactory;
\r
25 * Decorator for running delegate syncup in Future.
\r
27 public class SyncReactorFutureDecorator implements SyncReactor {
\r
29 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorFutureDecorator.class);
\r
31 private final SyncReactor delegate;
\r
32 private final ListeningExecutorService executorService;
\r
34 public static final String FRM_RPC_CLIENT_PREFIX = "FRM-RPC-client-";
\r
36 public SyncReactorFutureDecorator(SyncReactor delegate, ListeningExecutorService executorService) {
\r
37 this.delegate = delegate;
\r
38 this.executorService = executorService;
\r
41 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
\r
42 final FlowCapableNode configTree, final FlowCapableNode operationalTree) throws InterruptedException {
\r
43 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
\r
44 LOG.trace("syncup {}", nodeId.getValue());
\r
46 final ListenableFuture<Boolean> syncup = executorService.submit(new Callable<Boolean>() {
\r
47 public Boolean call() throws Exception {
\r
48 final String oldThreadName = updateThreadName(nodeId);
\r
51 final Boolean ret = doSyncupInFuture(flowcapableNodePath, configTree, operationalTree)
\r
52 .get(10000, TimeUnit.MILLISECONDS);
\r
53 LOG.trace("ret {} {}", nodeId.getValue(), ret);
\r
55 } catch (TimeoutException e) {
\r
56 LOG.error("doSyncupInFuture timeout occured {}", nodeId.getValue(), e);
\r
59 updateThreadName(oldThreadName);
\r
67 protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
\r
68 final FlowCapableNode configTree, final FlowCapableNode operationalTree)
\r
69 throws InterruptedException {
\r
70 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
\r
71 LOG.trace("doSyncupInFuture {}", nodeId.getValue());
\r
73 return delegate.syncup(flowcapableNodePath, configTree, operationalTree);
\r
76 static String threadName() {
\r
77 final Thread currentThread = Thread.currentThread();
\r
78 return currentThread.getName();
\r
81 protected String updateThreadName(NodeId nodeId) {
\r
82 final Thread currentThread = Thread.currentThread();
\r
83 final String oldName = currentThread.getName();
\r
85 if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) {
\r
86 currentThread.setName(oldName + "@" + nodeId.getValue());
\r
88 LOG.warn("try to update foreign thread name {} {}", nodeId, oldName);
\r
90 } catch (Exception e) {
\r
91 LOG.error("failed updating threadName {}", nodeId, e);
\r
96 protected String updateThreadName(String name) {
\r
97 final Thread currentThread = Thread.currentThread();
\r
98 final String oldName = currentThread.getName();
\r
100 if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) {
\r
101 currentThread.setName(name);
\r
103 LOG.warn("try to update foreign thread name {} {}", oldName, name);
\r
105 } catch (Exception e) {
\r
106 LOG.error("failed updating threadName {}", name, e);
\r