BUG-2109 : clear BGP session after it was already initialized
[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.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;
25
26 public class StrictBGPPeerRegistryTest {
27
28     private StrictBGPPeerRegistry droppingBGPSessionRegistry;
29     private BGPSessionPreferences mockPreferences;
30
31     @Before
32     public void setUp() throws Exception {
33         this.droppingBGPSessionRegistry = new StrictBGPPeerRegistry();
34         this.mockPreferences = getMockPreferences();
35     }
36
37     @Test
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());
42     }
43
44     @Test
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");
49
50         final ReusableBGPPeer session1 = getMockSession();
51         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
52
53         this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
54         try {
55             this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
56         } catch (final IllegalStateException e) {
57             Mockito.verify(session1).isSessionActive();
58             return;
59         }
60
61         fail("Same peer cannot be connected twice");
62     }
63
64     @Test
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");
69
70         try {
71             this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
72         } catch (final IllegalStateException e) {
73             return;
74         }
75         fail("Unknown peer cannot be connected");
76     }
77
78     @Test
79     public void testOk() throws Exception {
80         final Ipv4Address from = new Ipv4Address("0.0.0.1");
81
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);
86
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);
91
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);
96
97         Mockito.verifyZeroInteractions(session1);
98         Mockito.verifyZeroInteractions(session2);
99     }
100
101     @Test
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);
106
107         final ReusableBGPPeer session1 = getMockSession();
108         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
109
110         this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
111         try {
112             this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
113         } catch (final BGPDocumentedException e) {
114             Mockito.verify(session1).isSessionActive();
115             return;
116         }
117
118         fail("Same peer cannot be connected twice");
119     }
120
121     @Test
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);
126
127         final ReusableBGPPeer session1 = getMockSession();
128         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
129
130         this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
131         this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
132         Mockito.verify(session1).releaseConnection();
133     }
134
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();
139         return mock;
140     }
141
142     public BGPSessionPreferences getMockPreferences() {
143         return new BGPSessionPreferences(null, 1, null, null);
144     }
145 }