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 io.netty.channel.Channel;
27 import io.netty.channel.ChannelFuture;
28 import io.netty.channel.ChannelHandler;
29 import io.netty.channel.ChannelPipeline;
30 import io.netty.channel.EventLoop;
31 import io.netty.channel.embedded.EmbeddedChannel;
32 import io.netty.util.concurrent.GlobalEventExecutor;
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35 import java.net.UnknownHostException;
36 import java.util.ArrayList;
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.Ipv4AddressNoZone;
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;
74 import org.opendaylight.yangtools.yang.common.Uint16;
75 import org.opendaylight.yangtools.yang.common.Uint32;
76 import org.opendaylight.yangtools.yang.common.Uint8;
78 public class BGPSessionImplTest {
79 private static final Uint16 HOLD_TIMER = Uint16.valueOf(3);
80 private static final AsNumber AS_NUMBER = new AsNumber(Uint32.valueOf(30));
81 private static final Ipv4AddressNoZone BGP_ID = new Ipv4AddressNoZone("1.1.1.2");
82 private static final String LOCAL_IP = "1.1.1.4";
83 private static final int LOCAL_PORT = 12345;
86 private EventLoop eventLoop;
89 private Channel speakerListener;
92 private ChannelPipeline pipeline;
94 private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class,
95 UnicastSubsequentAddressFamily.class);
97 private final List<Notification> receivedMsgs = new ArrayList<>();
99 private Open classicOpen;
101 private BGPSessionImpl bgpSession;
103 private SimpleSessionListener listener;
106 public void setUp() throws UnknownHostException {
107 new EmbeddedChannel();
108 MockitoAnnotations.initMocks(this);
109 final List<BgpParameters> tlvs = new ArrayList<>();
110 this.classicOpen = new OpenBuilder()
111 .setMyAsNumber(Uint16.valueOf(AS_NUMBER.getValue()))
112 .setHoldTimer(HOLD_TIMER)
113 .setVersion(new ProtocolVersion(Uint8.valueOf(4)))
114 .setBgpParameters(tlvs)
115 .setBgpIdentifier(BGP_ID)
118 final List<OptionalCapabilities> capa = new ArrayList<>();
119 capa.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder()
120 .addAugmentation(CParameters1.class, new CParameters1Builder()
121 .setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
122 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build())
123 .setGracefulRestartCapability(new GracefulRestartCapabilityBuilder().build()).build())
124 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(AS_NUMBER).build()).build()).build());
125 capa.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY)
127 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capa).build());
129 final ChannelFuture f = mock(ChannelFuture.class);
130 doReturn(null).when(f).addListener(any());
132 doAnswer(invocation -> {
133 final Object[] args = invocation.getArguments();
134 BGPSessionImplTest.this.receivedMsgs.add((Notification) args[0]);
136 }).when(this.speakerListener).writeAndFlush(any(Notification.class));
137 doReturn(this.eventLoop).when(this.speakerListener).eventLoop();
138 doReturn(true).when(this.speakerListener).isActive();
139 doAnswer(invocation -> {
140 final Runnable command = (Runnable) invocation.getArguments()[0];
141 final long delay = (long) invocation.getArguments()[1];
142 final TimeUnit unit = (TimeUnit) invocation.getArguments()[2];
143 GlobalEventExecutor.INSTANCE.schedule(command, delay, unit);
145 }).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
146 doReturn("TestingChannel").when(this.speakerListener).toString();
147 doReturn(true).when(this.speakerListener).isWritable();
148 doReturn(new InetSocketAddress(InetAddress.getByName(BGP_ID.getValue()), 179)).when(this.speakerListener)
150 doReturn(new InetSocketAddress(InetAddress.getByName(LOCAL_IP), LOCAL_PORT)).when(this.speakerListener)
152 doReturn(this.pipeline).when(this.speakerListener).pipeline();
153 doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class),
154 any(ChannelHandler.class));
155 doReturn(null).when(this.pipeline).replace(ArgumentMatchers.<Class<ChannelHandler>>any(), any(String.class),
156 any(ChannelHandler.class));
157 doReturn(this.pipeline).when(this.pipeline).addLast(any(ChannelHandler.class));
158 final ChannelFuture futureChannel = mock(ChannelFuture.class);
159 doReturn(null).when(futureChannel).addListener(any());
160 doReturn(futureChannel).when(this.speakerListener).close();
161 this.listener = new SimpleSessionListener();
162 this.bgpSession = new BGPSessionImpl(this.listener, this.speakerListener, this.classicOpen,
163 this.classicOpen.getHoldTimer().toJava(), null);
164 this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
168 public void testBGPSession() throws BGPDocumentedException {
169 this.bgpSession.sessionUp();
170 assertEquals(State.UP, this.bgpSession.getState());
171 assertEquals(AS_NUMBER, this.bgpSession.getAsNumber());
172 assertEquals(BGP_ID, this.bgpSession.getBgpId());
173 assertEquals(1, this.bgpSession.getAdvertisedTableTypes().size());
174 assertEquals(State.UP, this.listener.getState());
176 this.bgpSession.handleMessage(new UpdateBuilder().build());
177 assertEquals(1, this.listener.getListMsg().size());
178 assertTrue(this.listener.getListMsg().get(0) instanceof Update);
179 this.bgpSession.close();
180 assertEquals(State.IDLE, this.bgpSession.getState());
181 assertEquals(1, this.receivedMsgs.size());
182 assertTrue(this.receivedMsgs.get(0) instanceof Notify);
183 final Notify error = (Notify) this.receivedMsgs.get(0);
184 assertEquals(BGPError.CEASE.getCode(), error.getErrorCode());
185 assertEquals(BGPError.CEASE.getSubcode(), error.getErrorSubcode());
186 verify(this.speakerListener).close();
190 public void testHandleOpenMsg() throws BGPDocumentedException {
191 this.bgpSession.handleMessage(this.classicOpen);
192 assertEquals(State.IDLE, this.bgpSession.getState());
193 assertEquals(1, this.receivedMsgs.size());
194 assertTrue(this.receivedMsgs.get(0) instanceof Notify);
195 final Notify error = (Notify) this.receivedMsgs.get(0);
196 assertEquals(BGPError.FSM_ERROR.getCode(), error.getErrorCode());
197 assertEquals(BGPError.FSM_ERROR.getSubcode(), error.getErrorSubcode());
198 verify(this.speakerListener).close();
202 public void testHandleNotifyMsg() throws BGPDocumentedException {
203 this.bgpSession.handleMessage(new NotifyBuilder().setErrorCode(BGPError.BAD_BGP_ID.getCode())
204 .setErrorSubcode(BGPError.BAD_BGP_ID.getSubcode()).build());
205 assertEquals(State.IDLE, this.bgpSession.getState());
206 verify(this.speakerListener).close();
210 public void testEndOfInput() throws InterruptedException {
211 this.bgpSession.sessionUp();
212 assertEquals(State.UP, this.listener.getState());
213 this.bgpSession.endOfInput();
214 checkIdleState(this.listener);
218 public void testHoldTimerExpire() throws InterruptedException {
219 this.bgpSession.sessionUp();
220 checkIdleState(this.listener);
221 assertEquals(3, this.receivedMsgs.size());
222 assertTrue(this.receivedMsgs.get(2) instanceof Notify);
223 final Notify error = (Notify) this.receivedMsgs.get(2);
224 assertEquals(BGPError.HOLD_TIMER_EXPIRED.getCode(), error.getErrorCode());
225 assertEquals(BGPError.HOLD_TIMER_EXPIRED.getSubcode(), error.getErrorSubcode());
226 verify(this.speakerListener).close();
230 public void testSessionRecoveryOnException() throws Exception {
231 final BGPSessionListener mockListener = mock(BGPSessionListener.class);
232 final IllegalStateException mockedEx = new IllegalStateException("Mocked runtime exception.");
234 doThrow(mockedEx).when(mockListener).onSessionUp(any());
235 doNothing().when(mockListener).onSessionTerminated(any(), any());
236 this.bgpSession = spy(new BGPSessionImpl(mockListener, this.speakerListener, this.classicOpen,
237 this.classicOpen.getHoldTimer().toJava(), null));
238 this.bgpSession.setChannelExtMsgCoder(this.classicOpen);
240 verify(this.bgpSession, never()).handleException(any());
241 verify(this.bgpSession, never()).writeAndFlush(any(Notification.class));
242 verify(this.bgpSession, never()).terminate(any(BGPDocumentedException.class));
244 this.bgpSession.sessionUp();
245 // expect the exception to be populated
247 } catch (final IllegalStateException e) {
248 assertSame(mockedEx, e);
250 assertNotEquals(State.UP, this.bgpSession.getState());
251 verify(this.bgpSession).handleException(any());
252 verify(this.bgpSession).writeAndFlush(any(Notification.class));
253 verify(this.bgpSession).terminate(any(BGPDocumentedException.class));
254 verify(mockListener).onSessionTerminated(this.bgpSession, new BGPTerminationReason(BGPError.CEASE));