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 static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
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.rev130715.AsNumber;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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;
71 public class BGPSessionImplTest {
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;
80 private EventLoop eventLoop;
83 private Channel speakerListener;
86 private ChannelPipeline pipeline;
88 private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
90 private final List<Notification> receivedMsgs = Lists.newArrayList();
92 private Open classicOpen;
94 private BGPSessionImpl bgpSession;
96 private SimpleSessionListener listener;
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();
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());
115 final ChannelFuture f = mock(ChannelFuture.class);
116 doReturn(null).when(f).addListener(Mockito.<GenericFutureListener<? extends Future<? super Void>>>any());
118 doAnswer(new Answer<Object>() {
120 public Object answer(final InvocationOnMock invocation) {
121 final Object[] args = invocation.getArguments();
122 BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
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>() {
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);
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);
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());
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());
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());
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());
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());
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());
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();
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();
239 public void testEndOfInput() throws InterruptedException {
240 this.bgpSession.sessionUp();
241 Assert.assertEquals(BGPSessionImpl.State.UP, this.listener.getState());
242 this.bgpSession.endOfInput();
244 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.listener.getState());
248 public void testHoldTimerExpire() throws InterruptedException {
249 this.bgpSession.sessionUp();
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();