Translate GR RPC 54/78754/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Dec 2018 12:43:56 +0000 (13:43 +0100)
committerRobert Varga <nite@hq.sk>
Thu, 13 Dec 2018 14:48:47 +0000 (14:48 +0000)
Rather than blocking current thread, bridge futures so that we
return without blocking.

Change-Id: I8fec49c96b104b952de7a87b83b1bc4daf17f0d6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BgpPeerRpc.java

index 146a1d51ae13dcba610d434e3d3e247991323bb0..f92de681f8b7d8ee64b870dad3f1359be1f091ec 100644 (file)
@@ -14,9 +14,9 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.JdkFutureAdapters;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.SettableFuture;
 import io.netty.channel.ChannelFuture;
 import java.util.Set;
-import java.util.concurrent.ExecutionException;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
 import org.opendaylight.protocol.bgp.rib.spi.PeerRPCs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefresh;
@@ -43,7 +43,6 @@ public class BgpPeerRpc implements BgpPeerRpcService {
     private static final Logger LOG = LoggerFactory.getLogger(BgpPeerRpc.class);
     private static final String FAILURE_MSG = "Failed to send Route Refresh message";
     private static final String FAILURE_RESET_SESSION_MSG = "Failed to reset session";
-    private static final String FAILURE_GRACEFUL_RESTART_MSG = "Failed to perform graceful restart";
 
     private final BGPSession session;
     private final Set<TablesKey> supportedFamilies;
@@ -68,28 +67,22 @@ public class BgpPeerRpc implements BgpPeerRpcService {
     }
 
     @Override
-    public ListenableFuture<RpcResult<RestartGracefullyOutput>> restartGracefully(RestartGracefullyInput input) {
-        final ListenableFuture<?> f = this.peerRPCs.restartGracefully(input.getSelectionDeferralTime());
-        final RpcResultBuilder<RestartGracefullyOutput> rpcResult = RpcResultBuilder.failed();
-        Futures.addCallback(f, new FutureCallback<Object>() {
-                    @Override
-                    public void onSuccess(Object result) {
-                        rpcResult.success(new RestartGracefullyOutputBuilder().build()).build();
-                    }
+    public ListenableFuture<RpcResult<RestartGracefullyOutput>> restartGracefully(final RestartGracefullyInput input) {
+        final SettableFuture<RpcResult<RestartGracefullyOutput>> ret = SettableFuture.create();
+        Futures.addCallback(peerRPCs.restartGracefully(input.getSelectionDeferralTime()), new FutureCallback<Object>() {
+            @Override
+            public void onSuccess(final Object result) {
+                ret.set(RpcResultBuilder.success(new RestartGracefullyOutputBuilder().build()).build());
+            }
 
-                    @Override
-                    public void onFailure(Throwable throwable) {
-                        rpcResult.withError(ErrorType.RPC, String.format("%s: %s", FAILURE_GRACEFUL_RESTART_MSG,
-                                throwable.getMessage()));
-                    }
-                },
-                MoreExecutors.directExecutor());
-        try {
-            f.get();
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.error(FAILURE_GRACEFUL_RESTART_MSG, e);
-        }
-        return Futures.immediateFuture(rpcResult.build());
+            @Override
+            public void onFailure(final Throwable throwable) {
+                LOG.error("Failed to perform graceful restart", throwable);
+                ret.set(RpcResultBuilder.<RestartGracefullyOutput>failed()
+                    .withError(ErrorType.RPC, throwable.getMessage()).build());
+            }
+        }, MoreExecutors.directExecutor());
+        return ret;
     }
 
     @Override