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;
32 import org.opendaylight.yangtools.yang.common.Uint8;
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.rev181109.open.object.Open
43 localPrefs = new OpenBuilder().setKeepalive(Uint8.ONE).build();
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.
55 public void testSessionCharsAccBoth() {
56 this.serverSession.channelActive(null);
57 assertEquals(1, this.msgsSend.size());
58 assertTrue(this.msgsSend.get(0) instanceof Open);
59 this.serverSession.handleMessage(this.openMsg);
60 assertEquals(2, this.msgsSend.size());
61 assertTrue(this.msgsSend.get(1) instanceof Keepalive);
62 this.serverSession.handleMessage(this.kaMsg);
63 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
67 * Establish PCEPS TLS connection with peer.
70 public void testEstablishTLS() {
71 final DefaultPCEPSessionNegotiator negotiator =
72 new DefaultPCEPSessionNegotiator(new DefaultPromise<>(GlobalEventExecutor.INSTANCE),
73 this.channel, this.listener, (short) 1, 20, new OpenBuilder().setKeepalive(Uint8.ONE).build(),
74 SslContextFactoryTest.createTlsConfig());
75 negotiator.channelActive(null);
76 assertEquals(1, this.msgsSend.size());
77 assertTrue(this.msgsSend.get(0) instanceof Starttls);
78 assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, negotiator.getState());
79 negotiator.handleMessage(this.startTlsMsg);
80 assertEquals(DefaultPCEPSessionNegotiator.State.OPEN_WAIT, negotiator.getState());
81 assertEquals(2, this.msgsSend.size());
82 assertTrue(this.msgsSend.get(1) instanceof Open);
83 negotiator.handleMessage(this.openMsg);
84 assertEquals(DefaultPCEPSessionNegotiator.State.KEEP_WAIT, negotiator.getState());
88 * As Tls is not configured properly, PCE will send error PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.
91 public void testFailedToEstablishTLS() {
92 this.tlsSessionNegotiator.channelActive(null);
93 assertEquals(1, this.msgsSend.size());
94 assertTrue(this.msgsSend.get(0) instanceof Starttls);
95 assertEquals(DefaultPCEPSessionNegotiator.State.START_TLS_WAIT, this.tlsSessionNegotiator.getState());
96 this.tlsSessionNegotiator.handleMessage(this.startTlsMsg);
97 assertEquals(2, this.msgsSend.size());
98 assertTrue(this.msgsSend.get(1) instanceof Pcerr);
99 final Errors obj = ((Pcerr) this.msgsSend.get(1)).getPcerrMessage().getErrors().get(0);
100 assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorType(), obj.getErrorObject().getType());
101 assertEquals(PCEPErrors.NOT_POSSIBLE_WITHOUT_TLS.getErrorValue(), obj.getErrorObject().getValue());
102 assertEquals(DefaultPCEPSessionNegotiator.State.FINISHED, this.tlsSessionNegotiator.getState());
106 * 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());
119 assertEquals(PCEPErrors.NON_STARTTLS_MSG_RCVD.getErrorValue(), obj.getErrorObject().getValue());
120 assertEquals(this.tlsSessionNegotiator.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
124 * Mock PCE does not accept session characteristics the first time.
127 public void testSessionCharsAccMe() {
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,
136 remote.getOpenMessage().getOpen()));
137 assertEquals(3, this.msgsSend.size());
138 assertTrue(this.msgsSend.get(2) instanceof Open);
139 this.serverSession.handleMessage(this.kaMsg);
140 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
144 * Sending different PCEP Message than Open in session establishment phase.
146 * @throws Exception exception
149 public void testErrorOneOne() throws Exception {
150 this.serverSession.channelActive(null);
151 assertEquals(1, this.msgsSend.size());
152 assertTrue(this.msgsSend.get(0) instanceof Open);
153 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(Uint8.ONE, obj.getErrorObject().getType());
159 assertEquals(Uint8.ONE, obj.getErrorObject().getValue());
166 * KeepWaitTimer expired.
168 * @throws Exception exception
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);
177 for (final Notification m : this.msgsSend) {
178 if (m instanceof Pcerr) {
179 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
180 assertEquals(Uint8.ONE, obj.getErrorObject().getType());
181 assertEquals(Uint8.valueOf(7), obj.getErrorObject().getValue());
188 * OpenWait timer expired.
190 * @throws InterruptedException exception
193 public void testErrorOneTwo() throws Exception {
194 this.serverSession.channelActive(null);
195 assertEquals(1, this.msgsSend.size());
196 assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
198 for (final Notification m : this.msgsSend) {
199 if (m instanceof Pcerr) {
200 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
201 assertEquals(Uint8.ONE, obj.getErrorObject().getType());
202 assertEquals(Uint8.valueOf(2), obj.getErrorObject().getValue());
209 public void testUnknownMessage() throws Exception {
210 final SimpleSessionListener client = new SimpleSessionListener();
211 final PCEPSessionImpl session = new PCEPSessionImpl(client, 5, this.channel,
212 this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
213 PCEPSessionImpl.setTicker(this.ticker);
214 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
215 final Queue<Long> qeue = session.getUnknownMessagesTimes();
216 CheckTestUtil.checkEquals(() -> assertEquals(1, qeue.size()));
217 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
218 CheckTestUtil.checkEquals(() -> assertEquals(2, qeue.size()));
219 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220 CheckTestUtil.checkEquals(() -> assertEquals(3, qeue.size()));
221 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222 CheckTestUtil.checkEquals(() -> assertEquals(4, 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(3, qeue.size()));
227 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228 CheckTestUtil.checkEquals(() -> assertEquals(4, qeue.size()));
229 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
230 CheckTestUtil.checkEquals(() -> assertEquals(5, qeue.size()));
231 session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232 synchronized (client) {
237 CheckTestUtil.checkEquals(() -> assertTrue(!client.up));
240 private final class TestTicker extends Ticker {
241 private long counter = 0L;
248 if (this.counter == 8) {
251 } else if (this.counter == 10) {
255 return this.counter++;