Bug-731: Fixed few major Sonar warnings
[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
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;
50
51 public class FiniteStateMachineTest {
52
53     private DefaultPCEPSessionNegotiator serverSession;
54
55     @Mock
56     private Channel clientListener;
57
58     @Mock
59     private EventLoop eventLoop;
60
61     @Mock
62     private ScheduledFuture<?> future;
63
64     @Mock
65     private ChannelPipeline pipeline;
66
67     @Mock
68     private SocketAddress address;
69
70     private final List<Notification> receivedMsgs = Lists.newArrayList();
71
72     private Open openmsg;
73
74     private Keepalive kamsg;
75
76     @Before
77     public void setUp() {
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(
80                 (short) 1).build();
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>() {
84             @Override
85             public Object answer(final InvocationOnMock invocation) {
86                 final Object[] args = invocation.getArguments();
87                 FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
88                 return future;
89             }
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();
108     }
109
110     /**
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.
113      *
114      * @throws Exception
115      */
116     @Test
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);
126     }
127
128     /**
129      * Mock PCE does not accept session characteristics the first time.
130      *
131      * @throws Exception
132      */
133     @Test
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);
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(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());
220         Thread.sleep(10000);
221         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
222         assertEquals(2, s.getUnknownMessagesTimes().size());
223         Thread.sleep(10000);
224         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
225         assertEquals(3, s.getUnknownMessagesTimes().size());
226         Thread.sleep(20000);
227         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
228         assertEquals(4, s.getUnknownMessagesTimes().size());
229         Thread.sleep(30000);
230         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
231         assertEquals(3, s.getUnknownMessagesTimes().size());
232         Thread.sleep(10000);
233         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
234         assertEquals(3, s.getUnknownMessagesTimes().size());
235         Thread.sleep(5000);
236         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
237         assertEquals(4, s.getUnknownMessagesTimes().size());
238         Thread.sleep(1000);
239         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
240         assertEquals(5, s.getUnknownMessagesTimes().size());
241         Thread.sleep(1000);
242         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
243         synchronized (client) {
244             while (client.up) {
245                 client.wait();
246             }
247         }
248         assertTrue(!client.up);
249     }
250
251     @After
252     public void tearDown() {
253     }
254 }