2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
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.openflowplugin.applications.frsync.SyncReactor;
16 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
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;
25 * Decorator for running delegate syncup in Future.
27 public class SyncReactorFutureDecorator implements SyncReactor {
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;
34 public SyncReactorFutureDecorator(SyncReactor delegate, ListeningExecutorService executorService) {
35 this.delegate = delegate;
36 this.executorService = executorService;
39 public ListenableFuture<Boolean> syncup(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
40 final SyncupEntry syncupEntry) throws InterruptedException {
41 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
42 LOG.trace("syncup future decorator: {}", nodeId.getValue());
44 return executorService.submit(() -> {
45 final String oldThreadName = updateThreadName(nodeId);
48 final Boolean ret = doSyncupInFuture(flowcapableNodePath, syncupEntry)
49 .get(10000, TimeUnit.MILLISECONDS);
50 LOG.trace("syncup return in future decorator: {} [{}]", nodeId.getValue(), ret);
52 } catch (TimeoutException e) {
53 LOG.error("doSyncupInFuture timeout occured {}", nodeId.getValue(), e);
56 updateThreadName(oldThreadName);
61 protected ListenableFuture<Boolean> doSyncupInFuture(final InstanceIdentifier<FlowCapableNode> flowcapableNodePath,
62 final SyncupEntry syncupEntry) throws InterruptedException {
63 final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath);
64 LOG.trace("doSyncupInFuture future decorator: {}", nodeId.getValue());
66 return delegate.syncup(flowcapableNodePath, syncupEntry);
69 private String updateThreadName(NodeId nodeId) {
70 final Thread currentThread = Thread.currentThread();
71 final String oldName = currentThread.getName();
73 if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) {
74 currentThread.setName(oldName + "@" + nodeId.getValue());
76 LOG.warn("try to update foreign thread name {} {}", nodeId, oldName);
78 } catch (Exception e) {
79 LOG.error("failed updating threadName {}", nodeId, e);
84 private void updateThreadName(String name) {
85 final Thread currentThread = Thread.currentThread();
86 final String oldName = currentThread.getName();
88 if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) {
89 currentThread.setName(name);
91 LOG.warn("try to update foreign thread name {} {}", oldName, name);
93 } catch (Exception e) {
94 LOG.error("failed updating threadName {}", name, e);