e447ea66edcf621317598aeafca14f6fe5cde63d
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / AbstractBGPDispatcherTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.epoll.Epoll;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import java.net.InetSocketAddress;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.TimeUnit;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
25 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
26 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
27 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
28 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
29 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
30 import org.opendaylight.protocol.util.InetSocketAddressUtil;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapabilityBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
47 import org.opendaylight.yangtools.yang.common.Uint32;
48 import org.slf4j.LoggerFactory;
49
50 public class AbstractBGPDispatcherTest {
51     protected static final AsNumber AS_NUMBER = new AsNumber(Uint32.valueOf(30));
52     static final int RETRY_TIMER = 1;
53     protected static final BgpTableType IPV_4_TT = new BgpTableTypeImpl(Ipv4AddressFamily.class,
54         UnicastSubsequentAddressFamily.class);
55     private static final short HOLD_TIMER = 30;
56     protected BGPDispatcherImpl clientDispatcher;
57     protected BGPPeerRegistry registry;
58     protected SimpleSessionListener clientListener;
59     protected BGPDispatcherImpl serverDispatcher;
60     protected SimpleSessionListener serverListener;
61     protected InetSocketAddress clientAddress;
62     private EventLoopGroup boss;
63     private EventLoopGroup worker;
64
65     @Before
66     public void setUp() {
67         if (!Epoll.isAvailable()) {
68             this.boss = new NioEventLoopGroup();
69             this.worker = new NioEventLoopGroup();
70         }
71         this.registry = new StrictBGPPeerRegistry();
72         this.clientListener = new SimpleSessionListener();
73         this.serverListener = new SimpleSessionListener();
74         final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
75         this.serverDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker, this.registry);
76
77         this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
78         final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(this.clientAddress.getAddress().getHostAddress()));
79         this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(this.clientAddress));
80         this.clientDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker, this.registry);
81     }
82
83     @After
84     public void tearDown() throws Exception {
85         this.serverDispatcher.close();
86         this.registry.close();
87         if (!Epoll.isAvailable()) {
88             this.worker.shutdownGracefully(0, 0, TimeUnit.SECONDS);
89             this.boss.shutdownGracefully(0, 0, TimeUnit.SECONDS);
90         }
91     }
92
93     protected BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
94         final List<BgpParameters> tlvs = new ArrayList<>();
95         final List<OptionalCapabilities> capas = new ArrayList<>();
96         capas.add(new OptionalCapabilitiesBuilder()
97             .setCParameters(new CParametersBuilder()
98                 .addAugmentation(CParameters1.class, new CParameters1Builder()
99                         .setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
100                                 .setAfi(IPV_4_TT.getAfi())
101                                 .setSafi(IPV_4_TT.getSafi())
102                                 .build())
103                         .build())
104                 .setAs4BytesCapability(new As4BytesCapabilityBuilder()
105                     .setAsNumber(new AsNumber(Uint32.valueOf(30)))
106                     .build())
107                 .build())
108             .build());
109         capas.add(new OptionalCapabilitiesBuilder()
110                 .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);
114     }
115
116     Channel createServer(final InetSocketAddress serverAddress) {
117         this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())),
118                 this.serverListener, createPreferences(serverAddress));
119         LoggerFactory.getLogger(AbstractBGPDispatcherTest.class).info("createServer");
120         final ChannelFuture future = this.serverDispatcher.createServer(serverAddress);
121         future.addListener(future1 -> Preconditions.checkArgument(future1.isSuccess(),
122             "Unable to start bgp server on %s", future1.cause()));
123         waitFutureSuccess(future);
124         return future.channel();
125     }
126 }