X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=applications%2Fforwardingrules-sync%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fapplications%2Ffrsync%2Fimpl%2FSyncReactorFutureDecorator.java;h=bb418b2b447a57d7d7b8b12f0240d07aafa5d64a;hb=26d73dba20bcdcbe3d97e8b41e6ee8801495569e;hp=33db6ea9c54f467253abea1125571fba0a2f2f0d;hpb=70c266f1eef0a823f29c4ad119a1e8f7e6164507;p=openflowplugin.git diff --git a/applications/forwardingrules-sync/src/main/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureDecorator.java b/applications/forwardingrules-sync/src/main/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureDecorator.java index 33db6ea9c5..bb418b2b44 100644 --- a/applications/forwardingrules-sync/src/main/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureDecorator.java +++ b/applications/forwardingrules-sync/src/main/java/org/opendaylight/openflowplugin/applications/frsync/impl/SyncReactorFutureDecorator.java @@ -1,110 +1,54 @@ -/** - * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.openflowplugin.applications.frsync.impl; - -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.ListeningExecutorService; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import org.opendaylight.openflowplugin.applications.frsync.SyncReactor; -import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil; -import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; -import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Decorator for running delegate syncup in Future. - */ -public class SyncReactorFutureDecorator implements SyncReactor { - - private static final Logger LOG = LoggerFactory.getLogger(SyncReactorFutureDecorator.class); - - private final SyncReactor delegate; - private final ListeningExecutorService executorService; - - public static final String FRM_RPC_CLIENT_PREFIX = "FRM-RPC-client-"; - - public SyncReactorFutureDecorator(SyncReactor delegate, ListeningExecutorService executorService) { - this.delegate = delegate; - this.executorService = executorService; - } - - public ListenableFuture syncup(final InstanceIdentifier flowcapableNodePath, - final FlowCapableNode configTree, final FlowCapableNode operationalTree) throws InterruptedException { - final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath); - LOG.trace("syncup {}", nodeId.getValue()); - - final ListenableFuture syncup = executorService.submit(new Callable() { - public Boolean call() throws Exception { - final String oldThreadName = updateThreadName(nodeId); - - try { - final Boolean ret = doSyncupInFuture(flowcapableNodePath, configTree, operationalTree) - .get(10000, TimeUnit.MILLISECONDS); - LOG.trace("ret {} {}", nodeId.getValue(), ret); - return true; - } catch (TimeoutException e) { - LOG.error("doSyncupInFuture timeout occured {}", nodeId.getValue(), e); - return false; - } finally { - updateThreadName(oldThreadName); - } - } - }); - - return syncup; - } - - protected ListenableFuture doSyncupInFuture(final InstanceIdentifier flowcapableNodePath, - final FlowCapableNode configTree, final FlowCapableNode operationalTree) - throws InterruptedException { - final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath); - LOG.trace("doSyncupInFuture {}", nodeId.getValue()); - - return delegate.syncup(flowcapableNodePath, configTree, operationalTree); - } - - static String threadName() { - final Thread currentThread = Thread.currentThread(); - return currentThread.getName(); - } - - protected String updateThreadName(NodeId nodeId) { - final Thread currentThread = Thread.currentThread(); - final String oldName = currentThread.getName(); - try { - if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) { - currentThread.setName(oldName + "@" + nodeId.getValue()); - } else { - LOG.warn("try to update foreign thread name {} {}", nodeId, oldName); - } - } catch (Exception e) { - LOG.error("failed updating threadName {}", nodeId, e); - } - return oldName; - } - - protected String updateThreadName(String name) { - final Thread currentThread = Thread.currentThread(); - final String oldName = currentThread.getName(); - try { - if (oldName.startsWith(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX)) { - currentThread.setName(name); - } else { - LOG.warn("try to update foreign thread name {} {}", oldName, name); - } - } catch (Exception e) { - LOG.error("failed updating threadName {}", name, e); - } - return oldName; - } -} +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.openflowplugin.applications.frsync.impl; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.opendaylight.openflowplugin.applications.frsync.SyncReactor; +import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil; +import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Decorator for running delegate syncup in Future. + */ +public class SyncReactorFutureDecorator implements SyncReactor { + + private static final Logger LOG = LoggerFactory.getLogger(SyncReactorFutureDecorator.class); + private final SyncReactor delegate; + private final ListeningExecutorService executorService; + + public SyncReactorFutureDecorator(final SyncReactor delegate, final ListeningExecutorService executorService) { + this.delegate = delegate; + this.executorService = executorService; + } + + public ListenableFuture syncup(final InstanceIdentifier flowcapableNodePath, + final SyncupEntry syncupEntry) { + final NodeId nodeId = PathUtil.digNodeId(flowcapableNodePath); + return executorService.submit(() -> { + try { + return doSyncupInFuture(flowcapableNodePath, syncupEntry).get(10000, TimeUnit.MILLISECONDS); + } catch (TimeoutException e) { + LOG.warn("Syncup future timeout occured {}", nodeId.getValue()); + return Boolean.FALSE; + } + }); + } + + protected ListenableFuture doSyncupInFuture(final InstanceIdentifier flowcapableNodePath, + final SyncupEntry syncupEntry) { + return delegate.syncup(flowcapableNodePath, syncupEntry); + } +}