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