MVPN RFC6514 Extendend communities
[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.collect.Lists;
16 import com.google.common.util.concurrent.Futures;
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.impl.spi.PeerRegistrySessionListener;
27 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
39
40 public class StrictBGPPeerRegistryTest {
41
42     private static final AsNumber LOCAL_AS = new AsNumber(1234L);
43     private static final AsNumber REMOTE_AS = new AsNumber(1235L);
44     private static final Ipv4Address FROM = new Ipv4Address("0.0.0.1");
45     private static final IpAddress REMOTE_IP = new IpAddress(FROM);
46     private static final Ipv4Address TO = new Ipv4Address("255.255.255.255");
47
48     private final BGPSessionListener peer1 = getMockSession();
49     private final Open classicOpen = createOpen(TO, LOCAL_AS);
50     private StrictBGPPeerRegistry peerRegistry;
51     private BGPSessionPreferences mockPreferences;
52
53     private static Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
54         final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
55         .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
56         .setCParameters(new CParametersBuilder()
57         .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build()).build()).build())).build());
58         return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
59     }
60
61     @Before
62     public void setUp() {
63         this.peerRegistry = new StrictBGPPeerRegistry();
64         this.mockPreferences = new BGPSessionPreferences(LOCAL_AS, 1, new BgpId("0.0.0.1"), LOCAL_AS,
65                 Collections.emptyList());
66     }
67
68     private static BGPSessionListener getMockSession() {
69         final BGPSessionListener mock = Mockito.mock(BGPSessionListener.class);
70         Mockito.doReturn(Futures.immediateFuture(null)).when(mock).releaseConnection();
71         return mock;
72     }
73
74     private static PeerRegistrySessionListener getMockSessionListener() {
75         final PeerRegistrySessionListener mock = Mockito.mock(PeerRegistrySessionListener.class);
76         Mockito.doNothing().when(mock).onSessionCreated(Mockito.any(IpAddress.class));
77         Mockito.doNothing().when(mock).onSessionRemoved(Mockito.any(IpAddress.class));
78         return mock;
79     }
80
81     @Test
82     public void testIpAddressConstruction() throws Exception {
83         final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
84         final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
85         assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
86     }
87
88     @Test
89     public void testDuplicatePeerConnection() throws Exception {
90         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
91         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
92         try {
93             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
94         } catch (final BGPDocumentedException e) {
95             assertEquals(BGPError.CEASE, e.getError());
96             return;
97         }
98         fail("Same peer cannot be connected twice");
99     }
100
101     @Test
102     public void testPeerNotConfigured() throws Exception {
103         try {
104             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
105         } catch (final IllegalStateException e) {
106             return;
107         }
108         fail("Unknown peer cannot be connected");
109     }
110
111     @Test
112     public void testPeerConnectionSuccessfull() throws Exception {
113         final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
114         final IpAddress remoteIp2 = new IpAddress(to2);
115
116         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
117         final BGPSessionListener session2 = getMockSession();
118         this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
119
120         final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
121         assertSame(this.peer1, returnedSession1);
122         final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, FROM, to2, this.classicOpen);
123         assertSame(session2, returnedSession2);
124
125         Mockito.verifyZeroInteractions(this.peer1);
126         Mockito.verifyZeroInteractions(session2);
127     }
128
129     @Test
130     public void testDropSecondPeer() throws Exception {
131         final Ipv4Address higher = new Ipv4Address("192.168.200.200");
132         final Ipv4Address lower = new Ipv4Address("10.10.10.10");
133         final IpAddress remoteIp = new IpAddress(lower);
134
135         this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
136
137         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
138         try {
139             this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
140         } catch (final BGPDocumentedException e) {
141             assertEquals(BGPError.CEASE, e.getError());
142             return;
143         }
144         fail("Same peer cannot be connected twice");
145     }
146
147     @Test
148     public void testDropFirstPeer() throws Exception {
149         final Ipv4Address higher = new Ipv4Address("123.123.123.123");
150         final Ipv4Address lower = new Ipv4Address("123.123.123.122");
151         final IpAddress remoteIp = new IpAddress(lower);
152
153         this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
154
155         this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
156         this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
157         Mockito.verify(this.peer1).releaseConnection();
158     }
159
160     @Test
161     public void testDuplicatePeersWDifferentIds() throws Exception {
162         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
163
164         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
165         try {
166             this.peerRegistry.getPeer(REMOTE_IP, TO, TO, this.classicOpen);
167         } catch (final BGPDocumentedException e) {
168             assertEquals(BGPError.CEASE, e.getError());
169             return;
170         }
171         fail("Same peer cannot be connected twice");
172     }
173
174     @Test
175     public void testDuplicatePeersHigherAs() throws Exception {
176         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
177
178         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
179         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, REMOTE_AS));
180         Mockito.verify(this.peer1).releaseConnection();
181     }
182
183     @Test
184     public void testDuplicatePeersLowerAs() throws Exception {
185         final AsNumber as2 = new AsNumber(3L);
186
187         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
188
189         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
190         try {
191             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
192         } catch (final BGPDocumentedException e) {
193             assertEquals(BGPError.CEASE, e.getError());
194             return;
195         }
196         fail("Same peer cannot be connected twice");
197     }
198
199     @Test
200     public void testAsMismatch() throws Exception {
201         final AsNumber as2 = new AsNumber(3L);
202
203         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
204         try {
205             this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
206         } catch (final BGPDocumentedException e) {
207             assertEquals(BGPError.BAD_PEER_AS, e.getError());
208             return;
209         }
210         fail("Peer AS number mismatch");
211     }
212
213     @Test
214     public void testRegisterPeerSessionListener() throws Exception {
215         final PeerRegistrySessionListener sessionListener1 = getMockSessionListener();
216         this.peerRegistry.registerPeerSessionListener(sessionListener1);
217
218         final PeerRegistrySessionListener sessionListener2 = getMockSessionListener();
219         this.peerRegistry.registerPeerSessionListener(sessionListener2);
220
221         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
222         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
223         Mockito.verify(sessionListener1, Mockito.times(1)).onSessionCreated(REMOTE_IP);
224         Mockito.verify(sessionListener2, Mockito.times(1)).onSessionCreated(REMOTE_IP);
225
226         this.peerRegistry.removePeerSession(REMOTE_IP);
227         Mockito.verify(sessionListener1, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
228         Mockito.verify(sessionListener2, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
229     }
230
231     @Test
232     public void testClosePeerSessionOneListener() throws Exception {
233         final PeerRegistrySessionListener sessionListener1 = getMockSessionListener();
234         final AutoCloseable registration1 = this.peerRegistry.registerPeerSessionListener(sessionListener1);
235
236         final PeerRegistrySessionListener sessionListener2 = getMockSessionListener();
237         this.peerRegistry.registerPeerSessionListener(sessionListener2);
238
239         this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
240         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
241         this.peerRegistry.removePeerSession(REMOTE_IP);
242
243         registration1.close();
244         this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
245         this.peerRegistry.removePeerSession(REMOTE_IP);
246
247         Mockito.verify(sessionListener1, Mockito.times(1)).onSessionCreated(REMOTE_IP);
248         Mockito.verify(sessionListener2, Mockito.times(2)).onSessionCreated(REMOTE_IP);
249         Mockito.verify(sessionListener1, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
250         Mockito.verify(sessionListener2, Mockito.times(2)).onSessionRemoved(REMOTE_IP);
251     }
252 }