BUG-4692: Migrate TCP-MD5 support in bgp package to netty's native-epoll
[bgpcep.git] / bgp / openconfig-impl / src / test / java / org / opendaylight / protocol / bgp / openconfig / impl / openconfig / BGPNeighborProviderImplTest.java
1 /*
2  * Copyright (c) 2015 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.openconfig.impl.openconfig;
10
11 import static org.junit.Assert.assertEquals;
12
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Lists;
15 import java.math.BigDecimal;
16 import java.util.Collections;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
22 import org.opendaylight.protocol.bgp.openconfig.impl.spi.BGPConfigHolder;
23 import org.opendaylight.protocol.bgp.openconfig.impl.spi.BGPConfigStateStore;
24 import org.opendaylight.protocol.bgp.openconfig.spi.InstanceConfigurationIdentifier;
25 import org.opendaylight.protocol.bgp.openconfig.spi.pojo.BGPPeerInstanceConfiguration;
26 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.AfiSafisBuilder;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.ConfigBuilder;
31 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.RouteReflectorBuilder;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.TimersBuilder;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.TransportBuilder;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborBuilder;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.NeighborKey;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.PeerType;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.FlowspecSubsequentAddressFamily;
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.openconfig.extensions.rev160614.IPV4FLOW;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.IPV6FLOW;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
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.Ipv6AddressFamily;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.BgpPeer;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.ModuleKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
52
53 public class BGPNeighborProviderImplTest {
54
55     private BGPNeighborProviderImpl neighborProvider;
56     private static final String PASSWORD = "Ug1Yp4Ssw0Rd";
57     private static final IpAddress IP = new IpAddress(new Ipv4Address("1.2.3.4"));
58     private static final PortNumber PORT = new PortNumber(123);
59     private static final short TIMER = (short) 10;
60     private static final AsNumber AS = new AsNumber(10L);
61
62     @SuppressWarnings("unchecked")
63     @Before
64     public void setUp() throws Exception {
65         final BindingTransactionChain txChain = Mockito.mock(BindingTransactionChain.class);
66         final BGPConfigStateStore stateHolders = Mockito.mock(BGPConfigStateStore.class);
67         final BGPConfigHolder<Neighbor> configHolder = Mockito.mock(BGPConfigHolder.class);
68         Mockito.doReturn(configHolder).when(stateHolders).getBGPConfigHolder(Mockito.any(Class.class));
69         this.neighborProvider = new BGPNeighborProviderImpl(txChain, stateHolders);
70     }
71
72     @Test
73     public void testCreateModuleKey() {
74         assertEquals(new ModuleKey("instanceName", BgpPeer.class), this.neighborProvider.createModuleKey("instanceName"));
75     }
76
77     @Test
78     public void testGetInstanceConfigurationType() {
79         assertEquals(BGPPeerInstanceConfiguration.class, this.neighborProvider.getInstanceConfigurationType());
80     }
81
82     @Test
83     public void testApply() {
84         final boolean active = false;
85         final PeerRole role = PeerRole.RrClient;
86         final Neighbor neighbor = this.neighborProvider.apply(createConfiguration(new InstanceConfigurationIdentifier("instanceName"),
87             IP, PORT, TIMER, role, active,
88             Lists.<BgpTableType>newArrayList(new BgpTableTypeImpl(Ipv6AddressFamily.class, FlowspecSubsequentAddressFamily.class)),
89             AS, Optional.<Rfc2385Key>absent()));
90         final Neighbor expectedNeighbor = createNeighbor(Lists.<AfiSafi>newArrayList(new AfiSafiBuilder().setAfiSafiName(IPV6FLOW.class).build()),
91             IP, null, AS, PeerType.INTERNAL, role, TIMER, !active);
92         assertEquals(expectedNeighbor, neighbor);
93     }
94
95     @Test
96     public void testApply2() {
97         final boolean active = false;
98         final PeerRole role = PeerRole.Ebgp;
99         final Neighbor neighbor = this.neighborProvider.apply(createConfiguration(new InstanceConfigurationIdentifier("instanceName"),
100             IP, PORT, TIMER, role, active,
101             Lists.<BgpTableType>newArrayList(new BgpTableTypeImpl(Ipv6AddressFamily.class, FlowspecSubsequentAddressFamily.class)),
102             AS, Optional.of(new Rfc2385Key(PASSWORD)) ));
103         final Neighbor expectedNeighbor = createNeighbor(Lists.<AfiSafi>newArrayList(new AfiSafiBuilder().setAfiSafiName(IPV6FLOW.class).build()),
104             IP, PASSWORD, AS, PeerType.EXTERNAL, role, TIMER, !active);
105         assertEquals(expectedNeighbor, neighbor);
106     }
107
108     @Test
109     public void testApply3() {
110         final boolean active = true;
111         final PeerRole role = PeerRole.Internal;
112         final Neighbor neighbor = this.neighborProvider.apply(createConfiguration(new InstanceConfigurationIdentifier("instanceName"),
113             IP, PORT, TIMER, role, active,
114             Lists.<BgpTableType>newArrayList(new BgpTableTypeImpl(Ipv4AddressFamily.class, FlowspecSubsequentAddressFamily.class)),
115             AS, Optional.of(new Rfc2385Key(PASSWORD)) ));
116         final Neighbor expectedNeighbor = createNeighbor(Lists.<AfiSafi>newArrayList(new AfiSafiBuilder().setAfiSafiName(IPV4FLOW.class).build()),
117             IP, PASSWORD, AS, null, role, TIMER, !active);
118         assertEquals(expectedNeighbor, neighbor);
119     }
120
121     private BGPPeerInstanceConfiguration createConfiguration(final InstanceConfigurationIdentifier confId, final IpAddress ip, final PortNumber port,
122         final short holdTimer, final PeerRole role, final boolean active, final List<BgpTableType> advertized, final AsNumber as, final Optional<Rfc2385Key> passwd) {
123         return new BGPPeerInstanceConfiguration(confId, ip, port, holdTimer, role, active, advertized, as, passwd, Collections.emptyList());
124     }
125
126     private Neighbor createNeighbor(final List<AfiSafi> families, final IpAddress ip, final String passwd, final AsNumber as, final PeerType peerType, final PeerRole role, final short timer, final boolean passive) {
127         return new NeighborBuilder()
128         .setAfiSafis(new AfiSafisBuilder().setAfiSafi(families).build())
129         .setNeighborAddress(ip)
130         .setKey(new NeighborKey(ip))
131         .setConfig(new ConfigBuilder().setAuthPassword(passwd).setPeerAs(as).setPeerType(peerType).build())
132         .setRouteReflector(new RouteReflectorBuilder().setConfig(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.route.reflector.ConfigBuilder().setRouteReflectorClient(role == PeerRole.RrClient).build()).build())
133         .setTimers(new TimersBuilder().setConfig(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.timers.ConfigBuilder().setHoldTime(BigDecimal.valueOf(timer)).build()).build())
134         .setTransport(new TransportBuilder().setConfig(new org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.transport.ConfigBuilder().setPassiveMode(passive).build()).build())
135         .build();
136     }
137 }