32db6165bc79212339140910b55dfdbc674a9b14
[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 import com.google.common.collect.Lists;
15 import java.net.InetSocketAddress;
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.protocol.bgp.parser.BGPDocumentedException;
22 import org.opendaylight.protocol.bgp.parser.BGPError;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
25 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
36
37 public class StrictBGPPeerRegistryTest {
38
39     private StrictBGPPeerRegistry peerRegistry;
40     private BGPSessionPreferences mockPreferences;
41     private static final AsNumber AS1 = new AsNumber(1234L);
42
43     private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
44         final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
45             .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
46                 .setCParameters(new CParametersBuilder()
47                     .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build()).build()).build())).build());
48         return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
49     }
50
51     @Before
52     public void setUp() throws Exception {
53         this.peerRegistry = new StrictBGPPeerRegistry();
54         this.mockPreferences = getMockPreferences(AS1);
55     }
56
57     @Test
58     public void testIpAddressConstruction() throws Exception {
59         final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
60         final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
61         assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
62     }
63
64     @Test
65     public void testDuplicate() throws Exception {
66         final Ipv4Address from = new Ipv4Address("0.0.0.1");
67         final IpAddress remoteIp = new IpAddress(from);
68         final Ipv4Address to = new Ipv4Address("255.255.255.255");
69
70         final ReusableBGPPeer session1 = getMockSession();
71         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
72
73         this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
74         try {
75             this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
76         } catch (final BGPDocumentedException e) {
77             assertEquals(BGPError.CEASE, e.getError());
78             return;
79         }
80
81         fail("Same peer cannot be connected twice");
82     }
83
84     @Test
85     public void testNotAllowed() throws Exception {
86         final Ipv4Address from = new Ipv4Address("0.0.0.1");
87         final IpAddress remoteIp = new IpAddress(from);
88         final Ipv4Address to = new Ipv4Address("255.255.255.255");
89
90         try {
91             this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
92         } catch (final IllegalStateException e) {
93             return;
94         }
95         fail("Unknown peer cannot be connected");
96     }
97
98     @Test
99     public void testOk() throws Exception {
100         final Ipv4Address from = new Ipv4Address("0.0.0.1");
101
102         final Ipv4Address to = new Ipv4Address("255.255.255.255");
103         final IpAddress remoteIp = new IpAddress(to);
104         final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
105         final IpAddress remoteIp2 = new IpAddress(to2);
106
107         final ReusableBGPPeer session1 = getMockSession();
108         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
109         final ReusableBGPPeer session2 = getMockSession();
110         this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
111
112         final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
113         assertSame(session1, returnedSession1);
114         final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, from, to2, createOpen(to, AS1));
115         assertSame(session2, returnedSession2);
116
117         Mockito.verifyZeroInteractions(session1);
118         Mockito.verifyZeroInteractions(session2);
119     }
120
121     @Test
122     public void testDropSecond() 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         final ReusableBGPPeer session1 = getMockSession();
128         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
129
130         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
131         try {
132             this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
133         } catch (final BGPDocumentedException e) {
134             assertEquals(BGPError.CEASE, e.getError());
135             return;
136         }
137
138         fail("Same peer cannot be connected twice");
139     }
140
141     @Test
142     public void testDropFirst() throws Exception {
143         final Ipv4Address higher = new Ipv4Address("123.123.123.123");
144         final Ipv4Address lower = new Ipv4Address("123.123.123.122");
145         final IpAddress remoteIp = new IpAddress(lower);
146
147         final ReusableBGPPeer session1 = getMockSession();
148         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
149
150         this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
151         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
152         Mockito.verify(session1).releaseConnection();
153     }
154
155     @Test
156     public void testDuplicateDifferentIds() throws Exception {
157         final Ipv4Address from = new Ipv4Address("0.0.0.1");
158         final IpAddress remoteIp = new IpAddress(from);
159         final Ipv4Address to = new Ipv4Address("255.255.255.255");
160
161         final ReusableBGPPeer session1 = getMockSession();
162         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
163
164         this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
165         try {
166             this.peerRegistry.getPeer(remoteIp, to, to, createOpen(to, AS1));
167         } catch (final BGPDocumentedException e) {
168             assertEquals(BGPError.CEASE, e.getError());
169             return;
170         }
171
172         fail("Same peer cannot be connected twice");
173     }
174
175     @Test
176     public void testDuplicateHigerAs() throws Exception {
177         final Ipv4Address from = new Ipv4Address("0.0.0.1");
178         final IpAddress remoteIp = new IpAddress(from);
179         final Ipv4Address to = new Ipv4Address("255.255.255.255");
180         final AsNumber as2 = new AsNumber(1235L);
181
182         final ReusableBGPPeer session1 = getMockSession();
183         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
184
185         this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
186         this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
187         Mockito.verify(session1).releaseConnection();
188     }
189
190     @Test
191     public void testDuplicateLowerAs() throws Exception {
192         final Ipv4Address from = new Ipv4Address("0.0.0.1");
193         final IpAddress remoteIp = new IpAddress(from);
194         final Ipv4Address to = new Ipv4Address("255.255.255.255");
195         final AsNumber as2 = new AsNumber(3L);
196
197         final ReusableBGPPeer session1 = getMockSession();
198         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
199
200         this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
201         try {
202             this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
203         } catch (final BGPDocumentedException e) {
204             assertEquals(BGPError.CEASE, e.getError());
205             return;
206         }
207
208         fail("Same peer cannot be connected twice");
209     }
210
211     @Test
212     public void testAsMismatch() throws Exception {
213         final Ipv4Address from = new Ipv4Address("0.0.0.1");
214         final IpAddress remoteIp = new IpAddress(from);
215         final Ipv4Address to = new Ipv4Address("255.255.255.255");
216         final AsNumber as2 = new AsNumber(3L);
217
218         final ReusableBGPPeer session1 = getMockSession();
219         this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
220
221         try {
222             this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
223         } catch (final BGPDocumentedException e) {
224             assertEquals(BGPError.BAD_PEER_AS, e.getError());
225             return;
226         }
227
228         fail("Peer AS number mismatch");
229     }
230
231     private static ReusableBGPPeer getMockSession() {
232         final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
233         Mockito.doNothing().when(mock).releaseConnection();
234         return mock;
235     }
236
237     public BGPSessionPreferences getMockPreferences(final AsNumber remoteAs) {
238         return new BGPSessionPreferences(AS1, 1, new Ipv4Address("0.0.0.1"), remoteAs, Collections.<BgpParameters> emptyList());
239     }
240 }