BGPCEP-736: BMP Testtool retry connection 49/66549/5
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Fri, 15 Dec 2017 11:22:00 +0000 (12:22 +0100)
committerClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Sat, 24 Feb 2018 14:47:33 +0000 (14:47 +0000)
capability

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

index a3d819cbf513688d920d75e600c73fb2b9c4ab3b..2caae84ab6878e6f5c17cbdef98cd0cb8711d495 100644 (file)
@@ -8,20 +8,24 @@
 
 package org.opendaylight.protocol.bmp.mock;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import io.netty.bootstrap.Bootstrap;
 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;
@@ -33,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;
 
@@ -41,8 +45,8 @@ final class BmpMockDispatcher {
     private final EventLoopGroup workerGroup = new NioEventLoopGroup();
 
     BmpMockDispatcher(final BmpMessageRegistry registry, final BmpSessionFactory sessionFactory) {
-        this.sessionFactory = Preconditions.checkNotNull(sessionFactory);
-        Preconditions.checkNotNull(registry);
+        this.sessionFactory = requireNonNull(sessionFactory);
+        requireNonNull(registry);
         this.hf = new BmpHandlerFactory(registry);
     }
 
@@ -67,14 +71,16 @@ final class BmpMockDispatcher {
         return bootstrap;
     }
 
-    ChannelFuture createClient(final SocketAddress localAddress, final SocketAddress remoteAddress) {
-        Preconditions.checkNotNull(localAddress);
-        Preconditions.checkNotNull(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;
     }
 
@@ -96,10 +102,36 @@ final class BmpMockDispatcher {
     }
 
     ChannelFuture createServer(final InetSocketAddress localAddress) {
-        Preconditions.checkNotNull(localAddress);
+        requireNonNull(localAddress);
         final ServerBootstrap serverBootstrap = createServerInstance();
         final ChannelFuture channelFuture = serverBootstrap.bind(localAddress);
         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 0c5bd6b9b1c875d126f3d1b125f30b280ddf3681..84ddfcc4bae826d44bd06ae29d4717374dfcfc45 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();