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.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
25 import org.opendaylight.yangtools.yang.binding.Notification;
27 public class FiniteStateMachineTest extends AbstractPCEPSessionTest {
29 private DefaultPCEPSessionNegotiator serverSession;
33 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open localPrefs = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setKeepalive(
35 this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.channel, this.listener, (short) 1, 20, localPrefs);
39 * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
40 * establish pce session for the 2nd time.
45 public void testSessionCharsAccBoth() throws Exception {
46 this.serverSession.channelActive(null);
47 assertEquals(1, this.msgsSend.size());
48 assertTrue(this.msgsSend.get(0) instanceof Open);
49 this.serverSession.handleMessage(this.openMsg);
50 assertEquals(2, this.msgsSend.size());
51 assertTrue(this.msgsSend.get(1) instanceof Keepalive);
52 this.serverSession.handleMessage(this.kaMsg);
53 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
57 * Mock PCE does not accept session characteristics the first time.
62 public void testSessionCharsAccMe() throws Exception {
63 this.serverSession.channelActive(null);
64 assertEquals(1, this.msgsSend.size());
65 assertTrue(this.msgsSend.get(0) instanceof Open);
66 final Open remote = (Open) this.msgsSend.get(0);
67 this.serverSession.handleMessage(this.openMsg);
68 assertEquals(2, this.msgsSend.size());
69 assertTrue(this.msgsSend.get(1) instanceof Keepalive);
70 this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, remote.getOpenMessage().getOpen()));
71 assertEquals(3, this.msgsSend.size());
72 assertTrue(this.msgsSend.get(2) instanceof Open);
73 this.serverSession.handleMessage(this.kaMsg);
74 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.FINISHED);
78 * Sending different PCEP Message than Open in session establishment phase.
83 public void testErrorOneOne() throws Exception {
84 this.serverSession.channelActive(null);
85 assertEquals(1, this.msgsSend.size());
86 assertTrue(this.msgsSend.get(0) instanceof Open);
87 this.serverSession.handleMessage(this.kaMsg);
88 for (final Notification m : this.msgsSend) {
89 if (m instanceof Pcerr) {
90 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
91 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
92 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
98 * KeepWaitTimer expired.
103 public void testErrorOneSeven() throws Exception {
104 this.serverSession.channelActive(null);
105 assertEquals(1, this.msgsSend.size());
106 assertTrue(this.msgsSend.get(0) instanceof Open);
107 this.serverSession.handleMessage(this.openMsg);
109 for (final Notification m : this.msgsSend) {
110 if (m instanceof Pcerr) {
111 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
112 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
113 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
118 /************* Tests commented because of their long duration (tested timers) **************/
121 * OpenWait timer expired.
123 * @throws InterruptedException
127 public void testErrorOneTwo() throws InterruptedException {
128 this.serverSession.channelActive(null);
129 assertEquals(1, this.msgsSend.size());
130 assertTrue(this.msgsSend.get(0) instanceof OpenMessage);
131 Thread.sleep(60 * 1000);
132 for (final Notification m : this.msgsSend) {
133 if (m instanceof Pcerr) {
134 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
135 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
136 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
143 public void testUnknownMessage() throws InterruptedException {
144 final SimpleSessionListener client = new SimpleSessionListener();
145 final PCEPSessionImpl s = new PCEPSessionImpl(client, 5, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
146 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
147 assertEquals(1, s.getUnknownMessagesTimes().size());
149 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
150 assertEquals(2, s.getUnknownMessagesTimes().size());
152 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
153 assertEquals(3, s.getUnknownMessagesTimes().size());
155 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
156 assertEquals(4, s.getUnknownMessagesTimes().size());
158 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
159 assertEquals(3, s.getUnknownMessagesTimes().size());
161 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
162 assertEquals(3, s.getUnknownMessagesTimes().size());
164 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
165 assertEquals(4, s.getUnknownMessagesTimes().size());
167 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
168 assertEquals(5, s.getUnknownMessagesTimes().size());
170 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
171 synchronized (client) {
176 assertTrue(!client.up);
180 public void tearDown() {