Bug-2208: pcc-mock code refactoring 70/15070/5
authorMilos Fabian <milfabia@cisco.com>
Mon, 9 Feb 2015 14:37:35 +0000 (15:37 +0100)
committerRobert Varga <nite@hq.sk>
Sun, 15 Feb 2015 16:04:58 +0000 (16:04 +0000)
-removed unnecessary code
-switched to use immediate reconnecting strategy
-refactored client dispatcher and main class
-improved junit tests

Change-Id: I3d44ad88a5bf81212443bd0079ad82719e367b1c
Signed-off-by: Milos Fabian <milfabia@cisco.com>
pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/BindableDispatcher.java [deleted file]
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/PCCActivator.java [deleted file]
pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcher.java [new file with mode: 0644]
pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMock.java [deleted file]
pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCSrpObjectParser.java [deleted file]
pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMockTest.java
pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/TestingSessionListener.java [new file with mode: 0644]
pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/TestingSessionListenerFactory.java [new file with mode: 0644]

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
deleted file mode 100644 (file)
index 4470543..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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 8623214abb06d0d824624ea91be2d79db1215603..cf641b227c858088b1554458d24189cd19abf45a 100644 (file)
@@ -22,13 +22,12 @@ 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.ReconnectImmediatelyStrategy;
 import org.opendaylight.protocol.framework.SessionListenerFactory;
 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
 import org.opendaylight.protocol.pcep.PCEPSessionListener;
 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
-import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
@@ -88,31 +87,34 @@ public final class Main {
             final InetAddress localAddress, final List<InetAddress> remoteAddress, final short keepalive, final short deadtimer) throws InterruptedException, ExecutionException {
         final StatefulActivator activator07 = new StatefulActivator();
         activator07.start(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance());
-        for (final InetAddress pceAddress : remoteAddress) {
-            InetAddress currentAddress = localAddress;
-            int i = 0;
-            while (i < pccCount) {
-                final InetAddress pccAddress = currentAddress;
-                final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = new DefaultPCEPSessionNegotiatorFactory(
-                        new OpenBuilder().setKeepalive(keepalive).setDeadTimer(deadtimer).setSessionId((short) 0).build(), 0);
-
-                final PCCMock<Message, PCEPSessionImpl, PCEPSessionListener> pcc = new PCCMock<Message, PCEPSessionImpl, PCEPSessionListener>(snf, new PCEPHandlerFactory(
-                        ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry()));
-                pcc.createClient(new InetSocketAddress(pccAddress, 0), new InetSocketAddress(pceAddress, DEFAULT_PORT),
-                        new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT),
-                        new SessionListenerFactory<PCEPSessionListener>() {
+        InetAddress currentAddress = localAddress;
+        final PCCDispatcher pccDispatcher = new PCCDispatcher(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
+                getSessionNegotiatorFactory(keepalive, deadtimer));
+        for (int i = 0; i < pccCount; i++) {
+            createPCC(lspsPerPcc, pcerr, currentAddress, remoteAddress, keepalive, deadtimer, pccDispatcher);
+            currentAddress = InetAddresses.increment(currentAddress);
+        }
+    }
 
-                            @Override
-                            public PCEPSessionListener getSessionListener() {
-                                return new SimpleSessionListener(lspsPerPcc, pcerr, pccAddress);
-                            }
-                        }).get();
-                i++;
-                currentAddress = InetAddresses.increment(currentAddress);
-            }
+    private static void createPCC(final int lspsPerPcc, final boolean pcerr, final InetAddress localAddress,
+            final List<InetAddress> remoteAddress, final short keepalive, final short deadtimer, final PCCDispatcher pccDispatcher) throws InterruptedException, ExecutionException {
+        final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf = getSessionNegotiatorFactory(keepalive, deadtimer);
+        for (final InetAddress pceAddress : remoteAddress) {
+            pccDispatcher.createClient(new InetSocketAddress(localAddress, 0), new InetSocketAddress(pceAddress, DEFAULT_PORT), new ReconnectImmediatelyStrategy(GlobalEventExecutor.INSTANCE, RECONNECT_STRATEGY_TIMEOUT), new SessionListenerFactory<PCEPSessionListener>() {
+                @Override
+                public PCEPSessionListener getSessionListener() {
+                    return new SimpleSessionListener(lspsPerPcc, pcerr, localAddress);
+                }
+            }, snf);
         }
     }
 
