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 StrictBGPPeerRegistry peerRegistry;
40 private BGPSessionPreferences mockPreferences;
41 private static final AsNumber AS1 = new AsNumber(1234L);
43 private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
44 final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
45 .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
46 .setCParameters(new CParametersBuilder()
47 .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build()).build()).build())).build());
48 return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
52 public void setUp() throws Exception {
53 this.peerRegistry = new StrictBGPPeerRegistry();
54 this.mockPreferences = getMockPreferences(AS1);
58 public void testIpAddressConstruction() throws Exception {
59 final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
60 final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
61 assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
65 public void testDuplicate() throws Exception {
66 final Ipv4Address from = new Ipv4Address("0.0.0.1");
67 final IpAddress remoteIp = new IpAddress(from);
68 final Ipv4Address to = new Ipv4Address("255.255.255.255");
70 final ReusableBGPPeer session1 = getMockSession();
71 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
73 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
75 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
76 } catch (final BGPDocumentedException e) {
77 assertEquals(BGPError.CEASE, e.getError());
81 fail("Same peer cannot be connected twice");
85 public void testNotAllowed() throws Exception {
86 final Ipv4Address from = new Ipv4Address("0.0.0.1");
87 final IpAddress remoteIp = new IpAddress(from);
88 final Ipv4Address to = new Ipv4Address("255.255.255.255");
91 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
92 } catch (final IllegalStateException e) {
95 fail("Unknown peer cannot be connected");
99 public void testOk() throws Exception {
100 final Ipv4Address from = new Ipv4Address("0.0.0.1");
102 final Ipv4Address to = new Ipv4Address("255.255.255.255");
103 final IpAddress remoteIp = new IpAddress(to);
104 final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
105 final IpAddress remoteIp2 = new IpAddress(to2);
107 final ReusableBGPPeer session1 = getMockSession();
108 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
109 final ReusableBGPPeer session2 = getMockSession();
110 this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
112 final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
113 assertSame(session1, returnedSession1);
114 final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, from, to2, createOpen(to, AS1));
115 assertSame(session2, returnedSession2);
117 Mockito.verifyZeroInteractions(session1);
118 Mockito.verifyZeroInteractions(session2);
122 public void testDropSecond() throws Exception {
123 final Ipv4Address higher = new Ipv4Address("192.168.200.200");
124 final Ipv4Address lower = new Ipv4Address("10.10.10.10");
125 final IpAddress remoteIp = new IpAddress(lower);
127 final ReusableBGPPeer session1 = getMockSession();
128 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
130 this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
132 this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
133 } catch (final BGPDocumentedException e) {
134 assertEquals(BGPError.CEASE, e.getError());
138 fail("Same peer cannot be connected twice");
142 public void testDropFirst() throws Exception {
143 final Ipv4Address higher = new Ipv4Address("123.123.123.123");
144 final Ipv4Address lower = new Ipv4Address("123.123.123.122");
145 final IpAddress remoteIp = new IpAddress(lower);
147 final ReusableBGPPeer session1 = getMockSession();
148 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
150 this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
151 this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
152 Mockito.verify(session1).releaseConnection();
156 public void testDuplicateDifferentIds() throws Exception {
157 final Ipv4Address from = new Ipv4Address("0.0.0.1");
158 final IpAddress remoteIp = new IpAddress(from);
159 final Ipv4Address to = new Ipv4Address("255.255.255.255");
161 final ReusableBGPPeer session1 = getMockSession();
162 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
164 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
166 this.peerRegistry.getPeer(remoteIp, to, to, createOpen(to, AS1));
167 } catch (final BGPDocumentedException e) {
168 assertEquals(BGPError.CEASE, e.getError());
172 fail("Same peer cannot be connected twice");
176 public void testDuplicateHigerAs() throws Exception {
177 final Ipv4Address from = new Ipv4Address("0.0.0.1");
178 final IpAddress remoteIp = new IpAddress(from);
179 final Ipv4Address to = new Ipv4Address("255.255.255.255");
180 final AsNumber as2 = new AsNumber(1235L);
182 final ReusableBGPPeer session1 = getMockSession();
183 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
185 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
186 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
187 Mockito.verify(session1).releaseConnection();
191 public void testDuplicateLowerAs() throws Exception {
192 final Ipv4Address from = new Ipv4Address("0.0.0.1");
193 final IpAddress remoteIp = new IpAddress(from);
194 final Ipv4Address to = new Ipv4Address("255.255.255.255");
195 final AsNumber as2 = new AsNumber(3L);
197 final ReusableBGPPeer session1 = getMockSession();
198 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
200 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
202 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
203 } catch (final BGPDocumentedException e) {
204 assertEquals(BGPError.CEASE, e.getError());
208 fail("Same peer cannot be connected twice");
212 public void testAsMismatch() throws Exception {
213 final Ipv4Address from = new Ipv4Address("0.0.0.1");
214 final IpAddress remoteIp = new IpAddress(from);
215 final Ipv4Address to = new Ipv4Address("255.255.255.255");
216 final AsNumber as2 = new AsNumber(3L);
218 final ReusableBGPPeer session1 = getMockSession();
219 this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
222 this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
223 } catch (final BGPDocumentedException e) {
224 assertEquals(BGPError.BAD_PEER_AS, e.getError());
228 fail("Peer AS number mismatch");
231 private static ReusableBGPPeer getMockSession() {
232 final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
233 Mockito.doNothing().when(mock).releaseConnection();
237 public BGPSessionPreferences getMockPreferences(final AsNumber remoteAs) {
238 return new BGPSessionPreferences(AS1, 1, new Ipv4Address("0.0.0.1"), remoteAs, Collections.<BgpParameters> emptyList());