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