2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.pcep.pcc.mock;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
16 import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
17 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
19 import com.google.common.base.Optional;
20 import com.google.common.base.Stopwatch;
21 import com.google.common.net.InetAddresses;
22 import com.google.common.util.concurrent.Uninterruptibles;
23 import io.netty.channel.Channel;
24 import io.netty.channel.ChannelFuture;
25 import io.netty.channel.nio.NioEventLoopGroup;
26 import io.netty.util.HashedWheelTimer;
27 import io.netty.util.concurrent.Future;
28 import java.math.BigInteger;
29 import java.net.InetAddress;
30 import java.net.InetSocketAddress;
31 import java.util.List;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.Before;
34 import org.opendaylight.protocol.concepts.KeyMapping;
35 import org.opendaylight.protocol.pcep.PCEPCapability;
36 import org.opendaylight.protocol.pcep.PCEPDispatcher;
37 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
38 import org.opendaylight.protocol.pcep.PCEPSession;
39 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
40 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
41 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
42 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
43 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
44 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
45 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
46 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
47 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener;
48 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
49 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
50 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
51 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
52 import org.opendaylight.protocol.util.InetSocketAddressUtil;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev171025.Tlvs3;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev171025.Stateful1;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Pcrpt;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Tlvs1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.object.Lsp;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.pcrpt.message.Reports;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
61 public abstract class PCCMockCommon {
62 private static final short KEEP_ALIVE = 30;
63 private static final short DEAD_TIMER = 120;
64 private static final long SLEEP_FOR = 50;
65 private final int port = InetSocketAddressUtil.getRandomPort();
66 protected final InetSocketAddress remoteAddress = InetSocketAddressUtil
67 .getRandomLoopbackInetSocketAddress(this.port);
68 protected final InetSocketAddress localAddress = new InetSocketAddress("127.0.0.1", this.port);
69 protected PCCSessionListener pccSessionListener;
70 private PCEPDispatcher pceDispatcher;
71 private PCEPExtensionProviderContext extensionProvider;
72 private MessageRegistry messageRegistry;
74 protected abstract List<PCEPCapability> getCapabilities();
78 final BasePCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
80 final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(proposal, 0);
81 this.extensionProvider = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
82 this.messageRegistry = this.extensionProvider.getMessageHandlerRegistry();
83 this.pceDispatcher = new PCEPDispatcherImpl(this.messageRegistry, nf, new NioEventLoopGroup(),
84 new NioEventLoopGroup());
87 protected static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel,
88 final TestingSessionListenerFactory factory,
89 final String localAddress) throws
91 final TestingSessionListener sessionListener = checkSessionListenerNotNull(factory, localAddress);
92 assertTrue(sessionListener.isUp());
93 checkReceivedMessages(sessionListener, numMessages);
94 assertEquals(numMessages, sessionListener.messages().size());
95 channel.close().get();
96 return sessionListener;
99 static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory,
100 final String localAddress) {
101 final Stopwatch sw = Stopwatch.createStarted();
102 TestingSessionListener listener;
103 final InetAddress address = InetAddresses.forString(localAddress);
104 while (sw.elapsed(TimeUnit.SECONDS) <= 60) {
105 listener = factory.getSessionListenerByRemoteAddress(address);
106 if (listener == null) {
107 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
112 throw new NullPointerException();
115 protected Channel createServer(final TestingSessionListenerFactory factory,
116 final InetSocketAddress serverAddress2) throws InterruptedException {
117 return createServer(factory, serverAddress2, null);
120 protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
121 serverAddress2, final PCEPPeerProposal peerProposal) {
122 final StatefulActivator activator07 = new StatefulActivator();
123 final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
124 activator07.start(this.extensionProvider);
125 optimizationsActivator.start(this.extensionProvider);
126 final ChannelFuture future = this.pceDispatcher.createServer(serverAddress2, factory, peerProposal);
127 waitFutureSuccess(future);
128 return future.channel();
131 protected static void checkSynchronizedSession(final int numberOfLsp,
132 final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws Exception {
133 assertTrue(pceSessionListener.isUp());
134 //Send Open with LspDBV = 1
135 final int numberOfSyncMessage = 1;
136 int numberOfLspExpected = numberOfLsp;
137 if (!expectedeInitialDb.equals(BigInteger.ZERO)) {
138 checkEquals(()-> checkSequequenceDBVersionSync(pceSessionListener, expectedeInitialDb));
139 numberOfLspExpected += numberOfSyncMessage;
141 checkReceivedMessages(pceSessionListener, numberOfLspExpected);
142 final PCEPSession session = pceSessionListener.getSession();
143 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
145 assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful()
146 .getAugmentation(Stateful1.class).isInitiation());
147 assertNull(session.localSessionCharacteristics().getAugmentation(Tlvs3.class)
148 .getLspDbVersion().getLspDbVersionValue());
151 protected static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp,
152 final int expectedTotalMessages, final BigInteger startingDBVersion, final BigInteger expectedDBVersion,
153 final TestingSessionListener pceSessionListener) throws Exception {
154 assertNotNull(pceSessionListener.getSession());
155 assertTrue(pceSessionListener.isUp());
156 final List<Message> messages;
157 checkReceivedMessages(pceSessionListener, expectedTotalMessages);
158 if (startAtNumberLsp.isPresent()) {
159 messages = pceSessionListener.messages().subList(startAtNumberLsp.get(),
160 startAtNumberLsp.get() + expectedNumberOfLsp);
162 messages = pceSessionListener.messages();
164 checkEquals(()-> checkSequequenceDBVersionSync(pceSessionListener, expectedDBVersion));
165 assertEquals(expectedNumberOfLsp, messages.size());
166 final PCEPSession session = pceSessionListener.getSession();
168 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
170 assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful()
171 .getAugmentation(Stateful1.class).isInitiation());
172 final BigInteger pceDBVersion = session.localSessionCharacteristics().getAugmentation(Tlvs3.class)
173 .getLspDbVersion().getLspDbVersionValue();
174 assertEquals(startingDBVersion, pceDBVersion);
177 protected static void checkSession(final PCEPSession session, final int expectedDeadTimer,
178 final int expectedKeepAlive) {
179 assertNotNull(session);
180 assertEquals(expectedDeadTimer, session.getPeerPref().getDeadtimer().shortValue());
181 assertEquals(expectedKeepAlive, session.getPeerPref().getKeepalive().shortValue());
182 final Stateful1 stateful = session.getRemoteTlvs().getAugmentation(Tlvs1.class)
183 .getStateful().getAugmentation(Stateful1.class);
184 assertTrue(stateful.isInitiation());
187 protected static void checkSequequenceDBVersionSync(final TestingSessionListener pceSessionListener,
188 final BigInteger expectedDbVersion) {
189 for (final Message msg : pceSessionListener.messages()) {
190 final List<Reports> pcrt = ((Pcrpt) msg).getPcrptMessage().getReports();
191 for (final Reports report : pcrt) {
192 final Lsp lsp = report.getLsp();
193 if (lsp.getPlspId().getValue() == 0) {
194 assertEquals(false, lsp.isSync());
196 assertEquals(true, lsp.isSync());
198 final BigInteger actuaLspDBVersion = lsp.getTlvs().getAugmentation(org.opendaylight.yang.gen
199 .v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev171025.Tlvs1.class)
200 .getLspDbVersion().getLspDbVersionValue();
201 assertEquals(expectedDbVersion, actuaLspDBVersion);
206 protected Future<PCEPSession> createPCCSession(final BigInteger dbVersion) {
207 final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(this.messageRegistry);
208 final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
209 final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, this.localAddress.getAddress(),
210 0, -1, new HashedWheelTimer(), Optional.absent());
212 return pccDispatcher.createClient(this.remoteAddress, -1,
214 this.pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
215 return this.pccSessionListener;
216 }, snf, KeyMapping.getKeyMapping(), this.localAddress, dbVersion);
219 private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
220 return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
221 getCapabilities()), 0);
224 protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
225 return checkSessionListenerNotNull(factory, this.localAddress.getHostString());