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.util.HashedWheelTimer;
24 import io.netty.util.concurrent.Future;
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.ServiceLoader;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.opendaylight.protocol.concepts.KeyMapping;
34 import org.opendaylight.protocol.pcep.MessageRegistry;
35 import org.opendaylight.protocol.pcep.PCEPCapability;
36 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
37 import org.opendaylight.protocol.pcep.PCEPSession;
38 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
39 import org.opendaylight.protocol.pcep.PCEPTimerProposal;
40 import org.opendaylight.protocol.pcep.ietf.stateful.StatefulActivator;
41 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
42 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
43 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
44 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCPeerProposal;
45 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener;
46 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderActivator;
47 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
48 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
49 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
50 import org.opendaylight.protocol.util.InetSocketAddressUtil;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev200720.Tlvs3;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.initiated.rev200720.Stateful1;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Pcrpt;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.Tlvs1;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.lsp.object.Lsp;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev200720.pcrpt.message.pcrpt.message.Reports;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
58 import org.opendaylight.yangtools.yang.common.Uint16;
59 import org.opendaylight.yangtools.yang.common.Uint64;
60 import org.opendaylight.yangtools.yang.common.Uint8;
62 public abstract class PCCMockCommon {
63 private static final Uint8 KEEP_ALIVE = Uint8.valueOf(30);
64 private static final Uint8 DEAD_TIMER = Uint8.valueOf(120);
65 private static final long SLEEP_FOR = 50;
66 private final int port = InetSocketAddressUtil.getRandomPort();
67 final InetSocketAddress remoteAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
68 final InetSocketAddress localAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress(port);
69 PCCSessionListener pccSessionListener;
70 private PCEPDispatcherImpl pceDispatcher;
71 private final PCEPExtensionProviderContext extensionProvider = new SimplePCEPExtensionProviderContext();
72 private MessageRegistry messageRegistry;
74 protected abstract List<PCEPCapability> getCapabilities();
78 ServiceLoader.load(PCEPExtensionProviderActivator.class).forEach(act -> act.start(extensionProvider));
79 messageRegistry = extensionProvider.getMessageHandlerRegistry();
80 pceDispatcher = new PCEPDispatcherImpl();
85 pceDispatcher.close();
88 static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel,
89 final TestingSessionListenerFactory factory, final String localAddress) throws Exception {
90 final TestingSessionListener sessionListener = checkSessionListenerNotNull(factory, localAddress);
91 assertTrue(sessionListener.isUp());
92 checkReceivedMessages(sessionListener, numMessages);
93 assertEquals(numMessages, sessionListener.messages().size());
94 channel.close().get();
95 return sessionListener;
98 static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory,
99 final String localAddress) {
100 final Stopwatch sw = Stopwatch.createStarted();
101 TestingSessionListener listener;
102 final InetAddress address = InetAddresses.forString(localAddress);
103 while (sw.elapsed(TimeUnit.SECONDS) <= 60) {
104 listener = factory.getSessionListenerByRemoteAddress(address);
105 if (listener == null) {
106 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
111 throw new NullPointerException();
114 Channel createServer(final TestingSessionListenerFactory factory,
115 final InetSocketAddress serverAddress2) {
116 return createServer(factory, serverAddress2, null);
119 Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
120 serverAddress2, final PCEPPeerProposal peerProposal) {
121 final StatefulActivator activator07 = new StatefulActivator();
122 final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
123 activator07.start(extensionProvider);
124 optimizationsActivator.start(extensionProvider);
126 final ChannelFuture future = pceDispatcher.createServer(serverAddress2, KeyMapping.of(), messageRegistry,
127 new CustomPCEPSessionNegotiatorFactory(factory, new PCEPTimerProposal(KEEP_ALIVE, DEAD_TIMER),
128 getCapabilities(), Uint16.ZERO, null, peerProposal));
129 waitFutureSuccess(future);
130 return future.channel();
133 static void checkSynchronizedSession(final int numberOfLsp,
134 final TestingSessionListener pceSessionListener, final Uint64 expectedeInitialDb) throws Exception {
135 assertTrue(pceSessionListener.isUp());
136 //Send Open with LspDBV = 1
137 final int numberOfSyncMessage = 1;
138 int numberOfLspExpected = numberOfLsp;
139 if (!expectedeInitialDb.equals(Uint64.ZERO)) {
140 checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedeInitialDb));
141 numberOfLspExpected += numberOfSyncMessage;
143 checkReceivedMessages(pceSessionListener, numberOfLspExpected);
144 final PCEPSession session = pceSessionListener.getSession();
145 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
147 assertTrue(session.getRemoteTlvs().augmentation(Tlvs1.class).getStateful()
148 .augmentation(Stateful1.class).getInitiation());
149 assertNull(session.getLocalTlvs().augmentation(Tlvs3.class)
150 .getLspDbVersion().getLspDbVersionValue());
153 static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp,
154 final int expectedTotalMessages, final Uint64 startingDBVersion, final Uint64 expectedDBVersion,
155 final TestingSessionListener pceSessionListener) throws Exception {
156 assertNotNull(pceSessionListener.getSession());
157 assertTrue(pceSessionListener.isUp());
158 final List<Message> messages;
159 checkReceivedMessages(pceSessionListener, expectedTotalMessages);
160 if (startAtNumberLsp.isPresent()) {
161 final int offset = startAtNumberLsp.orElseThrow();
162 messages = pceSessionListener.messages().subList(offset, offset + expectedNumberOfLsp);
164 messages = pceSessionListener.messages();
166 checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedDBVersion));
167 assertEquals(expectedNumberOfLsp, messages.size());
168 final PCEPSession session = pceSessionListener.getSession();
170 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
172 assertTrue(session.getRemoteTlvs().augmentation(Tlvs1.class).getStateful()
173 .augmentation(Stateful1.class).getInitiation());
174 final Uint64 pceDBVersion = session.getLocalTlvs().augmentation(Tlvs3.class)
175 .getLspDbVersion().getLspDbVersionValue();
176 assertEquals(startingDBVersion, pceDBVersion);
179 static void checkSession(final PCEPSession session, final Uint8 expectedDeadTimer,
180 final Uint8 expectedKeepAlive) {
181 assertNotNull(session);
182 assertEquals(expectedDeadTimer, session.getPeerPref().getDeadtimer());
183 assertEquals(expectedKeepAlive, session.getPeerPref().getKeepalive());
184 final Stateful1 stateful = session.getRemoteTlvs().augmentation(Tlvs1.class)
185 .getStateful().augmentation(Stateful1.class);
186 assertTrue(stateful.getInitiation());
189 protected static void checkSequequenceDBVersionSync(final TestingSessionListener pceSessionListener,
190 final Uint64 expectedDbVersion) {
191 for (final Message msg : pceSessionListener.messages()) {
192 final List<Reports> pcrt = ((Pcrpt) msg).getPcrptMessage().getReports();
193 for (final Reports report : pcrt) {
194 final Lsp lsp = report.getLsp();
195 if (lsp.getPlspId().getValue().toJava() == 0) {
196 assertEquals(false, lsp.getSync());
198 assertEquals(true, lsp.getSync());
200 final Uint64 actuaLspDBVersion = lsp.getTlvs()
201 .augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep
202 .sync.optimizations.rev200720.Tlvs1.class)
203 .getLspDbVersion().getLspDbVersionValue();
204 assertEquals(expectedDbVersion, actuaLspDBVersion);
209 Future<PCEPSession> createPCCSession(final Uint64 dbVersion) {
210 final PCCDispatcherImpl pccDispatcher = new PCCDispatcherImpl(messageRegistry);
211 final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, localAddress.getAddress(),
212 0, -1, new HashedWheelTimer(), Optional.empty());
213 final PCEPSessionNegotiatorFactory snf = new CustomPCEPSessionNegotiatorFactory(() -> {
214 pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
215 return pccSessionListener;
216 }, new PCEPTimerProposal(KEEP_ALIVE, DEAD_TIMER), getCapabilities(), Uint16.ZERO, null,
217 new PCCPeerProposal(dbVersion));
219 return pccDispatcher.createClient(remoteAddress, -1, snf, KeyMapping.of(), localAddress);
222 TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
223 return checkSessionListenerNotNull(factory, localAddress.getHostString());