56e77c36ce16fb545a7e36e023e72ad9c13c6783
[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.util.HashedWheelTimer;
21 import io.netty.util.concurrent.DefaultPromise;
22 import io.netty.util.concurrent.GlobalEventExecutor;
23
24 import java.util.Arrays;
25 import java.util.List;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.mockito.invocation.InvocationOnMock;
34 import org.mockito.stubbing.Answer;
35 import org.opendaylight.protocol.pcep.spi.PCEPErrorMapping;
36 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Keepalive;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.error.type.SessionBuilder;
51 import org.opendaylight.yangtools.yang.binding.Notification;
52
53 import com.google.common.collect.Lists;
54
55 public class FiniteStateMachineTest {
56
57         private DefaultPCEPSessionNegotiator serverSession;
58
59         @Mock
60         private Channel clientListener;
61
62         @Mock
63         private ChannelPipeline pipeline;
64
65         private final List<Notification> receivedMsgs = Lists.newArrayList();
66
67         private Open openmsg;
68
69         private Keepalive kamsg;
70
71         @Before
72         public void setUp() {
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(
75                                 (short) 1).build();
76                 this.serverSession = new DefaultPCEPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<PCEPSessionImpl>(GlobalEventExecutor.INSTANCE), this.clientListener, new SimpleSessionListener(), (short) 1, 20, localPrefs);
77                 doAnswer(new Answer<Object>() {
78                         @Override
79                         public Object answer(final InvocationOnMock invocation) {
80                                 final Object[] args = invocation.getArguments();
81                                 FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
82                                 return null;
83                         }
84                 }).when(this.clientListener).writeAndFlush(any(Notification.class));
85                 doReturn("TestingChannel").when(this.clientListener).toString();
86                 doReturn(this.pipeline).when(this.clientListener).pipeline();
87                 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
88                 doReturn(mock(ChannelFuture.class)).when(this.clientListener).close();
89                 this.openmsg = new OpenBuilder().setOpenMessage(
90                                 new OpenMessageBuilder().setOpen(
91                                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(
92                                                                 (short) 3).build()).build()).build();
93                 this.kamsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
94         }
95
96         /**
97          * Both PCEs accept session characteristics. Also tests KeepAliveTimer and error message and when pce attempts to
98          * establish pce session for the 2nd time.
99          * 
100          * @throws Exception
101          */
102         @Test
103         public void testSessionCharsAccBoth() throws Exception {
104                 this.serverSession.channelActive(null);
105                 assertEquals(1, this.receivedMsgs.size());
106                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
107                 this.serverSession.handleMessage(this.openmsg);
108                 assertEquals(2, this.receivedMsgs.size());
109                 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
110                 this.serverSession.handleMessage(this.kamsg);
111                 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
112         }
113
114         /**
115          * Mock PCE does not accept session characteristics the first time.
116          * 
117          * @throws Exception
118          */
119         @Test
120         public void testSessionCharsAccMe() throws Exception {
121                 this.serverSession.channelActive(null);
122                 assertEquals(1, this.receivedMsgs.size());
123                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
124                 this.serverSession.handleMessage(this.openmsg);
125                 assertEquals(2, this.receivedMsgs.size());
126                 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
127                 this.serverSession.handleMessage(createErrorMessageWOpen(PCEPErrors.NON_ACC_NEG_SESSION_CHAR));
128                 assertEquals(3, this.receivedMsgs.size());
129                 assertTrue(this.receivedMsgs.get(2) instanceof Open);
130                 this.serverSession.handleMessage(this.kamsg);
131                 assertEquals(this.serverSession.getState(), DefaultPCEPSessionNegotiator.State.Finished);
132         }
133
134         private Pcerr createErrorMessageWOpen(final PCEPErrors e) {
135                 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
136                 return new PcerrBuilder().setPcerrMessage(
137                                 new PcerrMessageBuilder().setErrorType(
138                                                 new SessionBuilder().setOpen(
139                                                                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setKeepalive(
140                                                                                 (short) 1).build()).build()).setErrors(
141                                                 Arrays.asList(new ErrorsBuilder().setErrorObject(
142                                                                 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e).type).setValue(
143                                                                                 maping.getFromErrorsEnum(e).value).build()).build())).build()).build();
144         }
145
146         /**
147          * Sending different PCEP Message than Open in session establishment phase.
148          * 
149          * @throws Exception
150          */
151         @Test
152         public void testErrorOneOne() throws Exception {
153                 this.serverSession.channelActive(null);
154                 assertEquals(1, this.receivedMsgs.size());
155                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
156                 this.serverSession.handleMessage(this.kamsg);
157                 for (final Notification m : this.receivedMsgs) {
158                         if (m instanceof Pcerr) {
159                                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
160                                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
161                                 assertEquals(new Short((short) 1), obj.getErrorObject().getValue());
162                         }
163                 }
164         }
165
166         /**
167          * KeepWaitTimer expired.
168          * 
169          * @throws Exception
170          */
171         @Test
172         public void testErrorOneSeven() throws Exception {
173                 this.serverSession.channelActive(null);
174                 assertEquals(1, this.receivedMsgs.size());
175                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
176                 this.serverSession.handleMessage(this.openmsg);
177                 Thread.sleep(1000);
178                 for (final Notification m : this.receivedMsgs) {
179                         if (m instanceof Pcerr) {
180                                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
181                                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
182                                 assertEquals(new Short((short) 7), obj.getErrorObject().getValue());
183                         }
184                 }
185         }
186
187         /************* Tests commented because of their long duration (tested timers) **************/
188
189         /**
190          * OpenWait timer expired.
191          * 
192          * @throws InterruptedException
193          */
194         @Test
195         @Ignore
196         public void testErrorOneTwo() throws InterruptedException {
197                 this.serverSession.channelActive(null);
198                 assertEquals(1, this.receivedMsgs.size());
199                 assertTrue(this.receivedMsgs.get(0) instanceof OpenMessage);
200                 Thread.sleep(60 * 1000);
201                 for (final Notification m : this.receivedMsgs) {
202                         if (m instanceof Pcerr) {
203                                 final Errors obj = ((Pcerr) m).getPcerrMessage().getErrors().get(0);
204                                 assertEquals(new Short((short) 1), obj.getErrorObject().getType());
205                                 assertEquals(new Short((short) 2), obj.getErrorObject().getValue());
206                         }
207                 }
208         }
209
210         @Test
211         @Ignore
212         public void testUnknownMessage() throws InterruptedException {
213                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
214                 // assertEquals(1, this.serverSession.unknownMessagesTimes.size());
215                 // Thread.sleep(10000);
216                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
217                 // assertEquals(2, this.serverSession.unknownMessagesTimes.size());
218                 // Thread.sleep(10000);
219                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220                 // assertEquals(3, this.serverSession.unknownMessagesTimes.size());
221                 // Thread.sleep(20000);
222                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
223                 // assertEquals(4, this.serverSession.unknownMessagesTimes.size());
224                 // Thread.sleep(30000);
225                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226                 // assertEquals(3, this.serverSession.unknownMessagesTimes.size());
227                 // Thread.sleep(10000);
228                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
229                 // assertEquals(3, this.serverSession.unknownMessagesTimes.size());
230                 // Thread.sleep(5000);
231                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232                 // assertEquals(4, this.serverSession.unknownMessagesTimes.size());
233                 // Thread.sleep(1000);
234                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
235                 // assertEquals(5, this.serverSession.unknownMessagesTimes.size());
236                 // Thread.sleep(1000);
237                 // this.serverSession.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
238                 // synchronized (this.client) {
239                 // while (!this.client.down) {
240                 // try {
241                 // this.client.wait();
242                 // } catch (final InterruptedException e) {
243                 // e.printStackTrace();
244                 // }
245                 // }
246                 // }
247                 // assertTrue(this.client.down);
248         }
249
250         @After
251         public void tearDown() {
252         }
253 }