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.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;
37 public class StrictBGPPeerRegistryTest {
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");
45 private final ReusableBGPPeer peer1 = getMockSession();
46 private final Open classicOpen = createOpen(TO, LOCAL_AS);
47 private StrictBGPPeerRegistry peerRegistry;
48 private BGPSessionPreferences mockPreferences;
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();
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());
64 private static ReusableBGPPeer getMockSession() {
65 final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
66 Mockito.doNothing().when(mock).releaseConnection();
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());
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);
82 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
83 } catch (final BGPDocumentedException e) {
84 assertEquals(BGPError.CEASE, e.getError());
87 fail("Same peer cannot be connected twice");
91 public void testPeerNotConfigured() throws Exception {
93 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
94 } catch (final IllegalStateException e) {
97 fail("Unknown peer cannot be connected");
101 public void testPeerConnectionSuccessfull() throws Exception {
102 final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
103 final IpAddress remoteIp2 = new IpAddress(to2);
105 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
106 final ReusableBGPPeer session2 = getMockSession();
107 this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
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);
114 Mockito.verifyZeroInteractions(this.peer1);
115 Mockito.verifyZeroInteractions(session2);
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);
124 this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
126 this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
128 this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
129 } catch (final BGPDocumentedException e) {
130 assertEquals(BGPError.CEASE, e.getError());
133 fail("Same peer cannot be connected twice");
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);
142 this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
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();
150 public void testDuplicatePeersWDifferentIds() throws Exception {
151 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
153 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
155 this.peerRegistry.getPeer(REMOTE_IP, TO, TO, this.classicOpen);
156 } catch (final BGPDocumentedException e) {
157 assertEquals(BGPError.CEASE, e.getError());
160 fail("Same peer cannot be connected twice");
164 public void testDuplicatePeersHigherAs() throws Exception {
165 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
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();
173 public void testDuplicatePeersLowerAs() throws Exception {
174 final AsNumber as2 = new AsNumber(3L);
176 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
178 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
180 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
181 } catch (final BGPDocumentedException e) {
182 assertEquals(BGPError.CEASE, e.getError());
185 fail("Same peer cannot be connected twice");
189 public void testAsMismatch() throws Exception {
190 final AsNumber as2 = new AsNumber(3L);
192 this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
194 this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
195 } catch (final BGPDocumentedException e) {
196 assertEquals(BGPError.BAD_PEER_AS, e.getError());
199 fail("Peer AS number mismatch");