From 14a729f9688c77c6163fc44ee830e425d432e0e8 Mon Sep 17 00:00:00 2001 From: Sarguna Dharani Date: Mon, 10 Feb 2020 17:25:44 +0530 Subject: [PATCH] NETCONF-125 connection timeout and between timeout are fixed Signed-off-by: Sarguna Dharani Change-Id: I56275dfa10dcfd4ed1a30fbb6c34169e301269db (cherry picked from commit fe5d5bcfdc2dff7c6fe83e1c6b7dbb69d386f06c) --- .../client/NetconfClientSessionNegotiatorFactory.java | 4 ++++ .../netconf/client/SshClientChannelInitializer.java | 1 + .../netconf/client/SshClientChannelInitializerTest.java | 5 +++++ .../netconf/nettyutil/NetconfSessionPromise.java | 2 -- .../nettyutil/handler/ssh/client/AsyncSshHandler.java | 4 +++- .../nettyutil/handler/ssh/client/AsyncSshHandlerTest.java | 7 +++++++ 6 files changed, 20 insertions(+), 3 deletions(-) diff --git a/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorFactory.java b/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorFactory.java index 1ffe7a132f..0606ca44c9 100644 --- a/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorFactory.java +++ b/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorFactory.java @@ -102,6 +102,10 @@ public class NetconfClientSessionNegotiatorFactory this.clientCapabilities = capabilities; } + public long getConnectionTimeoutMillis() { + return connectionTimeoutMillis; + } + @Override public NetconfClientSessionNegotiator getSessionNegotiator( final NetconfSessionListenerFactory sessionListenerFactory, diff --git a/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/SshClientChannelInitializer.java b/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/SshClientChannelInitializer.java index 66278ef2ec..3f8c35ce98 100644 --- a/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/SshClientChannelInitializer.java +++ b/netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/SshClientChannelInitializer.java @@ -49,5 +49,6 @@ final class SshClientChannelInitializer extends AbstractChannelInitializer promise) { ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR, negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise)); + ch.config().setConnectTimeoutMillis((int)negotiatorFactory.getConnectionTimeoutMillis()); } } diff --git a/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/SshClientChannelInitializerTest.java b/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/SshClientChannelInitializerTest.java index a13abada42..73599e3af0 100644 --- a/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/SshClientChannelInitializerTest.java +++ b/netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/SshClientChannelInitializerTest.java @@ -15,6 +15,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import io.netty.channel.Channel; +import io.netty.channel.ChannelConfig; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.util.concurrent.Promise; @@ -41,6 +42,10 @@ public class SshClientChannelInitializerTest { doReturn("").when(channel).toString(); doReturn(pipeline).when(pipeline).addFirst(any(ChannelHandler.class)); doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class)); + ChannelConfig channelConfig = mock(ChannelConfig.class); + doReturn(channelConfig).when(channel).config(); + doReturn(1L).when(negotiatorFactory).getConnectionTimeoutMillis(); + doReturn(channelConfig).when(channelConfig).setConnectTimeoutMillis(1); Promise promise = mock(Promise.class); doReturn("").when(promise).toString(); diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java index d32b248838..b0de043e51 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java @@ -13,7 +13,6 @@ import static java.util.Objects.requireNonNull; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelOption; import io.netty.util.concurrent.DefaultPromise; import io.netty.util.concurrent.EventExecutor; import io.netty.util.concurrent.Future; @@ -53,7 +52,6 @@ final class NetconfSessionPromise extends DefaultPromi if (this.address.isUnresolved()) { this.address = new InetSocketAddress(this.address.getHostName(), this.address.getPort()); } - this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout); final ChannelFuture connectFuture = this.bootstrap.connect(this.address); // Add listener that attempts reconnect by invoking this method again. connectFuture.addListener(new BootstrapConnectListener()); diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java index 37453eb082..cddffb2e4b 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java @@ -16,6 +16,7 @@ import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import java.io.IOException; import java.net.SocketAddress; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.sshd.client.SshClient; import org.apache.sshd.client.channel.ClientChannel; @@ -103,7 +104,8 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter { private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) throws IOException { LOG.debug("Starting SSH to {} on channel: {}", address, ctx.channel()); - final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address); + final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address) + .verify(ctx.channel().config().getConnectTimeoutMillis(), TimeUnit.MILLISECONDS); sshConnectionFuture.addListener(future -> { if (future.isConnected()) { handleSshSessionCreated(future, ctx); diff --git a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java index bec8a3e021..9f8ec61da8 100644 --- a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java +++ b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java @@ -29,6 +29,7 @@ import com.google.common.util.concurrent.SettableFuture; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; +import io.netty.channel.ChannelConfig; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; @@ -36,6 +37,7 @@ import io.netty.channel.DefaultChannelPromise; import io.netty.channel.EventLoop; import java.io.IOException; import java.net.SocketAddress; +import java.util.concurrent.TimeUnit; import org.apache.sshd.client.SshClient; import org.apache.sshd.client.channel.ChannelSubsystem; import org.apache.sshd.client.channel.ClientChannel; @@ -76,6 +78,8 @@ public class AsyncSshHandlerTest { private SocketAddress localAddress; @Mock private EventLoop eventLoop; + @Mock + private ChannelConfig channelConfig; private AsyncSshHandler asyncSshHandler; @@ -166,6 +170,9 @@ public class AsyncSshHandlerTest { } }, MoreExecutors.directExecutor()); doReturn(connectFuture).when(sshClient).connect("usr", remoteAddress); + doReturn(channelConfig).when(channel).config(); + doReturn(1).when(channelConfig).getConnectTimeoutMillis(); + doReturn(connectFuture).when(connectFuture).verify(1,TimeUnit.MILLISECONDS); } @Test -- 2.36.6