BUG 3094, Check AS number 76/19576/8
authorClaudio D. Gasparini <cgaspari@cisco.com>
Tue, 5 May 2015 08:33:11 +0000 (10:33 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 7 May 2015 13:43:27 +0000 (13:43 +0000)
Change-Id: I345a3c5c2cb4a496b45e87aa11d1c752c39bc75c
Signed-off-by: Claudio D. Gasparini <cgaspari@cisco.com>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistryTest.java

index 895a9b4990226c7e92e925e535529517b40e7811..d9cc66c838d91ae71cde932bf389e8bd8633e9cd 100644 (file)
@@ -59,7 +59,11 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         Preconditions.checkNotNull(ip);
         Preconditions.checkArgument(!this.peers.containsKey(ip), "Peer for %s already present", ip);
         this.peers.put(ip, Preconditions.checkNotNull(peer));
-        this.peerPreferences.put(ip, Preconditions.checkNotNull(preferences));
+        Preconditions.checkNotNull(preferences.getMyAs());
+        Preconditions.checkNotNull(preferences.getHoldTime());
+        Preconditions.checkNotNull(preferences.getParams());
+        Preconditions.checkNotNull(preferences.getBgpId());
+        this.peerPreferences.put(ip, preferences);
     }
 
     @Override
@@ -85,12 +89,12 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
     }
 
     @Override
-    public synchronized BGPSessionListener getPeer(final IpAddress ip,
-        final Ipv4Address sourceId, final Ipv4Address remoteId, final AsNumber asNumber)
-            throws BGPDocumentedException {
+    public synchronized BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId,
+        final Ipv4Address remoteId, final AsNumber asNumber) throws BGPDocumentedException {
         Preconditions.checkNotNull(ip);
         Preconditions.checkNotNull(sourceId);
         Preconditions.checkNotNull(remoteId);
+        Preconditions.checkNotNull(asNumber);
 
         checkPeerConfigured(ip);
 
@@ -143,6 +147,11 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
                         ip, currentConnection),
                         BGPError.CEASE);
             }
+        } else {
+            if (!getPeerPreferences(ip).getMyAs().equals(asNumber)) {
+                LOG.warn("Unexpected remote AS number. Expecting {}, got {}", asNumber, getPeerPreferences(ip).getMyAs());
+                throw new BGPDocumentedException("Peer AS number mismatch", BGPError.BAD_PEER_AS);
+            }
         }
 
         // Map session id to peer IP address
@@ -260,4 +269,4 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
                 .toString();
         }
     }
-}
+}
\ No newline at end of file
index c8e59bf29306d5f9ee758bf9f3444a7de2a3a2df..d00851c0dc5215a8d4d130dfbb97d406eebe7d86 100644 (file)
@@ -13,6 +13,8 @@ import static org.junit.Assert.assertSame;
 import static org.junit.Assert.fail;
 
 import java.net.InetSocketAddress;
+import java.util.Collections;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -24,12 +26,13 @@ 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.BgpParameters;
 
 public class StrictBGPPeerRegistryTest {
 
     private StrictBGPPeerRegistry droppingBGPSessionRegistry;
     private BGPSessionPreferences mockPreferences;
-
+    private final AsNumber AS1 = new AsNumber(1234L);
     @Before
     public void setUp() throws Exception {
         this.droppingBGPSessionRegistry = new StrictBGPPeerRegistry();
@@ -48,14 +51,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, AS1);
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, AS1);
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -69,10 +71,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, AS1);
         } catch (final IllegalStateException e) {
             return;
         }
@@ -87,16 +88,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, AS1);
         assertSame(session1, returnedSession1);
-        final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2, as);
+        final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2, AS1);
         assertSame(session2, returnedSession2);
 
         Mockito.verifyZeroInteractions(session1);
@@ -108,14 +108,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, AS1);
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, as);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, AS1);
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -129,13 +128,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, AS1);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, AS1);
         Mockito.verify(session1).releaseConnection();
     }
 
@@ -144,14 +142,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, AS1);
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to, as);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to, AS1);
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -165,13 +162,12 @@ 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, AS1);
         this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2);
         Mockito.verify(session1).releaseConnection();
     }
@@ -181,13 +177,12 @@ 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, AS1);
         try {
             this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2);
         } catch (final BGPDocumentedException e) {
@@ -198,6 +193,26 @@ public class StrictBGPPeerRegistryTest {
         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);
+        } catch (final BGPDocumentedException e) {
+            assertEquals(BGPError.BAD_PEER_AS, e.getError());
+            return;
+        }
+
+        fail("Peer AS number mismatch");
+    }
+
     private ReusableBGPPeer getMockSession() {
         final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
         Mockito.doNothing().when(mock).releaseConnection();
@@ -205,6 +220,6 @@ public class StrictBGPPeerRegistryTest {
     }
 
     public BGPSessionPreferences getMockPreferences() {
-        return new BGPSessionPreferences(null, 1, null, null);
+        return new BGPSessionPreferences(AS1, 1,  new Ipv4Address("0.0.0.1"), Collections.<BgpParameters>emptyList());
     }
-}
+}
\ No newline at end of file