+    private static SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> getSessionNegotiatorFactory(final short keepalive,
+            final short deadtimer) {
+        return new DefaultPCEPSessionNegotiatorFactory(
+                new OpenBuilder().setKeepalive(keepalive).setDeadTimer(deadtimer).setSessionId((short) 0).build(), 0);
+    }
+
     private static ch.qos.logback.classic.Logger getRootLogger(final LoggerContext lc) {
         return Iterables.find(lc.getLoggerList(), new Predicate<Logger>() {
             @Override
diff --git a/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCActivator.java b/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCActivator.java
deleted file mode 100644 (file)
index aa4836c..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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 java.util.ArrayList;
-import java.util.List;
-import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07SrpObjectParser;
-import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
-import org.opendaylight.protocol.pcep.spi.pojo.AbstractPCEPExtensionProviderActivator;
-
-public class PCCActivator extends AbstractPCEPExtensionProviderActivator {
-
-    @Override
-    protected List<AutoCloseable> startImpl(PCEPExtensionProviderContext context) {
-        final List<AutoCloseable> regs = new ArrayList<>();
-        regs.add(context.registerObjectParser(Stateful07SrpObjectParser.CLASS, Stateful07SrpObjectParser.TYPE,
-                new PCCSrpObjectParser(context.getTlvHandlerRegistry(), context.getVendorInformationTlvRegistry())));
-        return regs;
-    }
-}
diff --git a/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcher.java b/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCDispatcher.java
new file mode 100644 (file)
index 0000000..251162f
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2015 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.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.SessionListenerFactory;
+import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
+import org.opendaylight.protocol.pcep.PCEPSessionListener;
+import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
+import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
+import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
+import org.opendaylight.protocol.pcep.spi.MessageRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
+
+public final class PCCDispatcher extends PCEPDispatcherImpl {
+
+    private InetSocketAddress localAddress;
+    private final PCEPHandlerFactory factory;
+
+    public PCCDispatcher(final MessageRegistry registry,
+            final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory) {
+        super(registry, negotiatorFactory, new NioEventLoopGroup(), new NioEventLoopGroup(), null, null);
+        this.factory = new PCEPHandlerFactory(registry);
+    }
+
+    @Override
+    protected void customizeBootstrap(final Bootstrap b) {
+        super.customizeBootstrap(b);
+        if (this.localAddress != null) {
+            b.localAddress(this.localAddress);
+        }
+    }
+
+    public synchronized Future<PCEPSessionImpl> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress,
+            final ReconnectStrategy strategy, final SessionListenerFactory<PCEPSessionListener> listenerFactory,
+            final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory) {
+        this.localAddress = localAddress;
+        final Future<PCEPSessionImpl> futureClient = super.createClient(remoteAddress, strategy, new PipelineInitializer<PCEPSessionImpl>() {
+            @Override
+            public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
+                ch.pipeline().addLast(PCCDispatcher.this.factory.getDecoders());
+                ch.pipeline().addLast("negotiator",
+                        negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
+                ch.pipeline().addLast(PCCDispatcher.this.factory.getEncoders());
+            }
+        });
+        this.localAddress = null;
+        return futureClient;
+    }
+}
diff --git a/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMock.java b/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCMock.java
deleted file mode 100644 (file)
index 0715c18..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * 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 com.google.common.base.Preconditions;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.SocketChannel;
-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.ProtocolSession;
-import org.opendaylight.protocol.framework.ReconnectStrategy;
-import org.opendaylight.protocol.framework.SessionListener;
-import org.opendaylight.protocol.framework.SessionListenerFactory;
-import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
-import org.opendaylight.protocol.pcep.impl.PCEPHandlerFactory;
-
-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;
-
-    public PCCMock(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final PCEPHandlerFactory factory) {
-        super(GlobalEventExecutor.INSTANCE, new NioEventLoopGroup(), new NioEventLoopGroup());
-        this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
-        this.factory = Preconditions.checkNotNull(factory);
-    }
-
-    public Future<S> createClient(final InetSocketAddress localAddress, final InetSocketAddress remoteAddress, final ReconnectStrategy strategy,
-            final SessionListenerFactory<L> listenerFactory) {
-        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());
-                ch.pipeline().addLast("negotiator",
-                        PCCMock.this.negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
-                ch.pipeline().addLast(PCCMock.this.factory.getEncoders());
-            }
-        });
-    }
-}
diff --git a/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCSrpObjectParser.java b/pcep/pcc-mock/src/main/java/org/opendaylight/protocol/pcep/pcc/mock/PCCSrpObjectParser.java
deleted file mode 100644 (file)
index a77e36b..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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 com.google.common.base.Preconditions;
-import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07SrpObjectParser;
-import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.TlvRegistry;
-import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
-
-public class PCCSrpObjectParser extends Stateful07SrpObjectParser {
-
-    protected PCCSrpObjectParser(TlvRegistry tlvReg, VendorInformationTlvRegistry viTlvReg) {
-        super(tlvReg, viTlvReg);
-    }
-
-    @Override
-    public Srp parseObject(ObjectHeader header, ByteBuf bytes) throws PCEPDeserializerException {
-        Preconditions.checkArgument(bytes != null && bytes.isReadable(),
-                "Array of bytes is mandatory. Can't be null or empty.");
-        if (bytes.readableBytes() < MIN_SIZE) {
-            throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
-                    + "; Expected: >=" + MIN_SIZE + ".");
-        }
-        final SrpBuilder builder = new SrpBuilder();
-        builder.setIgnore(header.isIgnore());
-        builder.setProcessingRule(header.isProcessingRule());
-        bytes.readerIndex(bytes.readerIndex() + FLAGS_SIZE);
-        builder.setOperationId(new SrpIdNumber(bytes.readUnsignedInt()));
-        final TlvsBuilder tlvsBuilder = new TlvsBuilder();
-        parseTlvs(tlvsBuilder, bytes.slice());
-        builder.setTlvs(tlvsBuilder.build());
-        return builder.build();
-    }
-}
index 401369e10a9050a00561787a3ff474f7a31a04bf..f20a9f7715796f0b399603735e2e57dacc23ace8 100644 (file)
 
 package org.opendaylight.protocol.pcep.pcc.mock;
 
