b50b1a3df62fc474f958723a637e4c0568fb8b30
[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 io.netty.util.concurrent.DefaultPromise;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Ignore;
17 import org.junit.Test;
18 import org.opendaylight.protocol.pcep.impl.spi.Util;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.app.config.rev160707.pcep.dispatcher.config.TlsBuilder;
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 TlsBuilder().build());
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 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 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     @Test
107     public void testTLSUnexpectedMessage() {
108         this.tlsSessionNegotiator.channelActive(null);
109         assertEquals(1, this.msgsSend.size());
110         assertTrue(this.msgsSend.get(0) instanceof Starttls);
111         assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, this.tlsSessionNegotiator.getState());
112         this.tlsSessionNegotiator.handleMessage(this.openMsg);
113         assertEquals(2, this.msgsSend.size());
114         assertTrue(this.msgsSend.get(1) instanceof Pcerr);
115         final Errors obj = ((Pcerr) this.msgsSend.get(1)).getPcerrMessage().getErrors().get(0);
116         assertEquals(PCEPErrors.NON_STARTTLS_MSG_RCVD.getErrorType(), obj.getErrorObject().getType().shortValue());
117         assertEquals(PCEPErrors.NON_STARTTLS_MSG_RCVD.getErrorValue(), obj.getErrorObject().getValue().shortValue());
118         assertEquals(this.tlsSessionNegotiator.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
119     }
120
121     /**
122      * Mock PCE does not accept session characteristics the first time.
123      *
124      * @throws Exception exception
125      */
126     @Test
127     public void testSessionCharsAccMe() throws Exception {
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, remote.getOpenMessage().getOpen()));
136         assertEquals(3, this.msgsSend.size());
137         assertTrue(this.msgsSend.get(2) instanceof Open);
138         this.serverSession.handleMessage(this.kaMsg);
139         assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
140     }
141
142     /**
143      * Sending different PCEP Message than Open in session establishment phase.
144      *
145      * @throws Exception exception
146      */
147     @Test
148     public void testErrorOneOne() throws Exception {
149         this.serverSession.channelActive(null);
150         assertEquals(1, this.msgsSend.size());
151         assertTrue(this.msgsSend.get(0) instanceof Open);
152         this.serverSession.handleMessage(this.kaMsg);
153         for (final Notification m : this.msgsSend) {
154             if (m instanceof Pcerr) {
155                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
156                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
157                 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
158             }
159         }
160     }
161
162     /**
163      * KeepWaitTimer expired.
164      *
165      * @throws Exception exception
166      */
167     @Test
168     public void testErrorOneSeven() throws Exception {
169         this.serverSession.channelActive(null);
170         assertEquals(1, this.msgsSend.size());
171         assertTrue(this.msgsSend.get(0) instanceof Open);
172         this.serverSession.handleMessage(this.openMsg);
173         Thread.sleep(1000);
174         for (final Notification m : this.msgsSend) {
175             if (m instanceof Pcerr) {
176                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
177                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
178                 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
179             }
180         }
181     }
182
183     /************* Tests commented because of their long duration (tested timers) **************/
184
185     /**
186      * OpenWait timer expired.
187      *
188      * @throws InterruptedException exception
189      */
190     @Test
191     @Ignore
192     public void testErrorOneTwo() throws InterruptedException {
193         this.serverSession.channelActive(null);
194         assertEquals(1, this.msgsSend.size());
195         assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
196         Thread.sleep(60 * 1000);
197         for (final Notification m : this.msgsSend) {
198             if (m instanceof Pcerr) {
199                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
200                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
201                 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
202             }
203         }
204     }
205
206     @Test
207     @Ignore
208     public void testUnknownMessage() throws InterruptedException {
209         final SimpleSessionListener client = new SimpleSessionListener();
210         final PCEPSessionImpl s = new PCEPSessionImpl(client, 5, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
211         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
212         assertEquals(1, s.getUnknownMessagesTimes().size());
213         Thread.sleep(10000);
214         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
215         assertEquals(2, s.getUnknownMessagesTimes().size());
216         Thread.sleep(10000);
217         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
218         assertEquals(3, s.getUnknownMessagesTimes().size());
219         Thread.sleep(20000);
220         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
221         assertEquals(4, s.getUnknownMessagesTimes().size());
222         Thread.sleep(30000);
223         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
224         assertEquals(3, s.getUnknownMessagesTimes().size());
225         Thread.sleep(10000);
226         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
227         assertEquals(3, s.getUnknownMessagesTimes().size());
228         Thread.sleep(5000);
229         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
230         assertEquals(4, s.getUnknownMessagesTimes().size());
231         Thread.sleep(1000);
232         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
233         assertEquals(5, s.getUnknownMessagesTimes().size());
234         Thread.sleep(1000);
235         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
236         synchronized (client) {
237             while (client.up) {
238                 client.wait();
239             }
240         }
241         assertTrue(!client.up);
242     }
243
244     @After
245     public void tearDown() {
246     }
247 }