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