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.PCEPDispatcherDependencies;
38 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
39 import org.opendaylight.protocol.pcep.PCEPSession;
40 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
41 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
42 import org.opendaylight.protocol.pcep.SpeakerIdMapping;
43 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
44 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
45 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
46 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
47 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
48 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
49 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
50 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener;
51 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
52 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
53 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
54 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
55 import org.opendaylight.protocol.util.InetSocketAddressUtil;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev171025.Tlvs3;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev171025.Stateful1;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Pcrpt;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Tlvs1;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.object.Lsp;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.pcrpt.message.Reports;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
64 public abstract class PCCMockCommon {
65 private static final short KEEP_ALIVE = 30;
66 private static final short DEAD_TIMER = 120;
67 private static final long SLEEP_FOR = 50;
68 private final int port = InetSocketAddressUtil.getRandomPort();
69 final InetSocketAddress remoteAddress = InetSocketAddressUtil
70 .getRandomLoopbackInetSocketAddress(this.port);
71 final InetSocketAddress localAddress = InetSocketAddressUtil
72 .getRandomLoopbackInetSocketAddress(this.port);
73 PCCSessionListener pccSessionListener;
74 private PCEPDispatcher pceDispatcher;
75 private PCEPExtensionProviderContext extensionProvider;
76 private MessageRegistry messageRegistry;
78 protected abstract List<PCEPCapability> getCapabilities();
82 final BasePCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
84 final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(proposal, 0);
85 this.extensionProvider = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
86 this.messageRegistry = this.extensionProvider.getMessageHandlerRegistry();
87 this.pceDispatcher = new PCEPDispatcherImpl(this.messageRegistry, nf, new NioEventLoopGroup(),
88 new NioEventLoopGroup());
91 static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel,
92 final TestingSessionListenerFactory factory,
93 final String localAddress) throws
95 final TestingSessionListener sessionListener = checkSessionListenerNotNull(factory, localAddress);
96 assertTrue(sessionListener.isUp());
97 checkReceivedMessages(sessionListener, numMessages);
98 assertEquals(numMessages, sessionListener.messages().size());
99 channel.close().get();
100 return sessionListener;
103 static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory,
104 final String localAddress) {
105 final Stopwatch sw = Stopwatch.createStarted();
106 TestingSessionListener listener;
107 final InetAddress address = InetAddresses.forString(localAddress);
108 while (sw.elapsed(TimeUnit.SECONDS) <= 60) {
109 listener = factory.getSessionListenerByRemoteAddress(address);
110 if (listener == null) {
111 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
116 throw new NullPointerException();
119 Channel createServer(final TestingSessionListenerFactory factory,
120 final InetSocketAddress serverAddress2) throws InterruptedException {
121 return createServer(factory, serverAddress2, null);
124 Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
125 serverAddress2, final PCEPPeerProposal peerProposal) {
126 final StatefulActivator activator07 = new StatefulActivator();
127 final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
128 activator07.start(this.extensionProvider);
129 optimizationsActivator.start(this.extensionProvider);
131 final ChannelFuture future = this.pceDispatcher
132 .createServer(new DispatcherDependencies(serverAddress2, factory, peerProposal));
133 waitFutureSuccess(future);
134 return future.channel();
137 static void checkSynchronizedSession(final int numberOfLsp,
138 final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws Exception {
139 assertTrue(pceSessionListener.isUp());
140 //Send Open with LspDBV = 1
141 final int numberOfSyncMessage = 1;
142 int numberOfLspExpected = numberOfLsp;
143 if (!expectedeInitialDb.equals(BigInteger.ZERO)) {
144 checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedeInitialDb));
145 numberOfLspExpected += numberOfSyncMessage;
147 checkReceivedMessages(pceSessionListener, numberOfLspExpected);
148 final PCEPSession session = pceSessionListener.getSession();
149 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
151 assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful()
152 .getAugmentation(Stateful1.class).isInitiation());
153 assertNull(session.getLocalTlvs().getAugmentation(Tlvs3.class)
154 .getLspDbVersion().getLspDbVersionValue());
157 static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp,
158 final int expectedTotalMessages, final BigInteger startingDBVersion, final BigInteger expectedDBVersion,
159 final TestingSessionListener pceSessionListener) throws Exception {
160 assertNotNull(pceSessionListener.getSession());
161 assertTrue(pceSessionListener.isUp());
162 final List<Message> messages;
163 checkReceivedMessages(pceSessionListener, expectedTotalMessages);
164 if (startAtNumberLsp.isPresent()) {
165 messages = pceSessionListener.messages().subList(startAtNumberLsp.get(),
166 startAtNumberLsp.get() + expectedNumberOfLsp);
168 messages = pceSessionListener.messages();
170 checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedDBVersion));
171 assertEquals(expectedNumberOfLsp, messages.size());
172 final PCEPSession session = pceSessionListener.getSession();
174 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
176 assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful()
177 .getAugmentation(Stateful1.class).isInitiation());
178 final BigInteger pceDBVersion = session.getLocalTlvs().getAugmentation(Tlvs3.class)
179 .getLspDbVersion().getLspDbVersionValue();
180 assertEquals(startingDBVersion, pceDBVersion);
183 static void checkSession(final PCEPSession session, final int expectedDeadTimer,
184 final int expectedKeepAlive) {
185 assertNotNull(session);
186 assertEquals(expectedDeadTimer, session.getPeerPref().getDeadtimer().shortValue());
187 assertEquals(expectedKeepAlive, session.getPeerPref().getKeepalive().shortValue());
188 final Stateful1 stateful = session.getRemoteTlvs().getAugmentation(Tlvs1.class)
189 .getStateful().getAugmentation(Stateful1.class);
190 assertTrue(stateful.isInitiation());
193 protected static void checkSequequenceDBVersionSync(final TestingSessionListener pceSessionListener,
194 final BigInteger expectedDbVersion) {
195 for (final Message msg : pceSessionListener.messages()) {
196 final List<Reports> pcrt = ((Pcrpt) msg).getPcrptMessage().getReports();
197 for (final Reports report : pcrt) {
198 final Lsp lsp = report.getLsp();
199 if (lsp.getPlspId().getValue() == 0) {
200 assertEquals(false, lsp.isSync());
202 assertEquals(true, lsp.isSync());
204 final BigInteger actuaLspDBVersion = lsp.getTlvs().getAugmentation(org.opendaylight.yang.gen
205 .v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev171025.Tlvs1.class)
206 .getLspDbVersion().getLspDbVersionValue();
207 assertEquals(expectedDbVersion, actuaLspDBVersion);
212 Future<PCEPSession> createPCCSession(final BigInteger dbVersion) {
213 final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(this.messageRegistry);
214 final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
215 final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, this.localAddress.getAddress(),
216 0, -1, new HashedWheelTimer(), Optional.absent());
218 return pccDispatcher.createClient(this.remoteAddress, -1, () -> {
219 this.pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
220 return this.pccSessionListener;
221 }, snf, KeyMapping.getKeyMapping(), this.localAddress, dbVersion);
224 private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
225 return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
226 getCapabilities()), 0);
229 TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
230 return checkSessionListenerNotNull(factory, this.localAddress.getHostString());
233 private class DispatcherDependencies implements PCEPDispatcherDependencies {
234 final KeyMapping keys = KeyMapping.getKeyMapping();
235 final SpeakerIdMapping ids = SpeakerIdMapping.getSpeakerIdMap();
236 private final InetSocketAddress address;
237 private final TestingSessionListenerFactory listenerFactory;
238 private final PCEPPeerProposal peerProposal;
240 DispatcherDependencies(
241 final InetSocketAddress address,
242 final TestingSessionListenerFactory listenerFactory,
243 final PCEPPeerProposal peerProposal) {
244 this.address = address;
245 this.listenerFactory = listenerFactory;
246 this.peerProposal = peerProposal;
250 public InetSocketAddress getAddress() {
255 public KeyMapping getKeys() {
260 public SpeakerIdMapping getSpeakerIdMapping() {
265 public PCEPSessionListenerFactory getListenerFactory() {
266 return this.listenerFactory;
270 public PCEPPeerProposal getPeerProposal() {
271 return this.peerProposal;