BGPCEP-701: Remove old statistics, keep only openconfig stats
[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.protocol.bgp.parser.BGPDocumentedException;
39 import org.opendaylight.protocol.bgp.parser.BGPError;
40 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
41 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
42 import org.opendaylight.protocol.bgp.rib.spi.State;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.GracefulRestartCapabilityBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.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 BGPSessionImplTest {
68
69     private static final int HOLD_TIMER = 3;
70     private static final AsNumber AS_NUMBER = new AsNumber(30L);
71     private static final Ipv4Address BGP_ID = new Ipv4Address("1.1.1.2");
72     private static final String LOCAL_IP = "1.1.1.4";
73     private static final int LOCAL_PORT = 12345;
74
75     @Mock
76     private EventLoop eventLoop;
77
78     @Mock
79     private Channel speakerListener;
80
81     @Mock
82     private ChannelPipeline pipeline;
83
84     private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
85
86     private final List<Notification> receivedMsgs = Lists.newArrayList();
87
88     private Open classicOpen;
89
90     private BGPSessionImpl bgpSession;
91
92     private SimpleSessionListener listener;
93
94     @Before
95     public void setUp() throws UnknownHostException {
96         new EmbeddedChannel();
97         MockitoAnnotations.initMocks(this);
98         final List<BgpParameters> tlvs = Lists.newArrayList();
99         this.classicOpen = new OpenBuilder().setMyAsNumber(AS_NUMBER.getValue().intValue()).setHoldTimer(HOLD_TIMER)
100                 .setVersion(new ProtocolVersion((short) 4)).setBgpParameters(tlvs).setBgpIdentifier(BGP_ID).build();
101
102         final List<OptionalCapabilities> capa = Lists.newArrayList();
103         capa.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
104             new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
105                 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build())
106                 .setGracefulRestartCapability(new GracefulRestartCapabilityBuilder().build()).build())
107                 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(AS_NUMBER).build()).build()).build());
108         capa.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
109         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
110
111         final ChannelFuture f = mock(ChannelFuture.class);
112         doReturn(null).when(f).addListener(Mockito.any());
113
114         doAnswer(invocation -> {
115             final Object[] args = invocation.getArguments();
116             BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
117             return f;
118         }).when(this.speakerListener).writeAndFlush(Mockito.any(Notification.class));
119         doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
120         doReturn(true).when(this.speakerListener).isActive();
121         doAnswer(invocation -> {
122             final Runnable command = (Runnable) invocation.getArguments()[0];
123             final long delay = (long) invocation.getArguments()[1];
124             final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
125             GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
126             return null;
127         }).when(this.eventLoop).schedule(Mockito.any(Runnable.class), Mockito.any(long.class), Mockito.any(TimeUnit.class));
128         doReturn("TestingChannel").when(this.speakerListener).toString();
129         doReturn(true).when(this.speakerListener).isWritable();
130         doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener).remoteAddress();
131         doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener).localAddress();
132         doReturn(this.pipeline).when(this.speakerListener).pipeline();
133         doReturn(this.pipeline).when(this.pipeline).replace(Mockito.any(ChannelHandler.class), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
134         doReturn(null).when(this.pipeline).replace(Matchers.<Class<ChannelHandler>>any(), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
135         doReturn(this.pipeline).when(this.pipeline).addLast(Mockito.any(ChannelHandler.class));
136         final ChannelFuture futureChannel = mock(ChannelFuture.class);
137         doReturn(null).when(futureChannel).addListener(Mockito.any());
138         doReturn(futureChannel).when(this.speakerListener).close();
139         this.listener = new SimpleSessionListener();
140         this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null);
141         this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
142     }
143
144     @Test
145     public void testBGPSession() throws BGPDocumentedException {
146         this.bgpSession.sessionUp();
147         assertEquals(State.UP, this.bgpSession.getState());
148         assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
149         assertEquals(BGP_ID, this.bgpSession.getBgpId());
150         assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
151         Assert.assertEquals(State.UP, this.listener.getState());
152
153         this.bgpSession.handleMessage(new UpdateBuilder().build());
154         assertEquals(1, this.listener.getListMsg().size());
155         assertTrue(this.listener.getListMsg().get(0) instanceof Update);
156         this.bgpSession.close();
157         assertEquals(State.IDLE, this.bgpSession.getState());
158         assertEquals(1, this.receivedMsgs.size());
159         assertTrue(this.receivedMsgs.get(0) instanceof Notify);
160         final Notify error = (Notify) this.receivedMsgs.get(0);
161         assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
162         assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
163         Mockito.verify(this.speakerListener).close();
164     }
165
166     @Test
167     public void testHandleOpenMsg() throws BGPDocumentedException {
168         this.bgpSession.handleMessage(this.classicOpen);
169         Assert.assertEquals(State.IDLE, this.bgpSession.getState());
170         Assert.assertEquals(1, this.receivedMsgs.size());
171         Assert.assertTrue(this.receivedMsgs.get(0) instanceof Notify);
172         final Notify error = (Notify) this.receivedMsgs.get(0);
173         Assert.assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
174         Assert.assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
175         Mockito.verify(this.speakerListener).close();
176     }
177
178     @Test
179     public void testHandleNotifyMsg() throws BGPDocumentedException {
180         this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode())
181                 .setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
182         Assert.assertEquals(State.IDLE, this.bgpSession.getState());
183         Mockito.verify(this.speakerListener).close();
184     }
185
186     @Test
187     public void testEndOfInput() throws InterruptedException {
188         this.bgpSession.sessionUp();
189         Assert.assertEquals(State.UP, this.listener.getState());
190         this.bgpSession.endOfInput();
191         checkIdleState(this.listener);
192     }
193
194     @Test
195     public void testHoldTimerExpire() throws InterruptedException {
196         this.bgpSession.sessionUp();
197         checkIdleState(this.listener);
198         Assert.assertEquals(3, this.receivedMsgs.size());
199         Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
200         final Notify error = (Notify) this.receivedMsgs.get(2);
201         Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
202         Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
203         Mockito.verify(this.speakerListener).close();
204     }
205 }