478edb6da2c2b883a66089154c4a8ec82312069e
[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 static final AsNumber LOCAL_AS = new AsNumber(1234L);
40     private static final AsNumber REMOTE_AS = new AsNumber(1235L);
41     private static final Ipv4Address FROM = new Ipv4Address("0.0.0.1");
42     private static final IpAddress REMOTE_IP = new IpAddress(FROM);
43     private static final Ipv4Address TO = new Ipv4Address("255.255.255.255");
44
45     private final ReusableBGPPeer peer1 = getMockSession();
46     private final Open classicOpen = createOpen(TO, LOCAL_AS);
47     private StrictBGPPeerRegistry peerRegistry;
48     private BGPSessionPreferences mockPreferences;
49
50     private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
51         final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
52             .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
53                 .setCParameters(new CParametersBuilder()
54                     .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build()).build()).build())).build());
55         return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
56     }
57
58     @Before
59     public void setUp() throws Exception {
60         this.peerRegistry = new StrictBGPPeerRegistry();
61         this.mockPreferences =  new BGPSessionPreferences(LOCAL_AS, 1, new Ipv4Address("0.0.0.1"), LOCAL_AS, Collections.<BgpParameters> emptyList());
62     }
63
64     private static ReusableBGPPeer getMockSession() {
65         final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
66         Mockito.doNothing().when(mock).releaseConnection();
67         return mock;
68     }
69
70     @Test
71     public void testIpAddressConstruction() throws Exception {
72         final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
73         final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
74         assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
75     }
76
77     @Test
78     public void testDuplicatePeerConnection() throws Exception {
79         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
80         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
81         try {
82             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
83         } catch (final BGPDocumentedException e) {
84             assertEquals(BGPError.CEASE, e.getError());
85             return;
86         }
87         fail("Same peer cannot be connected twice");
88     }
89
90     @Test
91     public void testPeerNotConfigured() throws Exception {
92         try {
93             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
94         } catch (final IllegalStateException e) {
95             return;
96         }
97         fail("Unknown peer cannot be connected");
98     }
99
100     @Test
101     public void testPeerConnectionSuccessfull() throws Exception {
102         final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
103         final IpAddress remoteIp2 = new IpAddress(to2);
104
105         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
106         final ReusableBGPPeer session2 = getMockSession();
107         this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
108
109         final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
110         assertSame(this.peer1, returnedSession1);
111         final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, FROM, to2, this.classicOpen);
112         assertSame(session2, returnedSession2);
113
114         Mockito.verifyZeroInteractions(this.peer1);
115         Mockito.verifyZeroInteractions(session2);
116     }
117
118     @Test
119     public void testDropSecondPeer() throws Exception {
120         final Ipv4Address higher = new Ipv4Address("192.168.200.200");
121         final Ipv4Address lower = new Ipv4Address("10.10.10.10");
122         final IpAddress remoteIp = new IpAddress(lower);
123
124         this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
125
126         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
127         try {
128             this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
129         } catch (final BGPDocumentedException e) {
130             assertEquals(BGPError.CEASE, e.getError());
131             return;
132         }
133         fail("Same peer cannot be connected twice");
134     }
135
136     @Test
137     public void testDropFirstPeer() throws Exception {
138         final Ipv4Address higher = new Ipv4Address("123.123.123.123");
139         final Ipv4Address lower = new Ipv4Address("123.123.123.122");
140         final IpAddress remoteIp = new IpAddress(lower);
141
142         this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
143
144         this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
145         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
146         Mockito.verify(this.peer1).releaseConnection();
147     }
148
149     @Test
150     public void testDuplicatePeersWDifferentIds() throws Exception {
151         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
152
153         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
154         try {
155             this.peerRegistry.getPeer(REMOTE_IP, TO, TO, this.classicOpen);
156         } catch (final BGPDocumentedException e) {
157             assertEquals(BGPError.CEASE, e.getError());
158             return;
159         }
160         fail("Same peer cannot be connected twice");
161     }
162
163     @Test
164     public void testDuplicatePeersHigherAs() throws Exception {
165         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
166
167         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
168         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, REMOTE_AS));
169         Mockito.verify(this.peer1).releaseConnection();
170     }
171
172     @Test
173     public void testDuplicatePeersLowerAs() throws Exception {
174         final AsNumber as2 = new AsNumber(3L);
175
176         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
177
178         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
179         try {
180             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
181         } catch (final BGPDocumentedException e) {
182             assertEquals(BGPError.CEASE, e.getError());
183             return;
184         }
185         fail("Same peer cannot be connected twice");
186     }
187
188     @Test
189     public void testAsMismatch() throws Exception {
190         final AsNumber as2 = new AsNumber(3L);
191
192         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
193         try {
194             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
195         } catch (final BGPDocumentedException e) {
196             assertEquals(BGPError.BAD_PEER_AS, e.getError());
197             return;
198         }
199         fail("Peer AS number mismatch");
200     }
201 }