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