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