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;
16 import com.google.common.base.Optional;
17 import com.google.common.net.InetAddresses;
18 import io.netty.channel.Channel;
19 import io.netty.channel.nio.NioEventLoopGroup;
20 import io.netty.util.HashedWheelTimer;
21 import io.netty.util.concurrent.Future;
22 import java.math.BigInteger;
23 import java.net.InetSocketAddress;
24 import java.util.List;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Before;
27 import org.opendaylight.protocol.pcep.PCEPCapability;
28 import org.opendaylight.protocol.pcep.PCEPDispatcher;
29 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
30 import org.opendaylight.protocol.pcep.PCEPSession;
31 import org.opendaylight.protocol.pcep.PCEPSessionListener;
32 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
33 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
34 import org.opendaylight.protocol.pcep.ietf.stateful07.StatefulActivator;
35 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
36 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
37 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
38 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
39 import org.opendaylight.protocol.pcep.pcc.mock.api.PCCTunnelManager;
40 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCDispatcherImpl;
41 import org.opendaylight.protocol.pcep.pcc.mock.protocol.PCCSessionListener;
42 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
43 import org.opendaylight.protocol.pcep.spi.pojo.ServiceLoaderPCEPExtensionProviderContext;
44 import org.opendaylight.protocol.pcep.sync.optimizations.SyncOptimizationsActivator;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs3;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcrpt;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.Reports;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
53 public abstract class PCCMockCommon {
54 protected static final String REMOTE_ADDRESS = "127.0.1.0";
55 protected static final String LOCAL_ADDRESS = "127.0.0.1";
56 private final static short KEEP_ALIVE = 40;
57 private final static short DEAD_TIMER = 120;
58 protected final InetSocketAddress socket = new InetSocketAddress(PCCMockCommon.REMOTE_ADDRESS, getPort());
59 private PCEPDispatcher pceDispatcher;
60 private PCCDispatcherImpl pccDispatcher;
61 protected PCCSessionListener pccSessionListener;
63 protected abstract List<PCEPCapability> getCapabilities();
65 protected abstract int getPort();
69 final BasePCEPSessionProposalFactory proposal = new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE, getCapabilities());
70 final DefaultPCEPSessionNegotiatorFactory nf = new DefaultPCEPSessionNegotiatorFactory(proposal, 0);
71 this.pceDispatcher = new PCEPDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry(),
72 nf, new NioEventLoopGroup(), new NioEventLoopGroup());
75 protected static TestingSessionListener checkSessionListener(final int numMessages, final Channel channel, final TestingSessionListenerFactory factory, final String localAddress) throws
76 ExecutionException, InterruptedException {
77 final TestingSessionListener sessionListener = factory.getSessionListenerByRemoteAddress(InetAddresses.forString(localAddress));
78 assertNotNull(sessionListener);
79 assertTrue(sessionListener.isUp());
80 assertEquals(numMessages, sessionListener.messages().size());
81 channel.close().get();
82 return sessionListener;
85 protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress serverAddress2) {
86 return createServer(factory, serverAddress2, null);
89 protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress
90 serverAddress2, final PCEPPeerProposal peerProposal) {
91 final PCEPExtensionProviderContext ctx = ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance();
92 final StatefulActivator activator07 = new StatefulActivator();
93 final SyncOptimizationsActivator optimizationsActivator = new SyncOptimizationsActivator();
94 activator07.start(ctx);
95 optimizationsActivator.start(ctx);
96 return this.pceDispatcher.createServer(serverAddress2, factory, peerProposal).channel();
99 protected static void checkSynchronizedSession(final int numberOfLsp, final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws InterruptedException {
100 assertTrue(pceSessionListener.isUp());
102 //Send Open with LspDBV = 1
103 final List<Message> messages = pceSessionListener.messages();
104 int numberOfSyncMessage = 1;
105 int numberOfLspExpected = numberOfLsp;
106 if(!expectedeInitialDb.equals(BigInteger.ZERO)) {
107 checkSequequenceDBVersionSync(messages, expectedeInitialDb);
108 numberOfLspExpected += numberOfSyncMessage;
110 assertEquals(numberOfLspExpected, messages.size());
111 final PCEPSession session = pceSessionListener.getSession();
112 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
114 assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class).isInitiation());
115 assertNull(session.localSessionCharacteristics().getAugmentation(Tlvs3.class).getLspDbVersion().getLspDbVersionValue());
118 protected static void checkResyncSession(final Optional<Integer> startAtNumberLsp, final int expectedNumberOfLsp, final BigInteger startingDBVersion,
119 final BigInteger expectedDBVersion, final TestingSessionListener pceSessionListener) {
120 assertTrue(pceSessionListener.isUp());
121 List<Message> messages;
122 if(startAtNumberLsp.isPresent()) {
123 messages = pceSessionListener.messages().subList(startAtNumberLsp.get(), startAtNumberLsp.get() + expectedNumberOfLsp);
125 messages = pceSessionListener.messages();
127 checkSequequenceDBVersionSync(messages, expectedDBVersion);
128 assertEquals(expectedNumberOfLsp, messages.size());
129 final PCEPSession session = pceSessionListener.getSession();
131 checkSession(session, DEAD_TIMER, KEEP_ALIVE);
133 assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class).isInitiation());
134 final BigInteger pceDBVersion = session.localSessionCharacteristics().getAugmentation(Tlvs3.class).getLspDbVersion().getLspDbVersionValue();
135 assertEquals(startingDBVersion, pceDBVersion);
138 protected static void checkSession(final PCEPSession session, final int expectedDeadTimer, final int expectedKeepAlive) {
139 assertNotNull(session);
140 assertEquals(expectedDeadTimer, session.getPeerPref().getDeadtimer().shortValue());
141 assertEquals(expectedKeepAlive, session.getPeerPref().getKeepalive().shortValue());
142 final Stateful1 stateful = session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class);
143 assertTrue(stateful.isInitiation());
146 protected static void checkSequequenceDBVersionSync(final List<Message> messages, final BigInteger expectedDbVersion) {
147 for (Message msg : messages) {
148 final List<Reports> pcrt = ((Pcrpt) msg).getPcrptMessage().getReports();
149 for (Reports report : pcrt) {
150 final Lsp lsp = report.getLsp();
151 if (lsp.getPlspId().getValue() == 0) {
152 assertEquals(false, lsp.isSync().booleanValue());
154 assertEquals(true, lsp.isSync().booleanValue());
156 final BigInteger actuaLspDBVersion = lsp.getTlvs().getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params
157 .xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Tlvs1.class).getLspDbVersion().getLspDbVersionValue();
158 assertEquals(expectedDbVersion, actuaLspDBVersion);
163 protected Future<PCEPSession> createPCCSession(BigInteger DBVersion) {
164 this.pccDispatcher = new PCCDispatcherImpl(ServiceLoaderPCEPExtensionProviderContext.getSingletonInstance().getMessageHandlerRegistry());
165 final PCEPSessionNegotiatorFactory<PCEPSessionImpl> snf = getSessionNegotiatorFactory();
166 final PCCTunnelManager tunnelManager = new PCCTunnelManagerImpl(3, getLocalAdress().getAddress(), 0, -1, new HashedWheelTimer(),
167 Optional.<TimerHandler>absent());
169 return pccDispatcher.createClient(getRemoteAdress(), -1,
170 new PCEPSessionListenerFactory() {
172 public PCEPSessionListener getSessionListener() {
173 pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
174 return pccSessionListener;
176 }, snf, null, getLocalAdress(), DBVersion);
179 private InetSocketAddress getLocalAdress() {
180 return new InetSocketAddress(PCCMockTest.LOCAL_ADDRESS, getPort());
183 private InetSocketAddress getRemoteAdress() {
184 return new InetSocketAddress(PCCMockTest.REMOTE_ADDRESS, getPort());
187 private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
188 return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE, getCapabilities()), 0);
191 protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
192 return factory.getSessionListenerByRemoteAddress(InetAddresses.forString(PCCMockTest.LOCAL_ADDRESS));