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;
15 import java.net.InetSocketAddress;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.BGPError;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
23 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
27 public class StrictBGPPeerRegistryTest {
29 private StrictBGPPeerRegistry droppingBGPSessionRegistry;
30 private BGPSessionPreferences mockPreferences;
33 public void setUp() throws Exception {
34 this.droppingBGPSessionRegistry = new StrictBGPPeerRegistry();
35 this.mockPreferences = getMockPreferences();
39 public void testIpAddressConstruction() throws Exception {
40 final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
41 final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
42 assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
46 public void testDuplicate() throws Exception {
47 final Ipv4Address from = new Ipv4Address("0.0.0.1");
48 final IpAddress remoteIp = new IpAddress(from);
49 final Ipv4Address to = new Ipv4Address("255.255.255.255");
51 final ReusableBGPPeer session1 = getMockSession();
52 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
54 this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
56 this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
57 } catch (final BGPDocumentedException e) {
58 assertEquals(BGPError.CEASE, e.getError());
62 fail("Same peer cannot be connected twice");
66 public void testNotAllowed() throws Exception {
67 final Ipv4Address from = new Ipv4Address("0.0.0.1");
68 final IpAddress remoteIp = new IpAddress(from);
69 final Ipv4Address to = new Ipv4Address("255.255.255.255");
72 this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
73 } catch (final IllegalStateException e) {
76 fail("Unknown peer cannot be connected");
80 public void testOk() throws Exception {
81 final Ipv4Address from = new Ipv4Address("0.0.0.1");
83 final Ipv4Address to = new Ipv4Address("255.255.255.255");
84 final IpAddress remoteIp = new IpAddress(to);
85 final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
86 final IpAddress remoteIp2 = new IpAddress(to2);
88 final ReusableBGPPeer session1 = getMockSession();
89 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
90 final ReusableBGPPeer session2 = getMockSession();
91 this.droppingBGPSessionRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
93 final BGPSessionListener returnedSession1 = this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
94 assertSame(session1, returnedSession1);
95 final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2);
96 assertSame(session2, returnedSession2);
98 Mockito.verifyZeroInteractions(session1);
99 Mockito.verifyZeroInteractions(session2);
103 public void testDropSecond() throws Exception {
104 final Ipv4Address higher = new Ipv4Address("192.168.200.200");
105 final Ipv4Address lower = new Ipv4Address("10.10.10.10");
106 final IpAddress remoteIp = new IpAddress(lower);
108 final ReusableBGPPeer session1 = getMockSession();
109 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
111 this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
113 this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
114 } catch (final BGPDocumentedException e) {
115 assertEquals(BGPError.CEASE, e.getError());
119 fail("Same peer cannot be connected twice");
123 public void testDropFirst() throws Exception {
124 final Ipv4Address higher = new Ipv4Address("123.123.123.123");
125 final Ipv4Address lower = new Ipv4Address("123.123.123.122");
126 final IpAddress remoteIp = new IpAddress(lower);
128 final ReusableBGPPeer session1 = getMockSession();
129 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
131 this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
132 this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
133 Mockito.verify(session1).releaseConnection();
137 public void testDuplicateDifferentIds() throws Exception {
138 final Ipv4Address from = new Ipv4Address("0.0.0.1");
139 final IpAddress remoteIp = new IpAddress(from);
140 final Ipv4Address to = new Ipv4Address("255.255.255.255");
142 final ReusableBGPPeer session1 = getMockSession();
143 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
145 this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
147 this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to);
148 } catch (final BGPDocumentedException e) {
149 assertEquals(BGPError.CEASE, e.getError());
153 fail("Same peer cannot be connected twice");
156 private ReusableBGPPeer getMockSession() {
157 final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
158 Mockito.doNothing().when(mock).releaseConnection();
162 public BGPSessionPreferences getMockPreferences() {
163 return new BGPSessionPreferences(null, 1, null, null);