9ecbc613f01e79b3d8bf822ecbeb93864e2d4b3e
[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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
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
18 import com.google.common.collect.Lists;
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.channel.EventLoop;
24 import io.netty.util.concurrent.DefaultPromise;
25 import io.netty.util.concurrent.GenericFutureListener;
26 import io.netty.util.concurrent.GlobalEventExecutor;
27 import java.net.InetAddress;
28 import java.net.InetSocketAddress;
29 import java.net.UnknownHostException;
30 import java.util.List;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.invocation.InvocationOnMock;
38 import org.mockito.stubbing.Answer;
39 import org.opendaylight.protocol.bgp.parser.BGPError;
40 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Keepalive;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilitiesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.as4.bytes._case.As4BytesCapabilityBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.GracefulRestartCaseBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.graceful.restart._case.GracefulRestartCapabilityBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
67 import org.opendaylight.yangtools.yang.binding.Notification;
68
69 public class FSMTest {
70
71
72     @Mock
73     private EventLoop eventLoop;
74
75     private BGPClientSessionNegotiator clientSession;
76
77     @Mock
78     private Channel speakerListener;
79
80     @Mock
81     private ChannelPipeline pipeline;
82
83     private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
84
85     private final BgpTableType linkstatett = new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
86
87     private final List<Notification> receivedMsgs = Lists.newArrayList();
88
89     private Open classicOpen;
90
91     @Before
92     public void setUp() throws UnknownHostException {
93         MockitoAnnotations.initMocks(this);
94         final List<BgpParameters> tlvs = Lists.newArrayList();
95         final List<OptionalCapabilities> capas = Lists.newArrayList();
96
97         capas.add(new OptionalCapabilitiesBuilder().setCParameters(
98             new MultiprotocolCaseBuilder().setMultiprotocolCapability(
99                 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build());
100         capas.add(new OptionalCapabilitiesBuilder().setCParameters(
101             new MultiprotocolCaseBuilder().setMultiprotocolCapability(
102                 new MultiprotocolCapabilityBuilder().setAfi(this.linkstatett.getAfi()).setSafi(this.linkstatett.getSafi()).build()).build()).build());
103         capas.add(new OptionalCapabilitiesBuilder().setCParameters(new As4BytesCaseBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(
104             new AsNumber(30L)).build()).build()).build());
105         capas.add(new OptionalCapabilitiesBuilder().setCParameters(
106             new GracefulRestartCaseBuilder().setGracefulRestartCapability(
107                 new GracefulRestartCapabilityBuilder().build()).build()).build());
108         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
109         final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber(30L), (short) 3, new Ipv4Address("1.1.1.1"), tlvs);
110
111         final ChannelFuture f = mock(ChannelFuture.class);
112         doReturn(null).when(f).addListener(any(GenericFutureListener.class));
113
114         final InetAddress peerAddress = InetAddress.getByName("1.1.1.2");
115         final BGPPeerRegistry peerRegistry = new StrictBGPPeerRegistry();
116         peerRegistry.addPeer(new IpAddress(new Ipv4Address(peerAddress.getHostAddress())), new SimpleSessionListener(), prefs);
117
118         this.clientSession = new BGPClientSessionNegotiator(new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, peerRegistry, new BGPClientSessionValidator(new AsNumber(30L)));
119         doAnswer(new Answer<Object>() {
120             @Override
121             public Object answer(final InvocationOnMock invocation) {
122                 final Object[] args = invocation.getArguments();
123                 FSMTest.this.receivedMsgs.add((Notification) args[0]);
124                 return f;
125             }
126         }).when(this.speakerListener).writeAndFlush(any(Notification.class));
127         doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
128         doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
129         doReturn("TestingChannel").when(this.speakerListener).toString();
130         doReturn(new InetSocketAddress(peerAddress, 179)).when(this.speakerListener).remoteAddress();
131         doReturn(new InetSocketAddress(peerAddress, 179)).when(this.speakerListener).localAddress();
132         doReturn(this.pipeline).when(this.speakerListener).pipeline();
133         doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
134         doReturn(this.pipeline).when(this.pipeline).addLast(any(ChannelHandler.class));
135         doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
136         this.classicOpen = new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(
137             tlvs).setBgpIdentifier(new Ipv4Address("1.1.1.2")).build();
138     }
139
140     @Test
141     public void testDenyPeer() {
142         this.clientSession = new BGPClientSessionNegotiator(new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, new StrictBGPPeerRegistry(), new BGPClientSessionValidator(new AsNumber(30L)));
143         this.clientSession.channelActive(null);
144         assertEquals(1, this.receivedMsgs.size());
145         assertTrue(this.receivedMsgs.get(0) instanceof Notify);
146     }
147
148     @Test
149     public void testAccSessionChar() throws InterruptedException {
150         this.clientSession.channelActive(null);
151         assertEquals(1, this.receivedMsgs.size());
152         assertTrue(this.receivedMsgs.get(0) instanceof Open);
153         this.clientSession.handleMessage(this.classicOpen);
154         assertEquals(2, this.receivedMsgs.size());
155         assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
156         this.clientSession.handleMessage(new KeepaliveBuilder().build());
157         assertEquals(this.clientSession.getState(), BGPClientSessionNegotiator.State.FINISHED);
158         Thread.sleep(1000);
159         Thread.sleep(100);
160     }
161
162     @Test
163     public void testNotAccChars() throws InterruptedException {
164         this.clientSession.channelActive(null);
165         assertEquals(1, this.receivedMsgs.size());
166         assertTrue(this.receivedMsgs.get(0) instanceof Open);
167         this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).build());
168         assertEquals(2, this.receivedMsgs.size());
169         assertTrue(this.receivedMsgs.get(1) instanceof Notify);
170         final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
171         assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
172     }
173
174     @Test
175     public void testNoAs4BytesCapability() {
176         this.clientSession.channelActive(null);
177         assertEquals(1, this.receivedMsgs.size());
178         assertTrue(this.receivedMsgs.get(0) instanceof Open);
179
180         final List<BgpParameters> tlvs = Lists.newArrayList();
181         final List<OptionalCapabilities> capas = Lists.newArrayList();
182         capas.add(new OptionalCapabilitiesBuilder().setCParameters(
183             new MultiprotocolCaseBuilder().setMultiprotocolCapability(
184                 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build());
185         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
186         // Open Message without advertised four-octet AS Number capability
187         this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(tlvs).build());
188         assertEquals(2, this.receivedMsgs.size());
189         assertTrue(this.receivedMsgs.get(1) instanceof Notify);
190         final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
191         assertEquals(BGPError.UNSUPPORTED_CAPABILITY, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
192         assertNotNull(((Notify) m).getData());
193     }
194
195     @Test
196     public void sendNotification() {
197         this.clientSession.channelActive(null);
198         this.clientSession.handleMessage(this.classicOpen);
199         this.clientSession.handleMessage(new KeepaliveBuilder().build());
200         assertEquals(this.clientSession.getState(), BGPClientSessionNegotiator.State.FINISHED);
201         this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).build());
202         assertEquals(3, this.receivedMsgs.size());
203         assertTrue(this.receivedMsgs.get(2) instanceof Notify);
204         final Notification m = this.receivedMsgs.get(2);
205         assertEquals(BGPError.FSM_ERROR.getCode(), ((Notify) m).getErrorCode().shortValue());
206         assertEquals(BGPError.FSM_ERROR.getSubcode(), ((Notify) m).getErrorSubcode().shortValue());
207     }
208
209     @Test
210     public void sameBGPIDs() {
211         this.clientSession.channelActive(null);
212         assertEquals(1, this.receivedMsgs.size());
213         assertTrue(this.receivedMsgs.get(0) instanceof Open);
214
215         this.clientSession.handleMessage(new OpenBuilder(this.classicOpen).setBgpIdentifier(new Ipv4Address("1.1.1.1")).build());
216         assertEquals(2, this.receivedMsgs.size());
217         assertTrue(this.receivedMsgs.get(1) instanceof Notify);
218         final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
219         assertEquals(BGPError.BAD_BGP_ID, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
220     }
221
222     @After
223     public void tearDown() {
224
225     }
226 }