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.rib.impl.spi.BGPSessionPreferences;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
22 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
26 public class StrictBGPPeerRegistryTest {
28 private StrictBGPPeerRegistry droppingBGPSessionRegistry;
29 private BGPSessionPreferences mockPreferences;
32 public void setUp() throws Exception {
33 this.droppingBGPSessionRegistry = new StrictBGPPeerRegistry();
34 this.mockPreferences = getMockPreferences();
38 public void testIpAddressConstruction() throws Exception {
39 final InetSocketAddress adr = new InetSocketAddress("127.0.0.1", 179);
40 final IpAddress ipAdr = StrictBGPPeerRegistry.getIpAddress(adr);
41 assertEquals("127.0.0.1", ipAdr.getIpv4Address().getValue());
45 public void testDuplicate() throws Exception {
46 final Ipv4Address from = new Ipv4Address("0.0.0.1");
47 final IpAddress remoteIp = new IpAddress(from);
48 final Ipv4Address to = new Ipv4Address("255.255.255.255");
50 final ReusableBGPPeer session1 = getMockSession();
51 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
53 this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
55 this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
56 } catch (final IllegalStateException e) {
57 Mockito.verify(session1).isSessionActive();
61 fail("Same peer cannot be connected twice");
65 public void testNotAllowed() 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");
71 this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
72 } catch (final IllegalStateException e) {
75 fail("Unknown peer cannot be connected");
79 public void testOk() throws Exception {
80 final Ipv4Address from = new Ipv4Address("0.0.0.1");
82 final Ipv4Address to = new Ipv4Address("255.255.255.255");
83 final IpAddress remoteIp = new IpAddress(to);
84 final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
85 final IpAddress remoteIp2 = new IpAddress(to2);
87 final ReusableBGPPeer session1 = getMockSession();
88 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
89 final ReusableBGPPeer session2 = getMockSession();
90 this.droppingBGPSessionRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
92 final BGPSessionListener returnedSession1 = this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
93 assertSame(session1, returnedSession1);
94 final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2);
95 assertSame(session2, returnedSession2);
97 Mockito.verifyZeroInteractions(session1);
98 Mockito.verifyZeroInteractions(session2);
102 public void testDropSecond() throws Exception {
103 final Ipv4Address higher = new Ipv4Address("192.168.200.200");
104 final Ipv4Address lower = new Ipv4Address("10.10.10.10");
105 final IpAddress remoteIp = new IpAddress(lower);
107 final ReusableBGPPeer session1 = getMockSession();
108 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
110 this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
112 this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
113 } catch (final BGPDocumentedException e) {
114 Mockito.verify(session1).isSessionActive();
118 fail("Same peer cannot be connected twice");
122 public void testDropFirst() throws Exception {
123 final Ipv4Address higher = new Ipv4Address("123.123.123.123");
124 final Ipv4Address lower = new Ipv4Address("123.123.123.122");
125 final IpAddress remoteIp = new IpAddress(lower);
127 final ReusableBGPPeer session1 = getMockSession();
128 this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
130 this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
131 this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
132 Mockito.verify(session1).releaseConnection();
135 private ReusableBGPPeer getMockSession() {
136 final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
137 Mockito.doNothing().when(mock).releaseConnection();
138 Mockito.doReturn(Boolean.TRUE).when(mock).isSessionActive();
142 public BGPSessionPreferences getMockPreferences() {
143 return new BGPSessionPreferences(null, 1, null, null);