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.opendaylight.protocol.bgp.rib.impl.CheckUtil.waitFutureSuccess;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Stopwatch;
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Sets;
18 import com.google.common.util.concurrent.Uninterruptibles;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelFuture;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.epoll.Epoll;
23 import io.netty.channel.epoll.EpollEventLoopGroup;
24 import io.netty.channel.nio.NioEventLoopGroup;
25 import io.netty.util.concurrent.Future;
26 import io.netty.util.concurrent.GenericFutureListener;
27 import java.net.InetSocketAddress;
28 import java.util.List;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.After;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
37 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
38 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
39 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
40 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
43 import org.opendaylight.protocol.util.InetSocketAddressUtil;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
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.rev130919.open.message.BgpParameters;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
60 import org.slf4j.LoggerFactory;
62 public class BGPDispatcherImplTest {
63 private static final short HOLD_TIMER = 30;
64 private static final AsNumber AS_NUMBER = new AsNumber(30L);
65 private static final int RETRY_TIMER = 1;
66 private static final BgpTableType IPV_4_TT = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
67 private BGPDispatcherImpl serverDispatcher;
68 private TestClientDispatcher clientDispatcher;
69 private BGPPeerRegistry registry;
70 private SimpleSessionListener clientListener;
71 private SimpleSessionListener serverListener;
72 private EventLoopGroup boss;
73 private EventLoopGroup worker;
76 public void setUp() throws BGPDocumentedException {
77 if (Epoll.isAvailable()) {
78 this.boss = new EpollEventLoopGroup();
79 this.worker = new EpollEventLoopGroup();
81 this.boss = new NioEventLoopGroup();
82 this.worker = new NioEventLoopGroup();
84 this.registry = new StrictBGPPeerRegistry();
85 this.clientListener = new SimpleSessionListener();
86 final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
87 this.serverDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker);
91 static <T extends Future> void waitFutureSuccess(final T future) throws InterruptedException {
92 final CountDownLatch latch = new CountDownLatch(1);
93 future.addListener(future1 -> latch.countDown());
94 Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
98 public static void checkIdleState (final SimpleSessionListener listener){
99 Stopwatch sw = Stopwatch.createStarted();
100 while(sw.elapsed(TimeUnit.SECONDS) <= 10) {
101 if (BGPSessionImpl.State.IDLE != listener.getState()){
102 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
110 private void configureClient(final BGPExtensionProviderContext ctx) {
111 final InetSocketAddress clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
112 final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(clientAddress.getAddress().getHostAddress()));
113 this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(clientAddress));
114 this.clientDispatcher = new TestClientDispatcher(this.boss, this.worker, ctx.getMessageRegistry(), clientAddress);
117 private Channel createServer(final InetSocketAddress serverAddress) throws InterruptedException {
118 this.serverListener = new SimpleSessionListener();
119 this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())), this.serverListener, createPreferences(serverAddress));
120 LoggerFactory.getLogger(BGPDispatcherImplTest.class).info("createServer");
121 final ChannelFuture future = this.serverDispatcher.createServer(this.registry, serverAddress);
122 future.addListener(new GenericFutureListener<Future<Void>>() {
124 public void operationComplete(final Future<Void> future) {
125 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", future.cause());
128 waitFutureSuccess(future);
129 return future.channel();
133 public void tearDown() throws Exception {
134 this.serverDispatcher.close();
135 this.registry.close();
136 this.worker.shutdownGracefully().awaitUninterruptibly();
137 this.boss.shutdownGracefully().awaitUninterruptibly();
141 public void testCreateClient() throws InterruptedException, ExecutionException {
142 final InetSocketAddress serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
143 final Channel serverChannel = createServer(serverAddress);
145 final Future<BGPSessionImpl> futureClient = this.clientDispatcher.createClient(serverAddress, this.registry, 2, Optional.absent());
146 waitFutureSuccess(futureClient);
147 final BGPSessionImpl session = futureClient.get();
148 Assert.assertEquals(BGPSessionImpl.State.UP, this.clientListener.getState());
149 Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
150 Assert.assertEquals(AS_NUMBER, session.getAsNumber());
151 Assert.assertEquals(Sets.newHashSet(IPV_4_TT), session.getAdvertisedTableTypes());
152 Assert.assertTrue(serverChannel.isWritable());
154 this.serverListener.releaseConnection();
155 checkIdleState(this.clientListener);
156 checkIdleState(this.serverListener);
160 public void testCreateReconnectingClient() throws Exception {
161 final InetSocketAddress serverAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
162 final Future<Void> future = this.clientDispatcher.createReconnectingClient(serverAddress, this.registry, RETRY_TIMER, Optional.absent());
163 waitFutureSuccess(future);
164 final Channel serverChannel = createServer(serverAddress);
165 Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
166 Assert.assertTrue(serverChannel.isWritable());
168 this.serverListener.releaseConnection();
169 checkIdleState(this.serverListener);
172 private BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
173 final List<BgpParameters> tlvs = Lists.newArrayList();
174 final List<OptionalCapabilities> capas = Lists.newArrayList();
175 capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
176 CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
177 .setAfi(IPV_4_TT.getAfi()).setSafi(IPV_4_TT.getSafi()).build()).build())
178 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
180 capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
181 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
182 final BgpId bgpId = new BgpId(new Ipv4Address(socketAddress.getAddress().getHostAddress()));
183 return new BGPSessionPreferences(AS_NUMBER, HOLD_TIMER, bgpId, AS_NUMBER, tlvs, Optional.absent());