BUG-76 : fixed all FSM tests.
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / FiniteStateMachineTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
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;
24
25 import java.util.Arrays;
26 import java.util.List;
27
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;
53
54 import com.google.common.collect.Lists;
55
56 public class FiniteStateMachineTest {
57
58         private DefaultPCEPSessionNegotiator serverSession;
59
60         @Mock
61         private Channel clientListener;
62
63         @Mock
64         private ChannelPipeline pipeline;
65
66         private final List<Notification> receivedMsgs = Lists.newArrayList();
67
68         private Open openmsg;
69
70         private Keepalive kamsg;
71
72         @Before
73         public void setUp() {
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(
76                                 (short) 1).build();
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>() {
80                         @Override
81                         public Object answer(final InvocationOnMock invocation) {
82                                 final Object[] args = invocation.getArguments();
83                                 FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
84                                 return future;
85                         }
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();
97         }
98
99         /**
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.
102          * 
103          * @throws Exception
104          */
105         @Test
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);
115         }
116
117         /**
118          * Mock PCE does not accept session characteristics the first time.
119          * 
120          * @throws Exception
121          */
122         @Test
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);
135         }
136
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();
147         }
148
149         /**
150          * Sending different PCEP Message than Open in session establishment phase.
151          * 
152          * @throws Exception
153          */
154         @Test
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());
165                         }
166                 }
167         }
168
169         /**
170          * KeepWaitTimer expired.
171          * 
172          * @throws Exception
173          */
174         @Test
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);
180                 Thread.sleep(1000);
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());
186                         }
187                 }
188         }
189
190         /************* Tests commented because of their long duration (tested timers) **************/
191
192         /**
193          * OpenWait timer expired.
194          * 
195          * @throws InterruptedException
196          */
197         @Test
198         @Ignore
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());
209                         }
210                 }
211         }
212
213         @Test
214         @Ignore
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());
220                 Thread.sleep(10000);
221                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222                 assertEquals(2, s.unknownMessagesTimes.size());
223                 Thread.sleep(10000);
224                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
225                 assertEquals(3, s.unknownMessagesTimes.size());
226                 Thread.sleep(20000);
227                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228                 assertEquals(4, s.unknownMessagesTimes.size());
229                 Thread.sleep(30000);
230                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
231                 assertEquals(3, s.unknownMessagesTimes.size());
232                 Thread.sleep(10000);
233                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
234                 assertEquals(3, s.unknownMessagesTimes.size());
235                 Thread.sleep(5000);
236                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
237                 assertEquals(4, s.unknownMessagesTimes.size());
238                 Thread.sleep(1000);
239                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
240                 assertEquals(5, s.unknownMessagesTimes.size());
241                 Thread.sleep(1000);
242                 s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
243                 synchronized (client) {
244                         while (client.up) {
245                                 try {
246                                         client.wait();
247                                 } catch (final InterruptedException e) {
248                                         e.printStackTrace();
249                                 }
250                         }
251                 }
252                 assertTrue(!client.up);
253         }
254
255         @After
256         public void tearDown() {
257         }
258 }