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