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