BGPCEP-736: BMP Testtool retry connection 05/66505/1
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Fri, 15 Dec 2017 11:22:00 +0000 (12:22 +0100)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Fri, 15 Dec 2017 11:50:58 +0000 (12:50 +0100)
capability

Change-Id: I70169ff59c621e6a0c60b9b185bf9e2a2337b8b7
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
bmp/bmp-mock/src/main/java/org/opendaylight/protocol/bmp/mock/BmpMockDispatcher.java
bmp/bmp-mock/src/main/java/org/opendaylight/protocol/bmp/mock/BmpMockSession.java

index fb94861819539d6d6ba7a6a702fe71f18a02091a..2caae84ab6878e6f5c17cbdef98cd0cb8711d495 100644 (file)
@@ -15,14 +15,17 @@ import io.netty.bootstrap.ServerBootstrap;
 import io.netty.buffer.PooledByteBufAllocator;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoop;
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.channel.socket.nio.NioSocketChannel;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
+import java.util.concurrent.TimeUnit;
 import org.opendaylight.protocol.bmp.api.BmpSessionFactory;
 import org.opendaylight.protocol.bmp.impl.BmpHandlerFactory;
 import org.opendaylight.protocol.bmp.spi.registry.BmpMessageRegistry;
@@ -34,7 +37,7 @@ final class BmpMockDispatcher {
     private static final Logger LOG = LoggerFactory.getLogger(BmpMockDispatcher.class);
     private static final int CONNECT_TIMEOUT = 2000;
     private static final int MAX_CONNECTIONS_COUNT = 128;
-
+    private static final int INITIAL_BACKOFF = 15_000;
     private final BmpHandlerFactory hf;
     private final BmpSessionFactory sessionFactory;
 
@@ -68,14 +71,16 @@ final class BmpMockDispatcher {
         return bootstrap;
     }
 
-    ChannelFuture createClient(final SocketAddress localAddress, final SocketAddress remoteAddress) {
+    ChannelFuture createClient(final SocketAddress localAddress, final InetSocketAddress remoteAddress) {
         requireNonNull(localAddress);
         requireNonNull(remoteAddress);
 
         // ideally we should use Bootstrap clones here
         final Bootstrap bootstrap = createClientInstance(localAddress);
+        bootstrap.remoteAddress(remoteAddress);
         final ChannelFuture channelFuture = bootstrap.connect(remoteAddress);
         LOG.info("BMP client {} <--> {} deployed", localAddress, remoteAddress);
+        channelFuture.addListener(new BootstrapListener(bootstrap, remoteAddress));
         return channelFuture;
     }
 
@@ -103,4 +108,30 @@ final class BmpMockDispatcher {
         LOG.info("Initiated BMP server at {}.", localAddress);
         return channelFuture;
     }
+
+    private static class BootstrapListener implements ChannelFutureListener {
+        private final Bootstrap bootstrap;
+        private final InetSocketAddress address;
+        private long delay;
+
+        BootstrapListener(final Bootstrap bootstrap, final InetSocketAddress address) {
+            this.bootstrap = bootstrap;
+            this.address = address;
+            this.delay = INITIAL_BACKOFF;
+        }
+
+        @Override
+        public void operationComplete(final ChannelFuture cf) throws Exception {
+            if (cf.isCancelled()) {
+                LOG.debug("Connection {} cancelled!", cf);
+            } else if (cf.isSuccess()) {
+                LOG.debug("Connection {} succeeded!", cf);
+            } else {
+                final EventLoop loop = cf.channel().eventLoop();
+                loop.schedule(() -> this.bootstrap.connect().addListener(this), this.delay, TimeUnit.MILLISECONDS);
+                LOG.info("The connection try to BMP router {} failed. Next reconnection attempt in {} milliseconds.",
+                        this.address, this.delay);
+            }
+        }
+    }
 }
index c3e76159335aed7d31b2dd7cb0dbda963cbc7e01..ae0c2fbace5230abc15861edf4b35c2165a2cdbb 100644 (file)
@@ -66,7 +66,7 @@ public final class BmpMockSession extends SimpleChannelInboundHandler<Notificati
     public void channelActive(final ChannelHandlerContext ctx) {
         this.channel = ctx.channel();
         this.channel.closeFuture().addListener((ChannelFutureListener) future ->
-                LOG.info("BMP session {} final successfully established.", BmpMockSession.this.channel));
+                LOG.info("BMP session {} close.", BmpMockSession.this.channel));
         LOG.info("BMP session {} successfully established.", this.channel);
         final InetSocketAddress localAddress = (InetSocketAddress) this.channel.localAddress();
         this.remoteAddress = (InetSocketAddress) this.channel.remoteAddress();