NETCONF-125 connection timeout and between timeout are fixed 96/88296/1
authorSarguna Dharani <sarguna.dharani@verizon.com>
Mon, 10 Feb 2020 11:55:44 +0000 (17:25 +0530)
committerRobert Varga <nite@hq.sk>
Mon, 16 Mar 2020 09:49:17 +0000 (09:49 +0000)
Signed-off-by: Sarguna Dharani <sarguna.dharani@verizon.com>
Change-Id: I56275dfa10dcfd4ed1a30fbb6c34169e301269db
(cherry picked from commit fe5d5bcfdc2dff7c6fe83e1c6b7dbb69d386f06c)
(cherry picked from commit 14a729f9688c77c6163fc44ee830e425d432e0e8)

netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientSessionNegotiatorFactory.java
netconf/netconf-client/src/main/java/org/opendaylight/netconf/client/SshClientChannelInitializer.java
netconf/netconf-client/src/test/java/org/opendaylight/netconf/client/SshClientChannelInitializerTest.java
netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java
netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java
netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerTest.java

index 1ffe7a132f5d5303736ad29fb42ee44f3431db7e..0606ca44c9130dfdf64f14e263ac004d1e6a619d 100644 (file)
@@ -102,6 +102,10 @@ public class NetconfClientSessionNegotiatorFactory
         this.clientCapabilities = capabilities;
     }
 
+    public long getConnectionTimeoutMillis() {
+        return connectionTimeoutMillis;
+    }
+
     @Override
     public NetconfClientSessionNegotiator getSessionNegotiator(
             final NetconfSessionListenerFactory<NetconfClientSessionListener> sessionListenerFactory,
index 66278ef2ec853be69ade42ffdc1ddad7f15938de..3f8c35ce98d0b94164377ab2dbad70428023a7ea 100644 (file)
@@ -49,5 +49,6 @@ final class SshClientChannelInitializer extends AbstractChannelInitializer<Netco
                                                final Promise<NetconfClientSession> promise) {
         ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
                 negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
+        ch.config().setConnectTimeoutMillis((int)negotiatorFactory.getConnectionTimeoutMillis());
     }
 }
index a13abada420ee9926735c9b6ce813ea8e925e31b..73599e3af005477c4defaa002da2585310d680bf 100644 (file)
@@ -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<NetconfClientSession> promise = mock(Promise.class);
         doReturn("").when(promise).toString();
index 91d0ba99d526589baeb998ec11fdb4355a203a48..622b4531e4db00c3b591cce3d23b1719a954fc2b 100644 (file)
@@ -11,7 +11,6 @@ import com.google.common.base.Preconditions;
 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;
@@ -51,7 +50,6 @@ final class NetconfSessionPromise<S extends NetconfSession> 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());
index 513873cbdd6c0f678f9eb07ee46c5cf479e55e63..6a3f07b2109391d5a6fd133cc6c4bb6dc29b27be 100644 (file)
@@ -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;
@@ -104,7 +105,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);
index bec8a3e021c571e68e9a176d9220cdbeef4b4017..9f8ec61da82707c29231ffe372d48b075a4a892b 100644 (file)
@@ -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