Bug 5566: BGP listener TCP MD5 support is not working
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / StrictBGPPeerRegistryTest.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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.fail;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Lists;
17 import java.net.InetSocketAddress;
18 import java.util.Collections;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
24 import org.opendaylight.protocol.bgp.parser.BGPError;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
38
39 public class StrictBGPPeerRegistryTest {
40
41     private static final AsNumber LOCAL_AS = new AsNumber(1234L);
42     private static final AsNumber REMOTE_AS = new AsNumber(1235L);
43     private static final Ipv4Address FROM = new Ipv4Address("0.0.0.1");
44     private static final IpAddress REMOTE_IP = new IpAddress(FROM);
45     private static final Ipv4Address TO = new Ipv4Address("255.255.255.255");
46
47     private final BGPSessionListener peer1 = getMockSession();
48     private final Open classicOpen = createOpen(TO, LOCAL_AS);
49     private StrictBGPPeerRegistry peerRegistry;
50     private BGPSessionPreferences mockPreferences;
51
52     private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
53         final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
54         .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
55         .setCParameters(new CParametersBuilder()
56         .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build()).build()).build())).build());
57         return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
58     }
59
60     @Before
61     public void setUp() throws Exception {
62         this.peerRegistry = new StrictBGPPeerRegistry();
63         this.mockPreferences =  new BGPSessionPreferences(LOCAL_AS, 1, new BgpId("0.0.0.1"), LOCAL_AS, Collections.<BgpParameters> emptyList(),
64                 Optional.<byte[]>absent());
65     }
66
67     private static BGPSessionListener getMockSession() {
68         final BGPSessionListener mock = Mockito.mock(BGPSessionListener.class);
69         Mockito.doNothing().when(mock).releaseConnection();
70         return mock;
71     }
72
73     @Test
74     public void testIpAddressConstruction() throws Exception {
75         final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
76         final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
77         assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
78     }
79
80     @Test
81     public void testDuplicatePeerConnection() throws Exception {
82         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
83         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
84         try {
85             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
86         } catch (final BGPDocumentedException e) {
87             assertEquals(BGPError.CEASE, e.getError());
88             return;
89         }
90         fail("Same peer cannot be connected twice");
91     }
92
93     @Test
94     public void testPeerNotConfigured() throws Exception {
95         try {
96             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
97         } catch (final IllegalStateException e) {
98             return;
99         }
100         fail("Unknown peer cannot be connected");
101     }
102
103     @Test
104     public void testPeerConnectionSuccessfull() throws Exception {
105         final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
106         final IpAddress remoteIp2 = new IpAddress(to2);
107
108         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
109         final BGPSessionListener session2 = getMockSession();
110         this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
111
112         final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
113         assertSame(this.peer1, returnedSession1);
114         final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, FROM, to2, this.classicOpen);
115         assertSame(session2, returnedSession2);
116
117         Mockito.verifyZeroInteractions(this.peer1);
118         Mockito.verifyZeroInteractions(session2);
119     }
120
121     @Test
122     public void testDropSecondPeer() throws Exception {
123         final Ipv4Address higher = new Ipv4Address("192.168.200.200");
124         final Ipv4Address lower = new Ipv4Address("10.10.10.10");
125         final IpAddress remoteIp = new IpAddress(lower);
126
127         this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
128
129         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
130         try {
131             this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
132         } catch (final BGPDocumentedException e) {
133             assertEquals(BGPError.CEASE, e.getError());
134             return;
135         }
136         fail("Same peer cannot be connected twice");
137     }
138
139     @Test
140     public void testDropFirstPeer() throws Exception {
141         final Ipv4Address higher = new Ipv4Address("123.123.123.123");
142         final Ipv4Address lower = new Ipv4Address("123.123.123.122");
143         final IpAddress remoteIp = new IpAddress(lower);
144
145         this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
146
147         this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
148         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
149         Mockito.verify(this.peer1).releaseConnection();
150     }
151
152     @Test
153     public void testDuplicatePeersWDifferentIds() throws Exception {
154         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
155
156         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
157         try {
158             this.peerRegistry.getPeer(REMOTE_IP, TO, TO, this.classicOpen);
159         } catch (final BGPDocumentedException e) {
160             assertEquals(BGPError.CEASE, e.getError());
161             return;
162         }
163         fail("Same peer cannot be connected twice");
164     }
165
166     @Test
167     public void testDuplicatePeersHigherAs() throws Exception {
168         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
169
170         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
171         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, REMOTE_AS));
172         Mockito.verify(this.peer1).releaseConnection();
173     }
174
175     @Test
176     public void testDuplicatePeersLowerAs() throws Exception {
177         final AsNumber as2 = new AsNumber(3L);
178
179         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
180
181         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
182         try {
183             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
184         } catch (final BGPDocumentedException e) {
185             assertEquals(BGPError.CEASE, e.getError());
186             return;
187         }
188         fail("Same peer cannot be connected twice");
189     }
190
191     @Test
192     public void testAsMismatch() throws Exception {
193         final AsNumber as2 = new AsNumber(3L);
194
195         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
196         try {
197             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
198         } catch (final BGPDocumentedException e) {
199             assertEquals(BGPError.BAD_PEER_AS, e.getError());
200             return;
201         }
202         fail("Peer AS number mismatch");
203     }
204 }