Mass-convert all compontents to use -no-zone addresses
[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.IetfInetUtil;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapabilityBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49 import org.slf4j.LoggerFactory;
50
51 public class AbstractBGPDispatcherTest {
52     protected static final AsNumber AS_NUMBER = new AsNumber(Uint32.valueOf(30));
53     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;
65
66     @Before
67     public void setUp() {
68         if (!Epoll.isAvailable()) {
69             this.boss = new NioEventLoopGroup();
70             this.worker = new NioEventLoopGroup();
71         }
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);
77
78         this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
79         final IpAddressNoZone clientPeerIp = new IpAddressNoZone(new Ipv4AddressNoZone(
80             this.clientAddress.getAddress().getHostAddress()));
81         this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(this.clientAddress));
82         this.clientDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker, this.registry);
83     }
84
85     @After
86     public void tearDown() throws Exception {
87         this.serverDispatcher.close();
88         this.registry.close();
89         if (!Epoll.isAvailable()) {
90             this.worker.shutdownGracefully(0, 0, TimeUnit.SECONDS);
91             this.boss.shutdownGracefully(0, 0, TimeUnit.SECONDS);
92         }
93     }
94
95     protected BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
96         final List<BgpParameters> tlvs = new ArrayList<>();
97         final List<OptionalCapabilities> capas = new ArrayList<>();
98         capas.add(new OptionalCapabilitiesBuilder()
99             .setCParameters(new CParametersBuilder()
100                 .addAugmentation(CParameters1.class, new CParameters1Builder()
101                         .setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
102                                 .setAfi(IPV_4_TT.getAfi())
103                                 .setSafi(IPV_4_TT.getSafi())
104                                 .build())
105                         .build())
106                 .setAs4BytesCapability(new As4BytesCapabilityBuilder()
107                     .setAsNumber(new AsNumber(Uint32.valueOf(30)))
108                     .build())
109                 .build())
110             .build());
111         capas.add(new OptionalCapabilitiesBuilder()
112                 .setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
113         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
114         final BgpId bgpId = new BgpId(IetfInetUtil.INSTANCE.ipv4AddressNoZoneFor(socketAddress.getAddress()));
115         return new BGPSessionPreferences(AS_NUMBER, HOLD_TIMER, bgpId, AS_NUMBER, tlvs);
116     }
117
118     Channel createServer(final InetSocketAddress serverAddress) {
119         this.registry.addPeer(new IpAddressNoZone(new Ipv4AddressNoZone(serverAddress.getAddress().getHostAddress())),
120                 this.serverListener, createPreferences(serverAddress));
121         LoggerFactory.getLogger(AbstractBGPDispatcherTest.class).info("createServer");
122         final ChannelFuture future = this.serverDispatcher.createServer(serverAddress);
123         future.addListener(future1 -> Preconditions.checkArgument(future1.isSuccess(),
124             "Unable to start bgp server on %s", future1.cause()));
125         waitFutureSuccess(future);
126         return future.channel();
127     }
128 }