NETCONF-125 connection timeout and between timeout are fixed 35/87535/2
authorSarguna Dharani <sarguna.dharani@verizon.com>
Mon, 10 Feb 2020 11:55:44 +0000 (17:25 +0530)
committerRobert Varga <nite@hq.sk>
Mon, 9 Mar 2020 11:36:15 +0000 (11:36 +0000)
Signed-off-by: Sarguna Dharani <sarguna.dharani@verizon.com>
Change-Id: I56275dfa10dcfd4ed1a30fbb6c34169e301269db

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 d32b2488385cae24872d2db67abb4be450195b8d..b0de043e515bb492612d9a0c23fc51174b23aa4b 100644 (file)
@@ -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<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 37453eb082340d14ca8690b0dac35df3a348e1d4..cddffb2e4b4dd61df8fdf53b2266dda3cee91ce5 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;
@@ -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);
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