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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
57 import org.slf4j.LoggerFactory;
59 public class BGPDispatcherImplTest {
61 private static final AsNumber AS_NUMBER = new AsNumber(30L);
62 private static final int RETRY_TIMER = 1;
63 private static final BgpTableType IPV_4_TT = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
64 private BGPDispatcherImpl serverDispatcher;
65 private TestClientDispatcher clientDispatcher;
66 private BGPPeerRegistry registry;
67 private SimpleSessionListener clientListener;
68 private SimpleSessionListener serverListener;
69 private EventLoopGroup boss;
70 private EventLoopGroup worker;
73 public void setUp() throws BGPDocumentedException {
74 if (Epoll.isAvailable()) {
75 this.boss = new EpollEventLoopGroup();
76 this.worker = new EpollEventLoopGroup();
78 this.boss = new NioEventLoopGroup();
79 this.worker = new NioEventLoopGroup();
81 this.registry = new StrictBGPPeerRegistry();
82 this.clientListener = new SimpleSessionListener();
83 final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
84 this.serverDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker);
88 private static <T extends Future> void waitFutureSuccess(final T future) throws InterruptedException {
89 final CountDownLatch latch = new CountDownLatch(1);
90 future.addListener(future1 -> latch.countDown());
91 Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
95 public static void checkIdleState (final SimpleSessionListener listener){
96 Stopwatch sw = Stopwatch.createStarted();
97 while(sw.elapsed(TimeUnit.SECONDS) <= 10) {
98 if (BGPSessionImpl.State.IDLE != listener.getState()){
99 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
107 private void configureClient(final BGPExtensionProviderContext ctx) {
108 final InetSocketAddress clientAddress = new InetSocketAddress("127.0.11.0", 1791);
109 final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(clientAddress.getAddress().getHostAddress()));
110 this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(clientAddress));
111 this.clientDispatcher = new TestClientDispatcher(this.boss, this.worker, ctx.getMessageRegistry(), clientAddress);
114 private Channel createServer(final InetSocketAddress serverAddress) throws InterruptedException {
115 this.serverListener = new SimpleSessionListener();
116 this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())), this.serverListener, createPreferences(serverAddress));
117 LoggerFactory.getLogger(BGPDispatcherImplTest.class).info("createServer");
118 final ChannelFuture future = this.serverDispatcher.createServer(this.registry, serverAddress);
119 future.addListener(new GenericFutureListener<Future<Void>>() {
121 public void operationComplete(final Future<Void> future) {
122 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", future.cause());
125 waitFutureSuccess(future);
126 return future.channel();
130 public void tearDown() throws Exception {
131 this.serverDispatcher.close();
132 this.registry.close();
133 this.worker.shutdownGracefully().awaitUninterruptibly();
134 this.boss.shutdownGracefully().awaitUninterruptibly();
138 public void testCreateClient() throws InterruptedException, ExecutionException {
139 final InetSocketAddress serverAddress = new InetSocketAddress("127.0.10.0", 1790);
140 final Channel serverChannel = createServer(serverAddress);
142 final Future<BGPSessionImpl> futureClient = this.clientDispatcher.createClient(serverAddress, this.registry, 2, Optional.absent());
143 waitFutureSuccess(futureClient);
144 final BGPSessionImpl session = futureClient.get();
145 Assert.assertEquals(BGPSessionImpl.State.UP, this.clientListener.getState());
146 Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
147 Assert.assertEquals(AS_NUMBER, session.getAsNumber());
148 Assert.assertEquals(Sets.newHashSet(IPV_4_TT), session.getAdvertisedTableTypes());
149 Assert.assertTrue(serverChannel.isWritable());
152 checkIdleState(this.clientListener);
153 checkIdleState(this.serverListener);
157 public void testCreateReconnectingClient() throws Exception {
158 final InetSocketAddress serverAddress = new InetSocketAddress("127.0.20.0", 1792);
159 final Future<Void> future = this.clientDispatcher.createReconnectingClient(serverAddress, this.registry, RETRY_TIMER, Optional.absent());
160 waitFutureSuccess(future);
161 final Channel serverChannel = createServer(serverAddress);
162 Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
163 Assert.assertTrue(serverChannel.isWritable());
165 this.serverListener.releaseConnection();
166 checkIdleState(this.serverListener);
169 private BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
170 final List<BgpParameters> tlvs = Lists.newArrayList();
171 final List<OptionalCapabilities> capas = Lists.newArrayList();
172 capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
173 CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
174 .setAfi(IPV_4_TT.getAfi()).setSafi(IPV_4_TT.getSafi()).build()).build())
175 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
177 capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
178 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
179 final BgpId bgpId = new BgpId(new Ipv4Address(socketAddress.getAddress().getHostAddress()));
180 return new BGPSessionPreferences(AS_NUMBER, (short) 4, bgpId, AS_NUMBER, tlvs, Optional.absent());