BUG-6038: Fix race condition when Open message...
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BGPDispatcherImplTest.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
9 package org.opendaylight.protocol.bgp.rib.impl;
10
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.nio.NioEventLoopGroup;
18 import io.netty.util.concurrent.Future;
19 import io.netty.util.concurrent.GenericFutureListener;
20 import java.net.InetSocketAddress;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
28 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
29 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
30 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
31 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
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
50 public class BGPDispatcherImplTest {
51
52     private static final AsNumber AS_NUMBER = new AsNumber(30L);
53     private static final int RETRY_TIMER = 10;
54     private static final BgpTableType IPV_4_TT = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
55     private BGPDispatcherImpl serverDispatcher;
56     private TestClientDispatcher clientDispatcher;
57     private BGPPeerRegistry registry;
58     private SimpleSessionListener clientListener;
59     private SimpleSessionListener serverListener;
60
61     @Before
62     public void setUp() throws BGPDocumentedException {
63         this.registry = new StrictBGPPeerRegistry();
64         this.clientListener = new SimpleSessionListener();
65         final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
66         this.serverDispatcher = new BGPDispatcherImpl(ctx.getMessageRegistry(), new NioEventLoopGroup(), new NioEventLoopGroup());
67         configureClient(ctx);
68     }
69
70     private void configureClient(final BGPExtensionProviderContext ctx) {
71         final InetSocketAddress clientAddress = new InetSocketAddress("127.0.11.0", 1791);
72         final IpAddress clientPeerIp = new IpAddress(new Ipv4Address(clientAddress.getAddress().getHostAddress()));
73         this.registry.addPeer(clientPeerIp, this.clientListener, createPreferences(clientAddress));
74         this.clientDispatcher = new TestClientDispatcher(new NioEventLoopGroup(), new NioEventLoopGroup(), ctx.getMessageRegistry(), clientAddress);
75     }
76
77     private Channel createServer(final InetSocketAddress serverAddress) throws InterruptedException {
78         this.serverListener = new SimpleSessionListener();
79         this.registry.addPeer(new IpAddress(new Ipv4Address(serverAddress.getAddress().getHostAddress())), this.serverListener, createPreferences(serverAddress));
80         final ChannelFuture future = this.serverDispatcher.createServer(this.registry, serverAddress);
81         future.addListener(new GenericFutureListener<Future<Void>>() {
82             @Override
83             public void operationComplete(final Future<Void> future) {
84                 Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", future.cause());
85             }
86         });
87         return future.channel();
88     }
89
90     @After
91     public void tearDown() throws Exception {
92         this.serverDispatcher.close();
93         this.registry.close();
94     }
95
96     @Test
97     public void testCreateClient() throws InterruptedException, ExecutionException {
98         final InetSocketAddress serverAddress = new InetSocketAddress("127.0.10.0", 1790);
99         final Channel serverChannel = createServer(serverAddress);
100         Thread.sleep(1000);
101         final BGPSessionImpl session = this.clientDispatcher.createClient(serverAddress, this.registry, 0, Optional.absent()).get();
102         Thread.sleep(3000);
103         Assert.assertEquals(BGPSessionImpl.State.UP, this.clientListener.getState());
104         Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
105         Assert.assertEquals(AS_NUMBER, session.getAsNumber());
106         Assert.assertEquals(Sets.newHashSet(IPV_4_TT), session.getAdvertisedTableTypes());
107         Assert.assertTrue(serverChannel.isWritable());
108         session.close();
109
110         Thread.sleep(3000);
111         Assert.assertEquals(BGPSessionImpl.State.IDLE, this.clientListener.getState());
112         Assert.assertEquals(BGPSessionImpl.State.IDLE, this.serverListener.getState());
113     }
114
115     @Test
116     public void testCreateReconnectingClient() throws Exception {
117         final InetSocketAddress serverAddress = new InetSocketAddress("127.0.20.0", 1792);
118         final Channel serverChannel = createServer(serverAddress);
119         Thread.sleep(1000);
120         final Future<Void> future = this.clientDispatcher.createReconnectingClient(serverAddress, this.registry, RETRY_TIMER, Optional.absent());
121         Thread.sleep(3000);
122         Assert.assertEquals(BGPSessionImpl.State.UP, this.serverListener.getState());
123         Assert.assertTrue(serverChannel.isWritable());
124         future.cancel(true);
125         this.serverListener.releaseConnection();
126         Thread.sleep(3000);
127         Assert.assertEquals(BGPSessionImpl.State.IDLE, this.serverListener.getState());
128     }
129
130     private BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
131         final List<BgpParameters> tlvs = Lists.newArrayList();
132         final List<OptionalCapabilities> capas = Lists.newArrayList();
133         capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
134             CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
135                 .setAfi(IPV_4_TT.getAfi()).setSafi(IPV_4_TT.getSafi()).build()).build())
136             .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
137             .build()).build());
138         capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
139         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
140         return new BGPSessionPreferences(AS_NUMBER, (short) 4, new Ipv4Address(socketAddress.getAddress().getHostAddress()), AS_NUMBER, tlvs);
141     }
142
143 }