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
9 package org.opendaylight.protocol.bgp.rib.impl;
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;
36 public class StrictBGPPeerRegistryTest {
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");
44 private final BGPSessionListener peer1 = getMockSession();
45 private final Open classicOpen = createOpen(TO, LOCAL_AS);
46 private StrictBGPPeerRegistry peerRegistry;
47 private BGPSessionPreferences mockPreferences;
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();
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());
63 private static BGPSessionListener getMockSession() {
64 final BGPSessionListener mock = Mockito.mock(BGPSessionListener.class);
65 Mockito.doNothing().when(mock).releaseConnection();
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());
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);
81 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
82 } catch (final BGPDocumentedException e) {
83 assertEquals(BGPError.CEASE, e.getError());
86 fail("Same peer cannot be connected twice");
90 public void testPeerNotConfigured() throws Exception {
92 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
93 } catch (final IllegalStateException e) {
96 fail("Unknown peer cannot be connected");
100 public void testPeerConnectionSuccessfull() throws Exception {
101 final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
102 final IpAddress remoteIp2 = new IpAddress(to2);
104 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
105 final BGPSessionListener session2 = getMockSession();
106 this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
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);
113 Mockito.verifyZeroInteractions(this.peer1);
114 Mockito.verifyZeroInteractions(session2);
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);
123 this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
125 this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
127 this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
128 } catch (final BGPDocumentedException e) {
129 assertEquals(BGPError.CEASE, e.getError());
132 fail("Same peer cannot be connected twice");
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);
141 this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
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();
149 public void testDuplicatePeersWDifferentIds() throws Exception {
150 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
152 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
154 this.peerRegistry.getPeer(REMOTE_IP, TO, TO, this.classicOpen);
155 } catch (final BGPDocumentedException e) {
156 assertEquals(BGPError.CEASE, e.getError());
159 fail("Same peer cannot be connected twice");
163 public void testDuplicatePeersHigherAs() throws Exception {
164 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
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();
172 public void testDuplicatePeersLowerAs() throws Exception {
173 final AsNumber as2 = new AsNumber(3L);
175 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
177 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
179 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
180 } catch (final BGPDocumentedException e) {
181 assertEquals(BGPError.CEASE, e.getError());
184 fail("Same peer cannot be connected twice");
188 public void testAsMismatch() throws Exception {
189 final AsNumber as2 = new AsNumber(3L);
191 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
193 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
194 } catch (final BGPDocumentedException e) {
195 assertEquals(BGPError.BAD_PEER_AS, e.getError());
198 fail("Peer AS number mismatch");