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;
12 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
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.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.protocol.pcep.impl.spi.Util;
22 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
23 import org.opendaylight.protocol.util.CheckUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.app.config.rev160707.pcep.dispatcher.config.TlsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Starttls;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
32 import org.opendaylight.yangtools.yang.binding.Notification;
34 public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
36 private DefaultPCEPSessionNegotiator serverSession;
37 private DefaultPCEPSessionNegotiator tlsSessionNegotiator;
38 private final TestTicker ticker = new TestTicker();
42 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open localPrefs = new OpenBuilder().setKeepalive(
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());
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.
54 * @throws Exception exception
57 public void testSessionCharsAccBoth() throws Exception {
58 this.serverSession.channelActive(null);
59 assertEquals(1, this.msgsSend.size());
60 assertTrue(this.msgsSend.get(0) instanceof Open);
61 this.serverSession.handleMessage(this.openMsg);
62 assertEquals(2, this.msgsSend.size());
63 assertTrue(this.msgsSend.get(1) instanceof Keepalive);
64 this.serverSession.handleMessage(this.kaMsg);
65 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
69 * Establish PCEPS TLS connection with peer
72 public void testEstablishTLS() {
73 final DefaultPCEPSessionNegotiator negotiator = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
74 this.channel, this.listener, (short) 1, 20, new OpenBuilder().setKeepalive((short) 1).build(),
75 SslContextFactoryTest.createTlsConfig());
76 negotiator.channelActive(null);
77 assertEquals(1, this.msgsSend.size());
78 assertTrue(this.msgsSend.get(0) instanceof Starttls);
79 assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, negotiator.getState());
80 negotiator.handleMessage(this.startTlsMsg);
81 assertEquals(DefaultPCEPSessionNegotiator.State.OPEN_WAIT, negotiator.getState());
82 assertEquals(2, this.msgsSend.size());
83 assertTrue(this.msgsSend.get(1) instanceof Open);
84 negotiator.handleMessage(this.openMsg);
85 assertEquals(DefaultPCEPSessionNegotiator.State.KEEP_WAIT, negotiator.getState());
89 * As Tls is not configured properly, PCE will send error PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS
91 * @throws Exception exception
94 public void testFailedToEstablishTLS() throws Exception {
95 this.tlsSessionNegotiator.channelActive(null);
96 assertEquals(1, this.msgsSend.size());
97 assertTrue(this.msgsSend.get(0) instanceof Starttls);
98 assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, this.tlsSessionNegotiator.getState());
99 this.tlsSessionNegotiator.handleMessage(this.startTlsMsg);
100 assertEquals(2, this.msgsSend.size());
101 assertTrue(this.msgsSend.get(1) instanceof Pcerr);
102 final Errors obj = ((Pcerr) this.msgsSend.get(1)).getPcerrMessage().getErrors().get(0);
103 assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorType(), obj.getErrorObject().getType().shortValue());
104 assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorValue(), obj.getErrorObject().getValue().shortValue());
105 assertEquals(DefaultPCEPSessionNegotiator.State.FINISHED, this.tlsSessionNegotiator.getState());
109 * As PCE does not receive expected message (StartTLS), error PCEPErrors.NON_STARTTLS_MSG_RCVD is send
112 public void testTLSUnexpectedMessage() {
113 this.tlsSessionNegotiator.channelActive(null);
114 assertEquals(1, this.msgsSend.size());
115 assertTrue(this.msgsSend.get(0) instanceof Starttls);
116 assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, this.tlsSessionNegotiator.getState());
117 this.tlsSessionNegotiator.handleMessage(this.openMsg);
118 assertEquals(2, this.msgsSend.size());
119 assertTrue(this.msgsSend.get(1) instanceof Pcerr);
120 final Errors obj = ((Pcerr) this.msgsSend.get(1)).getPcerrMessage().getErrors().get(0);
121 assertEquals(PCEPErrors.NON_STARTTLS_MSG_RCVD.getErrorType(), obj.getErrorObject().getType().shortValue());
122 assertEquals(PCEPErrors.NON_STARTTLS_MSG_RCVD.getErrorValue(), obj.getErrorObject().getValue().shortValue());
123 assertEquals(this.tlsSessionNegotiator.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
127 * Mock PCE does not accept session characteristics the first time.
129 * @throws Exception exception
132 public void testSessionCharsAccMe() throws Exception {
133 this.serverSession.channelActive(null);
134 assertEquals(1, this.msgsSend.size());
135 assertTrue(this.msgsSend.get(0) instanceof Open);
136 final Open remote = (Open) this.msgsSend.get(0);
137 this.serverSession.handleMessage(this.openMsg);
138 assertEquals(2, this.msgsSend.size());
139 assertTrue(this.msgsSend.get(1) instanceof Keepalive);
140 this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, remote.getOpenMessage().getOpen()));
141 assertEquals(3, this.msgsSend.size());
142 assertTrue(this.msgsSend.get(2) instanceof Open);
143 this.serverSession.handleMessage(this.kaMsg);
144 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
148 * Sending different PCEP Message than Open in session establishment phase.
150 * @throws Exception exception
153 public void testErrorOneOne() throws Exception {
154 this.serverSession.channelActive(null);
155 assertEquals(1, this.msgsSend.size());
156 assertTrue(this.msgsSend.get(0) instanceof Open);
157 this.serverSession.handleMessage(this.kaMsg);
159 for (final Notification m : this.msgsSend) {
160 if (m instanceof Pcerr) {
161 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
162 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
163 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
170 * KeepWaitTimer expired.
172 * @throws Exception exception
175 public void testErrorOneSeven() throws Exception {
176 this.serverSession.channelActive(null);
177 assertEquals(1, this.msgsSend.size());
178 assertTrue(this.msgsSend.get(0) instanceof Open);
179 this.serverSession.handleMessage(this.openMsg);
181 for (final Notification m : this.msgsSend) {
182 if (m instanceof Pcerr) {
183 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
184 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
185 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
192 * OpenWait timer expired.
194 * @throws InterruptedException exception
197 public void testErrorOneTwo() throws Exception {
198 this.serverSession.channelActive(null);
199 assertEquals(1, this.msgsSend.size());
200 assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
202 for (final Notification m : this.msgsSend) {
203 if (m instanceof Pcerr) {
204 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
205 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
206 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
213 public void testUnknownMessage() throws Exception {
214 final SimpleSessionListener client = new SimpleSessionListener();
215 final PCEPSessionImpl session = new PCEPSessionImpl(client, 5, this.channel,
216 this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
217 PCEPSessionImpl.setTicker(this.ticker);
218 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
219 final Queue<Long> qeue = session.getUnknownMessagesTimes();
220 CheckUtil.checkEquals(()-> assertEquals(1, qeue.size()));
221 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222 CheckUtil.checkEquals(()-> assertEquals(2, qeue.size()));
223 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
224 CheckUtil.checkEquals(()-> assertEquals(3, qeue.size()));
225 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226 CheckUtil.checkEquals(()-> assertEquals(4, qeue.size()));
227 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228 CheckUtil.checkEquals(()-> assertEquals(3, qeue.size()));
229 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
230 CheckUtil.checkEquals(()-> assertEquals(3, qeue.size()));
231 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232 CheckUtil.checkEquals(()-> assertEquals(4, qeue.size()));
233 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
234 CheckUtil.checkEquals(()-> assertEquals(5, qeue.size()));
235 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
236 synchronized (client) {
241 CheckUtil.checkEquals(()-> assertTrue(!client.up));
245 public void tearDown() {
248 private final class TestTicker extends Ticker {
249 private long counter = 0L;
255 if (this.counter == 8) {
258 } else if (this.counter == 10) {
262 return this.counter++;