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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Stopwatch;
13 import com.google.common.collect.Lists;
14 import com.google.common.util.concurrent.Uninterruptibles;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.epoll.Epoll;
19 import io.netty.channel.epoll.EpollEventLoopGroup;
20 import io.netty.channel.nio.NioEventLoopGroup;
21 import io.netty.util.concurrent.Future;
22 import io.netty.util.concurrent.GenericFutureListener;
23 import java.net.InetSocketAddress;
24 import java.util.List;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import org.junit.After;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
31 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
32 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
33 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
34 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
35 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
37 import org.opendaylight.protocol.util.InetSocketAddressUtil;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
54 import org.slf4j.LoggerFactory;
56 public class AbstractBGPDispatcherTest {
57 protected static final AsNumber AS_NUMBER = new AsNumber(30L);
58 protected static final int RETRY_TIMER = 1;
59 protected static final BgpTableType IPV_4_TT = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
60 private static final short HOLD_TIMER = 30;
61 protected TestClientDispatcher clientDispatcher;
62 protected BGPPeerRegistry registry;
63 protected SimpleSessionListener clientListener;
64 protected BGPDispatcherImpl serverDispatcher;
65 protected SimpleSessionListener serverListener;
66 private EventLoopGroup boss;
67 private EventLoopGroup worker;
70 public void setUp() throws BGPDocumentedException {
71 if (Epoll.isAvailable()) {
72 this.boss = new EpollEventLoopGroup();
73 this.worker = new EpollEventLoopGroup();
75 this.boss = new NioEventLoopGroup();
76 this.worker = new NioEventLoopGroup();
78 this.registry = new StrictBGPPeerRegistry();
79 this.clientListener = new SimpleSessionListener();
80 this.serverListener = new SimpleSessionListener();
81 final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
82 this.serverDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker);
87 public void tearDown() throws Exception {
88 this.serverDispatcher.close();
89 this.registry.close();
90 this.worker.shutdownGracefully().awaitUninterruptibly();
91 this.boss.shutdownGracefully().awaitUninterruptibly();
95 private void configureClient(final BGPExtensionProviderContext ctx) {
96 final InetSocketAddress clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
97 final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(clientAddress.getAddress().getHostAddress()));
98 this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(clientAddress));
99 this.clientDispatcher = new TestClientDispatcher(this.boss, this.worker, ctx.getMessageRegistry(), clientAddress);
102 protected BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
103 final List<BgpParameters> tlvs = Lists.newArrayList();
104 final List<OptionalCapabilities> capas = Lists.newArrayList();
105 capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
106 CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
107 .setAfi(IPV_4_TT.getAfi()).setSafi(IPV_4_TT.getSafi()).build()).build())
108 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
110 capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
111 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
112 final BgpId bgpId = new BgpId(new Ipv4Address(socketAddress.getAddress().getHostAddress()));
113 return new BGPSessionPreferences(AS_NUMBER, HOLD_TIMER, bgpId, AS_NUMBER, tlvs, Optional.absent());
116 protected static <T extends Future> void waitFutureSuccess(final T future) {
117 final CountDownLatch latch = new CountDownLatch(1);
118 future.addListener(future1 -> latch.countDown());
119 Uninterruptibles.awaitUninterruptibly(latch, 10, TimeUnit.SECONDS);
122 public static void checkIdleState(final SimpleSessionListener listener) {
123 Stopwatch sw = Stopwatch.createStarted();
124 while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
125 if (BGPSessionImpl.State.IDLE != listener.getState()) {
126 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
134 protected Channel createServer(final InetSocketAddress serverAddress) throws InterruptedException {
135 this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())), this.serverListener, createPreferences(serverAddress));
136 LoggerFactory.getLogger(AbstractBGPDispatcherTest.class).info("createServer");
137 final ChannelFuture future = this.serverDispatcher.createServer(this.registry, serverAddress);
138 future.addListener(new GenericFutureListener<Future<Void>>() {
140 public void operationComplete(final Future<Void> future) {
141 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", future.cause());
144 waitFutureSuccess(future);
145 return future.channel();