Bug-2208: connect and re-establish connection strategy for pcc-mock 00/17800/1
authorMilos Fabian <milfabia@cisco.com>
Mon, 6 Apr 2015 17:12:12 +0000 (19:12 +0200)
committerMilos Fabian <milfabia@cisco.com>
Mon, 6 Apr 2015 17:27:44 +0000 (19:27 +0200)
-new argument --reconnect <number>

-the number specifies how many seconds waits for new reconnect attempt
-if the argument is ommited, pcc will never try to reconnect

Change-Id: Icdb965c3cf7f504ab51e012dcd1272283f849327
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/Main.java
pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcher.java

index 74d8e526fba9e397008067bcc1ce2f4af97ac67c..6b032b94203d8223da061c5ac2fe511cc9022770 100644 (file)
@@ -24,9 +24,12 @@ import java.net.UnknownHostException;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
-import org.opendaylight.protocol.framework.ReconnectImmediatelyStrategy;
+import org.opendaylight.protocol.framework.NeverReconnectStrategy;
+import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
 import org.opendaylight.protocol.framework.SessionListenerFactory;
 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
+import org.opendaylight.protocol.framework.TimedReconnectStrategy;
 import org.opendaylight.protocol.pcep.PCEPSessionListener;
 import org.opendaylight.protocol.pcep.ietf.initiated00.CrabbeInitiatedActivator;
 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
@@ -70,6 +73,7 @@ public final class Main {
         short ka = DEFAULT_KEEP_ALIVE;
         short dt = DEFAULT_DEAD_TIMER;
         String password = null;
+        int reconnectTime = -1;
 
         getRootLogger(lc).setLevel(ch.qos.logback.classic.Level.INFO);
         int argIdx = 0;
@@ -92,17 +96,19 @@ public final class Main {
                 dt = Short.valueOf(args[++argIdx]);
             } else if (args[argIdx].equals("--password")) {
                 password = args[++argIdx];
+            } else if (args[argIdx].equals("--reconnect")) {
+                reconnectTime = Integer.valueOf(args[++argIdx]).intValue() * 1000;
             } else {
                 LOG.warn("WARNING: Unrecognized argument: {}", args[argIdx]);
             }
             argIdx++;
         }
-        createPCCs(lsps, pcError, pccCount, localAddress, remoteAddress, ka, dt, password);
+        createPCCs(lsps, pcError, pccCount, localAddress, remoteAddress, ka, dt, password, reconnectTime);
     }
 
     public static void createPCCs(final int lspsPerPcc, final boolean pcerr, final int pccCount,
             final InetSocketAddress localAddress, final List<InetSocketAddress> remoteAddress, final short keepalive, final short deadtimer,
-            final String password) throws InterruptedException, ExecutionException {
+            final String password, final int reconnectTime) throws InterruptedException, ExecutionException {
         startActivators();
         InetAddress currentAddress = localAddress.getAddress();
         final Open openMessage = getOpenMessage(keepalive, deadtimer);
@@ -110,17 +116,18 @@ public final class Main {
                 getSessionNegotiatorFactory(openMessage));
         for (int i = 0; i < pccCount; i++) {
             createPCC(lspsPerPcc, pcerr, new InetSocketAddress(currentAddress, localAddress.getPort()),
-                    remoteAddress, openMessage, pccDispatcher, password);
+                    remoteAddress, openMessage, pccDispatcher, password, reconnectTime);
             currentAddress = InetAddresses.increment(currentAddress);
         }
     }
 
+    @SuppressWarnings("deprecation")
     private static void createPCC(final int lspsPerPcc, final boolean pcerr, final InetSocketAddress localAddress,
             final List<InetSocketAddress> remoteAddress, final Open openMessage, final PCCDispatcher pccDispatcher,
-            final String password) throws InterruptedException, ExecutionException {
+            final String password, final int reconnectTime) throws InterruptedException, ExecutionException {
         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = getSessionNegotiatorFactory(openMessage);
         for (final InetSocketAddress pceAddress : remoteAddress) {
-            pccDispatcher.createClient(localAddress, pceAddress, new ReconnectImmediatelyStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT), new SessionListenerFactory<PCEPSessionListener>() {
+            pccDispatcher.createClient(localAddress, pceAddress, reconnectTime == -1 ? getNeverReconnectStrategyFactory() : getTimedReconnectStrategyFactory(reconnectTime), new SessionListenerFactory<PCEPSessionListener>() {
                 @Override
                 public PCEPSessionListener getSessionListener() {
                     return new SimpleSessionListener(lspsPerPcc, pcerr, localAddress.getAddress());
@@ -129,6 +136,7 @@ public final class Main {
         }
     }
 
+    @SuppressWarnings("deprecation")
     private static SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> getSessionNegotiatorFactory(final Open openMessage) {
         return new DefaultPCEPSessionNegotiatorFactory(openMessage, 0);
     }
@@ -182,4 +190,26 @@ public final class Main {
         activator.start(ctx);
     }
 
+    @SuppressWarnings("deprecation")
+    private static ReconnectStrategyFactory getNeverReconnectStrategyFactory() {
+        return new ReconnectStrategyFactory() {
+
+            @Override
+            public ReconnectStrategy createReconnectStrategy() {
+                return new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT);
+            }
+        };
+    }
+
+    @SuppressWarnings("deprecation")
+    private static ReconnectStrategyFactory getTimedReconnectStrategyFactory(final int reconnectTime) {
+        return new ReconnectStrategyFactory() {
+
+            @Override
+            public ReconnectStrategy createReconnectStrategy() {
+                return new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT, reconnectTime, 1.0, null, null, null);
+            }
+        };
+    }
+
 }
index c1d410d3d5331fd7c764c17b5fda3164642424fe..7592305d3e5f6e430ddadfa806f4e8be8b5cac70 100644 (file)
@@ -11,10 +11,9 @@ package org.opendaylight.protocol.pcep.pcc.mock;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
-import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.Promise;
 import java.net.InetSocketAddress;
-import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
 import org.opendaylight.protocol.framework.SessionListenerFactory;
 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
 import org.opendaylight.protocol.pcep.PCEPSessionListener;
@@ -63,12 +62,12 @@ public final class PCCDispatcher extends PCEPDispatcherImpl {
         }
     }
 
-    public synchronized Future<PCEPSessionImpl> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
-            final ReconnectStrategy strategy, final SessionListenerFactory<PCEPSessionListener> listenerFactory,
+    public synchronized void createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
+            final ReconnectStrategyFactory strategyFactory, final SessionListenerFactory<PCEPSessionListener> listenerFactory,
             final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory, final KeyMapping keys) {
         this.localAddress = localAddress;
         this.keys = keys;
-        final Future<PCEPSessionImpl> futureClient = super.createClient(remoteAddress, strategy, new PipelineInitializer<PCEPSessionImpl>() {
+        super.createReconnectingClient(remoteAddress, strategyFactory, new PipelineInitializer<PCEPSessionImpl>() {
             @Override
             public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
                 ch.pipeline().addLast(PCCDispatcher.this.factory.getDecoders());
@@ -79,7 +78,6 @@ public final class PCCDispatcher extends PCEPDispatcherImpl {
         });
         this.localAddress = null;
         this.keys = null;
-        return futureClient;
     }
 
     private static final class DeafultKeyAccessFactory {