BUG-3888 : refactoring, removed unused session validators
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / StrictBGPPeerRegistryTest.java
index 004ab5dada0a332dbf249320ccb0d960696743e1..1299d0ccbf81ad3e4115d6ce70629ed1620898ec 100644 (file)
@@ -11,27 +11,47 @@ package org.opendaylight.protocol.bgp.rib.impl;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.fail;
-
+import com.google.common.collect.Lists;
 import java.net.InetSocketAddress;
+import java.util.Collections;
+import java.util.List;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
 
 public class StrictBGPPeerRegistryTest {
 
     private StrictBGPPeerRegistry droppingBGPSessionRegistry;
     private BGPSessionPreferences mockPreferences;
+    private final AsNumber AS1 = new AsNumber(1234L);
+
+    private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
+        final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
+            .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder()
+                .setCParameters(new CParametersBuilder()
+                    .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(as).build()).build()).build())).build());
+        return new OpenBuilder().setBgpIdentifier(bgpId).setBgpParameters(params).build();
+    }
 
     @Before
     public void setUp() throws Exception {
         this.droppingBGPSessionRegistry = new StrictBGPPeerRegistry();
-        this.mockPreferences = getMockPreferences();
+        this.mockPreferences = getMockPreferences(this.AS1);
     }
 
     @Test
@@ -50,11 +70,11 @@ public class StrictBGPPeerRegistryTest {
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
-        } catch (final IllegalStateException e) {
-            Mockito.verify(session1).isSessionActive();
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+        } catch (final BGPDocumentedException e) {
+            assertEquals(BGPError.CEASE, e.getError());
             return;
         }
 
@@ -68,7 +88,7 @@ public class StrictBGPPeerRegistryTest {
         final Ipv4Address to = new Ipv4Address("255.255.255.255");
 
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         } catch (final IllegalStateException e) {
             return;
         }
@@ -89,9 +109,9 @@ public class StrictBGPPeerRegistryTest {
         final ReusableBGPPeer session2 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
 
-        final BGPSessionListener returnedSession1 = this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to);
+        final BGPSessionListener returnedSession1 = this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         assertSame(session1, returnedSession1);
-        final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2);
+        final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2, this.AS1, createOpen(to, this.AS1));
         assertSame(session2, returnedSession2);
 
         Mockito.verifyZeroInteractions(session1);
@@ -107,11 +127,11 @@ public class StrictBGPPeerRegistryTest {
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, this.AS1, createOpen(lower, this.AS1));
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, this.AS1, createOpen(higher, this.AS1));
         } catch (final BGPDocumentedException e) {
-            Mockito.verify(session1).isSessionActive();
+            assertEquals(BGPError.CEASE, e.getError());
             return;
         }
 
@@ -127,19 +147,94 @@ public class StrictBGPPeerRegistryTest {
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher);
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, this.AS1, createOpen(higher, this.AS1));
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, this.AS1, createOpen(lower, this.AS1));
+        Mockito.verify(session1).releaseConnection();
+    }
+
+    @Test
+    public void testDuplicateDifferentIds() throws Exception {
+        final Ipv4Address from = new Ipv4Address("0.0.0.1");
+        final IpAddress remoteIp = new IpAddress(from);
+        final Ipv4Address to = new Ipv4Address("255.255.255.255");
+
+        final ReusableBGPPeer session1 = getMockSession();
+        this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+        try {
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to, this.AS1, createOpen(to, this.AS1));
+        } catch (final BGPDocumentedException e) {
+            assertEquals(BGPError.CEASE, e.getError());
+            return;
+        }
+
+        fail("Same peer cannot be connected twice");
+    }
+
+    @Test
+    public void testDuplicateHigerAs() throws Exception {
+        final Ipv4Address from = new Ipv4Address("0.0.0.1");
+        final IpAddress remoteIp = new IpAddress(from);
+        final Ipv4Address to = new Ipv4Address("255.255.255.255");
+        final AsNumber as2 = new AsNumber(1235L);
+
+        final ReusableBGPPeer session1 = getMockSession();
+        this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
         Mockito.verify(session1).releaseConnection();
     }
 
-    private ReusableBGPPeer getMockSession() {
+    @Test
+    public void testDuplicateLowerAs() throws Exception {
+        final Ipv4Address from = new Ipv4Address("0.0.0.1");
+        final IpAddress remoteIp = new IpAddress(from);
+        final Ipv4Address to = new Ipv4Address("255.255.255.255");
+        final AsNumber as2 = new AsNumber(3L);
+
+        final ReusableBGPPeer session1 = getMockSession();
+        this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
+        try {
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
+        } catch (final BGPDocumentedException e) {
+            assertEquals(BGPError.CEASE, e.getError());
+            return;
+        }
+
+        fail("Same peer cannot be connected twice");
+    }
+
+    @Test
+    public void testAsMismatch() throws Exception {
+        final Ipv4Address from = new Ipv4Address("0.0.0.1");
+        final IpAddress remoteIp = new IpAddress(from);
+        final Ipv4Address to = new Ipv4Address("255.255.255.255");
+        final AsNumber as2 = new AsNumber(3L);
+
+        final ReusableBGPPeer session1 = getMockSession();
+        this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+
+        try {
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
+        } catch (final BGPDocumentedException e) {
+            assertEquals(BGPError.BAD_PEER_AS, e.getError());
+            return;
+        }
+
+        fail("Peer AS number mismatch");
+    }
+
+    private static ReusableBGPPeer getMockSession() {
         final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
         Mockito.doNothing().when(mock).releaseConnection();
-        Mockito.doReturn(Boolean.TRUE).when(mock).isSessionActive();
         return mock;
     }
 
-    public BGPSessionPreferences getMockPreferences() {
-        return new BGPSessionPreferences(null, 1, null, null);
+    public BGPSessionPreferences getMockPreferences(final AsNumber remoteAs) {
+        return new BGPSessionPreferences(this.AS1, 1, new Ipv4Address("0.0.0.1"), remoteAs, Collections.<BgpParameters> emptyList());
     }
 }