2ba820d46391105dd5c1b005560eefa933d1a292
[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
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
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.pcep.pcc.mock.WaitForFutureSucces.waitFutureSuccess;
16
17 import com.google.common.base.Optional;
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.math.BigInteger;
27 import java.net.InetSocketAddress;
28 import java.util.List;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.opendaylight.protocol.pcep.PCEPCapability;
34 import org.opendaylight.protocol.pcep.PCEPDispatcher;
35 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
36 import org.opendaylight.protocol.pcep.PCEPSession;
37 import org.opendaylight.protocol.pcep.PCEPSessionListener;
38 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
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.PCEPExtensionProviderContext;
49 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
50 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcrpt;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.Reports;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
58
59 public abstract class PCCMockCommon {
60     protected static final String REMOTE_ADDRESS = "127.0.1.0";
61     protected static final String LOCAL_ADDRESS = "127.0.0.1";
62     private final static short KEEP_ALIVE = 40;
63     private final static short DEAD_TIMER = 120;
64     protected final InetSocketAddress socket = new InetSocketAddress(PCCMockCommon.REMOTE_ADDRESS, getPort());
65     protected PCCSessionListener pccSessionListener;
66     private PCEPDispatcher pceDispatcher;
67     private PCCDispatcherImpl pccDispatcher;
68
69     protected abstract List<PCEPCapability> getCapabilities();
70
71     protected abstract int getPort();
72
73     @Before
74     public void setUp() {
75         final BasePCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE, getCapabilities());
76         final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(proposal, 0);
77         this.pceDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
78             nf, new NioEventLoopGroup(), new NioEventLoopGroup());
79     }
80
81     protected static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel, final TestingSessionListenerFactory factory,
82         final String localAddress) throws
83         Exception {
84         final TestingSessionListener sessionListener = checkSessionListenerNotNull(factory, localAddress);
85         assertTrue(sessionListener.isUp());
86         checkNumberOfMessages(numMessages, sessionListener);
87         assertEquals(numMessages, sessionListener.messages().size());
88         channel.close().get();
89         return sessionListener;
90     }
91
92     private static void checkNumberOfMessages(final int expectedNMessages, final TestingSessionListener listener) throws Exception {
93         Stopwatch sw = Stopwatch.createStarted();
94         while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
95             if (expectedNMessages != listener.messages().size()) {
96                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
97             } else {
98                 return;
99             }
100         }
101         Assert.assertEquals(expectedNMessages, listener.messages().size());
102     }
103
104     static TestingSessionListener checkSessionListenerNotNull(final TestingSessionListenerFactory factory, final String localAddress) {
105         Stopwatch sw = Stopwatch.createStarted();
106         TestingSessionListener listener = null;
107         while (sw.elapsed(TimeUnit.SECONDS) <= 1000) {
108             listener = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(localAddress));
109             if (listener == null) {
110                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
111             } else {
112                 return listener;
113             }
114         }
115         throw new NullPointerException();
116     }
117
118     protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress serverAddress2) throws InterruptedException {
119         return createServer(factory, serverAddress2, null);
120     }
121
122     protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
123         serverAddress2, final PCEPPeerProposal peerProposal) throws InterruptedException {
124         final PCEPExtensionProviderContext ctx = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
125         final StatefulActivator activator07 = new StatefulActivator();
126         final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
127         activator07.start(ctx);
128         optimizationsActivator.start(ctx);
129         final ChannelFuture future = this.pceDispatcher.createServer(serverAddress2, factory, peerProposal);
130         waitFutureSuccess(future);
131         return future.channel();
132     }
133
134     protected static void checkSynchronizedSession(final int numberOfLsp, final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws InterruptedException {
135         assertTrue(pceSessionListener.isUp());
136         Thread.sleep(1000);
137         //Send Open with LspDBV = 1
138         final List<Message> messages = pceSessionListener.messages();
139         int numberOfSyncMessage = 1;
140         int numberOfLspExpected = numberOfLsp;
141         if (!expectedeInitialDb.equals(BigInteger.ZERO)) {
142             checkSequequenceDBVersionSync(messages, expectedeInitialDb);
143             numberOfLspExpected += numberOfSyncMessage;
144         }
145         assertEquals(numberOfLspExpected, messages.size());
146         final PCEPSession session = pceSessionListener.getSession();
147         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
148
149         assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class).isInitiation());
150         assertNull(session.localSessionCharacteristics().getAugmentation(Tlvs3.class).getLspDbVersion().getLspDbVersionValue());
151     }
152
153     protected static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp, final BigInteger startingDBVersion,
154         final BigInteger expectedDBVersion, final TestingSessionListener pceSessionListener) throws InterruptedException {
155         assertNotNull(pceSessionListener.getSession());
156         assertTrue(pceSessionListener.isUp());
157         Thread.sleep(50);
158         List<Message> messages;
159         if (startAtNumberLsp.isPresent()) {
160             messages = pceSessionListener.messages().subList(startAtNumberLsp.get(), startAtNumberLsp.get() + expectedNumberOfLsp);
161         } else {
162             messages = pceSessionListener.messages();
163         }
164         checkSequequenceDBVersionSync(messages, expectedDBVersion);
165         assertEquals(expectedNumberOfLsp, messages.size());
166         final PCEPSession session = pceSessionListener.getSession();
167
168         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
169
170         assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class).isInitiation());
171         final BigInteger pceDBVersion = session.localSessionCharacteristics().getAugmentation(Tlvs3.class).getLspDbVersion().getLspDbVersionValue();
172         assertEquals(startingDBVersion, pceDBVersion);
173     }
174
175     protected static void checkSession(final PCEPSession session, final int expectedDeadTimer, final int expectedKeepAlive) {
176         assertNotNull(session);
177         assertEquals(expectedDeadTimer, session.getPeerPref().getDeadtimer().shortValue());
178         assertEquals(expectedKeepAlive, session.getPeerPref().getKeepalive().shortValue());
179         final Stateful1 stateful = session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class);
180         assertTrue(stateful.isInitiation());
181     }
182
183     protected static void checkSequequenceDBVersionSync(final List<Message> messages, final BigInteger expectedDbVersion) {
184         for (Message msg : messages) {
185             final List<Reports> pcrt = ((Pcrpt) msg).getPcrptMessage().getReports();
186             for (Reports report : pcrt) {
187                 final Lsp lsp = report.getLsp();
188                 if (lsp.getPlspId().getValue() == 0) {
189                     assertEquals(false, lsp.isSync().booleanValue());
190                 } else {
191                     assertEquals(true, lsp.isSync().booleanValue());
192                 }
193                 final BigInteger actuaLspDBVersion = lsp.getTlvs().getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params
194                     .xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs1.class).getLspDbVersion().getLspDbVersionValue();
195                 assertEquals(expectedDbVersion, actuaLspDBVersion);
196             }
197         }
198     }
199
200     protected Future<PCEPSession> createPCCSession(BigInteger DBVersion) {
201         this.pccDispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry());
202         final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
203         final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, getLocalAdress().getAddress(), 0, -1, new HashedWheelTimer(),
204             Optional.<TimerHandler>absent());
205
206         return pccDispatcher.createClient(getRemoteAdress(), -1,
207             new PCEPSessionListenerFactory() {
208                 @Override
209                 public PCEPSessionListener getSessionListener() {
210                     pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
211                     return pccSessionListener;
212                 }
213             }, snf, null, getLocalAdress(), DBVersion);
214     }
215
216     private InetSocketAddress getLocalAdress() {
217         return new InetSocketAddress(PCCMockTest.LOCAL_ADDRESS, getPort());
218     }
219
220     private InetSocketAddress getRemoteAdress() {
221         return new InetSocketAddress(PCCMockTest.REMOTE_ADDRESS, getPort());
222     }
223
224     private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
225         return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE, getCapabilities()), 0);
226     }
227
228     protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
229         return checkSessionListenerNotNull(factory, PCCMockTest.LOCAL_ADDRESS);
230     }
231 }