BUG-1199 : fixed incorrect parsing of Error message.
[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
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;
27
28 import java.net.InetSocketAddress;
29 import java.net.SocketAddress;
30 import java.util.List;
31
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;
51
52 public class FiniteStateMachineTest {
53
54     private DefaultPCEPSessionNegotiator serverSession;
55
56     @Mock
57     private Channel clientListener;
58
59     @Mock
60     private ChannelPipeline pipeline;
61
62     @Mock
63     private SocketAddress address;
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         final ChannelFuture future = new DefaultChannelPromise(this.clientListener);
78         doAnswer(new Answer<Object>() {
79             @Override
80             public Object answer(final InvocationOnMock invocation) {
81                 final Object[] args = invocation.getArguments();
82                 FiniteStateMachineTest.this.receivedMsgs.add((Notification) args[0]);
83                 return future;
84             }
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();
100     }
101
102     /**
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.
105      *
106      * @throws Exception
107      */
108     @Test
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);
118     }
119
120     /**
121      * Mock PCE does not accept session characteristics the first time.
122      *
123      * @throws Exception
124      */
125     @Test
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);
139     }
140
141     /**
142      * Sending different PCEP Message than Open in session establishment phase.
143      *
144      * @throws Exception
145      */
146     @Test
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());
157             }
158         }
159     }
160
161     /**
162      * KeepWaitTimer expired.
163      *
164      * @throws Exception
165      */
166     @Test
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);
172         Thread.sleep(1000);
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());
178             }
179         }
180     }
181
182     /************* Tests commented because of their long duration (tested timers) **************/
183
184     /**
185      * OpenWait timer expired.
186      *
187      * @throws InterruptedException
188      */
189     @Test
190     @Ignore
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());
201             }
202         }
203     }
204
205     @Test
206     @Ignore
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());
212         Thread.sleep(10000);
213         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
214         assertEquals(2, s.unknownMessagesTimes.size());
215         Thread.sleep(10000);
216         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
217         assertEquals(3, s.unknownMessagesTimes.size());
218         Thread.sleep(20000);
219         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
220         assertEquals(4, s.unknownMessagesTimes.size());
221         Thread.sleep(30000);
222         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
223         assertEquals(3, s.unknownMessagesTimes.size());
224         Thread.sleep(10000);
225         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
226         assertEquals(3, s.unknownMessagesTimes.size());
227         Thread.sleep(5000);
228         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
229         assertEquals(4, s.unknownMessagesTimes.size());
230         Thread.sleep(1000);
231         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
232         assertEquals(5, s.unknownMessagesTimes.size());
233         Thread.sleep(1000);
234         s.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
235         synchronized (client) {
236             while (client.up) {
237                 client.wait();
238             }
239         }
240         assertTrue(!client.up);
241     }
242
243     @After
244     public void tearDown() {
245     }
246 }