--- /dev/null
+/*
+ * 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);
+ }
+}
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]);
}
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);
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>() {
}
}).get();
i++;
- inetAddress = InetAddresses.increment(inetAddress);
+ currentAddress = InetAddresses.increment(currentAddress);
}
}
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;
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;
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());
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;
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();
}