Make MP capabilities reusable
[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
14 import com.google.common.collect.Lists;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.ChannelOutboundHandlerAdapter;
17 import io.netty.channel.ChannelPromise;
18 import io.netty.channel.embedded.EmbeddedChannel;
19 import java.net.InetSocketAddress;
20 import java.net.UnknownHostException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
28 import org.opendaylight.protocol.bgp.parser.BGPError;
29 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
30 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
31 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefreshBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.GracefulRestartCapabilityBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
56 import org.opendaylight.yangtools.yang.binding.Notification;
57
58 public class BGPSessionImplTest {
59
60     private static final int HOLD_TIMER = 3;
61     private static final AsNumber AS_NUMBER = new AsNumber(30L);
62     private static final Ipv4Address BGP_ID = new Ipv4Address("1.1.1.2");
63     private static final String LOCAL_IP = "1.1.1.4";
64     private static final int LOCAL_PORT = 12345;
65
66     private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
67
68     private Open classicOpen;
69
70     private BGPSessionImpl bgpSession;
71
72     private SimpleSessionListener listener;
73
74     private EmbeddedChannel embeddedChannel;
75
76     private MessageCollector collector;
77
78     @Before
79     public void setUp() throws UnknownHostException {
80         this.collector = new MessageCollector();
81         this.embeddedChannel = Mockito.spy(new EmbeddedChannel());
82         final BGPHandlerFactory hf = new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry());
83         this.embeddedChannel.pipeline().addLast(hf.getDecoders());
84         this.embeddedChannel.pipeline().addLast(this.collector);
85         Mockito.doReturn(new InetSocketAddress(BGP_ID.getValue(), LOCAL_PORT)).when(this.embeddedChannel).remoteAddress();
86         Mockito.doReturn(new InetSocketAddress(LOCAL_IP, LOCAL_PORT)).when(this.embeddedChannel).localAddress();
87
88         final List<BgpParameters> tlvs = Lists.newArrayList();
89         final List<OptionalCapabilities> capa = Lists.newArrayList();
90         capa.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
91             new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
92                 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build()).build());
93         capa.add(new OptionalCapabilitiesBuilder().setCParameters(
94                 new CParametersBuilder().addAugmentation(
95                         CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(
96                                 new GracefulRestartCapabilityBuilder().build()).build()).build()).build());
97         capa.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
98         capa.add(new OptionalCapabilitiesBuilder().setCParameters(
99                 new CParametersBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(AS_NUMBER).build()).build()).build());
100         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
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         this.listener = new SimpleSessionListener();
105         this.bgpSession = new BGPSessionImpl(this.listener, this.embeddedChannel, this.classicOpen, this.classicOpen.getHoldTimer(), null);
106         this.embeddedChannel.pipeline().addFirst(this.bgpSession);
107     }
108
109     @Test
110     public void testBGPSession() {
111         assertEquals(BGPSessionImpl.State.UP, this.bgpSession.getState());
112         assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
113         assertEquals(BGP_ID, this.bgpSession.getBgpId());
114         assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
115         assertTrue(this.listener.up);
116         //test stats
117         final BgpSessionState state = this.bgpSession.getBgpSesionState();
118         assertEquals(HOLD_TIMER, state.getHoldtimeCurrent().intValue());
119         assertEquals(1, state.getKeepaliveCurrent().intValue());
120         assertEquals(BGPSessionImpl.State.UP.name(), state.getSessionState());
121         assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getAddress());
122         assertEquals(AS_NUMBER.getValue(), state.getPeerPreferences().getAs());
123         assertTrue(state.getPeerPreferences().getBgpExtendedMessageCapability());
124         assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getBgpId());
125         assertEquals(1, state.getPeerPreferences().getAdvertizedTableTypes().size());
126         assertEquals(HOLD_TIMER, state.getPeerPreferences().getHoldtime().intValue());
127         assertTrue(state.getPeerPreferences().getFourOctetAsCapability().booleanValue());
128         assertTrue(state.getPeerPreferences().getBgpExtendedMessageCapability().booleanValue());
129         assertTrue(state.getPeerPreferences().getGrCapability());
130         assertEquals(LOCAL_IP, state.getSpeakerPreferences().getAddress());
131         assertEquals(LOCAL_PORT, state.getSpeakerPreferences().getPort().intValue());
132         assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
133         assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
134
135         this.bgpSession.handleMessage(new UpdateBuilder().build());
136         assertEquals(1, this.listener.getListMsg().size());
137         assertTrue(this.listener.getListMsg().get(0) instanceof Update);
138         assertEquals(1, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
139         assertEquals(1, state.getMessagesStats().getUpdateMsgs().getReceived().getCount().longValue());
140         assertEquals(0, state.getMessagesStats().getUpdateMsgs().getSent().getCount().longValue());
141
142         this.bgpSession.handleMessage(new RouteRefreshBuilder().build());
143         assertEquals(2, this.listener.getListMsg().size());
144         assertEquals(1, state.getMessagesStats().getRouteRefreshMsgs().getReceived().getCount().longValue());
145         assertEquals(0, state.getMessagesStats().getRouteRefreshMsgs().getSent().getCount().longValue());
146
147         this.bgpSession.handleMessage(new KeepaliveBuilder().build());
148         this.bgpSession.handleMessage(new KeepaliveBuilder().build());
149         assertEquals(4, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
150         assertEquals(2, state.getMessagesStats().getKeepAliveMsgs().getReceived().getCount().longValue());
151         assertEquals(0, state.getMessagesStats().getKeepAliveMsgs().getSent().getCount().longValue());
152
153         this.bgpSession.close();
154         assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
155         assertEquals(1, this.collector.receivedMsgs.size());
156         assertTrue(this.collector.receivedMsgs.get(0) instanceof Notify);
157         final Notify error = (Notify) this.collector.receivedMsgs.get(0);
158         assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
159         assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
160         Mockito.verify(this.embeddedChannel).close();
161         assertEquals(4, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
162         assertEquals(1, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
163         assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
164         assertEquals(BGPError.CEASE.getCode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getCode().shortValue());
165         assertEquals(BGPError.CEASE.getSubcode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getSubCode().shortValue());
166
167         this.bgpSession.resetSessionStats();
168         assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
169         assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
170         assertEquals(0, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
171     }
172
173     @Test
174     public void testHandleOpenMsg() {
175         this.bgpSession.handleMessage(this.classicOpen);
176         Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
177         Assert.assertEquals(1, this.collector.receivedMsgs.size());
178         Assert.assertTrue(this.collector.receivedMsgs.get(0) instanceof Notify);
179         final Notify error = (Notify) this.collector.receivedMsgs.get(0);
180         Assert.assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
181         Assert.assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
182         Mockito.verify(this.embeddedChannel).close();
183     }
184
185     @Test
186     public void testHandleNotifyMsg() {
187         this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode()).setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
188         assertEquals(1, this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCount().longValue());
189         assertEquals(BGPError.BAD_BGP_ID.getCode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCode().shortValue());
190         assertEquals(BGPError.BAD_BGP_ID.getSubcode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getSubCode().shortValue());
191         Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
192         Mockito.verify(this.embeddedChannel).close();
193     }
194
195     @Test
196     public void testEndOfInput() {
197         Assert.assertFalse(this.listener.down);
198         this.bgpSession.endOfInput();
199         Assert.assertTrue(this.listener.down);
200     }
201
202     @Test
203     public void testHoldTimerExpire() throws InterruptedException {
204         while (this.embeddedChannel.runScheduledPendingTasks() != -1) {
205         }
206         Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
207         Assert.assertEquals(3, this.collector.receivedMsgs.size());
208         Assert.assertTrue(this.collector.receivedMsgs.get(2) instanceof Notify);
209         final Notify error = (Notify) this.collector.receivedMsgs.get(2);
210         Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
211         Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
212         Mockito.verify(this.embeddedChannel).close();
213     }
214
215     private static final class MessageCollector extends ChannelOutboundHandlerAdapter {
216
217         private final List<Notification> receivedMsgs = new ArrayList<>();
218
219         @Override
220         public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
221             this.receivedMsgs.add((Notification) msg);
222             super.write(ctx, msg, promise);
223         }
224     }
225 }