BUG-7706: Fix ServiceUnavailableException
[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.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;
54
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,
59         UnicastSubsequentAddressFamily.class);
60     private static final short HOLD_TIMER = 30;
61     protected BGPDispatcherImpl clientDispatcher;
62     protected BGPPeerRegistry registry;
63     protected SimpleSessionListener clientListener;
64     protected BGPDispatcherImpl serverDispatcher;
65     protected SimpleSessionListener serverListener;
66     protected InetSocketAddress clientAddress;
67     private EventLoopGroup boss;
68     private EventLoopGroup worker;
69
70     @Before
71     public void setUp() throws BGPDocumentedException {
72         if (!Epoll.isAvailable()) {
73             this.boss = new NioEventLoopGroup();
74             this.worker = new NioEventLoopGroup();
75         }
76         this.registry = new StrictBGPPeerRegistry();
77         this.clientListener = new SimpleSessionListener();
78         this.serverListener = new SimpleSessionListener();
79         final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
80         this.serverDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker, this.registry);
81
82         this.clientAddress = InetSocketAddressUtil.getRandomLoopbackInetSocketAddress();
83         final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(this.clientAddress.getAddress().getHostAddress()));
84         this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(this.clientAddress));
85         this.clientDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), this.boss, this.worker, this.registry);
86     }
87
88     @After
89     public void tearDown() throws Exception {
90         this.serverDispatcher.close();
91         this.registry.close();
92         if (!Epoll.isAvailable()) {
93             this.worker.shutdownGracefully(0, 0, TimeUnit.SECONDS);
94             this.boss.shutdownGracefully(0, 0, TimeUnit.SECONDS);
95         }
96     }
97
98     protected BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
99         final List<BgpParameters> tlvs = Lists.newArrayList();
100         final List<OptionalCapabilities> capas = Lists.newArrayList();
101         capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
102             CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
103                 .setAfi(IPV_4_TT.getAfi()).setSafi(IPV_4_TT.getSafi()).build()).build())
104             .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
105             .build()).build());
106         capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
107         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
108         final BgpId bgpId = new BgpId(new Ipv4Address(socketAddress.getAddress().getHostAddress()));
109         return new BGPSessionPreferences(AS_NUMBER, HOLD_TIMER, bgpId, AS_NUMBER, tlvs, Optional.absent());
110     }
111
112     protected static void checkIdleState(final SimpleSessionListener listener) {
113         final Stopwatch sw = Stopwatch.createStarted();
114         while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
115             if (State.IDLE != listener.getState()) {
116                 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
117             } else {
118                 return;
119             }
120         }
121         Assert.fail();
122     }
123
124     protected Channel createServer(final InetSocketAddress serverAddress) throws InterruptedException {
125         this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())),
126             this.serverListener, createPreferences(serverAddress));
127         LoggerFactory.getLogger(AbstractBGPDispatcherTest.class).info("createServer");
128         final ChannelFuture future = this.serverDispatcher.createServer(serverAddress);
129         future.addListener(future1 -> Preconditions.checkArgument(future1.isSuccess(),
130             "Unable to start bgp server on %s", future1.cause()));
131         waitFutureSuccess(future);
132         return future.channel();
133     }
134 }