Bug-2167: Enable pcc-mock to be bindable to differnet source addresses. 50/11850/7
authorMilos Fabian <milfabia@cisco.com>
Thu, 9 Oct 2014 08:58:42 +0000 (10:58 +0200)
committerMilos Fabian <milfabia@cisco.com>
Mon, 13 Oct 2014 11:48:55 +0000 (11:48 +0000)
Change-Id: I72fdb8af053dc9bd6b6cb2d2c4a97173266b3916
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/BindableDispatcher.java [new file with mode: 0644]
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/PCCMock.java
pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java

diff --git a/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/BindableDispatcher.java b/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/BindableDispatcher.java
new file mode 100644 (file)
index 0000000..4470543
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.pcep.pcc.mock;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.util.concurrent.EventExecutor;
+import io.netty.util.concurrent.Future;
+import java.net.InetSocketAddress;
+import org.opendaylight.protocol.framework.AbstractDispatcher;
+import org.opendaylight.protocol.framework.ProtocolSession;
+import org.opendaylight.protocol.framework.ReconnectStrategy;
+import org.opendaylight.protocol.framework.SessionListener;
+
+/**
+ * BindableDispatcher abstract class for creating clients that are bound to a specified local InetSocketAddress.
+ */
+public abstract class BindableDispatcher<S extends ProtocolSession<?>, L extends SessionListener<?, ?, ?>> extends AbstractDispatcher<S, L> {
+
+    private final EventLoopGroup workerGroup;
+
+    protected BindableDispatcher(final EventExecutor executor, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
+        super(executor, bossGroup, workerGroup);
+        this.workerGroup = workerGroup;
+    }
+
+    /**
+     * Creates a client.
+     *
+     * @param localAddress local address
+     * @param remoteAddress remote address
+     * @param connectStrategy Reconnection strategy to be used when initial connection fails
+     *
+     * @return Future representing the connection process. Its result represents the combined success of TCP connection
+     *         as well as session negotiation.
+     */
+    protected Future<S> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
+                final ReconnectStrategy strategy, final PipelineInitializer<S> initializer) {
+        Bootstrap bootstrap = new Bootstrap();
+        bootstrap.localAddress(localAddress).option(ChannelOption.SO_KEEPALIVE, true);
+        customizeBootstrap(bootstrap);
+        bootstrap.group(this.workerGroup);
+        bootstrap.channel(NioSocketChannel.class);
+        return super.createClient(remoteAddress, strategy, bootstrap, initializer);
+    }
+}
index a5889c11ed780b8402f2d4f3b64e0cf67b501115..9bda4d5990f80ce782af7995df70a3d5a907a5d8 100644 (file)
@@ -46,15 +46,19 @@ public final class Main {
             return;
         }
 
-        InetAddress address = null;
+        InetAddress localAddress = InetAddress.getByName("127.0.0.1");
+        InetAddress remoteAddress = null;
         int pccCount = 1;
         int lsps = 1;
         boolean pcError = false;
 
         int argIdx = 0;
         while (argIdx < args.length) {
-            if (args[argIdx].equals("--address")) {
-                address = InetAddress.getByName(args[argIdx + 1]);
+            if (args[argIdx].equals("--local-address")) {
+                localAddress = InetAddress.getByName(args[argIdx + 1]);
+                argIdx++;
+            } else if (args[argIdx].equals("--remote-address")) {
+                remoteAddress = InetAddress.getByName(args[argIdx + 1]);
                 argIdx++;
             } else if (args[argIdx].equals("--pcc")) {
                 pccCount = Integer.valueOf(args[argIdx + 1]);
@@ -70,12 +74,12 @@ public final class Main {
             }
             argIdx++;
         }
-        Preconditions.checkState(address != null, "Missing mandatory address parameter.");
-        createPCCs(lsps, pcError, pccCount, address);
+        Preconditions.checkState(remoteAddress != null, "Missing mandatory remote-address parameter.");
+        createPCCs(lsps, pcError, pccCount, localAddress, remoteAddress);
     }
 
     public static void createPCCs(final int lspsPerPcc, final boolean pcerr, final int pccCount,
-            final InetAddress address) throws InterruptedException, ExecutionException {
+            final InetAddress localAddress, final 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);
 
@@ -87,12 +91,13 @@ public final class Main {
                 ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry()),
                 new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
-        InetAddress inetAddress = address;
+        final InetAddress pceAddress = remoteAddress;
+        InetAddress currentAddress = localAddress;
         int i = 0;
         while (i < pccCount) {
             final int pccNumber = i + 1;
-            final InetAddress pccAddress = inetAddress;
-            pcc.createClient(new InetSocketAddress(pccAddress, DEFAULT_PORT),
+            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>() {
 
@@ -102,7 +107,7 @@ public final class Main {
                         }
                     }).get();
             i++;
-            inetAddress = InetAddresses.increment(inetAddress);
+            currentAddress = InetAddresses.increment(currentAddress);
         }
     }
 
index 392bdd7d4752246d9b00d01d42a6396ae96d7f26..ca82baf2e51390b68a447ed743d8fda80ba162da 100644 (file)
@@ -16,7 +16,6 @@ import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.GlobalEventExecutor;
 import io.netty.util.concurrent.Promise;
 import java.net.InetSocketAddress;
-import org.opendaylight.protocol.framework.AbstractDispatcher;
 import org.opendaylight.protocol.framework.ProtocolSession;
 import org.opendaylight.protocol.framework.ReconnectStrategy;
 import org.opendaylight.protocol.framework.SessionListener;
@@ -25,8 +24,7 @@ import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
 import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
 
-public class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends
-        AbstractDispatcher<S, L> {
+public class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends BindableDispatcher<S, L> {
 
     private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
     private final PCEPHandlerFactory factory;
@@ -38,9 +36,9 @@ public class PCCMock<M, S extends ProtocolSession<M>, L extends SessionListener<
         this.factory = Preconditions.checkNotNull(factory);
     }
 
-    public Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy,
+    public Future<S> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress, final ReconnectStrategy strategy,
             final SessionListenerFactory<L> listenerFactory) {
-        return super.createClient(address, strategy, new PipelineInitializer<S>() {
+        return super.createClient(localAddress, remoteAddress, strategy, new PipelineInitializer<S>() {
             @Override
             public void initializeChannel(final SocketChannel ch, final Promise<S> promise) {
                 ch.pipeline().addLast(PCCMock.this.factory.getDecoders());
index 7fb0b8629e41bc7dbc0ae33374a86cadcfd693df..cd9daee6a94a8db127e37c75c2d278b6ece020a1 100644 (file)
@@ -11,7 +11,7 @@ package org.opendaylight.protocol.pcep.pcc.mock;
 import io.netty.buffer.Unpooled;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
-import junit.framework.Assert;
+import org.junit.Assert;
 import org.junit.Test;
 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
@@ -30,7 +30,7 @@ public class PCCMockTest {
     public void testSessionEstablishment() {
         try {
             org.opendaylight.protocol.pcep.testtool.Main.main(new String[]{"-a", "127.0.1.0:4189", "-ka", "10", "-d", "0", "--stateful", "--active"});
-            Main.main(new String[] {"--address", "127.0.1.0", "--pcc", "1", "--lsp", "1"});
+            Main.main(new String[] {"--local-address", "127.0.0.1", "--remote-address", "127.0.1.0", "--pcc", "2", "--lsp", "1"});
         } catch (Exception e) {
             Assert.fail();
         }