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;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelFuture;
21 import io.netty.channel.ChannelHandler;
22 import io.netty.channel.ChannelPipeline;
23 import io.netty.channel.DefaultChannelPromise;
24 import io.netty.util.HashedWheelTimer;
25 import io.netty.util.concurrent.DefaultPromise;
26 import io.netty.util.concurrent.GlobalEventExecutor;
28 import java.net.InetSocketAddress;
29 import java.net.SocketAddress;
30 import java.util.List;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.mockito.invocation.InvocationOnMock;
39 import org.mockito.stubbing.Answer;
40 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
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.yangtools.yang.binding.Notification;
52 public class FiniteStateMachineTest {
54 private DefaultPCEPSessionNegotiator serverSession;
57 private Channel clientListener;
60 private ChannelPipeline pipeline;
63 private SocketAddress address;
65 private final List<Notification> receivedMsgs = Lists.newArrayList();
69 private Keepalive kamsg;
73 MockitoAnnotations.initMocks(this);
74 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(
76 this.serverSession = new DefaultPCEPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.clientListener, new SimpleSessionListener(), (short) 1, 20, localPrefs);
77 final ChannelFuture future = new DefaultChannelPromise(this.clientListener);
78 doAnswer(new Answer<Object>() {
80 public Object answer(final InvocationOnMock invocation) {
81 final Object[] args = invocation.getArguments();
82 FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
85 }).when(this.clientListener).writeAndFlush(any(Notification.class));
86 doReturn("TestingChannel").when(this.clientListener).toString();
87 doReturn(this.pipeline).when(this.clientListener).pipeline();
88 doReturn(this.address).when(this.clientListener).localAddress();
89 doReturn(this.address).when(this.clientListener).remoteAddress();
90 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
91 doReturn(true).when(this.clientListener).isActive();
92 doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
93 doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.clientListener).remoteAddress();
94 doReturn(InetSocketAddress.createUnresolved("127.0.0.1", 4189)).when(this.clientListener).localAddress();
95 this.openmsg = new OpenBuilder().setOpenMessage(
96 new OpenMessageBuilder().setOpen(
97 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
98 (short) 45).setKeepalive((short) 15).build()).build()).build();
99 this.kamsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
103 * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
104 * establish pce session for the 2nd time.
109 public void testSessionCharsAccBoth() throws Exception {
110 this.serverSession.channelActive(null);
111 assertEquals(1, this.receivedMsgs.size());
112 assertTrue(this.receivedMsgs.get(0) instanceof Open);
113 this.serverSession.handleMessage(this.openmsg);
114 assertEquals(2, this.receivedMsgs.size());
115 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
116 this.serverSession.handleMessage(this.kamsg);
117 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
121 * Mock PCE does not accept session characteristics the first time.
126 public void testSessionCharsAccMe() throws Exception {
127 this.serverSession.channelActive(null);
128 assertEquals(1, this.receivedMsgs.size());
129 assertTrue(this.receivedMsgs.get(0) instanceof Open);
130 Open remote = (Open) this.receivedMsgs.get(0);
131 this.serverSession.handleMessage(this.openmsg);
132 assertEquals(2, this.receivedMsgs.size());
133 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
134 this.serverSession.handleMessage(Util.createErrorMessage(PCEPErrors.NON_ACC_NEG_SESSION_CHAR, remote.getOpenMessage().getOpen()));
135 assertEquals(3, this.receivedMsgs.size());
136 assertTrue(this.receivedMsgs.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.
147 public void testErrorOneOne() throws Exception {
148 this.serverSession.channelActive(null);
149 assertEquals(1, this.receivedMsgs.size());
150 assertTrue(this.receivedMsgs.get(0) instanceof Open);
151 this.serverSession.handleMessage(this.kamsg);
152 for (final Notification m : this.receivedMsgs) {
153 if (m instanceof Pcerr) {
154 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
155 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
156 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
162 * KeepWaitTimer expired.
167 public void testErrorOneSeven() throws Exception {
168 this.serverSession.channelActive(null);
169 assertEquals(1, this.receivedMsgs.size());
170 assertTrue(this.receivedMsgs.get(0) instanceof Open);
171 this.serverSession.handleMessage(this.openmsg);
173 for (final Notification m : this.receivedMsgs) {
174 if (m instanceof Pcerr) {
175 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
176 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
177 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
182 /************* Tests commented because of their long duration (tested timers) **************/
185 * OpenWait timer expired.
187 * @throws InterruptedException
191 public void testErrorOneTwo() throws InterruptedException {
192 this.serverSession.channelActive(null);
193 assertEquals(1, this.receivedMsgs.size());
194 assertTrue(this.receivedMsgs.get(0) instanceof OpenMessage);
195 Thread.sleep(60 * 1000);
196 for (final Notification m : this.receivedMsgs) {
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 InterruptedException {
208 final SimpleSessionListener client = new SimpleSessionListener();
209 final PCEPSessionImpl s = new PCEPSessionImpl(new HashedWheelTimer(), client, 5, this.clientListener, this.openmsg.getOpenMessage().getOpen(), this.openmsg.getOpenMessage().getOpen());
210 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
211 assertEquals(1, s.unknownMessagesTimes.size());
213 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
214 assertEquals(2, s.unknownMessagesTimes.size());
216 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
217 assertEquals(3, s.unknownMessagesTimes.size());
219 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220 assertEquals(4, s.unknownMessagesTimes.size());
222 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
223 assertEquals(3, s.unknownMessagesTimes.size());
225 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226 assertEquals(3, s.unknownMessagesTimes.size());
228 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
229 assertEquals(4, s.unknownMessagesTimes.size());
231 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232 assertEquals(5, s.unknownMessagesTimes.size());
234 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
235 synchronized (client) {
240 assertTrue(!client.up);
244 public void tearDown() {