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
8 package org.opendaylight.protocol.pcep.pcc.mock;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.opendaylight.protocol.util.CheckTestUtil.checkEquals;
15 import static org.opendaylight.protocol.util.CheckTestUtil.checkReceivedMessages;
16 import static org.opendaylight.protocol.util.CheckTestUtil.waitFutureSuccess;
18 import com.google.common.base.Stopwatch;
19 import com.google.common.net.InetAddresses;
20 import com.google.common.util.concurrent.Uninterruptibles;
21 import io.netty.channel.Channel;
22 import io.netty.channel.ChannelFuture;
23 import io.netty.channel.nio.NioEventLoopGroup;
24 import io.netty.util.HashedWheelTimer;
25 import io.netty.util.concurrent.Future;
26 import java.net.InetAddress;
27 import java.net.InetSocketAddress;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.ServiceLoader;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.Before;
33 import org.opendaylight.protocol.concepts.KeyMapping;
34 import org.opendaylight.protocol.pcep.PCEPCapability;
35 import org.opendaylight.protocol.pcep.PCEPDispatcher;
36 import org.opendaylight.protocol.pcep.PCEPDispatcherDependencies;
37 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
38 import org.opendaylight.protocol.pcep.PCEPSession;
39 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
40 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
41 import org.opendaylight.protocol.pcep.ietf.stateful.StatefulActivator;
42 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
43 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
44 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
45 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
46 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
47 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
48 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener;
49 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
50 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderActivator;
51 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
52 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
53 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
54 import org.opendaylight.protocol.util.InetSocketAddressUtil;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Tlvs3;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.initiated.rev200720.Stateful1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Pcrpt;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Tlvs1;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.lsp.object.Lsp;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcrpt.message.pcrpt.message.Reports;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
62 import org.opendaylight.yangtools.yang.common.Uint64;
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(port);
71 final InetSocketAddress localAddress = InetSocketAddressUtil
72 .getRandomLoopbackInetSocketAddress(port);
73 PCCSessionListener pccSessionListener;
74 private PCEPDispatcher pceDispatcher;
75 private final PCEPExtensionProviderContext extensionProvider = new SimplePCEPExtensionProviderContext();
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);
86 ServiceLoader.load(PCEPExtensionProviderActivator.class).forEach(act -> act.start(extensionProvider));
88 messageRegistry = extensionProvider.getMessageHandlerRegistry();
89 pceDispatcher = new PCEPDispatcherImpl(messageRegistry, nf, new NioEventLoopGroup(),
90 new NioEventLoopGroup());
93 static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel,
94 final TestingSessionListenerFactory factory, final String localAddress) throws Exception {
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) {
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(extensionProvider);
129 optimizationsActivator.start(extensionProvider);
131 final ChannelFuture future = 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 Uint64 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(Uint64.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().augmentation(Tlvs1.class).getStateful()
152 .augmentation(Stateful1.class).getInitiation());
153 assertNull(session.getLocalTlvs().augmentation(Tlvs3.class)
154 .getLspDbVersion().getLspDbVersionValue());
157 static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp,
158 final int expectedTotalMessages, final Uint64 startingDBVersion, final Uint64 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().augmentation(Tlvs1.class).getStateful()
177 .augmentation(Stateful1.class).getInitiation());
178 final Uint64 pceDBVersion = session.getLocalTlvs().augmentation(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().augmentation(Tlvs1.class)
189 .getStateful().augmentation(Stateful1.class);
190 assertTrue(stateful.getInitiation());
193 protected static void checkSequequenceDBVersionSync(final TestingSessionListener pceSessionListener,
194 final Uint64 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().toJava() == 0) {
200 assertEquals(false, lsp.getSync());
202 assertEquals(true, lsp.getSync());
204 final Uint64 actuaLspDBVersion = lsp.getTlvs()
205 .augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep
206 .sync.optimizations.rev200720.Tlvs1.class)
207 .getLspDbVersion().getLspDbVersionValue();
208 assertEquals(expectedDbVersion, actuaLspDBVersion);
213 Future<PCEPSession> createPCCSession(final Uint64 dbVersion) {
214 final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(messageRegistry);
215 final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
216 final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, localAddress.getAddress(),
217 0, -1, new HashedWheelTimer(), Optional.empty());
219 return pccDispatcher.createClient(remoteAddress, -1, () -> {
220 pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
221 return pccSessionListener;
222 }, snf, KeyMapping.of(), localAddress, dbVersion);
225 private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
226 return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
227 getCapabilities()), 0);
230 TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
231 return checkSessionListenerNotNull(factory, localAddress.getHostString());
234 private static class DispatcherDependencies implements PCEPDispatcherDependencies {
235 private final KeyMapping keys = KeyMapping.of();
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 PCEPSessionListenerFactory getListenerFactory() {
261 return listenerFactory;
265 public PCEPPeerProposal getPeerProposal() {