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