3959c653bd6b39448705e57af4531275b785b4a2
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / FSMTest.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.bgp.rib.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelHandler;
20 import io.netty.channel.ChannelPipeline;
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.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.bgp.concepts.BGPAddressFamily;
36 import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
37 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
38 import org.opendaylight.protocol.bgp.parser.BGPError;
39 import org.opendaylight.protocol.bgp.parser.BGPMessage;
40 import org.opendaylight.protocol.bgp.parser.BGPParameter;
41 import org.opendaylight.protocol.bgp.parser.message.BGPKeepAliveMessage;
42 import org.opendaylight.protocol.bgp.parser.message.BGPNotificationMessage;
43 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
44 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
45 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
46 import org.opendaylight.protocol.concepts.ASNumber;
47
48 import com.google.common.collect.Lists;
49
50 public class FSMTest {
51
52         private BGPSessionNegotiator clientSession;
53
54         @Mock
55         private Channel speakerListener;
56
57         @Mock
58         private ChannelPipeline pipeline;
59
60         private final BGPTableType ipv4tt = new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.Unicast);
61
62         private final BGPTableType linkstatett = new BGPTableType(BGPAddressFamily.LinkState, BGPSubsequentAddressFamily.Linkstate);
63
64         private final List<BGPMessage> receivedMsgs = Lists.newArrayList();
65
66         private BGPOpenMessage classicOpen;
67
68         @Before
69         public void setUp() {
70                 MockitoAnnotations.initMocks(this);
71                 final List<BGPParameter> tlvs = Lists.newArrayList();
72                 tlvs.add(new MultiprotocolCapability(this.ipv4tt));
73                 tlvs.add(new MultiprotocolCapability(this.linkstatett));
74                 final BGPSessionPreferences prefs = new BGPSessionPreferences(new ASNumber(30), (short) 3, null, tlvs);
75                 this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener());
76                 doAnswer(new Answer<Object>() {
77                         @Override
78                         public Object answer(final InvocationOnMock invocation) {
79                                 final Object[] args = invocation.getArguments();
80                                 FSMTest.this.receivedMsgs.add((BGPMessage) args[0]);
81                                 return null;
82                         }
83                 }).when(this.speakerListener).writeAndFlush(any(BGPMessage.class));
84                 doReturn("TestingChannel").when(this.speakerListener).toString();
85                 doReturn(this.pipeline).when(this.speakerListener).pipeline();
86                 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
87                 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
88                 this.classicOpen = new BGPOpenMessage(new ASNumber(30), (short) 3, null, tlvs);
89         }
90
91         @Test
92         public void testAccSessionChar() throws InterruptedException {
93                 this.clientSession.channelActive(null);
94                 assertEquals(1, this.receivedMsgs.size());
95                 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
96                 this.clientSession.handleMessage(this.classicOpen);
97                 assertEquals(2, this.receivedMsgs.size());
98                 assertTrue(this.receivedMsgs.get(1) instanceof BGPKeepAliveMessage);
99                 this.clientSession.handleMessage(new BGPKeepAliveMessage());
100                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
101                 // Thread.sleep(3 * 1000);
102                 // Thread.sleep(100);
103                 // assertEquals(3, this.receivedMsgs.size());
104                 // assertTrue(this.receivedMsgs.get(2) instanceof BGPKeepAliveMessage); // test of keepalive timer
105                 // this.clientSession.handleMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
106                 // assertEquals(4, this.receivedMsgs.size());
107                 // assertTrue(this.receivedMsgs.get(3) instanceof BGPNotificationMessage);
108                 // final BGPMessage m = this.clientListener.getListMsg().get(3);
109                 // assertEquals(BGPError.FSM_ERROR, ((BGPNotificationMessage) m).getError());
110         }
111
112         @Test
113         public void testNotAccChars() throws InterruptedException {
114                 this.clientSession.channelActive(null);
115                 assertEquals(1, this.receivedMsgs.size());
116                 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
117                 this.clientSession.handleMessage(new BGPOpenMessage(new ASNumber(30), (short) 1, null, null));
118                 assertEquals(2, this.receivedMsgs.size());
119                 assertTrue(this.receivedMsgs.get(1) instanceof BGPNotificationMessage);
120                 final BGPMessage m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
121                 assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, ((BGPNotificationMessage) m).getError());
122         }
123
124         @Test
125         @Ignore
126         // long duration
127         public void testNoOpen() throws InterruptedException {
128                 this.clientSession.channelActive(null);
129                 assertEquals(1, this.receivedMsgs.size());
130                 assertTrue(this.receivedMsgs.get(0) instanceof BGPOpenMessage);
131                 Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
132                 Thread.sleep(100);
133                 final BGPMessage m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
134                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, ((BGPNotificationMessage) m).getError());
135         }
136
137         @Test
138         public void sendNotification() {
139                 this.clientSession.channelActive(null);
140                 this.clientSession.handleMessage(this.classicOpen);
141                 this.clientSession.handleMessage(new BGPKeepAliveMessage());
142                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
143                 try {
144                         this.clientSession.handleMessage(new BGPOpenMessage(new ASNumber(30), (short) 3, null, null));
145                         fail("Exception should be thrown.");
146                 } catch (final IllegalStateException e) {
147                         assertEquals("Unexpected state Finished", e.getMessage());
148                 }
149         }
150
151         @After
152         public void tearDown() {
153
154         }
155 }