2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.fail;
14 import com.google.common.collect.Lists;
15 import com.google.common.util.concurrent.Futures;
16 import java.net.InetSocketAddress;
17 import java.util.Collections;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
23 import org.opendaylight.protocol.bgp.parser.BGPError;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistrySessionListener;
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.rev180329.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
38 import org.opendaylight.yangtools.concepts.Registration;
39 import org.opendaylight.yangtools.yang.common.Uint32;
41 public class StrictBGPPeerRegistryTest {
43 private static final AsNumber LOCAL_AS = new AsNumber(Uint32.valueOf(1234));
44 private static final AsNumber REMOTE_AS = new AsNumber(Uint32.valueOf(1235));
45 private static final Ipv4Address FROM = new Ipv4Address("0.0.0.1");
46 private static final IpAddress REMOTE_IP = new IpAddress(FROM);
47 private static final Ipv4Address TO = new Ipv4Address("255.255.255.255");
49 private final BGPSessionListener peer1 = getMockSession();
50 private final Open classicOpen = createOpen(TO, LOCAL_AS);
51 private StrictBGPPeerRegistry peerRegistry;
52 private BGPSessionPreferences mockPreferences;
54 private static Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
55 final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
56 .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
57 .setCParameters(new CParametersBuilder()
58 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build())
59 .build()).build())).build());
60 return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
65 this.peerRegistry = new StrictBGPPeerRegistry();
66 this.mockPreferences = new BGPSessionPreferences(LOCAL_AS, 1, new BgpId("0.0.0.1"), LOCAL_AS,
67 Collections.emptyList());
70 private static BGPSessionListener getMockSession() {
71 final BGPSessionListener mock = Mockito.mock(BGPSessionListener.class);
72 Mockito.doReturn(Futures.immediateFuture(null)).when(mock).releaseConnection();
76 private static PeerRegistrySessionListener getMockSessionListener() {
77 final PeerRegistrySessionListener mock = Mockito.mock(PeerRegistrySessionListener.class);
78 Mockito.doNothing().when(mock).onSessionCreated(Mockito.any(IpAddress.class));
79 Mockito.doNothing().when(mock).onSessionRemoved(Mockito.any(IpAddress.class));
84 public void testIpAddressConstruction() throws BGPDocumentedException {
85 final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
86 final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
87 assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
91 public void testDuplicatePeerConnection() throws BGPDocumentedException {
92 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
93 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
95 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
96 } catch (final BGPDocumentedException e) {
97 assertEquals(BGPError.CEASE, e.getError());
100 fail("Same peer cannot be connected twice");
104 public void testPeerNotConfigured() throws BGPDocumentedException {
106 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
107 } catch (final IllegalStateException e) {
110 fail("Unknown peer cannot be connected");
114 public void testPeerConnectionSuccessfull() throws Exception {
115 final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
116 final IpAddress remoteIp2 = new IpAddress(to2);
118 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
119 final BGPSessionListener session2 = getMockSession();
120 this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
122 final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
123 assertSame(this.peer1, returnedSession1);
124 final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, FROM, to2, this.classicOpen);
125 assertSame(session2, returnedSession2);
127 Mockito.verifyZeroInteractions(this.peer1);
128 Mockito.verifyZeroInteractions(session2);
132 public void testDropSecondPeer() throws BGPDocumentedException {
133 final Ipv4Address higher = new Ipv4Address("192.168.200.200");
134 final Ipv4Address lower = new Ipv4Address("10.10.10.10");
135 final IpAddress remoteIp = new IpAddress(lower);
137 this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
139 this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
141 this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
142 } catch (final BGPDocumentedException e) {
143 assertEquals(BGPError.CEASE, e.getError());
146 fail("Same peer cannot be connected twice");
150 public void testDropFirstPeer() throws Exception {
151 final Ipv4Address higher = new Ipv4Address("123.123.123.123");
152 final Ipv4Address lower = new Ipv4Address("123.123.123.122");
153 final IpAddress remoteIp = new IpAddress(lower);
155 this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
157 this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
158 this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
159 Mockito.verify(this.peer1).releaseConnection();
163 public void testDuplicatePeersWDifferentIds() throws BGPDocumentedException {
164 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
166 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
168 this.peerRegistry.getPeer(REMOTE_IP, TO, TO, this.classicOpen);
169 } catch (final BGPDocumentedException e) {
170 assertEquals(BGPError.CEASE, e.getError());
173 fail("Same peer cannot be connected twice");
177 public void testDuplicatePeersHigherAs() throws BGPDocumentedException {
178 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
180 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
181 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, REMOTE_AS));
182 Mockito.verify(this.peer1).releaseConnection();
186 public void testDuplicatePeersLowerAs() throws Exception {
187 final AsNumber as2 = new AsNumber(Uint32.valueOf(3));
189 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
191 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
193 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
194 } catch (final BGPDocumentedException e) {
195 assertEquals(BGPError.CEASE, e.getError());
198 fail("Same peer cannot be connected twice");
202 public void testAsMismatch() {
203 final AsNumber as2 = new AsNumber(Uint32.valueOf(3));
205 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
207 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
208 } catch (final BGPDocumentedException e) {
209 assertEquals(BGPError.BAD_PEER_AS, e.getError());
212 fail("Peer AS number mismatch");
216 public void testRegisterPeerSessionListener() throws Exception {
217 final PeerRegistrySessionListener sessionListener1 = getMockSessionListener();
218 this.peerRegistry.registerPeerSessionListener(sessionListener1);
220 final PeerRegistrySessionListener sessionListener2 = getMockSessionListener();
221 this.peerRegistry.registerPeerSessionListener(sessionListener2);
223 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
224 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
225 Mockito.verify(sessionListener1, Mockito.times(1)).onSessionCreated(REMOTE_IP);
226 Mockito.verify(sessionListener2, Mockito.times(1)).onSessionCreated(REMOTE_IP);
228 this.peerRegistry.removePeerSession(REMOTE_IP);
229 Mockito.verify(sessionListener1, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
230 Mockito.verify(sessionListener2, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
234 public void testClosePeerSessionOneListener() throws BGPDocumentedException {
235 final PeerRegistrySessionListener sessionListener1 = getMockSessionListener();
236 final Registration registration1 = this.peerRegistry.registerPeerSessionListener(sessionListener1);
238 final PeerRegistrySessionListener sessionListener2 = getMockSessionListener();
239 this.peerRegistry.registerPeerSessionListener(sessionListener2);
241 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
242 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
243 this.peerRegistry.removePeerSession(REMOTE_IP);
245 registration1.close();
246 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
247 this.peerRegistry.removePeerSession(REMOTE_IP);
249 Mockito.verify(sessionListener1, Mockito.times(1)).onSessionCreated(REMOTE_IP);
250 Mockito.verify(sessionListener2, Mockito.times(2)).onSessionCreated(REMOTE_IP);
251 Mockito.verify(sessionListener1, Mockito.times(1)).onSessionRemoved(REMOTE_IP);
252 Mockito.verify(sessionListener2, Mockito.times(2)).onSessionRemoved(REMOTE_IP);