Bug-2208: Mocked PCC can connect to multiple PCEs. 80/13180/2
authorMilos Fabian <milfabia@cisco.com>
Wed, 12 Nov 2014 13:02:46 +0000 (14:02 +0100)
committerMilos Fabian <milfabia@cisco.com>
Mon, 1 Dec 2014 13:33:04 +0000 (13:33 +0000)
-input argument "--remote-address" can handle multiple
ip addresses (separated by comma).

Change-Id: I9e32781b9ced77d6351b4d681ed7677e357b039e
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/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java

index d7d17178cd44d0411c255a695fdff1ac2be6a6b4..04b9cad12e57107125ec7d71390ef7cdb4c82b78 100644 (file)
@@ -10,14 +10,18 @@ package org.opendaylight.protocol.pcep.pcc.mock;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.LoggerContext;
+import com.google.common.base.Function;
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import com.google.common.net.InetAddresses;
 import io.netty.util.concurrent.DefaultPromise;
 import io.netty.util.concurrent.GlobalEventExecutor;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
+import java.util.Arrays;
+import java.util.List;
 import java.util.concurrent.ExecutionException;
 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
 import org.opendaylight.protocol.framework.SessionListenerFactory;
@@ -44,7 +48,7 @@ public final class Main {
 
     public static void main(String[] args) throws InterruptedException, ExecutionException, UnknownHostException {
         InetAddress localAddress = InetAddress.getByName("127.0.0.1");
-        InetAddress remoteAddress = InetAddress.getByName("127.0.0.1");
+        List<InetAddress> remoteAddress = Lists.newArrayList(InetAddress.getByName("127.0.0.1"));
         int pccCount = 1;
         int lsps = 1;
         boolean pcError = false;
@@ -56,7 +60,7 @@ public final class Main {
             if (args[argIdx].equals("--local-address")) {
                 localAddress = InetAddress.getByName(args[++argIdx]);
             } else if (args[argIdx].equals("--remote-address")) {
-                remoteAddress = InetAddress.getByName(args[++argIdx]);
+                remoteAddress = parseAddresses(args[++argIdx]);
             } else if (args[argIdx].equals("--pcc")) {
                 pccCount = Integer.valueOf(args[++argIdx]);
             } else if (args[argIdx].equals("--lsp")) {
@@ -74,34 +78,33 @@ public final class Main {
     }
 
     public static void createPCCs(final int lspsPerPcc, final boolean pcerr, final int pccCount,
-            final InetAddress localAddress, final InetAddress remoteAddress) throws InterruptedException, ExecutionException {
+            final InetAddress localAddress, final List<InetAddress> remoteAddress) throws InterruptedException, ExecutionException {
         final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(
                 new OpenBuilder().setKeepalive(DEFAULT_KEEP_ALIVE).setDeadTimer(DEFAULT_DEAD_TIMER).setSessionId((short) 0).build(), 0);
 
         final StatefulActivator activator07 = new StatefulActivator();
-        final PCCActivator activator = new PCCActivator();
         activator07.start(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance());
-        activator.start(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance());
         final PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<>(snf, new PCEPHandlerFactory(
                 ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry()),
                 new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
-        final InetAddress pceAddress = remoteAddress;
-        InetAddress currentAddress = localAddress;
-        int i = 0;
-        while (i < pccCount) {
-            final InetAddress pccAddress = currentAddress;
-            pcc.createClient(new InetSocketAddress(pccAddress, 0), new InetSocketAddress(pceAddress, DEFAULT_PORT),
-                    new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT),
-                    new SessionListenerFactory<PCEPSessionListener>() {
+        for (final InetAddress pceAddress : remoteAddress) {
+            InetAddress currentAddress = localAddress;
+            int i = 0;
+            while (i < pccCount) {
+                final InetAddress pccAddress = currentAddress;
+                pcc.createClient(new InetSocketAddress(pccAddress, 0), new InetSocketAddress(pceAddress, DEFAULT_PORT),
+                        new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT),
+                        new SessionListenerFactory<PCEPSessionListener>() {
 
-                        @Override
-                        public PCEPSessionListener getSessionListener() {
-                            return new SimpleSessionListener(lspsPerPcc, pcerr, pccAddress);
-                        }
-                    }).get();
-            i++;
-            currentAddress = InetAddresses.increment(currentAddress);
+                            @Override
+                            public PCEPSessionListener getSessionListener() {
+                                return new SimpleSessionListener(lspsPerPcc, pcerr, pccAddress);
+                            }
+                        }).get();
+                i++;
+                currentAddress = InetAddresses.increment(currentAddress);
+            }
         }
     }
 
@@ -114,4 +117,17 @@ public final class Main {
         });
     }
 
+    private static List<InetAddress> parseAddresses(final String address) {
+        return Lists.transform(Arrays.asList(address.split(",")), new Function<String, InetAddress>() {
+            @Override
+            public InetAddress apply(String input) {
+                try {
+                    return InetAddress.getByName(input);
+                } catch (UnknownHostException e) {
+                    throw new RuntimeException("Not an IP address: " + input, e);
+                }
+            }
+        });
+    }
+
 }
index 31906002c32100565fcc6d5ecfae39f4f717fa67..173a9dd89470e956baffb818ee986cb5eab22952 100644 (file)
@@ -36,6 +36,18 @@ public class PCCMockTest {
         }
     }
 
+    @Test
+    public void testMockPCCToManyPCE() {
+        try {
+            org.opendaylight.protocol.pcep.testtool.Main.main(new String[]{"-a", "127.0.4.0:4189", "-ka", "10", "-d", "0", "--stateful", "--active"});
+            org.opendaylight.protocol.pcep.testtool.Main.main(new String[]{"-a", "127.0.2.0:4189", "-ka", "10", "-d", "0", "--stateful", "--active"});
+            org.opendaylight.protocol.pcep.testtool.Main.main(new String[]{"-a", "127.0.3.0:4189", "-ka", "10", "-d", "0", "--stateful", "--active"});
+            Main.main(new String[] {"--local-address", "127.0.0.1", "--remote-address", "127.0.4.0,127.0.2.0,127.0.3.0"});
+        } catch (Exception e) {
+            Assert.fail(e.getMessage());
+        }
+    }
+
     @Test
     public void testSrpObjectParser() throws PCEPDeserializerException {
         final byte[] bytes = {