Bump mdsal to 5.0.2
[bgpcep.git] / pcep / pcc-mock / src / test / java / org / opendaylight / protocol / pcep / pcc / mock / PCCMockCommon.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.pcc.mock;
9
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;
17
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.concurrent.TimeUnit;
31 import org.junit.Before;
32 import org.opendaylight.protocol.concepts.KeyMapping;
33 import org.opendaylight.protocol.pcep.PCEPCapability;
34 import org.opendaylight.protocol.pcep.PCEPDispatcher;
35 import org.opendaylight.protocol.pcep.PCEPDispatcherDependencies;
36 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
37 import org.opendaylight.protocol.pcep.PCEPSession;
38 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
39 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
40 import org.opendaylight.protocol.pcep.SpeakerIdMapping;
41 import org.opendaylight.protocol.pcep.ietf.stateful07.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.PCEPExtensionProviderContext;
51 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
52 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
53 import org.opendaylight.protocol.util.InetSocketAddressUtil;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev181109.Tlvs3;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev181109.Stateful1;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Pcrpt;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Tlvs1;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.Lsp;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.pcrpt.message.pcrpt.message.Reports;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
61 import org.opendaylight.yangtools.yang.common.Uint64;
62
63 public abstract class PCCMockCommon {
64     private static final short KEEP_ALIVE = 30;
65     private static final short DEAD_TIMER = 120;
66     private static final long SLEEP_FOR = 50;
67     private final int port = InetSocketAddressUtil.getRandomPort();
68     final InetSocketAddress remoteAddress = InetSocketAddressUtil
69             .getRandomLoopbackInetSocketAddress(this.port);
70     final InetSocketAddress localAddress = InetSocketAddressUtil
71             .getRandomLoopbackInetSocketAddress(this.port);
72     PCCSessionListener pccSessionListener;
73     private PCEPDispatcher pceDispatcher;
74     private PCEPExtensionProviderContext extensionProvider;
75     private MessageRegistry messageRegistry;
76
77     protected abstract List<PCEPCapability> getCapabilities();
78
79     @Before
80     public void setUp() {
81         final BasePCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
82                 getCapabilities());
83         final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(proposal, 0);
84         this.extensionProvider = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
85         this.messageRegistry = this.extensionProvider.getMessageHandlerRegistry();
86         this.pceDispatcher = new PCEPDispatcherImpl(this.messageRegistry, nf, new NioEventLoopGroup(),
87                 new NioEventLoopGroup());
88     }
89
90     static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel,
91             final TestingSessionListenerFactory factory,
92             final String localAddress) throws
93             Exception {
94         final TestingSessionListener sessionListener = checkSessionListenerNotNull(factory, localAddress);
95         assertTrue(sessionListener.isUp());
96         checkReceivedMessages(sessionListener, numMessages);
97         assertEquals(numMessages, sessionListener.messages().size());
98         channel.close().get();
99         return sessionListener;
100     }
101
102     static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory,
103             final String localAddress) {
104         final Stopwatch sw = Stopwatch.createStarted();
105         TestingSessionListener listener;
106         final InetAddress address = InetAddresses.forString(localAddress);
107         while (sw.elapsed(TimeUnit.SECONDS) <= 60) {
108             listener = factory.getSessionListenerByRemoteAddress(address);
109             if (listener == null) {
110                 Uninterruptibles.sleepUninterruptibly(SLEEP_FOR, TimeUnit.MILLISECONDS);
111             } else {
112                 return listener;
113             }
114         }
115         throw new NullPointerException();
116     }
117
118     Channel createServer(final TestingSessionListenerFactory factory,
119             final InetSocketAddress serverAddress2) {
120         return createServer(factory, serverAddress2, null);
121     }
122
123     Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
124             serverAddress2, final PCEPPeerProposal peerProposal) {
125         final StatefulActivator activator07 = new StatefulActivator();
126         final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
127         activator07.start(this.extensionProvider);
128         optimizationsActivator.start(this.extensionProvider);
129
130         final ChannelFuture future = this.pceDispatcher
131                 .createServer(new DispatcherDependencies(serverAddress2, factory, peerProposal));
132         waitFutureSuccess(future);
133         return future.channel();
134     }
135
136     static void checkSynchronizedSession(final int numberOfLsp,
137             final TestingSessionListener pceSessionListener, final Uint64 expectedeInitialDb) throws Exception {
138         assertTrue(pceSessionListener.isUp());
139         //Send Open with LspDBV = 1
140         final int numberOfSyncMessage = 1;
141         int numberOfLspExpected = numberOfLsp;
142         if (!expectedeInitialDb.equals(Uint64.ZERO)) {
143             checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedeInitialDb));
144             numberOfLspExpected += numberOfSyncMessage;
145         }
146         checkReceivedMessages(pceSessionListener, numberOfLspExpected);
147         final PCEPSession session = pceSessionListener.getSession();
148         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
149
150         assertTrue(session.getRemoteTlvs().augmentation(Tlvs1.class).getStateful()
151                 .augmentation(Stateful1.class).isInitiation());
152         assertNull(session.getLocalTlvs().augmentation(Tlvs3.class)
153                 .getLspDbVersion().getLspDbVersionValue());
154     }
155
156     static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp,
157             final int expectedTotalMessages, final Uint64 startingDBVersion, final Uint64 expectedDBVersion,
158             final TestingSessionListener pceSessionListener) throws Exception {
159         assertNotNull(pceSessionListener.getSession());
160         assertTrue(pceSessionListener.isUp());
161         final List<Message> messages;
162         checkReceivedMessages(pceSessionListener, expectedTotalMessages);
163         if (startAtNumberLsp.isPresent()) {
164             messages = pceSessionListener.messages().subList(startAtNumberLsp.get(),
165                     startAtNumberLsp.get() + expectedNumberOfLsp);
166         } else {
167             messages = pceSessionListener.messages();
168         }
169         checkEquals(() -> checkSequequenceDBVersionSync(pceSessionListener, expectedDBVersion));
170         assertEquals(expectedNumberOfLsp, messages.size());
171         final PCEPSession session = pceSessionListener.getSession();
172
173         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
174
175         assertTrue(session.getRemoteTlvs().augmentation(Tlvs1.class).getStateful()
176                 .augmentation(Stateful1.class).isInitiation());
177         final Uint64 pceDBVersion = session.getLocalTlvs().augmentation(Tlvs3.class)
178                 .getLspDbVersion().getLspDbVersionValue();
179         assertEquals(startingDBVersion, pceDBVersion);
180     }
181
182     static void checkSession(final PCEPSession session, final int expectedDeadTimer,
183             final int expectedKeepAlive) {
184         assertNotNull(session);
185         assertEquals(expectedDeadTimer, session.getPeerPref().getDeadtimer().shortValue());
186         assertEquals(expectedKeepAlive, session.getPeerPref().getKeepalive().shortValue());
187         final Stateful1 stateful = session.getRemoteTlvs().augmentation(Tlvs1.class)
188                 .getStateful().augmentation(Stateful1.class);
189         assertTrue(stateful.isInitiation());
190     }
191
192     protected static void checkSequequenceDBVersionSync(final TestingSessionListener pceSessionListener,
193             final Uint64 expectedDbVersion) {
194         for (final Message msg : pceSessionListener.messages()) {
195             final List<Reports> pcrt = ((Pcrpt) msg).getPcrptMessage().getReports();
196             for (final Reports report : pcrt) {
197                 final Lsp lsp = report.getLsp();
198                 if (lsp.getPlspId().getValue().toJava() == 0) {
199                     assertEquals(false, lsp.isSync());
200                 } else {
201                     assertEquals(true, lsp.isSync());
202                 }
203                 final Uint64 actuaLspDBVersion = lsp.getTlvs()
204                         .augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep
205                             .sync.optimizations.rev181109.Tlvs1.class)
206                         .getLspDbVersion().getLspDbVersionValue();
207                 assertEquals(expectedDbVersion, actuaLspDBVersion);
208             }
209         }
210     }
211
212     Future<PCEPSession> createPCCSession(final Uint64 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.empty());
217
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);
222     }
223
224     private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
225         return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE,
226                 getCapabilities()), 0);
227     }
228
229     TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
230         return checkSessionListenerNotNull(factory, this.localAddress.getHostString());
231     }
232
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;
239
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;
247         }
248
249         @Override
250         public InetSocketAddress getAddress() {
251             return this.address;
252         }
253
254         @Override
255         public KeyMapping getKeys() {
256             return keys;
257         }
258
259         @Override
260         public SpeakerIdMapping getSpeakerIdMapping() {
261             return ids;
262         }
263
264         @Override
265         public PCEPSessionListenerFactory getListenerFactory() {
266             return this.listenerFactory;
267         }
268
269         @Override
270         public PCEPPeerProposal getPeerProposal() {
271             return this.peerProposal;
272         }
273     }
274 }