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