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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doAnswer;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.doThrow;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.opendaylight.protocol.bgp.rib.impl.CheckUtil.checkIdleState;
26 import com.google.common.collect.Lists;
27 import io.netty.channel.Channel;
28 import io.netty.channel.ChannelFuture;
29 import io.netty.channel.ChannelHandler;
30 import io.netty.channel.ChannelPipeline;
31 import io.netty.channel.EventLoop;
32 import io.netty.channel.embedded.EmbeddedChannel;
33 import io.netty.util.concurrent.GlobalEventExecutor;
34 import java.net.InetAddress;
35 import java.net.InetSocketAddress;
36 import java.net.UnknownHostException;
37 import java.util.List;
38 import java.util.concurrent.TimeUnit;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentMatchers;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
45 import org.opendaylight.protocol.bgp.parser.BGPError;
46 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
47 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
48 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
49 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
50 import org.opendaylight.protocol.bgp.rib.spi.State;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Notify;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.NotifyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.ProtocolVersion;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.GracefulRestartCapabilityBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapabilityBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
73 import org.opendaylight.yangtools.yang.binding.Notification;
75 public class BGPSessionImplTest {
76 private static final int HOLD_TIMER = 3;
77 private static final AsNumber AS_NUMBER = new AsNumber(30L);
78 private static final Ipv4Address BGP_ID = new Ipv4Address("1.1.1.2");
79 private static final String LOCAL_IP = "1.1.1.4";
80 private static final int LOCAL_PORT = 12345;
83 private EventLoop eventLoop;
86 private Channel speakerListener;
89 private ChannelPipeline pipeline;
91 private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class,
92 UnicastSubsequentAddressFamily.class);
94 private final List<Notification> receivedMsgs = Lists.newArrayList();
96 private Open classicOpen;
98 private BGPSessionImpl bgpSession;
100 private SimpleSessionListener listener;
103 public void setUp() throws UnknownHostException {
104 new EmbeddedChannel();
105 MockitoAnnotations.initMocks(this);
106 final List<BgpParameters> tlvs = Lists.newArrayList();
107 this.classicOpen = new OpenBuilder().setMyAsNumber(AS_NUMBER.getValue().intValue()).setHoldTimer(HOLD_TIMER)
108 .setVersion(new ProtocolVersion((short) 4)).setBgpParameters(tlvs).setBgpIdentifier(BGP_ID).build();
110 final List<OptionalCapabilities> capa = Lists.newArrayList();
111 capa.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder()
112 .addAugmentation(CParameters1.class, new CParameters1Builder()
113 .setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
114 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build())
115 .setGracefulRestartCapability(new GracefulRestartCapabilityBuilder().build()).build())
116 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(AS_NUMBER).build()).build()).build());
117 capa.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY)
119 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
121 final ChannelFuture f = mock(ChannelFuture.class);
122 doReturn(null).when(f).addListener(any());
124 doAnswer(invocation -> {
125 final Object[] args = invocation.getArguments();
126 BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
128 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
129 doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
130 doReturn(true).when(this.speakerListener).isActive();
131 doAnswer(invocation -> {
132 final Runnable command = (Runnable) invocation.getArguments()[0];
133 final long delay = (long) invocation.getArguments()[1];
134 final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
135 GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
137 }).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), 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)
142 doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener)
144 doReturn(this.pipeline).when(this.speakerListener).pipeline();
145 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class),
146 any(ChannelHandler.class));
147 doReturn(null).when(this.pipeline).replace(ArgumentMatchers.<Class<ChannelHandler>>any(), any(String.class),
148 any(ChannelHandler.class));
149 doReturn(this.pipeline).when(this.pipeline).addLast(any(ChannelHandler.class));
150 final ChannelFuture futureChannel = mock(ChannelFuture.class);
151 doReturn(null).when(futureChannel).addListener(any());
152 doReturn(futureChannel).when(this.speakerListener).close();
153 this.listener = new SimpleSessionListener();
154 this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen,
155 this.classicOpen.getHoldTimer(), null);
156 this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
160 public void testBGPSession() throws BGPDocumentedException {
161 this.bgpSession.sessionUp();
162 assertEquals(State.UP, this.bgpSession.getState());
163 assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
164 assertEquals(BGP_ID, this.bgpSession.getBgpId());
165 assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
166 assertEquals(State.UP, this.listener.getState());
168 this.bgpSession.handleMessage(new UpdateBuilder().build());
169 assertEquals(1, this.listener.getListMsg().size());
170 assertTrue(this.listener.getListMsg().get(0) instanceof Update);
171 this.bgpSession.close();
172 assertEquals(State.IDLE, this.bgpSession.getState());
173 assertEquals(1, this.receivedMsgs.size());
174 assertTrue(this.receivedMsgs.get(0) instanceof Notify);
175 final Notify error = (Notify) this.receivedMsgs.get(0);
176 assertEquals(BGPError.CEASE.getCode(), error.getErrorCode().shortValue());
177 assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode().shortValue());
178 verify(this.speakerListener).close();
182 public void testHandleOpenMsg() throws BGPDocumentedException {
183 this.bgpSession.handleMessage(this.classicOpen);
184 assertEquals(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.FSM_ERROR.getCode(), error.getErrorCode().shortValue());
189 assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode().shortValue());
190 verify(this.speakerListener).close();
194 public void testHandleNotifyMsg() throws BGPDocumentedException {
195 this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode())
196 .setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
197 assertEquals(State.IDLE, this.bgpSession.getState());
198 verify(this.speakerListener).close();
202 public void testEndOfInput() throws InterruptedException {
203 this.bgpSession.sessionUp();
204 assertEquals(State.UP, this.listener.getState());
205 this.bgpSession.endOfInput();
206 checkIdleState(this.listener);
210 public void testHoldTimerExpire() throws InterruptedException {
211 this.bgpSession.sessionUp();
212 checkIdleState(this.listener);
213 assertEquals(3, this.receivedMsgs.size());
214 assertTrue(this.receivedMsgs.get(2) instanceof Notify);
215 final Notify error = (Notify) this.receivedMsgs.get(2);
216 assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode().shortValue());
217 assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode().shortValue());
218 verify(this.speakerListener).close();
222 public void testSessionRecoveryOnException() throws Exception {
223 final BGPSessionListener mockListener = mock(BGPSessionListener.class);
224 final IllegalStateException mockedEx = new IllegalStateException("Mocked runtime exception.");
226 doThrow(mockedEx).when(mockListener).onSessionUp(any());
227 doNothing().when(mockListener).onSessionTerminated(any(), any());
228 this.bgpSession = spy(new BGPSessionImpl(mockListener, this.speakerListener, this.classicOpen,
229 this.classicOpen.getHoldTimer(), null));
230 this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
232 verify(this.bgpSession, never()).handleException(any());
233 verify(this.bgpSession, never()).writeAndFlush(any(Notification.class));
234 verify(this.bgpSession, never()).terminate(any(BGPDocumentedException.class));
236 this.bgpSession.sessionUp();
237 // expect the exception to be populated
239 } catch (final IllegalStateException e) {
240 assertSame(mockedEx, e);
242 assertNotEquals(State.UP, this.bgpSession.getState());
243 verify(this.bgpSession).handleException(any());
244 verify(this.bgpSession).writeAndFlush(any(Notification.class));
245 verify(this.bgpSession).terminate(any(BGPDocumentedException.class));
246 verify(mockListener).onSessionTerminated(this.bgpSession, new BGPTerminationReason(BGPError.CEASE));