cee7899dd43c380e7b1de3375fd2cca82b91c311
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / StrictBGPPeerRegistryTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.rib.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.fail;
14
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;
26
27 public class StrictBGPPeerRegistryTest {
28
29     private StrictBGPPeerRegistry droppingBGPSessionRegistry;
30     private BGPSessionPreferences mockPreferences;
31
32     @Before
33     public void setUp() throws Exception {
34         this.droppingBGPSessionRegistry = new StrictBGPPeerRegistry();
35         this.mockPreferences = getMockPreferences();
36     }
37
38     @Test
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());
43     }
44
45     @Test
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");
50
51         final ReusableBGPPeer session1 = getMockSession();
52         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
53
54         this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
55         try {
56             this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
57         } catch (final BGPDocumentedException e) {
58             assertEquals(BGPError.CEASE, e.getError());
59             return;
60         }
61
62         fail("Same peer cannot be connected twice");
63     }
64
65     @Test
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");
70
71         try {
72             this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
73         } catch (final IllegalStateException e) {
74             return;
75         }
76         fail("Unknown peer cannot be connected");
77     }
78
79     @Test
80     public void testOk() throws Exception {
81         final Ipv4Address from = new Ipv4Address("0.0.0.1");
82
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);
87
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);
92
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);
97
98         Mockito.verifyZeroInteractions(session1);
99         Mockito.verifyZeroInteractions(session2);
100     }
101
102     @Test
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);
107
108         final ReusableBGPPeer session1 = getMockSession();
109         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
110
111         this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
112         try {
113             this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
114         } catch (final BGPDocumentedException e) {
115             assertEquals(BGPError.CEASE, e.getError());
116             return;
117         }
118
119         fail("Same peer cannot be connected twice");
120     }
121
122     @Test
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);
127
128         final ReusableBGPPeer session1 = getMockSession();
129         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
130
131         this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
132         this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
133         Mockito.verify(session1).releaseConnection();
134     }
135
136     @Test
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");
141
142         final ReusableBGPPeer session1 = getMockSession();
143         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
144
145         this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
146         try {
147             this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to);
148         } catch (final BGPDocumentedException e) {
149             assertEquals(BGPError.CEASE, e.getError());
150             return;
151         }
152
153         fail("Same peer cannot be connected twice");
154     }
155
156     private ReusableBGPPeer getMockSession() {
157         final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
158         Mockito.doNothing().when(mock).releaseConnection();
159         return mock;
160     }
161
162     public BGPSessionPreferences getMockPreferences() {
163         return new BGPSessionPreferences(null, 1, null, null);
164     }
165 }