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.Matchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
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.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.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.mockito.invocation.InvocationOnMock;
39 import org.mockito.stubbing.Answer;
40 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
41 import org.opendaylight.protocol.bgp.parser.BGPError;
42 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.KeepaliveBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Notify;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.NotifyBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.GracefulRestartCapabilityBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCapabilityBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
66 import org.opendaylight.yangtools.yang.binding.Notification;
68 public class BGPSessionImplTest {
70 private static final int HOLD_TIMER = 3;
71 private static final AsNumber AS_NUMBER = new AsNumber(30L);
72 private static final Ipv4Address BGP_ID = new Ipv4Address("1.1.1.2");
73 private static final String LOCAL_IP = "1.1.1.4";
74 private static final int LOCAL_PORT = 12345;
77 private EventLoop eventLoop;
80 private Channel speakerListener;
83 private ChannelPipeline pipeline;
85 private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
87 private final List<Notification> receivedMsgs = Lists.newArrayList();
89 private Open classicOpen;
91 private BGPSessionImpl bgpSession;
93 private SimpleSessionListener listener;
96 public void setUp() throws UnknownHostException {
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();
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()
109 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
111 final ChannelFuture f = mock(ChannelFuture.class);
112 doReturn(null).when(f).addListener(Mockito.<GenericFutureListener<? extends Future<? super Void>>>any());
114 doAnswer(new Answer<Object>() {
116 public Object answer(final InvocationOnMock invocation) {
117 final Object[] args = invocation.getArguments();
118 BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
121 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
122 doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
123 doReturn(true).when(this.speakerListener).isActive();
124 doAnswer(new Answer<Void>() {
126 public Void answer(final InvocationOnMock invocation) throws Throwable {
127 final Runnable command = (Runnable) invocation.getArguments()[0];
128 final long delay = (long) invocation.getArguments()[1];
129 final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
130 GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
133 }).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
134 doReturn("TestingChannel").when(this.speakerListener).toString();
135 doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener).remoteAddress();
136 doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener).localAddress();
137 doReturn(this.pipeline).when(this.speakerListener).pipeline();
138 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
139 doReturn(this.pipeline).when(this.pipeline).addLast(any(ChannelHandler.class));
140 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
141 this.listener = new SimpleSessionListener();
142 this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null);
146 public void testBGPSession() {
147 this.bgpSession.sessionUp();
148 assertEquals(BGPSessionImpl.State.UP, this.bgpSession.getState());
149 assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
150 assertEquals(BGP_ID, this.bgpSession.getBgpId());
151 assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
152 assertTrue(this.listener.up);
154 final BgpSessionState state = this.bgpSession.getBgpSesionState();
155 assertEquals(HOLD_TIMER, state.getHoldtimeCurrent().intValue());
156 assertEquals(1, state.getKeepaliveCurrent().intValue());
157 assertEquals(BGPSessionImpl.State.UP.name(), state.getSessionState());
158 assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getAddress());
159 assertEquals(AS_NUMBER.getValue(), state.getPeerPreferences().getAs());
160 assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getBgpId());
161 assertEquals(1, state.getPeerPreferences().getAdvertizedTableTypes().size());
162 assertEquals(HOLD_TIMER, state.getPeerPreferences().getHoldtime().intValue());
163 assertTrue(state.getPeerPreferences().getFourOctetAsCapability().booleanValue());
164 assertTrue(state.getPeerPreferences().getGrCapability());
165 assertEquals(LOCAL_IP, state.getSpeakerPreferences().getAddress());
166 assertEquals(LOCAL_PORT, state.getSpeakerPreferences().getPort().intValue());
167 assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
168 assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
170 this.bgpSession.handleMessage(new UpdateBuilder().build());
171 assertEquals(1, this.listener.getListMsg().size());
172 assertTrue(this.listener.getListMsg().get(0) instanceof Update);
173 assertEquals(1, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
174 assertEquals(1, state.getMessagesStats().getUpdateMsgs().getReceived().getCount().longValue());
175 assertEquals(0, state.getMessagesStats().getUpdateMsgs().getSent().getCount().longValue());
177 this.bgpSession.handleMessage(new KeepaliveBuilder().build());
178 this.bgpSession.handleMessage(new KeepaliveBuilder().build());
179 assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
180 assertEquals(2, state.getMessagesStats().getKeepAliveMsgs().getReceived().getCount().longValue());
181 assertEquals(0, state.getMessagesStats().getKeepAliveMsgs().getSent().getCount().longValue());
183 this.bgpSession.close();
184 assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
185 assertEquals(1, this.receivedMsgs.size());
186 assertTrue(this.receivedMsgs.get(0) instanceof Notify);
187 final Notify error = (Notify) this.receivedMsgs.get(0);
188 assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
189 assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
190 Mockito.verify(this.speakerListener).close();
191 assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
192 assertEquals(1, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
193 assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
194 assertEquals(BGPError.CEASE.getCode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getCode().shortValue());
195 assertEquals(BGPError.CEASE.getSubcode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getSubCode().shortValue());
197 this.bgpSession.resetSessionStats();
198 assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
199 assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
200 assertEquals(0, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
204 public void testHandleOpenMsg() {
205 this.bgpSession.handleMessage(this.classicOpen);
206 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
207 Assert.assertEquals(1, this.receivedMsgs.size());
208 Assert.assertTrue(this.receivedMsgs.get(0) instanceof Notify);
209 final Notify error = (Notify) this.receivedMsgs.get(0);
210 Assert.assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
211 Assert.assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
212 Mockito.verify(this.speakerListener).close();
216 public void testHandleNotifyMsg() {
217 this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode()).setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
218 assertEquals(1, this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCount().longValue());
219 assertEquals(BGPError.BAD_BGP_ID.getCode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCode().shortValue());
220 assertEquals(BGPError.BAD_BGP_ID.getSubcode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getSubCode().shortValue());
221 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
222 Mockito.verify(this.speakerListener).close();
226 public void testEndOfInput() {
227 this.bgpSession.sessionUp();
228 Assert.assertFalse(this.listener.down);
229 this.bgpSession.endOfInput();
230 Assert.assertTrue(this.listener.down);
234 public void testHoldTimerExpire() throws InterruptedException {
235 this.bgpSession.sessionUp();
237 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
238 Assert.assertEquals(3, this.receivedMsgs.size());
239 Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
240 final Notify error = (Notify) this.receivedMsgs.get(2);
241 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
242 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
243 Mockito.verify(this.speakerListener).close();