Move SpeakerIdMapping to topology provider
[bgpcep.git] / pcep / pcc-mock / src / test / java / org / opendaylight / protocol / pcep / pcc / mock / PCCMockCommon.java
index 603be76f74995f10df801c436c425745b5592b9b..0962b836dde2c06dcb81f46b8a00b57e6e61cd79 100644 (file)
@@ -5,18 +5,16 @@
  * 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 static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
-import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
-import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
+import static org.opendaylight.protocol.util.CheckTestUtil.checkEquals;
+import static org.opendaylight.protocol.util.CheckTestUtil.checkReceivedMessages;
+import static org.opendaylight.protocol.util.CheckTestUtil.waitFutureSuccess;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Stopwatch;
 import com.google.common.net.InetAddresses;
 import com.google.common.util.concurrent.Uninterruptibles;
@@ -25,18 +23,22 @@ import io.netty.channel.ChannelFuture;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.util.HashedWheelTimer;
 import io.netty.util.concurrent.Future;
-import java.math.BigInteger;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.util.List;
+import java.util.Optional;
+import java.util.ServiceLoader;
 import java.util.concurrent.TimeUnit;
 import org.junit.Before;
+import org.opendaylight.protocol.concepts.KeyMapping;
 import org.opendaylight.protocol.pcep.PCEPCapability;
 import org.opendaylight.protocol.pcep.PCEPDispatcher;
+import org.opendaylight.protocol.pcep.PCEPDispatcherDependencies;
 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
 import org.opendaylight.protocol.pcep.PCEPSession;
+import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
-import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
+import org.opendaylight.protocol.pcep.ietf.stateful.StatefulActivator;
 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
@@ -45,29 +47,32 @@ import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener;
 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
+import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderActivator;
 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
-import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
+import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
 import org.opendaylight.protocol.util.InetSocketAddressUtil;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcrpt;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.Reports;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Tlvs3;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.initiated.rev200720.Stateful1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Pcrpt;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Tlvs1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.lsp.object.Lsp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcrpt.message.pcrpt.message.Reports;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
+import org.opendaylight.yangtools.yang.common.Uint64;
 
 public abstract class PCCMockCommon {
     private static final short KEEP_ALIVE = 30;
     private static final short DEAD_TIMER = 120;
     private static final long SLEEP_FOR = 50;
     private final int port = InetSocketAddressUtil.getRandomPort();
-    protected final InetSocketAddress remoteAddress = InetSocketAddressUtil
-        .getRandomLoopbackInetSocketAddress(this.port);
-    protected final InetSocketAddress localAddress = new InetSocketAddress("127.0.0.1", this.port);
-    protected PCCSessionListener pccSessionListener;
+    final InetSocketAddress remoteAddress = InetSocketAddressUtil
+            .getRandomLoopbackInetSocketAddress(port);
+    final InetSocketAddress localAddress = InetSocketAddressUtil
+            .getRandomLoopbackInetSocketAddress(port);
+    PCCSessionListener pccSessionListener;
     private PCEPDispatcher pceDispatcher;
-    private PCEPExtensionProviderContext extensionProvider;
+    private final PCEPExtensionProviderContext extensionProvider = new SimplePCEPExtensionProviderContext();
     private MessageRegistry messageRegistry;
 
     protected abstract List<PCEPCapability> getCapabilities();
@@ -75,18 +80,18 @@ public abstract class PCCMockCommon {
     @Before
     public void setUp() {
         final BasePCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
-            getCapabilities());
+                getCapabilities());
         final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(proposal, 0);
-        this.extensionProvider  = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
-        this.messageRegistry = this.extensionProvider.getMessageHandlerRegistry();
-        this.pceDispatcher = new PCEPDispatcherImpl(this.messageRegistry, nf, new NioEventLoopGroup(),
-            new NioEventLoopGroup());
+
+        ServiceLoader.load(PCEPExtensionProviderActivator.class).forEach(act -> act.start(extensionProvider));
+
+        messageRegistry = extensionProvider.getMessageHandlerRegistry();
+        pceDispatcher = new PCEPDispatcherImpl(messageRegistry, nf, new NioEventLoopGroup(),
+                new NioEventLoopGroup());
     }
 
