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.BgpParameters;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilitiesBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.CParametersBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.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 doAnswer(new Answer<Void>() {
125 public Void answer(final InvocationOnMock invocation) throws Throwable {
126 final Runnable command = (Runnable) invocation.getArguments()[0];
127 final long delay = (long) invocation.getArguments()[1];
128 final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
129 GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
132 }).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
133 doReturn("TestingChannel").when(this.speakerListener).toString();
134 doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener).remoteAddress();
135 doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener).localAddress();
136 doReturn(this.pipeline).when(this.speakerListener).pipeline();
137 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
138 doReturn(this.pipeline).when(this.pipeline).addLast(any(ChannelHandler.class));
139 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
140 this.listener = new SimpleSessionListener();
141 this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null);
145 public void testBGPSession() {
146 this.bgpSession.sessionUp();
147 assertEquals(BGPSessionImpl.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 assertTrue(this.listener.up);
153 final BgpSessionState state = this.bgpSession.getBgpSesionState();
154 assertEquals(HOLD_TIMER, state.getHoldtimeCurrent().intValue());
155 assertEquals(1, state.getKeepaliveCurrent().intValue());
156 assertEquals(BGPSessionImpl.State.UP.name(), state.getSessionState());
157 assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getAddress());
158 assertEquals(AS_NUMBER.getValue(), state.getPeerPreferences().getAs());
159 assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getBgpId());
160 assertEquals(1, state.getPeerPreferences().getAdvertizedTableTypes().size());
161 assertEquals(HOLD_TIMER, state.getPeerPreferences().getHoldtime().intValue());
162 assertTrue(state.getPeerPreferences().getFourOctetAsCapability().booleanValue());
163 assertTrue(state.getPeerPreferences().getGrCapability());
164 assertEquals(LOCAL_IP, state.getSpeakerPreferences().getAddress());
165 assertEquals(LOCAL_PORT, state.getSpeakerPreferences().getPort().intValue());
166 assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
167 assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
169 this.bgpSession.handleMessage(new UpdateBuilder().build());
170 assertEquals(1, this.listener.getListMsg().size());
171 assertTrue(this.listener.getListMsg().get(0) instanceof Update);
172 assertEquals(1, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
173 assertEquals(1, state.getMessagesStats().getUpdateMsgs().getReceived().getCount().longValue());
174 assertEquals(0, state.getMessagesStats().getUpdateMsgs().getSent().getCount().longValue());
176 this.bgpSession.handleMessage(new KeepaliveBuilder().build());
177 this.bgpSession.handleMessage(new KeepaliveBuilder().build());
178 assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
179 assertEquals(2, state.getMessagesStats().getKeepAliveMsgs().getReceived().getCount().longValue());
180 assertEquals(0, state.getMessagesStats().getKeepAliveMsgs().getSent().getCount().longValue());
182 this.bgpSession.close();
183 assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
184 assertEquals(1, this.receivedMsgs.size());
185 assertTrue(this.receivedMsgs.get(0) instanceof Notify);
186 final Notify error = (Notify) this.receivedMsgs.get(0);
187 assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
188 assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
189 Mockito.verify(this.speakerListener).close();
190 assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
191 assertEquals(1, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
192 assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
193 assertEquals(BGPError.CEASE.getCode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getCode().shortValue());
194 assertEquals(BGPError.CEASE.getSubcode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getSubCode().shortValue());
196 this.bgpSession.resetSessionStats();
197 assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
198 assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
199 assertEquals(0, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
203 public void testHandleOpenMsg() {
204 this.bgpSession.handleMessage(this.classicOpen);
205 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
206 Assert.assertEquals(1, this.receivedMsgs.size());
207 Assert.assertTrue(this.receivedMsgs.get(0) instanceof Notify);
208 final Notify error = (Notify) this.receivedMsgs.get(0);
209 Assert.assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
210 Assert.assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
211 Mockito.verify(this.speakerListener).close();
215 public void testHandleNotifyMsg() {
216 this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode()).setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
217 assertEquals(1, this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCount().longValue());
218 assertEquals(BGPError.BAD_BGP_ID.getCode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCode().shortValue());
219 assertEquals(BGPError.BAD_BGP_ID.getSubcode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getSubCode().shortValue());
220 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
221 Mockito.verify(this.speakerListener).close();
225 public void testEndOfInput() {
226 this.bgpSession.sessionUp();
227 Assert.assertFalse(this.listener.down);
228 this.bgpSession.endOfInput();
229 Assert.assertTrue(this.listener.down);
233 public void testHoldTimerExpire() throws InterruptedException {
234 this.bgpSession.sessionUp();
236 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
237 Assert.assertEquals(3, this.receivedMsgs.size());
238 Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
239 final Notify error = (Notify) this.receivedMsgs.get(2);
240 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
241 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
242 Mockito.verify(this.speakerListener).close();