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
9 package org.opendaylight.protocol.pcep.pcc.mock;
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;
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;
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;
69 protected abstract List<PCEPCapability> getCapabilities();
71 protected abstract int getPort();
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());
81 protected static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel, final TestingSessionListenerFactory factory,
82 final String localAddress) throws
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;
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);
101 Assert.assertEquals(expectedNMessages, listener.messages().size());
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);
115 throw new NullPointerException();
118 protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress serverAddress2) throws InterruptedException {
119 return createServer(factory, serverAddress2, null);
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();
134 protected static void checkSynchronizedSession(final int numberOfLsp, final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws InterruptedException {
135 assertTrue(pceSessionListener.isUp());
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;
145 assertEquals(numberOfLspExpected, messages.size());
146 final PCEPSession session = pceSessionListener.getSession();
147 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
149 assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class).isInitiation());
150 assertNull(session.localSessionCharacteristics().getAugmentation(Tlvs3.class).getLspDbVersion().getLspDbVersionValue());
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());
158 List<Message> messages;
159 if (startAtNumberLsp.isPresent()) {
160 messages = pceSessionListener.messages().subList(startAtNumberLsp.get(), startAtNumberLsp.get() + expectedNumberOfLsp);
162 messages = pceSessionListener.messages();
164 checkSequequenceDBVersionSync(messages, expectedDBVersion);
165 assertEquals(expectedNumberOfLsp, messages.size());
166 final PCEPSession session = pceSessionListener.getSession();
168 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
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);
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());
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());
191 assertEquals(true, lsp.isSync().booleanValue());
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);
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());
206 return pccDispatcher.createClient(getRemoteAdress(), -1,
207 new PCEPSessionListenerFactory() {
209 public PCEPSessionListener getSessionListener() {
210 pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
211 return pccSessionListener;
213 }, snf, null, getLocalAdress(), DBVersion);
216 private InetSocketAddress getLocalAdress() {
217 return new InetSocketAddress(PCCMockTest.LOCAL_ADDRESS, getPort());
220 private InetSocketAddress getRemoteAdress() {
221 return new InetSocketAddress(PCCMockTest.REMOTE_ADDRESS, getPort());
224 private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
225 return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE, getCapabilities()), 0);
228 protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
229 return checkSessionListenerNotNull(factory, PCCMockTest.LOCAL_ADDRESS);