2 * Copyright (c) 2016 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.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.util.concurrent.GenericFutureListener;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.protocol.bgp.rib.spi.PeerRPCs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.PeerRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.ResetSessionInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.ResetSessionInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.ResetSessionOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RestartGracefullyInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RestartGracefullyInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RestartGracefullyOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RouteRefreshRequestInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RouteRefreshRequestInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev180329.RouteRefreshRequestOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.SubsequentAddressFamily;
43 import org.opendaylight.yangtools.yang.binding.Notification;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.Uint32;
47 @RunWith(MockitoJUnitRunner.StrictStubs.class)
48 public final class BgpPeerRpcTest {
50 private BGPSessionImpl session;
52 private PeerRPCs peerRpcs;
56 private ChannelFuture future;
57 private BgpPeerRpc rpc;
60 public void setUp() throws InterruptedException, ExecutionException {
61 rpc = new BgpPeerRpc(peerRpcs, session,
62 Set.of(new TablesKey(Ipv4AddressFamily.VALUE, SubsequentAddressFamily.VALUE)));
63 final ChannelOutputLimiter limiter = new ChannelOutputLimiter(session);
65 doReturn(limiter).when(session).getLimiter();
66 doReturn(future).when(session).writeAndFlush(any(Notification.class));
68 doReturn(true).when(future).isSuccess();
69 doAnswer(invocation -> {
70 GenericFutureListener<ChannelFuture> listener = invocation.getArgument(0);
71 listener.operationComplete(future);
73 }).when(future).addListener(any());
77 public void testRouteRefreshRequestSuccessRequest() throws InterruptedException, ExecutionException {
78 final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
79 .setAfi(Ipv4AddressFamily.VALUE)
80 .setSafi(SubsequentAddressFamily.VALUE)
81 .setPeerRef(peer).build();
82 final Future<RpcResult<RouteRefreshRequestOutput>> result = rpc.routeRefreshRequest(input);
83 assertTrue(result.get().getErrors().isEmpty());
87 public void testRouteRefreshRequestFailedRequest() throws InterruptedException, ExecutionException {
88 final RouteRefreshRequestInput input = new RouteRefreshRequestInputBuilder()
89 .setAfi(Ipv6AddressFamily.VALUE)
90 .setSafi(SubsequentAddressFamily.VALUE)
91 .setPeerRef(peer).build();
92 final Future<RpcResult<RouteRefreshRequestOutput>> result = rpc.routeRefreshRequest(input);
93 assertEquals(1, result.get().getErrors().size());
94 assertEquals("Failed to send Route Refresh message due to unsupported address families.",
95 result.get().getErrors().iterator().next().getMessage());
99 public void testResetSessionRequestSuccessRequest() throws InterruptedException, ExecutionException {
100 doReturn(Futures.immediateFuture(null)).when(peerRpcs).releaseConnection();
101 final ResetSessionInput input = new ResetSessionInputBuilder()
102 .setPeerRef(peer).build();
103 final Future<RpcResult<ResetSessionOutput>> result = rpc.resetSession(input);
104 assertTrue(result.get().getErrors().isEmpty());
108 public void testRestartGracefullyRequestFailedRequest() throws ExecutionException, InterruptedException {
109 final long referraltimerSeconds = 10L;
110 doReturn(new SimpleSessionListener().restartGracefully(referraltimerSeconds))
111 .when(peerRpcs).restartGracefully(referraltimerSeconds);
112 final RestartGracefullyInput input = new RestartGracefullyInputBuilder()
113 .setSelectionDeferralTime(Uint32.valueOf(referraltimerSeconds))
115 final ListenableFuture<RpcResult<RestartGracefullyOutput>> result = rpc.restartGracefully(input);
116 assertTrue(!result.get().getErrors().isEmpty());