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.collect.Lists;
14 import com.google.common.collect.Sets;
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.ExecutionException;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
32 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
33 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
34 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
35 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
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 // Disabling this test for now as it's failing frequently on jenkins, seems due to infrastructure issues. The
57 // tests succeed when run locally.
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 void configureClient(final BGPExtensionProviderContext ctx) {
89 final InetSocketAddress clientAddress = new InetSocketAddress("127.0.11.0", 1791);
90 final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(clientAddress.getAddress().getHostAddress()));
91 this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(clientAddress));
92 this.clientDispatcher = new TestClientDispatcher(this.boss, this.worker, ctx.getMessageRegistry(), clientAddress);
95 private Channel createServer(final InetSocketAddress serverAddress) throws InterruptedException {
96 this.serverListener = new SimpleSessionListener();
97 this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())), this.serverListener, createPreferences(serverAddress));
98 LoggerFactory.getLogger(BGPDispatcherImplTest.class).info("createServer");
99 final ChannelFuture future = this.serverDispatcher.createServer(this.registry, serverAddress);
100 future.addListener(new GenericFutureListener<Future<Void>>() {
102 public void operationComplete(final Future<Void> future) {
103 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", future.cause());
106 return future.channel();
110 public void tearDown() throws Exception {
111 this.serverDispatcher.close();
112 this.registry.close();
113 this.worker.shutdownGracefully().awaitUninterruptibly();
114 this.boss.shutdownGracefully().awaitUninterruptibly();
118 public void testCreateClient() throws InterruptedException, ExecutionException {
119 final InetSocketAddress serverAddress = new InetSocketAddress("127.0.10.0", 1790);
120 final Channel serverChannel = createServer(serverAddress);
122 final BGPSessionImpl session = this.clientDispatcher.createClient(serverAddress, this.registry, 0, Optional.absent()).get();
123 Assert.assertEquals(BGPSessionImpl.State.UP, this.clientListener.getState());
124 Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
125 Assert.assertEquals(AS_NUMBER, session.getAsNumber());
126 Assert.assertEquals(Sets.newHashSet(IPV_4_TT), session.getAdvertisedTableTypes());
127 Assert.assertTrue(serverChannel.isWritable());
131 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.clientListener.getState());
132 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.serverListener.getState());
136 public void testCreateReconnectingClient() throws Exception {
137 final InetSocketAddress serverAddress = new InetSocketAddress("127.0.20.0", 1792);
138 final Future<Void> future = this.clientDispatcher.createReconnectingClient(serverAddress, this.registry, RETRY_TIMER, Optional.absent());
139 final Channel serverChannel = createServer(serverAddress);
140 Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
141 Assert.assertTrue(serverChannel.isWritable());
143 this.serverListener.releaseConnection();
145 Assert.assertEquals(BGPSessionImpl.State.IDLE, this.serverListener.getState());
148 private BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
149 final List<BgpParameters> tlvs = Lists.newArrayList();
150 final List<OptionalCapabilities> capas = Lists.newArrayList();
151 capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
152 CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
153 .setAfi(IPV_4_TT.getAfi()).setSafi(IPV_4_TT.getSafi()).build()).build())
154 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
156 capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
157 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
158 return new BGPSessionPreferences(AS_NUMBER, (short) 4, new BgpId(socketAddress.getAddress().getHostAddress()), AS_NUMBER, tlvs);