BUG-3888 : refactoring, removed unused session validators
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / StrictBGPPeerRegistryTest.java
index c8e59bf29306d5f9ee758bf9f3444a7de2a3a2df..1299d0ccbf81ad3e4115d6ce70629ed1620898ec 100644 (file)
@@ -11,8 +11,10 @@ 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;
@@ -24,16 +26,32 @@ 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
@@ -48,14 +66,13 @@ public class StrictBGPPeerRegistryTest {
         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 as = new AsNumber(1234L);
 
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -69,10 +86,9 @@ public class StrictBGPPeerRegistryTest {
         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 as = new AsNumber(1234L);
 
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         } catch (final IllegalStateException e) {
             return;
         }
@@ -87,16 +103,15 @@ public class StrictBGPPeerRegistryTest {
         final IpAddress remoteIp = new IpAddress(to);
         final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
         final IpAddress remoteIp2 = new IpAddress(to2);
-        final AsNumber as = new AsNumber(1234L);
 
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
         final ReusableBGPPeer session2 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
 
-        final BGPSessionListener returnedSession1 = this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as);
+        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, as);
+        final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2, this.AS1, createOpen(to, this.AS1));
         assertSame(session2, returnedSession2);
 
         Mockito.verifyZeroInteractions(session1);
@@ -108,14 +123,13 @@ public class StrictBGPPeerRegistryTest {
         final Ipv4Address higher = new Ipv4Address("192.168.200.200");
         final Ipv4Address lower = new Ipv4Address("10.10.10.10");
         final IpAddress remoteIp = new IpAddress(lower);
-        final AsNumber as = new AsNumber(1234L);
 
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, as);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, this.AS1, createOpen(lower, this.AS1));
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, as);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, this.AS1, createOpen(higher, this.AS1));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -129,13 +143,12 @@ public class StrictBGPPeerRegistryTest {
         final Ipv4Address higher = new Ipv4Address("123.123.123.123");
         final Ipv4Address lower = new Ipv4Address("123.123.123.122");
         final IpAddress remoteIp = new IpAddress(lower);
-        final AsNumber as = new AsNumber(1234L);
 
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, as);
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, as);
+        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();
     }
 
@@ -144,14 +157,13 @@ public class StrictBGPPeerRegistryTest {
         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 as = new AsNumber(1234L);
 
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to, as);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to, this.AS1, createOpen(to, this.AS1));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -165,14 +177,13 @@ public class StrictBGPPeerRegistryTest {
         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 as1 = new AsNumber(1L);
-        final AsNumber as2 = new AsNumber(3L);
+        final AsNumber as2 = new AsNumber(1235L);
 
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as1);
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2);
+        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();
     }
 
@@ -181,15 +192,14 @@ public class StrictBGPPeerRegistryTest {
         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 as1 = new AsNumber(10L);
         final AsNumber as2 = new AsNumber(3L);
 
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as1);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -198,13 +208,33 @@ public class StrictBGPPeerRegistryTest {
         fail("Same peer cannot be connected twice");
     }
 
-    private ReusableBGPPeer getMockSession() {
+    @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();
         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());
     }
 }