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.c.parameters.As4BytesCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.as4.bytes._case.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.open.bgp.parameters.optional.capabilities.c.parameters.GracefulRestartCaseBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCaseBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.graceful.restart._case.GracefulRestartCapabilityBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.multiprotocol._case.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).setVersion(new ProtocolVersion((short) 4)).setBgpParameters(
100 tlvs).setBgpIdentifier(BGP_ID).build();
102 final List<OptionalCapabilities> capa = Lists.newArrayList();
103 capa.add(new OptionalCapabilitiesBuilder().setCParameters(
104 new MultiprotocolCaseBuilder().setMultiprotocolCapability(
105 new MultiprotocolCapabilityBuilder().setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build()).build());
106 capa.add(new OptionalCapabilitiesBuilder().setCParameters(new As4BytesCaseBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(
107 AS_NUMBER).build()).build()).build());
108 capa.add(new OptionalCapabilitiesBuilder().setCParameters(
109 new GracefulRestartCaseBuilder().setGracefulRestartCapability(
110 new GracefulRestartCapabilityBuilder().build()).build()).build());
111 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
113 final ChannelFuture f = mock(ChannelFuture.class);
114 doReturn(null).when(f).addListener(Mockito.<GenericFutureListener<? extends Future<? super Void>>>any());
116 doAnswer(new Answer<Object>() {
118 public Object answer(final InvocationOnMock invocation) {
119 final Object[] args = invocation.getArguments();
120 BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
123 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
124 doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
125 doAnswer(new Answer<Void>() {
127 public Void answer(final InvocationOnMock invocation) throws Throwable {
128 final Runnable command = (Runnable) invocation.getArguments()[0];
129 final long delay = (long) invocation.getArguments()[1];
130 final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
131 GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
134 }).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
135 doReturn("TestingChannel").when(this.speakerListener).toString();
136 doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener).remoteAddress();
137 doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener).localAddress();
138 doReturn(this.pipeline).when(this.speakerListener).pipeline();
139 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
140 doReturn(this.pipeline).when(this.pipeline).addLast(any(ChannelHandler.class));
141 doReturn(mock(ChannelFuture.class)).when(this.speakerListener).close();
142 this.listener = new SimpleSessionListener();
143 this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null);
147 public void testBGPSession() {
148 this.bgpSession.sessionUp();
149 assertEquals(BGPSessionImpl.State.UP, this.bgpSession.getState());
150 assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
151 assertEquals(BGP_ID, this.bgpSession.getBgpId());
152 assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
153 assertTrue(this.listener.up);
155 final BgpSessionState state = this.bgpSession.getBgpSesionState();
156 assertEquals(HOLD_TIMER, state.getHoldtimeCurrent().intValue());
157 assertEquals(1, state.getKeepaliveCurrent().intValue());
158 assertEquals(BGPSessionImpl.State.UP.name(), state.getSessionState());
159 assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getAddress());
160 assertEquals(AS_NUMBER.getValue(), state.getPeerPreferences().getAs());
161 assertEquals(BGP_ID.getValue(), state.getPeerPreferences().getBgpId());
162 assertEquals(1, state.getPeerPreferences().getAdvertizedTableTypes().size());
163 assertEquals(HOLD_TIMER, state.getPeerPreferences().getHoldtime().intValue());
164 assertTrue(state.getPeerPreferences().getFourOctetAsCapability());
165 assertTrue(state.getPeerPreferences().getGrCapability());
166 assertEquals(LOCAL_IP, state.getSpeakerPreferences().getAddress());
167 assertEquals(LOCAL_PORT, state.getSpeakerPreferences().getPort().intValue());
168 assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
169 assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
171 this.bgpSession.handleMessage(new UpdateBuilder().build());
172 assertEquals(1, this.listener.getListMsg().size());
173 assertTrue(this.listener.getListMsg().get(0) instanceof Update);
174 assertEquals(1, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
175 assertEquals(1, state.getMessagesStats().getUpdateMsgs().getReceived().getCount().longValue());
176 assertEquals(0, state.getMessagesStats().getUpdateMsgs().getSent().getCount().longValue());
178 this.bgpSession.handleMessage(new KeepaliveBuilder().build());
179 this.bgpSession.handleMessage(new KeepaliveBuilder().build());
180 assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
181 assertEquals(2, state.getMessagesStats().getKeepAliveMsgs().getReceived().getCount().longValue());
182 assertEquals(0, state.getMessagesStats().getKeepAliveMsgs().getSent().getCount().longValue());
184 this.bgpSession.close();
185 assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
186 assertEquals(1, this.receivedMsgs.size());
187 assertTrue(this.receivedMsgs.get(0) instanceof Notify);
188 final Notify error = (Notify) this.receivedMsgs.get(0);
189 assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
190 assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
191 Mockito.verify(this.speakerListener).close();
192 assertEquals(3, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
193 assertEquals(1, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
194 assertEquals(1, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
195 assertEquals(BGPError.CEASE.getCode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getCode().shortValue());
196 assertEquals(BGPError.CEASE.getSubcode(), state.getMessagesStats().getErrorMsgs().getErrorSent().getSubCode().shortValue());
198 this.bgpSession.resetSessionStats();
199 assertEquals(0, state.getMessagesStats().getTotalMsgs().getReceived().getCount().longValue());
200 assertEquals(0, state.getMessagesStats().getTotalMsgs().getSent().getCount().longValue());
201 assertEquals(0, state.getMessagesStats().getErrorMsgs().getErrorSent().getCount().longValue());
205 public void testHandleOpenMsg() {
206 this.bgpSession.handleMessage(this.classicOpen);
207 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
208 Assert.assertEquals(1, this.receivedMsgs.size());
209 Assert.assertTrue(this.receivedMsgs.get(0) instanceof Notify);
210 final Notify error = (Notify) this.receivedMsgs.get(0);
211 Assert.assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
212 Assert.assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
213 Mockito.verify(this.speakerListener).close();
217 public void testHandleNotifyMsg() {
218 this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode()).setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
219 assertEquals(1, this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCount().longValue());
220 assertEquals(BGPError.BAD_BGP_ID.getCode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getCode().shortValue());
221 assertEquals(BGPError.BAD_BGP_ID.getSubcode(), this.bgpSession.getBgpSesionState().getMessagesStats().getErrorMsgs().getErrorReceived().getSubCode().shortValue());
222 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
223 Mockito.verify(this.speakerListener).close();
227 public void testEndOfInput() {
228 this.bgpSession.sessionUp();
229 Assert.assertFalse(this.listener.down);
230 this.bgpSession.endOfInput();
231 Assert.assertTrue(this.listener.down);
235 public void testHoldTimerExpire() throws InterruptedException {
236 this.bgpSession.sessionUp();
238 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.bgpSession.getState());
239 Assert.assertEquals(3, this.receivedMsgs.size());
240 Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
241 final Notify error = (Notify) this.receivedMsgs.get(2);
242 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
243 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
244 Mockito.verify(this.speakerListener).close();