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