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