-import io.netty.buffer.Unpooled;
+import com.google.common.net.InetAddresses;
+import io.netty.channel.Channel;
+import io.netty.channel.nio.NioEventLoopGroup;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
+import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
+import java.util.concurrent.ExecutionException;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
-import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
+import org.opendaylight.protocol.pcep.PCEPDispatcher;
+import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
+import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SrpIdNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.Srp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
 
+public class PCCMockTest {
 
+    private static final short KEEP_ALIVE = 30;
+    private static final short DEAD_TIMER = 120;
+    private static final String REMOTE_ADDRESS = "127.0.1.0";
+    private static final String REMOTE_ADDRESS2 = "127.0.2.0";
+    private static final String REMOTE_ADDRESS3 = "127.0.3.0";
+    private static final String REMOTE_ADDRESS4 = "127.0.4.0";
+    private static final InetSocketAddress SERVER_ADDRESS = new InetSocketAddress(REMOTE_ADDRESS, 4189);
+    private static final InetSocketAddress SERVER_ADDRESS2 = new InetSocketAddress(REMOTE_ADDRESS2, 4189);
+    private static final InetSocketAddress SERVER_ADDRESS3 = new InetSocketAddress(REMOTE_ADDRESS3, 4189);
+    private static final InetSocketAddress SERVER_ADDRESS4 = new InetSocketAddress(REMOTE_ADDRESS4, 4189);
+    private static final String LOCAL_ADDRESS = "127.0.0.1";
+    private static final String LOCAL_ADDRESS2 = "127.0.0.2";
 
-public class PCCMockTest {
+    private PCEPDispatcher pceDispatcher;
 
-    @Test
-    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[] {"--local-address", "127.0.0.1", "--remote-address", "127.0.1.0", "--pcc", "2", "--lsp", "1", "--log-level", "DEBUG", "-ka", "10", "-d", "0",});
-        } catch (Exception e) {
-            Assert.fail(e.getMessage());
-        }
+    @Before
+    public void setUp() {
+        final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(
+                new OpenBuilder().setKeepalive(KEEP_ALIVE).setDeadTimer(DEAD_TIMER).setSessionId((short) 0).build(), 0);
+        this.pceDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
+                nf, new NioEventLoopGroup(), new NioEventLoopGroup());
     }
 
     @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", "--pcc", "3"});