-    protected static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel,
-        final TestingSessionListenerFactory factory,
-        final String localAddress) throws
-        Exception {
+    static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel,
+            final TestingSessionListenerFactory factory, final String localAddress) throws Exception {
         final TestingSessionListener sessionListener = checkSessionListenerNotNull(factory, localAddress);
         assertTrue(sessionListener.isUp());
         checkReceivedMessages(sessionListener, numMessages);
@@ -96,7 +101,7 @@ public abstract class PCCMockCommon {
     }
 
     static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory,
-        final String localAddress) {
+            final String localAddress) {
         final Stopwatch sw = Stopwatch.createStarted();
         TestingSessionListener listener;
         final InetAddress address = InetAddresses.forString(localAddress);
@@ -111,116 +116,154 @@ public abstract class PCCMockCommon {
         throw new NullPointerException();
     }
 
-    protected Channel createServer(final TestingSessionListenerFactory factory,
-        final InetSocketAddress serverAddress2) throws InterruptedException {
+    Channel createServer(final TestingSessionListenerFactory factory,
+            final InetSocketAddress serverAddress2) {
         return createServer(factory, serverAddress2, null);
     }
 
-    protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
-        serverAddress2, final PCEPPeerProposal peerProposal) {
+    Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
+            serverAddress2, final PCEPPeerProposal peerProposal) {
         final StatefulActivator activator07 = new StatefulActivator();
         final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
-        activator07.start(this.extensionProvider);
-        optimizationsActivator.start(this.extensionProvider);
-        final ChannelFuture future = this.pceDispatcher.createServer(serverAddress2, factory, peerProposal);
+        activator07.start(extensionProvider);
+        optimizationsActivator.start(extensionProvider);
+
+        final ChannelFuture future = pceDispatcher
+                .createServer(new DispatcherDependencies(serverAddress2, factory, peerProposal));
         waitFutureSuccess(future);
         return future.channel();
     }
 
-    protected static void checkSynchronizedSession(final int numberOfLsp,
-        final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws Exception {
+    static void checkSynchronizedSession(final int numberOfLsp,
+            final TestingSessionListener pceSessionListener, final Uint64 expectedeInitialDb) throws Exception {
         assertTrue(pceSessionListener.isUp());
         //Send Open with LspDBV = 1
         final int numberOfSyncMessage = 1;
         int numberOfLspExpected = numberOfLsp;
-        if (!expectedeInitialDb.equals(BigInteger.ZERO)) {
-            checkEquals(()-> checkSequequenceDBVersionSync(pceSessionListener, expectedeInitialDb));
+        if (!expectedeInitialDb.equals(Uint64.ZERO)) {
+            checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedeInitialDb));
             numberOfLspExpected += numberOfSyncMessage;
         }
         checkReceivedMessages(pceSessionListener, numberOfLspExpected);
         final PCEPSession session = pceSessionListener.getSession();
         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
 
-        assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful()
-            .getAugmentation(Stateful1.class).isInitiation());
-        assertNull(session.localSessionCharacteristics().getAugmentation(Tlvs3.class)
-            .getLspDbVersion().getLspDbVersionValue());
+        assertTrue(session.getRemoteTlvs().augmentation(Tlvs1.class).getStateful()
+                .augmentation(Stateful1.class).getInitiation());
+        assertNull(session.getLocalTlvs().augmentation(Tlvs3.class)
+                .getLspDbVersion().getLspDbVersionValue());
     }
 
-    protected static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp,
-        final int expectedTotalMessages, final BigInteger startingDBVersion, final BigInteger expectedDBVersion,
-        final TestingSessionListener pceSessionListener) throws Exception {
+    static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp,
+            final int expectedTotalMessages, final Uint64 startingDBVersion, final Uint64 expectedDBVersion,
+            final TestingSessionListener pceSessionListener) throws Exception {
         assertNotNull(pceSessionListener.getSession());
         assertTrue(pceSessionListener.isUp());
         final List<Message> messages;
         checkReceivedMessages(pceSessionListener, expectedTotalMessages);
         if (startAtNumberLsp.isPresent()) {
             messages = pceSessionListener.messages().subList(startAtNumberLsp.get(),
-                startAtNumberLsp.get() + expectedNumberOfLsp);
+                    startAtNumberLsp.get() + expectedNumberOfLsp);
         } else {
             messages = pceSessionListener.messages();
         }
-        checkEquals(()-> checkSequequenceDBVersionSync(pceSessionListener, expectedDBVersion));
+        checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedDBVersion));
         assertEquals(expectedNumberOfLsp, messages.size());
         final PCEPSession session = pceSessionListener.getSession();
 
         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
 
-        assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful()
-            .getAugmentation(Stateful1.class).isInitiation());
-        final BigInteger pceDBVersion = session.localSessionCharacteristics().getAugmentation(Tlvs3.class)
-            .getLspDbVersion().getLspDbVersionValue();
+        assertTrue(session.getRemoteTlvs().augmentation(Tlvs1.class).getStateful()
+                .augmentation(Stateful1.class).getInitiation());
+        final Uint64 pceDBVersion = session.getLocalTlvs().augmentation(Tlvs3.class)
+                .getLspDbVersion().getLspDbVersionValue();
         assertEquals(startingDBVersion, pceDBVersion);
     }
 
