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