-        } catch (Exception e) {
-            Assert.fail(e.getMessage());
-        }
+    public void testSessionEstablishment() throws UnknownHostException, InterruptedException, ExecutionException {
+        final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
+        final Channel channel = this.pceDispatcher.createServer(SERVER_ADDRESS, factory).channel();
+        Main.main(new String[] {"--local-address", LOCAL_ADDRESS, "--remote-address", REMOTE_ADDRESS, "--pcc", "1", "--lsp", "3",
+            "--log-level", "DEBUG", "-ka", "10", "-d", "40"});
+        Thread.sleep(1000);
+        final TestingSessionListener sessionListener = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
+        Assert.assertTrue(sessionListener.isUp());
+        //3 reported LSPs + syc
+        Assert.assertEquals(4, sessionListener.messages().size());
+        final PCEPSession session = sessionListener.getSession();
+        Assert.assertNotNull(session);
+        Assert.assertEquals(40, session.getPeerPref().getDeadtimer().shortValue());
+        Assert.assertEquals(10, session.getPeerPref().getKeepalive().shortValue());
+        channel.close().get();
     }
 
     @Test
-    public void testSrpObjectParser() throws PCEPDeserializerException {
-        final byte[] bytes = {
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-        };
-        final PCEPExtensionProviderContext ctx = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
-        final PCCSrpObjectParser parser = new PCCSrpObjectParser(ctx.getTlvHandlerRegistry(), ctx.getVendorInformationTlvRegistry());
-        final Srp srp = new SrpBuilder().setIgnore(true).setProcessingRule(true).setOperationId(new SrpIdNumber(0L)).setTlvs(new TlvsBuilder().build()).build();
-        Assert.assertEquals(srp, parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.wrappedBuffer(bytes)));
+    public void testMockPCCToManyPCE() throws InterruptedException, ExecutionException, UnknownHostException {
+        final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(
+                new OpenBuilder().setKeepalive(KEEP_ALIVE).setDeadTimer(DEAD_TIMER).setSessionId((short) 0).build(), 0);
+        PCEPDispatcher dispatcher2 = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
+                nf, new NioEventLoopGroup(), new NioEventLoopGroup());
+        final DefaultPCEPSessionNegotiatorFactory nf2 = new DefaultPCEPSessionNegotiatorFactory(
+                new OpenBuilder().setKeepalive(KEEP_ALIVE).setDeadTimer(DEAD_TIMER).setSessionId((short) 0).build(), 0);
+        PCEPDispatcher dispatcher3 = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
+                nf2, new NioEventLoopGroup(), new NioEventLoopGroup());
+        final TestingSessionListenerFactory factory = new TestingSessionListenerFactory();
+        final TestingSessionListenerFactory factory2 = new TestingSessionListenerFactory();
+        final TestingSessionListenerFactory factory3 = new TestingSessionListenerFactory();
+        final Channel channel = pceDispatcher.createServer(SERVER_ADDRESS2, factory).channel();
+        final Channel channel2 = dispatcher2.createServer(SERVER_ADDRESS3, factory2).channel();
+        final Channel channel3 = dispatcher3.createServer(SERVER_ADDRESS4, factory3).channel();
+
+        Main.main(new String[] {"--local-address", LOCAL_ADDRESS, "--remote-address", REMOTE_ADDRESS2 + "," + REMOTE_ADDRESS3 + "," + REMOTE_ADDRESS4,
+            "--pcc", "2"});
+        Thread.sleep(1000);
+        //PCE1
+        TestingSessionListener sessionListener1 = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
+        TestingSessionListener sessionListener2 = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS2));
+        Assert.assertNotNull(sessionListener1);
+        Assert.assertNotNull(sessionListener2);
+        Assert.assertTrue(sessionListener1.isUp());
+        Assert.assertTrue(sessionListener2.isUp());
+        Assert.assertEquals(2, sessionListener1.messages().size());
+        Assert.assertEquals(2, sessionListener2.messages().size());
+        //PCE2
+        sessionListener1 = factory2.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
+        sessionListener2 = factory2.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS2));
+        Assert.assertNotNull(sessionListener1);
+        Assert.assertNotNull(sessionListener2);
+        Assert.assertTrue(sessionListener1.isUp());
+        Assert.assertTrue(sessionListener2.isUp());
+        Assert.assertEquals(2, sessionListener1.messages().size());
+        Assert.assertEquals(2, sessionListener2.messages().size());
+        //PCE3
+        sessionListener1 = factory3.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS));
+        sessionListener2 = factory3.getSessionListenerByRemoteAddress(InetAddresses.forString(LOCAL_ADDRESS2));
+        Assert.assertNotNull(sessionListener1);
+        Assert.assertNotNull(sessionListener2);
+        Assert.assertTrue(sessionListener1.isUp());
+        Assert.assertTrue(sessionListener2.isUp());
+        Assert.assertEquals(2, sessionListener1.messages().size());
+        Assert.assertEquals(2, sessionListener2.messages().size());
+
+        channel.close().get();
+        channel2.close().get();
+        channel3.close().get();
     }
 
     @Test(expected=UnsupportedOperationException.class)
