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