Bug-2064: Implementation of RFC5492 Error handling.
[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.rev131125.LinkstateAddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.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.MultiprotocolCaseBuilder;
62 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;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
65 import org.opendaylight.yangtools.yang.binding.Notification;
66
67 public class FSMTest {
68
69
70     @Mock
71     private EventLoop eventLoop;
72
73     private BGPClientSessionNegotiator clientSession;
74
75     @Mock
76     private Channel speakerListener;
77
78     @Mock
79     private ChannelPipeline pipeline;
80
81     private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
82
83     private final BgpTableType linkstatett = new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
84
85     private final List<Notification> receivedMsgs = Lists.newArrayList();
86
87     private Open classicOpen;
88
89     @Before
90     public void setUp() throws UnknownHostException {
91         MockitoAnnotations.initMocks(this);
92         final List<BgpParameters> tlvs = Lists.newArrayList();
93         final List<OptionalCapabilities> capas = Lists.newArrayList();
94
95         capas.add(new OptionalCapabilitiesBuilder().setCParameters(
96             new MultiprotocolCaseBuilder().setMultiprotocolCapability(
97                 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build());
98         capas.add(new OptionalCapabilitiesBuilder().setCParameters(
99             new MultiprotocolCaseBuilder().setMultiprotocolCapability(
100                 new MultiprotocolCapabilityBuilder().setAfi(this.linkstatett.getAfi()).setSafi(this.linkstatett.getSafi()).build()).build()).build());
101         capas.add(new OptionalCapabilitiesBuilder().setCParameters(new As4BytesCaseBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(
102             new AsNumber(30L)).build()).build()).build());
103         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
104         final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber(30L), (short) 3, new Ipv4Address("1.1.1.1"), tlvs);
105
106         final ChannelFuture f = mock(ChannelFuture.class);
107         doReturn(null).when(f).addListener(any(GenericFutureListener.class));
108
109         final InetAddress peerAddress = InetAddress.getByName("1.1.1.2");
110         final BGPPeerRegistry peerRegistry = new StrictBGPPeerRegistry();
111         peerRegistry.addPeer(new IpAddress(new Ipv4Address(peerAddress.getHostAddress())), new SimpleSessionListener(), prefs);
112
113         this.clientSession = new BGPClientSessionNegotiator(new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, peerRegistry, new BGPClientSessionValidator(new AsNumber(30L)));
114         doAnswer(new Answer<Object>() {
115             @Override
116             public Object answer(final InvocationOnMock invocation) {
117                 final Object[] args = invocation.getArguments();
118                 FSMTest.this.receivedMsgs.add((Notification) args[0]);
119                 return f;
120             }
121         }).when(this.speakerListener).writeAndFlush(any(Notification.class));
122         doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
123         doReturn(null).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
124         doReturn("TestingChannel").when(this.speakerListener).toString();
125         doReturn(new InetSocketAddress(peerAddress, 179)).when(this.speakerListener).remoteAddress();
126         doReturn(new InetSocketAddress(peerAddress, 179)).when(this.speakerListener).localAddress();
127         doReturn(this.pipeline).when(this.speakerListener).pipeline();
128         doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
129         doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
130         this.classicOpen = new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(
131             tlvs).setBgpIdentifier(new Ipv4Address("1.1.1.2")).build();
132     }
133
134     @Test
135     public void testDenyPeer() {
136         this.clientSession = new BGPClientSessionNegotiator(new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE), this.speakerListener, new StrictBGPPeerRegistry(), new BGPClientSessionValidator(new AsNumber(30L)));
137         this.clientSession.channelActive(null);
138         assertEquals(1, this.receivedMsgs.size());
139         assertTrue(this.receivedMsgs.get(0) instanceof Notify);
140     }
141
142     @Test
143     public void testAccSessionChar() throws InterruptedException {
144         this.clientSession.channelActive(null);
145         assertEquals(1, this.receivedMsgs.size());
146         assertTrue(this.receivedMsgs.get(0) instanceof Open);
147         this.clientSession.handleMessage(this.classicOpen);
148         assertEquals(2, this.receivedMsgs.size());
149         assertTrue(this.receivedMsgs.get(1) instanceof Keepalive);
150         this.clientSession.handleMessage(new KeepaliveBuilder().build());
151         assertEquals(this.clientSession.getState(), BGPClientSessionNegotiator.State.Finished);
152         Thread.sleep(1000);
153         Thread.sleep(100);
154     }
155
156     @Test
157     public void testNotAccChars() throws InterruptedException {
158         this.clientSession.channelActive(null);
159         assertEquals(1, this.receivedMsgs.size());
160         assertTrue(this.receivedMsgs.get(0) instanceof Open);
161         this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).build());
162         assertEquals(2, this.receivedMsgs.size());
163         assertTrue(this.receivedMsgs.get(1) instanceof Notify);
164         final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
165         assertEquals(BGPError.UNSPECIFIC_OPEN_ERROR, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
166     }
167
168     @Test
169     public void testNoAs4BytesCapability() {
170         this.clientSession.channelActive(null);
171         assertEquals(1, this.receivedMsgs.size());
172         assertTrue(this.receivedMsgs.get(0) instanceof Open);
173
174         final List<BgpParameters> tlvs = Lists.newArrayList();
175         final List<OptionalCapabilities> capas = Lists.newArrayList();
176         capas.add(new OptionalCapabilitiesBuilder().setCParameters(
177             new MultiprotocolCaseBuilder().setMultiprotocolCapability(
178                 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build());
179         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
180         // Open Message without advertised four-octet AS Number capability
181         this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(1).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(tlvs).build());
182         assertEquals(2, this.receivedMsgs.size());
183         assertTrue(this.receivedMsgs.get(1) instanceof Notify);
184         final Notification m = this.receivedMsgs.get(this.receivedMsgs.size() - 1);
185         assertEquals(BGPError.UNSUPPORTED_CAPABILITY, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
186         assertNotNull(((Notify) m).getData());
187     }
188
189     @Test
190     public void sendNotification() {
191         this.clientSession.channelActive(null);
192         this.clientSession.handleMessage(this.classicOpen);
193         this.clientSession.handleMessage(new KeepaliveBuilder().build());
194         assertEquals(this.clientSession.getState(), BGPClientSessionNegotiator.State.Finished);
195         this.clientSession.handleMessage(new OpenBuilder().setMyAsNumber(30).setHoldTimer(3).setVersion(new ProtocolVersion((short) 4)).build());
196         assertEquals(3, this.receivedMsgs.size());
197         assertTrue(this.receivedMsgs.get(2) instanceof Notify);
198         final Notification m = this.receivedMsgs.get(2);
199         assertEquals(BGPError.FSM_ERROR.getCode(), ((Notify) m).getErrorCode().shortValue());
200         assertEquals(BGPError.FSM_ERROR.getSubcode(), ((Notify) m).getErrorSubcode().shortValue());
201     }
202
203     @After
204     public void tearDown() {
205
206     }
207 }