4cb597b1aaf515e16d4182e396ccb6038b3e8714
[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
13 import io.netty.util.concurrent.DefaultPromise;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.controller.config.yang.pcep.impl.Tls;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Starttls;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
28 import org.opendaylight.yangtools.yang.binding.Notification;
29
30 public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
31
32     private DefaultPCEPSessionNegotiator serverSession;
33     private DefaultPCEPSessionNegotiator tlsSessionNegotiator;
34
35     @Before
36     public void setup() {
37         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open localPrefs = new OpenBuilder().setKeepalive(
38                 (short) 1).build();
39         this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE),
40                 this.channel, this.listener, (short) 1, 20, localPrefs);
41         this.tlsSessionNegotiator = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE),
42                 this.channel, this.listener, (short) 1, 20, localPrefs, new Tls());
43     }
44
45     /**
46      * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
47      * establish pce session for the 2nd time.
48      *
49      * @throws Exception
50      */
51     @Test
52     public void testSessionCharsAccBoth() throws Exception {
53         this.serverSession.channelActive(null);
54         assertEquals(1, this.msgsSend.size());
55         assertTrue(this.msgsSend.get(0) instanceof Open);
56         this.serverSession.handleMessage(this.openMsg);
57         assertEquals(2, this.msgsSend.size());
58         assertTrue(this.msgsSend.get(1) instanceof Keepalive);
59         this.serverSession.handleMessage(this.kaMsg);
60         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
61     }
62
63     /**
64      * Establish PCEPS TLS connection with peer
65      */
66     @Test
67     public void testEstablishTLS() {
68         final DefaultPCEPSessionNegotiator negotiator = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE),
69                 this.channel, this.listener, (short) 1, 20, new OpenBuilder().setKeepalive((short) 1).build(),
70                 SslContextFactoryTest.createTlsConfig());
71         negotiator.channelActive(null);
72         assertEquals(1, this.msgsSend.size());
73         assertTrue(this.msgsSend.get(0) instanceof Starttls);
74         assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, negotiator.getState());
75         negotiator.handleMessage(this.startTlsMsg);
76         assertEquals(DefaultPCEPSessionNegotiator.State.OPEN_WAIT, negotiator.getState());
77         assertEquals(2, this.msgsSend.size());
78         assertTrue(this.msgsSend.get(1) instanceof Open);
79         negotiator.handleMessage(this.openMsg);
80         assertEquals(DefaultPCEPSessionNegotiator.State.KEEP_WAIT, negotiator.getState());
81     }
82
83     /**
84      * As Tls is not configured properly, PCE will send error PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS
85      *
86      * @throws Exception
87      */
88     @Test
89     public void testFailedToEstablishTLS() throws Exception {
90         this.tlsSessionNegotiator.channelActive(null);
91         assertEquals(1, this.msgsSend.size());
92         assertTrue(this.msgsSend.get(0) instanceof Starttls);
93         assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, this.tlsSessionNegotiator.getState());
94         this.tlsSessionNegotiator.handleMessage(this.startTlsMsg);
95         assertEquals(2, this.msgsSend.size());
96         assertTrue(this.msgsSend.get(1) instanceof Pcerr);
97         final Errors obj = ((Pcerr) this.msgsSend.get(1)).getPcerrMessage().getErrors().get(0);
98         assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorType(), obj.getErrorObject().getType().shortValue());
99         assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorValue(), obj.getErrorObject().getValue().shortValue());
100         assertEquals(DefaultPCEPSessionNegotiator.State.FINISHED, this.tlsSessionNegotiator.getState());
101     }
102
103     /**
104      * As PCE does not receive expected message (StartTLS), error PCEPErrors.NON_STARTTLS_MSG_RCVD is send
105      *
106      * @throws Exception
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      * @throws Exception
127      */
128     @Test
129     public void testSessionCharsAccMe() throws Exception {
130         this.serverSession.channelActive(null);
131         assertEquals(1, this.msgsSend.size());
132         assertTrue(this.msgsSend.get(0) instanceof Open);
133         final Open remote = (Open) this.msgsSend.get(0);
134         this.serverSession.handleMessage(this.openMsg);
135         assertEquals(2, this.msgsSend.size());
136         assertTrue(this.msgsSend.get(1) instanceof Keepalive);
137         this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, remote.getOpenMessage().getOpen()));
138         assertEquals(3, this.msgsSend.size());
139         assertTrue(this.msgsSend.get(2) instanceof Open);
140         this.serverSession.handleMessage(this.kaMsg);
141         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
142     }
143
144     /**
145      * Sending different PCEP Message than Open in session establishment phase.
146      *
147      * @throws Exception
148      */
149     @Test
150     public void testErrorOneOne() throws Exception {
151         this.serverSession.channelActive(null);
152         assertEquals(1, this.msgsSend.size());
153         assertTrue(this.msgsSend.get(0) instanceof Open);
154         this.serverSession.handleMessage(this.kaMsg);
155         for (final Notification m : this.msgsSend) {
156             if (m instanceof Pcerr) {
157                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
158                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
159                 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
160             }
161         }
162     }
163
164     /**
165      * KeepWaitTimer expired.
166      *
167      * @throws Exception
168      */
169     @Test
170     public void testErrorOneSeven() throws Exception {
171         this.serverSession.channelActive(null);
172         assertEquals(1, this.msgsSend.size());
173         assertTrue(this.msgsSend.get(0) instanceof Open);
174         this.serverSession.handleMessage(this.openMsg);
175         Thread.sleep(1000);
176         for (final Notification m : this.msgsSend) {
177             if (m instanceof Pcerr) {
178                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
179                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
180                 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
181             }
182         }
183     }
184
185     /************* Tests commented because of their long duration (tested timers) **************/
186
187     /**
188      * OpenWait timer expired.
189      *
190      * @throws InterruptedException
191      */
192     @Test
193     @Ignore
194     public void testErrorOneTwo() throws InterruptedException {
195         this.serverSession.channelActive(null);
196         assertEquals(1, this.msgsSend.size());
197         assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
198         Thread.sleep(60 * 1000);
199         for (final Notification m : this.msgsSend) {
200             if (m instanceof Pcerr) {
201                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
202                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
203                 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
204             }
205         }
206     }
207
208     @Test
209     @Ignore
210     public void testUnknownMessage() throws InterruptedException {
211         final SimpleSessionListener client = new SimpleSessionListener();
212         final PCEPSessionImpl s = new PCEPSessionImpl(client, 5, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
213         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
214         assertEquals(1, s.getUnknownMessagesTimes().size());
215         Thread.sleep(10000);
216         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
217         assertEquals(2, s.getUnknownMessagesTimes().size());
218         Thread.sleep(10000);
219         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220         assertEquals(3, s.getUnknownMessagesTimes().size());
221         Thread.sleep(20000);
222         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
223         assertEquals(4, s.getUnknownMessagesTimes().size());
224         Thread.sleep(30000);
225         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226         assertEquals(3, s.getUnknownMessagesTimes().size());
227         Thread.sleep(10000);
228         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
229         assertEquals(3, s.getUnknownMessagesTimes().size());
230         Thread.sleep(5000);
231         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232         assertEquals(4, s.getUnknownMessagesTimes().size());
233         Thread.sleep(1000);
234         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
235         assertEquals(5, s.getUnknownMessagesTimes().size());
236         Thread.sleep(1000);
237         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
238         synchronized (client) {
239             while (client.up) {
240                 client.wait();
241             }
242         }
243         assertTrue(!client.up);
244     }
245
246     @After
247     public void tearDown() {
248     }
249 }