0245ce31450156de3d34d8d8d4807cd5912d3305
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BGPSessionImplTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.bgp.rib.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkIdleState;
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.channel.embedded.EmbeddedChannel;
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 java.util.concurrent.TimeUnit;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Matchers;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
39 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
40 import org.opendaylight.protocol.bgp.parser.BGPError;
41 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
42 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
43 import org.opendaylight.protocol.bgp.rib.spi.State;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.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.CParameters1;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.GracefulRestartCapabilityBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.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 BGPSessionImplTest {
70
71     private static final int HOLD_TIMER = 3;
72     private static final AsNumber AS_NUMBER = new AsNumber(30L);
73     private static final Ipv4Address BGP_ID = new Ipv4Address("1.1.1.2");
74     private static final String LOCAL_IP = "1.1.1.4";
75     private static final int LOCAL_PORT = 12345;
76
77     @Mock
78     private EventLoop eventLoop;
79
80     @Mock
81     private Channel speakerListener;
82
83     @Mock
84     private ChannelPipeline pipeline;
85
86     private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
87
88     private final List<Notification> receivedMsgs = Lists.newArrayList();
89
90     private Open classicOpen;
91
92     private BGPSessionImpl bgpSession;
93
94     private SimpleSessionListener listener;
95
96     @Before
97     public void setUp() throws UnknownHostException {
98         new EmbeddedChannel();
99         MockitoAnnotations.initMocks(this);
100         final List<BgpParameters> tlvs = Lists.newArrayList();
101         this.classicOpen = new OpenBuilder().setMyAsNumber(AS_NUMBER.getValue().intValue()).setHoldTimer(HOLD_TIMER)
102                 .setVersion(new ProtocolVersion((short) 4)).setBgpParameters(tlvs).setBgpIdentifier(BGP_ID).build();
103
104         final List<OptionalCapabilities> capa = Lists.newArrayList();
105         capa.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
106             new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
107                 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build())
108                 .setGracefulRestartCapability(new GracefulRestartCapabilityBuilder().build()).build())
109                 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(AS_NUMBER).build()).build()).build());
110         capa.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
111         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
112
113         final ChannelFuture f = mock(ChannelFuture.class);
114         doReturn(null).when(f).addListener(Mockito.any());
115
116         doAnswer(invocation -> {
117             final Object[] args = invocation.getArguments();
118             BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
119             return f;
120         }).when(this.speakerListener).writeAndFlush(Mockito.any(Notification.class));
121         doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
122         doReturn(true).when(this.speakerListener).isActive();
123         doAnswer(invocation -> {
124             final Runnable command = (Runnable) invocation.getArguments()[0];
125             final long delay = (long) invocation.getArguments()[1];
126             final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
127             GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
128             return null;
129         }).when(this.eventLoop).schedule(Mockito.any(Runnable.class), Mockito.any(long.class), Mockito.any(TimeUnit.class));
130         doReturn("TestingChannel").when(this.speakerListener).toString();
131         doReturn(true).when(this.speakerListener).isWritable();
132         doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener).remoteAddress();
133         doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener).localAddress();
134         doReturn(this.pipeline).when(this.speakerListener).pipeline();
135         doReturn(this.pipeline).when(this.pipeline).replace(Mockito.any(ChannelHandler.class), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
136         doReturn(null).when(this.pipeline).replace(Matchers.<Class<ChannelHandler>>any(), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
137         doReturn(this.pipeline).when(this.pipeline).addLast(Mockito.any(ChannelHandler.class));
138         final ChannelFuture futureChannel = mock(ChannelFuture.class);
139         doReturn(null).when(futureChannel).addListener(Mockito.any());
140         doReturn(futureChannel).when(this.speakerListener).close();
141         this.listener = new SimpleSessionListener();
142         this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null);
143         this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
144     }
145
146     @Test
147     public void testBGPSession() throws BGPDocumentedException {
148         this.bgpSession.sessionUp();
149         assertEquals(State.UP, this.bgpSession.getState());
150         assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
151         assertEquals(BGP_ID, this.bgpSession.getBgpId());
152         assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
153         Assert.assertEquals(State.UP, this.listener.getState());
154
155         //test stats
156         final BgpSessionState state = this.bgpSession.getBgpSessionState();
157         assertEquals(HOLD_TIMER, state.getHoldtimeCurrent().intValue());
158         assertEquals(1, state.getKeepaliveCurrent().intValue());
159         assertEquals(State.UP.name(), state.getSessionState());
160         assertEquals(BGP_ID.getValue(), new String(state.getLocalPeerPreferences().getHost().getValue()));
161         assertEquals(AS_NUMBER.getValue(), state.getLocalPeerPreferences().getAs().getValue());
162         assertTrue(state.getLocalPeerPreferences().getBgpExtendedMessageCapability());
163         assertEquals(BGP_ID.getValue(), state.getLocalPeerPreferences().getBgpId().getValue());
164         assertEquals(1, state.getLocalPeerPreferences().getAdvertizedTableTypes().size());
165         assertEquals(HOLD_TIMER, state.getLocalPeerPreferences().getHoldtimer().intValue());
166         assertTrue(state.getLocalPeerPreferences().getFourOctetAsCapability().booleanValue());
167         assertTrue(state.getLocalPeerPreferences().getBgpExtendedMessageCapability().booleanValue());
168         assertTrue(state.getLocalPeerPreferences().getGrCapability());
169         assertEquals(LOCAL_IP, new String(state.getRemotePeerPreferences().getHost().getValue()));
170         assertEquals(LOCAL_PORT, state.getRemotePeerPreferences().getPort().getValue().intValue());
171         assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
172         assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().getValue().longValue());
173
174         this.bgpSession.handleMessage(new UpdateBuilder().build());
175         assertEquals(1, this.listener.getListMsg().size());
176         assertTrue(this.listener.getListMsg().get(0) instanceof Update);
177         assertEquals(1, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
178         assertEquals(1, state.getMessagesStats().getUpdateMsgs().getReceived().getCount().getValue().longValue());
179         assertEquals(0, state.getMessagesStats().getUpdateMsgs().getSent().getCount().getValue().longValue());
180
181         this.bgpSession.handleMessage(new KeepaliveBuilder().build());
182         this.bgpSession.handleMessage(new KeepaliveBuilder().build());
183         assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
184         assertEquals(2, state.getMessagesStats().getKeepAliveMsgs().getReceived().getCount().getValue().longValue());
185         assertEquals(0, state.getMessagesStats().getKeepAliveMsgs().getSent().getCount().getValue().longValue());
186
187         this.bgpSession.close();
188         assertEquals(State.IDLE, this.bgpSession.getState());
189         assertEquals(1, this.receivedMsgs.size());
190         assertTrue(this.receivedMsgs.get(0) instanceof Notify);
191         final Notify error = (Notify) this.receivedMsgs.get(0);
192         assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
193         assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
194         Mockito.verify(this.speakerListener).close();
195         assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
196         assertEquals(1, state.getMessagesStats().getTotalMsgs().getSent().getCount().getValue().longValue());
197         assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSentTotal().getCount().getValue().longValue());
198         assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSent().get(0).getCount().getValue().longValue());
199         assertEquals(BGPError.CEASE.getCode(), state.getMessagesStats().getErrorMsgs().getErrorSent().get(0).getErrorCode().shortValue());
200         assertEquals(BGPError.CEASE.getSubcode(), state.getMessagesStats().getErrorMsgs().getErrorSent().get(0).getErrorSubcode().shortValue());
201
202         this.bgpSession.resetBgpSessionStats();
203         assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().getValue().longValue());
204         assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().getValue().longValue());
205         assertEquals(0, state.getMessagesStats().getErrorMsgs().getErrorSentTotal().getCount().getValue().longValue());
206     }
207
208     @Test
209     public void testHandleOpenMsg() throws BGPDocumentedException {
210         this.bgpSession.handleMessage(this.classicOpen);
211         Assert.assertEquals(State.IDLE, this.bgpSession.getState());
212         Assert.assertEquals(1, this.receivedMsgs.size());
213         Assert.assertTrue(this.receivedMsgs.get(0) instanceof Notify);
214         final Notify error = (Notify) this.receivedMsgs.get(0);
215         Assert.assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
216         Assert.assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
217         Mockito.verify(this.speakerListener).close();
218     }
219
220     @Test
221     public void testHandleNotifyMsg() throws BGPDocumentedException {
222         this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode()).setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
223         assertEquals(1, this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceivedTotal().getCount().getValue().longValue());
224         assertEquals(1, this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceived().get(0).getCount().getValue().longValue());
225         assertEquals(BGPError.BAD_BGP_ID.getCode(), this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceived().get(0).getErrorCode().shortValue());
226         assertEquals(BGPError.BAD_BGP_ID.getSubcode(), this.bgpSession.getBgpSessionState().getMessagesStats().getErrorMsgs().getErrorReceived().get(0).getErrorSubcode().shortValue());
227         Assert.assertEquals(State.IDLE, this.bgpSession.getState());
228         Mockito.verify(this.speakerListener).close();
229     }
230
231     @Test
232     public void testEndOfInput() throws InterruptedException {
233         this.bgpSession.sessionUp();
234         Assert.assertEquals(State.UP, this.listener.getState());
235         this.bgpSession.endOfInput();
236         checkIdleState(this.listener);
237     }
238
239     @Test
240     public void testHoldTimerExpire() throws InterruptedException {
241         this.bgpSession.sessionUp();
242         checkIdleState(this.listener);
243         Assert.assertEquals(3, this.receivedMsgs.size());
244         Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
245         final Notify error = (Notify) this.receivedMsgs.get(2);
246         Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
247         Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
248         Mockito.verify(this.speakerListener).close();
249     }
250 }