-    protected static void checkSession(final PCEPSession session, final int expectedDeadTimer,
-        final int expectedKeepAlive) {
+    static void checkSession(final PCEPSession session, final int expectedDeadTimer,
+            final int expectedKeepAlive) {
         assertNotNull(session);
         assertEquals(expectedDeadTimer, session.getPeerPref().getDeadtimer().shortValue());
         assertEquals(expectedKeepAlive, session.getPeerPref().getKeepalive().shortValue());
-        final Stateful1 stateful = session.getRemoteTlvs().getAugmentation(Tlvs1.class)
-            .getStateful().getAugmentation(Stateful1.class);
-        assertTrue(stateful.isInitiation());
+        final Stateful1 stateful = session.getRemoteTlvs().augmentation(Tlvs1.class)
+                .getStateful().augmentation(Stateful1.class);
+        assertTrue(stateful.getInitiation());
     }
 
     protected static void checkSequequenceDBVersionSync(final TestingSessionListener pceSessionListener,
-        final BigInteger expectedDbVersion) {
+            final Uint64 expectedDbVersion) {
         for (final Message msg : pceSessionListener.messages()) {
             final List<Reports> pcrt = ((Pcrpt) msg).getPcrptMessage().getReports();
             for (final Reports report : pcrt) {
                 final Lsp lsp = report.getLsp();
-                if (lsp.getPlspId().getValue() == 0) {
-                    assertEquals(false, lsp.isSync().booleanValue());
+                if (lsp.getPlspId().getValue().toJava() == 0) {
+                    assertEquals(false, lsp.getSync());
                 } else {
-                    assertEquals(true, lsp.isSync().booleanValue());
+                    assertEquals(true, lsp.getSync());
                 }
-                final BigInteger actuaLspDBVersion = lsp.getTlvs().getAugmentation(org.opendaylight.yang.gen
-                    .v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs1.class)
-                    .getLspDbVersion().getLspDbVersionValue();
+                final Uint64 actuaLspDBVersion = lsp.getTlvs()
+                        .augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep
+                            .sync.optimizations.rev200720.Tlvs1.class)
+                        .getLspDbVersion().getLspDbVersionValue();
                 assertEquals(expectedDbVersion, actuaLspDBVersion);
             }
         }
     }
 
-    protected Future<PCEPSession> createPCCSession(final BigInteger dbVersion) {
-        final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(this.messageRegistry);
+    Future<PCEPSession> createPCCSession(final Uint64 dbVersion) {
+        final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(messageRegistry);
         final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
-        final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, this.localAddress.getAddress(),
-            0, -1, new HashedWheelTimer(), Optional.absent());
-
-        return pccDispatcher.createClient(this.remoteAddress, -1,
-            () -> {
-                this.pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
-                return this.pccSessionListener;
-            }, snf, null, this.localAddress, dbVersion);
+        final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, localAddress.getAddress(),
+                0, -1, new HashedWheelTimer(), Optional.empty());
+
+        return pccDispatcher.createClient(remoteAddress, -1, () -> {
+            pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
+            return pccSessionListener;
+        }, snf, KeyMapping.of(), localAddress, dbVersion);
     }
 
     private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
         return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
-            getCapabilities()), 0);
+                getCapabilities()), 0);
     }
 
-    protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
-        return checkSessionListenerNotNull(factory, this.localAddress.getHostString());
+    TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
+        return checkSessionListenerNotNull(factory, localAddress.getHostString());
+    }
+
+    private static class DispatcherDependencies implements PCEPDispatcherDependencies {
+        private final KeyMapping keys = KeyMapping.of();
+        private final InetSocketAddress address;
+        private final TestingSessionListenerFactory listenerFactory;
+        private final PCEPPeerProposal peerProposal;
+
+        DispatcherDependencies(
+                final InetSocketAddress address,
+                final TestingSessionListenerFactory listenerFactory,
+                final PCEPPeerProposal peerProposal) {
+            this.address = address;
+            this.listenerFactory = listenerFactory;
+            this.peerProposal = peerProposal;
+        }
+
+        @Override
+        public InetSocketAddress getAddress() {
+            return address;
+        }
+
+        @Override
+        public KeyMapping getKeys() {
+            return keys;
+        }
+
+        @Override
+        public PCEPSessionListenerFactory getListenerFactory() {
+            return listenerFactory;
+        }
+
+        @Override
+        public PCEPPeerProposal getPeerProposal() {
+            return peerProposal;
+        }
     }
 }