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;
17 import com.google.common.collect.Lists;
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelHandler;
21 import io.netty.channel.ChannelPipeline;
22 import io.netty.channel.DefaultChannelPromise;
23 import io.netty.channel.EventLoop;
24 import io.netty.util.concurrent.DefaultPromise;
25 import io.netty.util.concurrent.GlobalEventExecutor;
26 import io.netty.util.concurrent.ScheduledFuture;
27 import java.net.InetSocketAddress;
28 import java.net.SocketAddress;
29 import java.util.List;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.invocation.InvocationOnMock;
38 import org.mockito.stubbing.Answer;
39 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
49 import org.opendaylight.yangtools.yang.binding.Notification;
51 public class FiniteStateMachineTest {
53 private DefaultPCEPSessionNegotiator serverSession;
56 private Channel clientListener;
59 private EventLoop eventLoop;
62 private ScheduledFuture<?> future;
65 private ChannelPipeline pipeline;
68 private SocketAddress address;
70 private final List<Notification> receivedMsgs = Lists.newArrayList();
74 private Keepalive kamsg;
78 MockitoAnnotations.initMocks(this);
79 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(
81 this.serverSession = new DefaultPCEPSessionNegotiator(new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.clientListener, new SimpleSessionListener(), (short) 1, 20, localPrefs);
82 final ChannelFuture future = new DefaultChannelPromise(this.clientListener);
83 doAnswer(new Answer<Object>() {
85 public Object answer(final InvocationOnMock invocation) {
86 final Object[] args = invocation.getArguments();
87 FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
90 }).when(this.clientListener).writeAndFlush(any(Notification.class));
91 doReturn("TestingChannel").when(this.clientListener).toString();
92 doReturn(this.pipeline).when(this.clientListener).pipeline();
93 doReturn(this.address).when(this.clientListener).localAddress();
94 doReturn(this.address).when(this.clientListener).remoteAddress();
95 doReturn(this.eventLoop).when(this.clientListener).eventLoop();
96 doReturn(true).when(this.future).cancel(false);
97 doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
98 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
99 doReturn(true).when(this.clientListener).isActive();
100 doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
101 doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.clientListener).remoteAddress();
102 doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.clientListener).localAddress();
103 this.openmsg = new OpenBuilder().setOpenMessage(
104 new OpenMessageBuilder().setOpen(
105 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
106 (short) 45).setKeepalive((short) 15).build()).build()).build();
107 this.kamsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
111 * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
112 * establish pce session for the 2nd time.
117 public void testSessionCharsAccBoth() throws Exception {
118 this.serverSession.channelActive(null);
119 assertEquals(1, this.receivedMsgs.size());
120 assertTrue(this.receivedMsgs.get(0) instanceof Open);
121 this.serverSession.handleMessage(this.openmsg);
122 assertEquals(2, this.receivedMsgs.size());
123 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
124 this.serverSession.handleMessage(this.kamsg);
125 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
129 * Mock PCE does not accept session characteristics the first time.
134 public void testSessionCharsAccMe() throws Exception {
135 this.serverSession.channelActive(null);
136 assertEquals(1, this.receivedMsgs.size());
137 assertTrue(this.receivedMsgs.get(0) instanceof Open);
138 final Open remote = (Open) this.receivedMsgs.get(0);
139 this.serverSession.handleMessage(this.openmsg);
140 assertEquals(2, this.receivedMsgs.size());
141 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
142 this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, remote.getOpenMessage().getOpen()));
143 assertEquals(3, this.receivedMsgs.size());
144 assertTrue(this.receivedMsgs.get(2) instanceof Open);
145 this.serverSession.handleMessage(this.kamsg);
146 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
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(client, 5, this.clientListener, this.openmsg.getOpenMessage().getOpen(), this.openmsg.getOpenMessage().getOpen());
218 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
219 assertEquals(1, s.getUnknownMessagesTimes().size());
221 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222 assertEquals(2, s.getUnknownMessagesTimes().size());
224 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
225 assertEquals(3, s.getUnknownMessagesTimes().size());
227 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228 assertEquals(4, s.getUnknownMessagesTimes().size());
230 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
231 assertEquals(3, s.getUnknownMessagesTimes().size());
233 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
234 assertEquals(3, s.getUnknownMessagesTimes().size());
236 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
237 assertEquals(4, s.getUnknownMessagesTimes().size());
239 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
240 assertEquals(5, s.getUnknownMessagesTimes().size());
242 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
243 synchronized (client) {
248 assertTrue(!client.up);
252 public void tearDown() {