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.CheckTestUtil.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.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.impl.spi.Util;
21 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
22 import org.opendaylight.protocol.util.CheckTestUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.app.config.rev160707.pcep.dispatcher.config.TlsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Keepalive;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Open;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Starttls;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.OpenMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.OpenBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.Errors;
31 import org.opendaylight.yangtools.yang.binding.Notification;
33 public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
35 private DefaultPCEPSessionNegotiator serverSession;
36 private DefaultPCEPSessionNegotiator tlsSessionNegotiator;
37 private final TestTicker ticker = new TestTicker();
41 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.object.Open localPrefs = new OpenBuilder().setKeepalive(
43 this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
44 this.channel, this.listener, (short) 1, 20, localPrefs);
45 this.tlsSessionNegotiator = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
46 this.channel, this.listener, (short) 1, 20, localPrefs, new TlsBuilder().build());
50 * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
51 * establish pce session for the 2nd time.
54 public void testSessionCharsAccBoth() {
55 this.serverSession.channelActive(null);
56 assertEquals(1, this.msgsSend.size());
57 assertTrue(this.msgsSend.get(0) instanceof Open);
58 this.serverSession.handleMessage(this.openMsg);
59 assertEquals(2, this.msgsSend.size());
60 assertTrue(this.msgsSend.get(1) instanceof Keepalive);
61 this.serverSession.handleMessage(this.kaMsg);
62 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
66 * Establish PCEPS TLS connection with peer
69 public void testEstablishTLS() {
70 final DefaultPCEPSessionNegotiator negotiator = new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
71 this.channel, this.listener, (short) 1, 20, new OpenBuilder().setKeepalive((short) 1).build(),
72 SslContextFactoryTest.createTlsConfig());
73 negotiator.channelActive(null);
74 assertEquals(1, this.msgsSend.size());
75 assertTrue(this.msgsSend.get(0) instanceof Starttls);
76 assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, negotiator.getState());
77 negotiator.handleMessage(this.startTlsMsg);
78 assertEquals(DefaultPCEPSessionNegotiator.State.OPEN_WAIT, negotiator.getState());
79 assertEquals(2, this.msgsSend.size());
80 assertTrue(this.msgsSend.get(1) instanceof Open);
81 negotiator.handleMessage(this.openMsg);
82 assertEquals(DefaultPCEPSessionNegotiator.State.KEEP_WAIT, negotiator.getState());
86 * As Tls is not configured properly, PCE will send error PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS
89 public void testFailedToEstablishTLS() {
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
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);
122 * Mock PCE does not accept session characteristics the first time.
125 public void testSessionCharsAccMe() {
126 this.serverSession.channelActive(null);
127 assertEquals(1, this.msgsSend.size());
128 assertTrue(this.msgsSend.get(0) instanceof Open);
129 final Open remote = (Open) this.msgsSend.get(0);
130 this.serverSession.handleMessage(this.openMsg);
131 assertEquals(2, this.msgsSend.size());
132 assertTrue(this.msgsSend.get(1) instanceof Keepalive);
133 this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR,
134 remote.getOpenMessage().getOpen()));
135 assertEquals(3, this.msgsSend.size());
136 assertTrue(this.msgsSend.get(2) instanceof Open);
137 this.serverSession.handleMessage(this.kaMsg);
138 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
142 * Sending different PCEP Message than Open in session establishment phase.
144 * @throws Exception exception
147 public void testErrorOneOne() throws Exception {
148 this.serverSession.channelActive(null);
149 assertEquals(1, this.msgsSend.size());
150 assertTrue(this.msgsSend.get(0) instanceof Open);
151 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());
164 * KeepWaitTimer expired.
166 * @throws Exception exception
169 public void testErrorOneSeven() throws Exception {
170 this.serverSession.channelActive(null);
171 assertEquals(1, this.msgsSend.size());
172 assertTrue(this.msgsSend.get(0) instanceof Open);
173 this.serverSession.handleMessage(this.openMsg);
175 for (final Notification m : this.msgsSend) {
176 if (m instanceof Pcerr) {
177 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
178 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
179 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
186 * OpenWait timer expired.
188 * @throws InterruptedException exception
191 public void testErrorOneTwo() throws Exception {
192 this.serverSession.channelActive(null);
193 assertEquals(1, this.msgsSend.size());
194 assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
196 for (final Notification m : this.msgsSend) {
197 if (m instanceof Pcerr) {
198 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
199 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
200 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
207 public void testUnknownMessage() throws Exception {
208 final SimpleSessionListener client = new SimpleSessionListener();
209 final PCEPSessionImpl session = new PCEPSessionImpl(client, 5, this.channel,
210 this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
211 PCEPSessionImpl.setTicker(this.ticker);
212 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
213 final Queue<Long> qeue = session.getUnknownMessagesTimes();
214 CheckTestUtil.checkEquals(() -> assertEquals(1, qeue.size()));
215 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
216 CheckTestUtil.checkEquals(() -> assertEquals(2, qeue.size()));
217 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
218 CheckTestUtil.checkEquals(() -> assertEquals(3, qeue.size()));
219 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220 CheckTestUtil.checkEquals(() -> assertEquals(4, qeue.size()));
221 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222 CheckTestUtil.checkEquals(() -> assertEquals(3, qeue.size()));
223 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
224 CheckTestUtil.checkEquals(() -> assertEquals(3, qeue.size()));
225 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226 CheckTestUtil.checkEquals(() -> assertEquals(4, qeue.size()));
227 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228 CheckTestUtil.checkEquals(() -> assertEquals(5, qeue.size()));
229 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
230 synchronized (client) {
235 CheckTestUtil.checkEquals(() -> assertTrue(!client.up));
238 private final class TestTicker extends Ticker {
239 private long counter = 0L;
246 if (this.counter == 8) {
249 } else if (this.counter == 10) {
253 return this.counter++;