b24fdd7b7454bb45cb59b70b2d2cbfdff131df8a
[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.collect.Lists;
13 import com.google.common.collect.Sets;
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.EventLoopGroup;
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.pojo.ServiceLoaderBGPExtensionProviderContext;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
48
49 public class BGPDispatcherImplTest {
50
51     private static final InetSocketAddress ADDRESS = new InetSocketAddress("127.0.10.0", 1790);
52     private static final InetSocketAddress CLIENT_ADDRESS = new InetSocketAddress("127.0.11.0", 1791);
53     private static final InetSocketAddress CLIENT_ADDRESS2 = new InetSocketAddress("127.0.12.0", 1792);
54     private static final AsNumber AS_NUMBER = new AsNumber(30L);
55     private static final int TIMEOUT = 5000;
56     private static final int RETRY_TIMER = 10;
57
58     private final BgpTableType ipv4tt = new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
59
60     private BGPDispatcherImpl dispatcher;
61     private TestClientDispatcher clientDispatcher;
62
63     private BGPPeerRegistry registry;
64
65     private Channel channel;
66
67     @Before
68     public void setUp() throws BGPDocumentedException {
69         final EventLoopGroup group = new NioEventLoopGroup();
70         this.registry = new StrictBGPPeerRegistry();
71         this.registry.addPeer(new IpAddress(new Ipv4Address(CLIENT_ADDRESS.getAddress().getHostAddress())),
72                 new SimpleSessionListener(), createPreferences(CLIENT_ADDRESS));
73         this.registry.addPeer(new IpAddress(new Ipv4Address(ADDRESS.getAddress().getHostAddress())),
74                 new SimpleSessionListener(), createPreferences(ADDRESS));
75         this.dispatcher = new BGPDispatcherImpl(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry(), group, group);
76         this.clientDispatcher = new TestClientDispatcher(group, group, ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry(),
77                 CLIENT_ADDRESS);
78
79         final ChannelFuture future = this.dispatcher.createServer(this.registry, ADDRESS);
80         future.addListener(new GenericFutureListener<Future<Void>>() {
81             @Override
82             public void operationComplete(final Future<Void> future) {
83                 if(!future.isSuccess()) {
84                     Assert.fail("Failed to create server.");
85                 }
86             }
87         });
88         this.channel = future.channel();
89     }
90
91     @Test
92     public void testCreateClient() throws InterruptedException, ExecutionException {
93         final BGPSessionImpl session = this.clientDispatcher.createClient(ADDRESS, this.registry,
94                 0, Optional.<InetSocketAddress>absent()).get();
95         Assert.assertEquals(BGPSessionImpl.State.UP, session.getState());
96         Assert.assertEquals(AS_NUMBER, session.getAsNumber());
97         Assert.assertEquals(Sets.newHashSet(this.ipv4tt), session.getAdvertisedTableTypes());
98         session.close();
99     }
100
101     @After
102     public void tearDown() throws Exception {
103         this.channel.close().get();
104         this.dispatcher.close();
105         this.registry.close();
106     }
107
108     @Test
109     public void testCreateReconnectingClient() throws InterruptedException, ExecutionException {
110         final SimpleSessionListener listener = new SimpleSessionListener();
111         this.registry.addPeer(new IpAddress(new Ipv4Address(CLIENT_ADDRESS2.getAddress().getHostAddress())), listener, createPreferences(CLIENT_ADDRESS2));
112         final Future<Void> cf = this.clientDispatcher.createReconnectingClient(CLIENT_ADDRESS2, this.registry,
113                 RETRY_TIMER, Optional.<InetSocketAddress>absent());
114         final Channel channel2 = this.dispatcher.createServer(this.registry, CLIENT_ADDRESS2).channel();
115         Thread.sleep(1000);
116         Assert.assertTrue(listener.up);
117         Assert.assertTrue(channel2.isActive());
118         cf.cancel(true);
119         listener.releaseConnection();
120     }
121
122     private BGPSessionPreferences createPreferences(final InetSocketAddress socketAddress) {
123         final List<BgpParameters> tlvs = Lists.newArrayList();
124         final List<OptionalCapabilities> capas = Lists.newArrayList();
125         capas.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
126             CParameters1.class, new CParameters1Builder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder()
127                 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build())
128             .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build())
129             .build()).build());
130         capas.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
131         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
132         return new BGPSessionPreferences(AS_NUMBER, (short) 4, new Ipv4Address(socketAddress.getAddress().getHostAddress()), AS_NUMBER, tlvs);
133     }
134
135 }