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;
16 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkIdleState;
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.channel.embedded.EmbeddedChannel;
25 import io.netty.util.concurrent.GlobalEventExecutor;
26 import java.net.InetAddress;
27 import java.net.InetSocketAddress;
28 import java.net.UnknownHostException;
29 import java.util.List;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Matchers;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
39 import org.opendaylight.protocol.bgp.parser.BGPError;
40 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
41 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
42 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
43 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
44 import org.opendaylight.protocol.bgp.rib.spi.State;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Notify;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.NotifyBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.ProtocolVersion;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.GracefulRestartCapabilityBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapabilityBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
67 import org.opendaylight.yangtools.yang.binding.Notification;
69 public class BGPSessionImplTest {
71 private static final int HOLD_TIMER = 3;
72 private static final AsNumber AS_NUMBER = new AsNumber(30L);
73 private static final Ipv4Address BGP_ID = new Ipv4Address("1.1.1.2");
74 private static final String LOCAL_IP = "1.1.1.4";
75 private static final int LOCAL_PORT = 12345;
78 private EventLoop eventLoop;
81 private Channel speakerListener;
84 private ChannelPipeline pipeline;
86 private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
88 private final List<Notification> receivedMsgs = Lists.newArrayList();
90 private Open classicOpen;
92 private BGPSessionImpl bgpSession;
94 private SimpleSessionListener listener;
97 public void setUp() throws UnknownHostException {
98 new EmbeddedChannel();
99 MockitoAnnotations.initMocks(this);
100 final List<BgpParameters> tlvs = Lists.newArrayList();
101 this.classicOpen = new OpenBuilder().setMyAsNumber(AS_NUMBER.getValue().intValue()).setHoldTimer(HOLD_TIMER)
102 .setVersion(new ProtocolVersion((short) 4)).setBgpParameters(tlvs).setBgpIdentifier(BGP_ID).build();
104 final List<OptionalCapabilities> capa = Lists.newArrayList();
105 capa.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
106 new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
107 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build())
108 .setGracefulRestartCapability(new GracefulRestartCapabilityBuilder().build()).build())
109 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(AS_NUMBER).build()).build()).build());
110 capa.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
111 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
113 final ChannelFuture f = mock(ChannelFuture.class);
114 doReturn(null).when(f).addListener(Mockito.any());
116 doAnswer(invocation -> {
117 final Object[] args = invocation.getArguments();
118 BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
120 }).when(this.speakerListener).writeAndFlush(Mockito.any(Notification.class));
121 doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
122 doReturn(true).when(this.speakerListener).isActive();
123 doAnswer(invocation -> {
124 final Runnable command = (Runnable) invocation.getArguments()[0];
125 final long delay = (long) invocation.getArguments()[1];
126 final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
127 GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
129 }).when(this.eventLoop).schedule(Mockito.any(Runnable.class), Mockito.any(long.class), Mockito.any(TimeUnit.class));
130 doReturn("TestingChannel").when(this.speakerListener).toString();
131 doReturn(true).when(this.speakerListener).isWritable();
132 doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener).remoteAddress();
133 doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener).localAddress();
134 doReturn(this.pipeline).when(this.speakerListener).pipeline();
135 doReturn(this.pipeline).when(this.pipeline).replace(Mockito.any(ChannelHandler.class), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
136 doReturn(null).when(this.pipeline).replace(Matchers.<Class<ChannelHandler>>any(), Mockito.any(String.class), Mockito.any(ChannelHandler.class));
137 doReturn(this.pipeline).when(this.pipeline).addLast(Mockito.any(ChannelHandler.class));
138 final ChannelFuture futureChannel = mock(ChannelFuture.class);
139 doReturn(null).when(futureChannel).addListener(Mockito.any());
140 doReturn(futureChannel).when(this.speakerListener).close();
141 this.listener = new SimpleSessionListener();
142 this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen, this.classicOpen.getHoldTimer(), null);
143 this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
147 public void testBGPSession() throws BGPDocumentedException {
148 this.bgpSession.sessionUp();
149 assertEquals(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 Assert.assertEquals(State.UP, this.listener.getState());
155 this.bgpSession.handleMessage(new UpdateBuilder().build());
156 assertEquals(1, this.listener.getListMsg().size());
157 assertTrue(this.listener.getListMsg().get(0) instanceof Update);
158 this.bgpSession.close();
159 assertEquals(State.IDLE, this.bgpSession.getState());
160 assertEquals(1, this.receivedMsgs.size());
161 assertTrue(this.receivedMsgs.get(0) instanceof Notify);
162 final Notify error = (Notify) this.receivedMsgs.get(0);
163 assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
164 assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
165 Mockito.verify(this.speakerListener).close();
169 public void testHandleOpenMsg() throws BGPDocumentedException {
170 this.bgpSession.handleMessage(this.classicOpen);
171 Assert.assertEquals(State.IDLE, this.bgpSession.getState());
172 Assert.assertEquals(1, this.receivedMsgs.size());
173 Assert.assertTrue(this.receivedMsgs.get(0) instanceof Notify);
174 final Notify error = (Notify) this.receivedMsgs.get(0);
175 Assert.assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
176 Assert.assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
177 Mockito.verify(this.speakerListener).close();
181 public void testHandleNotifyMsg() throws BGPDocumentedException {
182 this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode())
183 .setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
184 Assert.assertEquals(State.IDLE, this.bgpSession.getState());
185 Mockito.verify(this.speakerListener).close();
189 public void testEndOfInput() throws InterruptedException {
190 this.bgpSession.sessionUp();
191 Assert.assertEquals(State.UP, this.listener.getState());
192 this.bgpSession.endOfInput();
193 checkIdleState(this.listener);
197 public void testHoldTimerExpire() throws InterruptedException {
198 this.bgpSession.sessionUp();
199 checkIdleState(this.listener);
200 Assert.assertEquals(3, this.receivedMsgs.size());
201 Assert.assertTrue(this.receivedMsgs.get(2) instanceof Notify);
202 final Notify error = (Notify) this.receivedMsgs.get(2);
203 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
204 Assert.assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
205 Mockito.verify(this.speakerListener).close();
209 public void testSessionRecoveryOnException() throws Exception {
210 final BGPSessionListener listener = mock(BGPSessionListener.class);
211 Mockito.doThrow(new RuntimeException("Mocked runtime exception."))
212 .when(listener).onSessionUp(Matchers.any());
213 this.bgpSession = Mockito.spy(new BGPSessionImpl(listener, this.speakerListener, this.classicOpen,
214 this.classicOpen.getHoldTimer(), null));
215 this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
217 Mockito.verify(this.bgpSession, Mockito.never()).handleException(Matchers.any());
218 Mockito.verify(this.bgpSession, Mockito.never()).writeAndFlush(Matchers.any(Notification.class));
219 Mockito.verify(this.bgpSession, Mockito.never()).terminate(Matchers.any(BGPDocumentedException.class));
221 this.bgpSession.sessionUp();
222 Assert.fail(); // expect the exception to be populated
223 } catch (final RuntimeException ignored) {}
224 Assert.assertNotEquals(State.UP, this.bgpSession.getState());
225 Mockito.verify(this.bgpSession).handleException(Matchers.any());
226 Mockito.verify(this.bgpSession).writeAndFlush(Matchers.any(Notification.class));
227 Mockito.verify(this.bgpSession).terminate(Matchers.any(BGPDocumentedException.class));
228 Mockito.verify(listener).onSessionTerminated(this.bgpSession, new BGPTerminationReason(BGPError.CEASE));