Further migration of test code from legacy setters
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / FiniteStateMachineTest.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.protocol.util.CheckTestUtil.checkEquals;
13
14 import com.google.common.base.Ticker;
15 import io.netty.util.concurrent.DefaultPromise;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import java.util.Queue;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.impl.spi.Util;
21 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
22 import org.opendaylight.protocol.util.CheckTestUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.app.config.rev160707.pcep.dispatcher.config.TlsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Keepalive;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Open;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Starttls;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.OpenMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.OpenBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.Errors;
31 import org.opendaylight.yangtools.yang.binding.Notification;
32 import org.opendaylight.yangtools.yang.common.Uint8;
33
34 public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
35
36     private DefaultPCEPSessionNegotiator serverSession;
37     private DefaultPCEPSessionNegotiator tlsSessionNegotiator;
38     private final TestTicker ticker = new TestTicker();
39
40     @Before
41     public void setup() {
42         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open
43             localPrefs = new OpenBuilder().setKeepalive(Uint8.ONE).build();
44         this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
45                 this.channel, this.listener, (short) 1, 20, localPrefs);
46         this.tlsSessionNegotiator = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
47                 this.channel, this.listener, (short) 1, 20, localPrefs, new TlsBuilder().build());
48     }
49
50     /**
51      * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
52      * establish pce session for the 2nd time.
53      */
54     @Test
55     public void testSessionCharsAccBoth() {
56         this.serverSession.channelActive(null);
57         assertEquals(1, this.msgsSend.size());
58         assertTrue(this.msgsSend.get(0) instanceof Open);
59         this.serverSession.handleMessage(this.openMsg);
60         assertEquals(2, this.msgsSend.size());
61         assertTrue(this.msgsSend.get(1) instanceof Keepalive);
62         this.serverSession.handleMessage(this.kaMsg);
63         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
64     }
65
66     /**
67      * Establish PCEPS TLS connection with peer.
68      */
69     @Test
70     public void testEstablishTLS() {
71         final DefaultPCEPSessionNegotiator negotiator =
72             new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
73                 this.channel, this.listener, (short) 1, 20, new OpenBuilder().setKeepalive(Uint8.ONE).build(),
74                 SslContextFactoryTest.createTlsConfig());
75         negotiator.channelActive(null);
76         assertEquals(1, this.msgsSend.size());
77         assertTrue(this.msgsSend.get(0) instanceof Starttls);
78         assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, negotiator.getState());
79         negotiator.handleMessage(this.startTlsMsg);
80         assertEquals(DefaultPCEPSessionNegotiator.State.OPEN_WAIT, negotiator.getState());
81         assertEquals(2, this.msgsSend.size());
82         assertTrue(this.msgsSend.get(1) instanceof Open);
83         negotiator.handleMessage(this.openMsg);
84         assertEquals(DefaultPCEPSessionNegotiator.State.KEEP_WAIT, negotiator.getState());
85     }
86
87     /**
88      * As Tls is not configured properly, PCE will send error PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.
89      */
90     @Test
91     public void testFailedToEstablishTLS() {
92         this.tlsSessionNegotiator.channelActive(null);
93         assertEquals(1, this.msgsSend.size());
94         assertTrue(this.msgsSend.get(0) instanceof Starttls);
95         assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, this.tlsSessionNegotiator.getState());
96         this.tlsSessionNegotiator.handleMessage(this.startTlsMsg);
97         assertEquals(2, this.msgsSend.size());
98         assertTrue(this.msgsSend.get(1) instanceof Pcerr);
99         final Errors obj = ((Pcerr) this.msgsSend.get(1)).getPcerrMessage().getErrors().get(0);
100         assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorType(), obj.getErrorObject().getType().shortValue());
101         assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorValue(), obj.getErrorObject().getValue().shortValue());
102         assertEquals(DefaultPCEPSessionNegotiator.State.FINISHED, this.tlsSessionNegotiator.getState());
103     }
104
105     /**
106      * As PCE does not receive expected message (StartTLS), error PCEPErrors.NON_STARTTLS_MSG_RCVD is send.
107      */
108     @Test
109     public void testTLSUnexpectedMessage() {
110         this.tlsSessionNegotiator.channelActive(null);
111         assertEquals(1, this.msgsSend.size());
112         assertTrue(this.msgsSend.get(0) instanceof Starttls);
113         assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, this.tlsSessionNegotiator.getState());
114         this.tlsSessionNegotiator.handleMessage(this.openMsg);
115         assertEquals(2, this.msgsSend.size());
116         assertTrue(this.msgsSend.get(1) instanceof Pcerr);
117         final Errors obj = ((Pcerr) this.msgsSend.get(1)).getPcerrMessage().getErrors().get(0);
118         assertEquals(PCEPErrors.NON_STARTTLS_MSG_RCVD.getErrorType(), obj.getErrorObject().getType().shortValue());
119         assertEquals(PCEPErrors.NON_STARTTLS_MSG_RCVD.getErrorValue(), obj.getErrorObject().getValue().shortValue());
120         assertEquals(this.tlsSessionNegotiator.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
121     }
122
123     /**
124      * Mock PCE does not accept session characteristics the first time.
125      */
126     @Test
127     public void testSessionCharsAccMe() {
128         this.serverSession.channelActive(null);
129         assertEquals(1, this.msgsSend.size());
130         assertTrue(this.msgsSend.get(0) instanceof Open);
131         final Open remote = (Open) this.msgsSend.get(0);
132         this.serverSession.handleMessage(this.openMsg);
133         assertEquals(2, this.msgsSend.size());
134         assertTrue(this.msgsSend.get(1) instanceof Keepalive);
135         this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR,
136             remote.getOpenMessage().getOpen()));
137         assertEquals(3, this.msgsSend.size());
138         assertTrue(this.msgsSend.get(2) instanceof Open);
139         this.serverSession.handleMessage(this.kaMsg);
140         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
141     }
142
143     /**
144      * Sending different PCEP Message than Open in session establishment phase.
145      *
146      * @throws Exception exception
147      */
148     @Test
149     public void testErrorOneOne() throws Exception {
150         this.serverSession.channelActive(null);
151         assertEquals(1, this.msgsSend.size());
152         assertTrue(this.msgsSend.get(0) instanceof Open);
153         this.serverSession.handleMessage(this.kaMsg);
154         checkEquals(() -> {
155             for (final Notification m : this.msgsSend) {
156                 if (m instanceof Pcerr) {
157                     final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
158                     assertEquals(Uint8.ONE, obj.getErrorObject().getType());
159                     assertEquals(Uint8.ONE, obj.getErrorObject().getValue());
160                 }
161             }
162         });
163     }
164
165     /**
166      * KeepWaitTimer expired.
167      *
168      * @throws Exception exception
169      */
170     @Test
171     public void testErrorOneSeven() throws Exception {
172         this.serverSession.channelActive(null);
173         assertEquals(1, this.msgsSend.size());
174         assertTrue(this.msgsSend.get(0) instanceof Open);
175         this.serverSession.handleMessage(this.openMsg);
176         checkEquals(() -> {
177             for (final Notification m : this.msgsSend) {
178                 if (m instanceof Pcerr) {
179                     final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
180                     assertEquals(Uint8.ONE, obj.getErrorObject().getType());
181                     assertEquals(Uint8.valueOf(7), obj.getErrorObject().getValue());
182                 }
183             }
184         });
185     }
186
187     /**
188      * OpenWait timer expired.
189      *
190      * @throws InterruptedException exception
191      */
192     @Test
193     public void testErrorOneTwo() throws Exception {
194         this.serverSession.channelActive(null);
195         assertEquals(1, this.msgsSend.size());
196         assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
197         checkEquals(() -> {
198             for (final Notification m : this.msgsSend) {
199                 if (m instanceof Pcerr) {
200                     final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
201                     assertEquals(Uint8.ONE, obj.getErrorObject().getType());
202                     assertEquals(Uint8.valueOf(2), obj.getErrorObject().getValue());
203                 }
204             }
205         });
206     }
207
208     @Test
209     public void testUnknownMessage() throws Exception {
210         final SimpleSessionListener client = new SimpleSessionListener();
211         final PCEPSessionImpl session = new PCEPSessionImpl(client, 5, this.channel,
212             this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
213         PCEPSessionImpl.setTicker(this.ticker);
214         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
215         final Queue<Long> qeue = session.getUnknownMessagesTimes();
216         CheckTestUtil.checkEquals(() -> assertEquals(1, qeue.size()));
217         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
218         CheckTestUtil.checkEquals(() -> assertEquals(2, qeue.size()));
219         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220         CheckTestUtil.checkEquals(() -> assertEquals(3, qeue.size()));
221         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222         CheckTestUtil.checkEquals(() -> assertEquals(4, qeue.size()));
223         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
224         CheckTestUtil.checkEquals(() -> assertEquals(3, qeue.size()));
225         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226         CheckTestUtil.checkEquals(() -> assertEquals(3, qeue.size()));
227         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228         CheckTestUtil.checkEquals(() -> assertEquals(4, qeue.size()));
229         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
230         CheckTestUtil.checkEquals(() -> assertEquals(5, qeue.size()));
231         session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232         synchronized (client) {
233             while (client.up) {
234                 client.wait();
235             }
236         }
237         CheckTestUtil.checkEquals(() -> assertTrue(!client.up));
238     }
239
240     private final class TestTicker extends Ticker {
241         private long counter = 0L;
242
243         TestTicker() {
244         }
245
246         @Override
247         public long read() {
248             if (this.counter == 8) {
249                 this.counter++;
250                 return 60000000003L;
251             } else if (this.counter == 10) {
252                 this.counter++;
253                 return 60000000006L;
254             }
255             return this.counter++;
256         }
257     }
258 }