@@ -70,5 +132,4 @@ public class PCCMockTest {
             throw e.getCause();
         }
     }
-
 }
diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/TestingSessionListener.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/TestingSessionListener.java
new file mode 100644 (file)
index 0000000..0421d19
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2015 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 com.google.common.collect.Lists;
+import java.util.List;
+import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.protocol.pcep.PCEPSessionListener;
+import org.opendaylight.protocol.pcep.PCEPTerminationReason;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestingSessionListener implements PCEPSessionListener {
+
+    private static final Logger LOG = LoggerFactory.getLogger(TestingSessionListener.class);
+
+    private final List<Message> messages = Lists.newArrayList();
+
+    private boolean up = false;
+    private PCEPSession session = null;
+
+    @Override
+    public void onMessage(final PCEPSession session, final Message message) {
+        LOG.debug("Received message: {}", message);
+        this.messages.add(message);
+    }
+
+    @Override
+    public void onSessionUp(final PCEPSession session) {
+        LOG.debug("Session up.");
+        this.up = true;
+        this.session = session;
+    }
+
+    @Override
+    public void onSessionDown(final PCEPSession session, final Exception e) {
+        LOG.debug("Session down. Cause : {} ", e, e);
+        this.up = false;
+        this.session = null;
+    }
+
+    @Override
+    public void onSessionTerminated(final PCEPSession session, final PCEPTerminationReason cause) {
+        LOG.debug("Session terminated. Cause : {}", cause);
+    }
+
+    public List<Message> messages() {
+        return this.messages;
+    }
+
+    public boolean isUp () {
+        return this.up;
+    }
+
+    public PCEPSession getSession() {
+        return this.session;
+    }
+}
diff --git a/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/TestingSessionListenerFactory.java b/pcep/pcc-mock/src/test/java/org/opendaylight/protocol/pcep/pcc/mock/TestingSessionListenerFactory.java
new file mode 100644 (file)
index 0000000..2adf54a
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2015 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 java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.List;
+import javax.annotation.concurrent.GuardedBy;
+import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.protocol.pcep.PCEPSessionListener;
+import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
+
+public class TestingSessionListenerFactory implements PCEPSessionListenerFactory {
+
+    @GuardedBy("this")
+    private List<TestingSessionListener> sessionListeners = new ArrayList<>();
+
+    @Override
+    public PCEPSessionListener getSessionListener() {
+        final TestingSessionListener sessionListener = new TestingSessionListener();
+        this.sessionListeners.add(sessionListener);
+        return sessionListener;
+    }
+
+    public TestingSessionListener getSessionListenerByRemoteAddress(final InetAddress ipAddress) {
+        for (final TestingSessionListener sessionListener : this.sessionListeners) {
+            if (sessionListener.isUp()) {
+                final PCEPSession session = sessionListener.getSession();
+                if (session.getRemoteAddress().equals(ipAddress)) {
+                    return sessionListener;
+                }
+            }
+        }
+        return null;
+    }
+}