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.mockito.Matchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import io.netty.channel.Channel;
17 import io.netty.channel.ChannelFuture;
18 import io.netty.channel.ChannelHandler;
19 import io.netty.channel.ChannelPipeline;
20 import io.netty.channel.DefaultChannelPromise;
21 import io.netty.util.HashedWheelTimer;
22 import io.netty.util.concurrent.DefaultPromise;
23 import io.netty.util.concurrent.GlobalEventExecutor;
25 import java.util.Arrays;
26 import java.util.List;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
36 import org.opendaylight.protocol.pcep.spi.PCEPErrorMapping;
37 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.SessionBuilder;
52 import org.opendaylight.yangtools.yang.binding.Notification;
54 import com.google.common.collect.Lists;
56 public class FiniteStateMachineTest {
58 private DefaultPCEPSessionNegotiator serverSession;
61 private Channel clientListener;
64 private ChannelPipeline pipeline;
66 private final List<Notification> receivedMsgs = Lists.newArrayList();
70 private Keepalive kamsg;
74 MockitoAnnotations.initMocks(this);
75 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(
77 this.serverSession = new DefaultPCEPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.clientListener, new SimpleSessionListener(), (short) 1, 20, localPrefs);
78 final ChannelFuture future = new DefaultChannelPromise(this.clientListener);
79 doAnswer(new Answer<Object>() {
81 public Object answer(final InvocationOnMock invocation) {
82 final Object[] args = invocation.getArguments();
83 FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
86 }).when(this.clientListener).writeAndFlush(any(Notification.class));
87 doReturn("TestingChannel").when(this.clientListener).toString();
88 doReturn(this.pipeline).when(this.clientListener).pipeline();
89 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
90 doReturn(true).when(this.clientListener).isActive();
91 doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
92 this.openmsg = new OpenBuilder().setOpenMessage(
93 new OpenMessageBuilder().setOpen(
94 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
95 (short) 45).setKeepalive((short) 15).build()).build()).build();
96 this.kamsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
100 * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
101 * establish pce session for the 2nd time.
106 public void testSessionCharsAccBoth() throws Exception {
107 this.serverSession.channelActive(null);
108 assertEquals(1, this.receivedMsgs.size());
109 assertTrue(this.receivedMsgs.get(0) instanceof Open);
110 this.serverSession.handleMessage(this.openmsg);
111 assertEquals(2, this.receivedMsgs.size());
112 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
113 this.serverSession.handleMessage(this.kamsg);
114 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
118 * Mock PCE does not accept session characteristics the first time.
123 public void testSessionCharsAccMe() throws Exception {
124 this.serverSession.channelActive(null);
125 assertEquals(1, this.receivedMsgs.size());
126 assertTrue(this.receivedMsgs.get(0) instanceof Open);
127 this.serverSession.handleMessage(this.openmsg);
128 assertEquals(2, this.receivedMsgs.size());
129 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
130 this.serverSession.handleMessage(createErrorMessageWOpen(PCEPErrors.NON_ACC_NEG_SESSION_CHAR));
131 assertEquals(3, this.receivedMsgs.size());
132 assertTrue(this.receivedMsgs.get(2) instanceof Open);
133 this.serverSession.handleMessage(this.kamsg);
134 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
137 private Pcerr createErrorMessageWOpen(final PCEPErrors e) {
138 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
139 return new PcerrBuilder().setPcerrMessage(
140 new PcerrMessageBuilder().setErrorType(
141 new SessionBuilder().setOpen(
142 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setKeepalive(
143 (short) 1).build()).build()).setErrors(
144 Arrays.asList(new ErrorsBuilder().setErrorObject(
145 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).type).setValue(
146 maping.getFromErrorsEnum(e).value).build()).build())).build()).build();
150 * Sending different PCEP Message than Open in session establishment phase.
155 public void testErrorOneOne() throws Exception {
156 this.serverSession.channelActive(null);
157 assertEquals(1, this.receivedMsgs.size());
158 assertTrue(this.receivedMsgs.get(0) instanceof Open);
159 this.serverSession.handleMessage(this.kamsg);
160 for (final Notification m : this.receivedMsgs) {
161 if (m instanceof Pcerr) {
162 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
163 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
164 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
170 * KeepWaitTimer expired.
175 public void testErrorOneSeven() throws Exception {
176 this.serverSession.channelActive(null);
177 assertEquals(1, this.receivedMsgs.size());
178 assertTrue(this.receivedMsgs.get(0) instanceof Open);
179 this.serverSession.handleMessage(this.openmsg);
181 for (final Notification m : this.receivedMsgs) {
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());
190 /************* Tests commented because of their long duration (tested timers) **************/
193 * OpenWait timer expired.
195 * @throws InterruptedException
199 public void testErrorOneTwo() throws InterruptedException {
200 this.serverSession.channelActive(null);
201 assertEquals(1, this.receivedMsgs.size());
202 assertTrue(this.receivedMsgs.get(0) instanceof OpenMessage);
203 Thread.sleep(60 * 1000);
204 for (final Notification m : this.receivedMsgs) {
205 if (m instanceof Pcerr) {
206 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
207 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
208 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
215 public void testUnknownMessage() throws InterruptedException {
216 final SimpleSessionListener client = new SimpleSessionListener();
217 final PCEPSessionImpl s = new PCEPSessionImpl(new HashedWheelTimer(), client, 5, this.clientListener, this.openmsg.getOpenMessage().getOpen(), this.openmsg.getOpenMessage().getOpen());
218 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
219 assertEquals(1, s.unknownMessagesTimes.size());
221 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222 assertEquals(2, s.unknownMessagesTimes.size());
224 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
225 assertEquals(3, s.unknownMessagesTimes.size());
227 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228 assertEquals(4, s.unknownMessagesTimes.size());
230 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
231 assertEquals(3, s.unknownMessagesTimes.size());
233 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
234 assertEquals(3, s.unknownMessagesTimes.size());
236 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
237 assertEquals(4, s.unknownMessagesTimes.size());
239 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
240 assertEquals(5, s.unknownMessagesTimes.size());
242 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
243 synchronized (client) {
247 } catch (final InterruptedException e) {
252 assertTrue(!client.up);
256 public void tearDown() {