BUG-2208: PCC-MOCK Stateful Sync Opt
[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
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;
52
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;
62
63     protected abstract List<PCEPCapability> getCapabilities();
64
65     protected abstract int getPort();
66
67     @Before
68     public void setUp() {
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());
73     }
74
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;
83     }
84
85     protected Channel createServer(final TestingSessionListenerFactory factory, final InetSocketAddress serverAddress2) {
86         return createServer(factory, serverAddress2, null);
87     }
88
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();
97     }
98
99     protected static void checkSynchronizedSession(final int numberOfLsp, final TestingSessionListener pceSessionListener, final BigInteger expectedeInitialDb) throws InterruptedException {
100         assertTrue(pceSessionListener.isUp());
101         Thread.sleep(1000);
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;
109         }
110         assertEquals(numberOfLspExpected, messages.size());
111         final PCEPSession session = pceSessionListener.getSession();
112         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
113
114         assertTrue(session.getRemoteTlvs().getAugmentation(Tlvs1.class).getStateful().getAugmentation(Stateful1.class).isInitiation());
115         assertNull(session.localSessionCharacteristics().getAugmentation(Tlvs3.class).getLspDbVersion().getLspDbVersionValue());
116     }
117
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);
124         } else {
125             messages = pceSessionListener.messages();
126         }
127         checkSequequenceDBVersionSync(messages, expectedDBVersion);
128         assertEquals(expectedNumberOfLsp, messages.size());
129         final PCEPSession session = pceSessionListener.getSession();
130
131         checkSession(session, DEAD_TIMER, KEEP_ALIVE);
132
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);
136     }
137
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());
144     }
145
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());
153                 } else {
154                     assertEquals(true, lsp.isSync().booleanValue());
155                 }
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);
159             }
160         }
161     }
162
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());
168
169         return pccDispatcher.createClient(getRemoteAdress(), -1,
170             new PCEPSessionListenerFactory() {
171                 @Override
172                 public PCEPSessionListener getSessionListener() {
173                     pccSessionListener = new PCCSessionListener(1, tunnelManager, false);
174                     return pccSessionListener;
175                 }
176             }, snf, null, getLocalAdress(), DBVersion);
177     }
178
179     private InetSocketAddress getLocalAdress() {
180         return new InetSocketAddress(PCCMockTest.LOCAL_ADDRESS, getPort());
181     }
182
183     private InetSocketAddress getRemoteAdress() {
184         return new InetSocketAddress(PCCMockTest.REMOTE_ADDRESS, getPort());
185     }
186
187     private PCEPSessionNegotiatorFactory<PCEPSessionImpl> getSessionNegotiatorFactory() {
188         return new DefaultPCEPSessionNegotiatorFactory(new BasePCEPSessionProposalFactory(DEAD_TIMER, KEEP_ALIVE, getCapabilities()), 0);
189     }
190
191     protected TestingSessionListener getListener(final TestingSessionListenerFactory factory) {
192         return factory.getSessionListenerByRemoteAddress(InetAddresses.forString(PCCMockTest.LOCAL_ADDRESS));
193     }
194 }