2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
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;
30 public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
32 private DefaultPCEPSessionNegotiator serverSession;
33 private DefaultPCEPSessionNegotiator tlsSessionNegotiator;
37 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open localPrefs = new OpenBuilder().setKeepalive(
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());
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.
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);
64 * Establish PCEPS TLS connection with peer
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());
84 * As Tls is not configured properly, PCE will send error PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS
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());
104 * As PCE does not receive expected message (StartTLS), error PCEPErrors.NON_STARTTLS_MSG_RCVD is send
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);
124 * Mock PCE does not accept session characteristics the first time.
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);
145 * Sending different PCEP Message than Open in session establishment phase.
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());
165 * KeepWaitTimer expired.
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);
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());
185 /************* Tests commented because of their long duration (tested timers) **************/
188 * OpenWait timer expired.
190 * @throws InterruptedException
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());
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());
216 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
217 assertEquals(2, s.getUnknownMessagesTimes().size());
219 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220 assertEquals(3, s.getUnknownMessagesTimes().size());
222 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
223 assertEquals(4, s.getUnknownMessagesTimes().size());
225 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226 assertEquals(3, s.getUnknownMessagesTimes().size());
228 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
229 assertEquals(3, s.getUnknownMessagesTimes().size());
231 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232 assertEquals(4, s.getUnknownMessagesTimes().size());
234 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
235 assertEquals(5, s.getUnknownMessagesTimes().size());
237 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
238 synchronized (client) {
243 assertTrue(!client.up);
247 public void tearDown() {