0825350c73819b9c77c222fa628a3125f0567d43
[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.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.util.HashedWheelTimer;
21 import io.netty.util.concurrent.DefaultPromise;
22 import io.netty.util.concurrent.GlobalEventExecutor;
23
24 import java.util.List;
25
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.opendaylight.protocol.bgp.parser.BGPError;
35 import org.opendaylight.protocol.bgp.parser.BGPTableType;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Keepalive;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.KeepaliveBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Notify;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Open;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.OpenBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.ProtocolVersion;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParametersBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.CMultiprotocolBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.open.bgp.parameters.c.parameters.c.multiprotocol.MultiprotocolCapabilityBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
51 import org.opendaylight.yangtools.yang.binding.Notification;
52
53 import com.google.common.collect.Lists;
54
55 public class FSMTest {
56
57         private BGPSessionNegotiator clientSession;
58
59         @Mock
60         private Channel speakerListener;
61
62         @Mock
63         private ChannelPipeline pipeline;
64
65         private final BGPTableType ipv4tt = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
66
67         private final BGPTableType linkstatett = new BGPTableType(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
68
69         private final List<Notification> receivedMsgs = Lists.newArrayList();
70
71         private Open classicOpen;
72
73         @Before
74         public void setUp() {
75                 MockitoAnnotations.initMocks(this);
76                 final List<BgpParameters> tlvs = Lists.newArrayList();
77
78                 tlvs.add(new BgpParametersBuilder().setCParameters(
79                                 new CMultiprotocolBuilder().setMultiprotocolCapability(
80                                                 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAddressFamily()).setSafi(
81                                                                 this.ipv4tt.getSubsequentAddressFamily()).build()).build()).build());
82                 tlvs.add(new BgpParametersBuilder().setCParameters(
83                                 new CMultiprotocolBuilder().setMultiprotocolCapability(
84                                                 new MultiprotocolCapabilityBuilder().setAfi(this.linkstatett.getAddressFamily()).setSafi(
85                                                                 this.linkstatett.getSubsequentAddressFamily()).build()).build()).build());
86                 final BGPSessionPreferences prefs = new BGPSessionPreferences(30, (short) 3, null, tlvs);
87                 this.clientSession = new BGPSessionNegotiator(new HashedWheelTimer(), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, prefs, new SimpleSessionListener());
88                 doAnswer(new Answer<Object>() {
89                         @Override
90                         public Object answer(final InvocationOnMock invocation) {
91                                 final Object[] args = invocation.getArguments();
92                                 FSMTest.this.receivedMsgs.add((Notification) args[0]);
93                                 return null;
94                         }
95                 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
96                 doReturn("TestingChannel").when(this.speakerListener).toString();
97                 doReturn(this.pipeline).when(this.speakerListener).pipeline();
98                 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
99                 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
100                 this.classicOpen = new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(
101                                 tlvs).build();
102         }
103
104         @Test
105         public void testAccSessionChar() throws InterruptedException {
106                 this.clientSession.channelActive(null);
107                 assertEquals(1, this.receivedMsgs.size());
108                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
109                 this.clientSession.handleMessage(this.classicOpen);
110                 assertEquals(2, this.receivedMsgs.size());
111                 assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
112                 this.clientSession.handleMessage(new KeepaliveBuilder().build());
113                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
114                 Thread.sleep(1000);
115                 Thread.sleep(100);
116                 assertEquals(3, this.receivedMsgs.size());
117                 assertTrue(this.receivedMsgs.get(2) instanceof Keepalive); // test of keepalive timer
118         }
119
120         @Test
121         public void testNotAccChars() throws InterruptedException {
122                 this.clientSession.channelActive(null);
123                 assertEquals(1, this.receivedMsgs.size());
124                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
125                 this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).build());
126                 assertEquals(2, this.receivedMsgs.size());
127                 assertTrue(this.receivedMsgs.get(1) instanceof Notify);
128                 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
129                 assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
130         }
131
132         @Test
133         @Ignore
134         // long duration
135         public void testNoOpen() throws InterruptedException {
136                 this.clientSession.channelActive(null);
137                 assertEquals(1, this.receivedMsgs.size());
138                 assertTrue(this.receivedMsgs.get(0) instanceof Open);
139                 Thread.sleep(BGPSessionNegotiator.INITIAL_HOLDTIMER * 1000 * 60);
140                 Thread.sleep(100);
141                 final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
142                 assertEquals(BGPError.HOLD_TIMER_EXPIRED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
143         }
144
145         @Test
146         public void sendNotification() {
147                 this.clientSession.channelActive(null);
148                 this.clientSession.handleMessage(this.classicOpen);
149                 this.clientSession.handleMessage(new KeepaliveBuilder().build());
150                 assertEquals(this.clientSession.getState(), BGPSessionNegotiator.State.Finished);
151                 this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).build());
152                 assertEquals(3, this.receivedMsgs.size());
153                 assertTrue(this.receivedMsgs.get(2) instanceof Notify);
154                 final Notification m = this.receivedMsgs.get(2);
155                 assertEquals(BGPError.FSM_ERROR.getCode(), ((Notify) m).getErrorCode().shortValue());
156                 assertEquals(BGPError.FSM_ERROR.getSubcode(), ((Notify) m).getErrorSubcode().shortValue());
157         }
158
159         @After
160         public void tearDown() {
161
162         }
163 }