BUG-3888 : fix comparing ASnumbers 96/24396/6
authorDana Kutenicsova <dkutenic@cisco.com>
Wed, 22 Jul 2015 12:56:46 +0000 (14:56 +0200)
committerMilos Fabian <milfabia@cisco.com>
Mon, 3 Aug 2015 17:09:56 +0000 (17:09 +0000)
In order to allow BGP connection, expected remote AS (configured in ODL)
needs to match AS number received in peer Open message. Added expected
remote AS to BGP Session Preferences to allow for this comparison.

Moved validate method from client validator to registry that is used
anytime a peer connects.

This commit is intended to be cherry-picked. Refactoring commit
will be followed up in master branch.

Change-Id: Ie8bfd8b51ee7654b284fe0b0d4ce1db5e365d55b
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
15 files changed:
bgp/rib-impl/src/main/java/org/opendaylight/controller/config/yang/bgp/rib/impl/BGPPeerModule.java
bgp/rib-impl/src/main/java/org/opendaylight/controller/config/yang/bgp/rib/impl/StrictBgpPeerRegistryModule.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/AbstractBGPSessionNegotiator.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPClientSessionNegotiator.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPServerSessionNegotiator.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPSessionProposalImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/spi/BGPPeerRegistry.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/spi/BGPSessionPreferences.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/ApiTest.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/BGPDispatcherImplTest.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/FSMTest.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistryTest.java
bgp/testtool/src/main/java/org/opendaylight/protocol/bgp/testtool/Main.java
bgp/testtool/src/test/java/org/opendaylight/protocol/bgp/testtool/BGPSpeakerMock.java

index 346852e318c0c9022c65de4ce22ab31919f29226..c1d0a699140dd944d61673e72107d1ee4e3cf3d7 100644 (file)
@@ -107,7 +107,7 @@ public final class BGPPeerModule extends org.opendaylight.controller.config.yang
         final List<BgpParameters> tlvs = getTlvs(r);
         final AsNumber remoteAs = getAsOrDefault(r);
         final String password = getPasswordOrNull();
-        final BGPSessionPreferences prefs = new BGPSessionPreferences(r.getLocalAs(), getHoldtimer(), r.getBgpIdentifier(), tlvs);
+        final BGPSessionPreferences prefs = new BGPSessionPreferences(r.getLocalAs(), getHoldtimer(), r.getBgpIdentifier(), remoteAs, tlvs);
         final BGPPeer bgpClientPeer;
         if (getPeerRole() != null) {
             bgpClientPeer = new BGPPeer(peerName(getHostWithoutValue()), r, getPeerRole());
index c6ca4e1e45f73dd876e73d3227649dcaa28f7e8a..6c2e85ff487b895bd11084b5621b7ac1b9812473 100644 (file)
@@ -10,6 +10,7 @@ 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;
 
 /**
 * Registry of BGP peers that allows only one connection per 2 peers
@@ -50,9 +51,9 @@ public class StrictBgpPeerRegistryModule extends org.opendaylight.controller.con
         }
 
         @Override
-        public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final AsNumber asNumber)
+        public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final AsNumber asNumber, final Open open)
                 throws BGPDocumentedException {
-            return this.global.getPeer(ip, sourceId, remoteId, asNumber);
+            return this.global.getPeer(ip, sourceId, remoteId, asNumber, open);
         }
 
         @Override
index 9bd2d00d93198ac434c572073b405d85aab916c2..781a9d2b5178a8b3637735d86dd4068a254dd95c 100644 (file)
@@ -195,9 +195,8 @@ public abstract class AbstractBGPSessionNegotiator extends ChannelInboundHandler
         }
 
         try {
-            final BGPSessionListener peer = this.registry.getPeer(getRemoteIp(), getSourceId(openObj, getPreferences()),
-                    getDestinationId(openObj, getPreferences()), getAsNumber(openObj, getPreferences()));
-            this.sendMessage(new KeepaliveBuilder().build());
+            final BGPSessionListener peer = this.registry.getPeer(getRemoteIp(), getSourceId(openObj, getPreferences()), getDestinationId(openObj, getPreferences()), getAsNumber(openObj, getPreferences()), openObj);
+            sendMessage(new KeepaliveBuilder().build());
             this.session = new BGPSessionImpl(peer, this.channel, openObj, getPreferences(), this.registry);
             this.state = State.OPEN_CONFIRM;
             LOG.debug("Channel {} moved to OpenConfirm state with remote proposal {}", this.channel, openObj);
index 41e9b8cc27441d047a1357ace5416acc9743f1b8..423c7881d737d35a92075a15e4e5e7c9c255d7ac 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.bgp.rib.impl;
 
 import io.netty.channel.Channel;
 import io.netty.util.concurrent.Promise;
+import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
@@ -37,7 +38,7 @@ public final class BGPClientSessionNegotiator extends AbstractBGPSessionNegotiat
     }
 
     @Override
-    protected AsNumber getAsNumber(Open openMsg, BGPSessionPreferences preferences) {
-        return preferences.getMyAs();
+    protected AsNumber getAsNumber(final Open openMsg, final BGPSessionPreferences preferences) {
+        return AsNumberUtil.advertizedAsNumber(openMsg);
     }
 }
index 75cc519ee8a128f7ac692280207517397feb348d..327d62ab70d6a9fc591664485db89d1a095f2271 100644 (file)
@@ -39,7 +39,7 @@ public final class BGPServerSessionNegotiator extends AbstractBGPSessionNegotiat
     }
 
     @Override
-    protected AsNumber getAsNumber(Open openMsg, BGPSessionPreferences preferences) {
-        return new AsNumber(AsNumberUtil.advertizedAsNumber(openMsg));
+    protected AsNumber getAsNumber(final Open openMsg, final BGPSessionPreferences preferences) {
+        return AsNumberUtil.advertizedAsNumber(openMsg);
     }
 }
index da4883bb90126ad77a04e901ed7cabb31a1251e5..3f84c9ed2d2a8b158764d867a0fbfd61b4dcec71 100644 (file)
@@ -41,11 +41,15 @@ public final class BGPSessionProposalImpl implements BGPSessionProposal {
 
     private final BGPSessionPreferences prefs;
 
+    private final AsNumber remoteAs;
+
     public BGPSessionProposalImpl(final short holdTimer, final AsNumber as, final Ipv4Address bgpId,
-            final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables) {
+        final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables,
+        final AsNumber remoteAs) {
         this.holdTimer = holdTimer;
         this.as = as;
         this.bgpId = bgpId;
+        this.remoteAs = remoteAs;
         final List<OptionalCapabilities> caps = new ArrayList<>();
 
         for (final Entry<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> e : tables.entrySet()) {
@@ -58,7 +62,7 @@ public final class BGPSessionProposalImpl implements BGPSessionProposal {
         caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(
             CParameters1.class, new CParameters1Builder().setGracefulRestartCapability(
                 new GracefulRestartCapabilityBuilder().build()).build()).build()).build());
-        this.prefs = new BGPSessionPreferences(as, holdTimer, bgpId, Lists.newArrayList(
+        this.prefs = new BGPSessionPreferences(as, holdTimer, bgpId, remoteAs, Lists.newArrayList(
             new BgpParametersBuilder().setOptionalCapabilities(caps).build()));
     }
 
index 1e997a9c16ec4ace97585fcf9cbe2056745a3028..826d2f284a8aa409b9db9028f9da0ae44b671651 100644 (file)
@@ -9,20 +9,26 @@
 package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.base.MoreObjects;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
 import com.google.common.net.InetAddresses;
 import com.google.common.primitives.UnsignedInts;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
+import java.util.List;
 import java.util.Map;
 import javax.annotation.concurrent.GuardedBy;
 import javax.annotation.concurrent.ThreadSafe;
+import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
+import org.opendaylight.protocol.bgp.parser.impl.message.open.As4CapabilityHandler;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
@@ -31,6 +37,12 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
+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.open.message.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParameters;
+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.As4BytesCapability;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -90,15 +102,17 @@ 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 {
+        final Ipv4Address remoteId, final AsNumber remoteAsNumber, final Open openObj) throws BGPDocumentedException {
         Preconditions.checkNotNull(ip);
         Preconditions.checkNotNull(sourceId);
         Preconditions.checkNotNull(remoteId);
-        Preconditions.checkNotNull(asNumber);
+        Preconditions.checkNotNull(remoteAsNumber);
+
+        final BGPSessionPreferences prefs = getPeerPreferences(ip);
 
         checkPeerConfigured(ip);
 
-        final BGPSessionId currentConnection = new BGPSessionId(sourceId, remoteId, asNumber);
+        final BGPSessionId currentConnection = new BGPSessionId(sourceId, remoteId, remoteAsNumber);
         final BGPSessionListener p = this.peers.get(ip);
 
         final BGPSessionId previousConnection = this.sessionIds.get(ip);
@@ -147,18 +161,58 @@ 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 {}", getPeerPreferences(ip).getMyAs(), asNumber);
-                throw new BGPDocumentedException("Peer AS number mismatch", BGPError.BAD_PEER_AS);
-            }
         }
+        validateAs(openObj, prefs);
 
         // Map session id to peer IP address
         this.sessionIds.put(ip, currentConnection);
         return p;
     }
 
+    private void validateAs(final Open openObj, final BGPSessionPreferences localPref) throws BGPDocumentedException {
+        final AsNumber remoteAs = AsNumberUtil.advertizedAsNumber(openObj);
+        if (!remoteAs.equals(localPref.getExpectedRemoteAs())) {
+            LOG.warn("Unexpected remote AS number. Expecting {}, got {}", remoteAs, localPref.getExpectedRemoteAs());
+            throw new BGPDocumentedException("Peer AS number mismatch", BGPError.BAD_PEER_AS);
+        }
+
+        // https://tools.ietf.org/html/rfc6286#section-2.2
+        if (openObj.getBgpIdentifier() != null && openObj.getBgpIdentifier().equals(localPref.getBgpId())) {
+            LOG.warn("Remote and local BGP Identifiers are the same: {}", openObj.getBgpIdentifier());
+            throw new BGPDocumentedException("Remote and local BGP Identifiers are the same.", BGPError.BAD_BGP_ID);
+        }
+        final List<BgpParameters> prefs = openObj.getBgpParameters();
+        if (prefs != null) {
+            if (getAs4BytesCapability(localPref.getParams()).isPresent() && !getAs4BytesCapability(prefs).isPresent()) {
+                throw new BGPDocumentedException("The peer must advertise AS4Bytes capability.", BGPError.UNSUPPORTED_CAPABILITY, serializeAs4BytesCapability(getAs4BytesCapability(localPref.getParams()).get()));
+            }
+            if (!prefs.containsAll(localPref.getParams())) {
+                LOG.info("BGP Open message session parameters differ, session still accepted.");
+            }
+        } else {
+            throw new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.", BGPError.UNSPECIFIC_OPEN_ERROR);
+        }
+    }
+
+    private static Optional<As4BytesCapability> getAs4BytesCapability(final List<BgpParameters> prefs) {
+        for (final BgpParameters param : prefs) {
+            for (final OptionalCapabilities capa : param.getOptionalCapabilities()) {
+                final CParameters cParam = capa.getCParameters();
+                if (cParam.getAs4BytesCapability() != null) {
+                    return Optional.of(cParam.getAs4BytesCapability());
+                }
+            }
+        }
+        return Optional.absent();
+    }
+
+    private static byte[] serializeAs4BytesCapability(final As4BytesCapability as4Capability) {
+        final ByteBuf buffer = Unpooled.buffer(1 /*CODE*/ + 1 /*LENGTH*/ + Integer.SIZE / Byte.SIZE /*4 byte value*/);
+        final As4CapabilityHandler serializer = new As4CapabilityHandler();
+        serializer.serializeCapability(new CParametersBuilder().setAs4BytesCapability(as4Capability).build(), buffer);
+        return buffer.array();
+    }
+
     @Override
     public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
         Preconditions.checkNotNull(ip);
@@ -199,7 +253,8 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
     }
 
     /**
-     * Session identifier that contains (source Bgp Id) -> (destination Bgp Id)
+     * Session identifier that contains (source Bgp Id) -> (destination Bgp Id) AsNumber is the remoteAs coming from
+     * remote Open message
      */
     private static final class BGPSessionId {
 
index 80a522b785b6eef0f27599fde598b633927dffcb..ac0ffea5865c946f45febb0033d01973051c3435 100644 (file)
@@ -13,6 +13,7 @@ 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;
 
 /**
  * Registry that contains configured bgp peers ready for when a bgp session is established with remote peer.
@@ -58,12 +59,13 @@ public interface BGPPeerRegistry extends AutoCloseable {
      * @param sourceId BGP ID of peer that initiated the session (current device or remote peer)
      * @param remoteId BGP ID of peer that accepted the session (current device or remote peer)
      * @param asNumber remote AS number
+     * @param open remote Open message
      * @return BGPSessionListener configured Peer as BGP listener
      *
      * @throws BGPDocumentedException if session establishment cannot be finished successfully
      * @throws java.lang.IllegalStateException if there is no peer configured for provided ip address
      */
-    BGPSessionListener getPeer(IpAddress ip, Ipv4Address sourceId, Ipv4Address remoteId, AsNumber asNumber) throws BGPDocumentedException;
+    BGPSessionListener getPeer(IpAddress ip, Ipv4Address sourceId, Ipv4Address remoteId, AsNumber asNumber, Open open) throws BGPDocumentedException;
 
     /**
      * @param ip address of remote peer
index 79cf1e121f480b3af48fbf9d3b00b213ba0b9c28..47b5f164cd9c293b1a56e7826d3b99c40a7f3111 100644 (file)
@@ -25,18 +25,23 @@ public final class BGPSessionPreferences {
 
     private final List<BgpParameters> params;
 
+    private final AsNumber remoteAs;
+
     /**
      * Creates a new DTO for Open message.
      *
      * @param as local AS number
      * @param hold preferred hold timer value, in seconds
      * @param bgpId local BGP Identifier
+     * @param remoteAs expected remote As Number
      * @param params list of advertised parameters
      */
-    public BGPSessionPreferences(final AsNumber as, final int hold, final Ipv4Address bgpId, final List<BgpParameters> params) {
+    public BGPSessionPreferences(final AsNumber as, final int hold, final Ipv4Address bgpId, final AsNumber remoteAs,
+        final List<BgpParameters> params) {
         this.as = as;
         this.hold = hold;
         this.bgpId = bgpId;
+        this.remoteAs = remoteAs;
         this.params = params;
     }
 
@@ -67,6 +72,15 @@ public final class BGPSessionPreferences {
         return this.bgpId;
     }
 
+    /**
+     * Returns expected remote AS number.
+     *
+     * @return AS number
+     */
+    public AsNumber getExpectedRemoteAs() {
+        return this.remoteAs;
+    }
+
     /**
      * Gets a list of advertised bgp parameters.
      *
index 2bc7f851c017a070260fb1b54926b74277c41af8..651aded837aae577108705dbf45c4820314c63c8 100644 (file)
@@ -45,7 +45,7 @@ public class ApiTest {
         final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> map = new HashMap<>();
         map.put(key, value);
 
-        final BGPSessionProposalImpl proposal = new BGPSessionProposalImpl((short) 5, new AsNumber(58L), null, map);
+        final BGPSessionProposalImpl proposal = new BGPSessionProposalImpl((short) 5, new AsNumber(58L), null, map, null);
         final BGPSessionPreferences sp = proposal.getProposal();
         assertNull(sp.getBgpId());
         assertEquals(proposal.getHoldTimer(), sp.getHoldTime());
index 4daf30553dbb3fb3258106bb1f8787d69225741f..5f2671e651d5d53963599c10194581001373b8b0 100644 (file)
@@ -81,7 +81,7 @@ public class BGPDispatcherImplTest {
         final ChannelFuture future = this.dispatcher.createServer(this.registry, ADDRESS, new BGPServerSessionValidator());
         future.addListener(new GenericFutureListener<Future<Void>>() {
             @Override
-            public void operationComplete(Future<Void> future) {
+            public void operationComplete(final Future<Void> future) {
                 if(!future.isSuccess()) {
                     Assert.fail("Failed to create server.");
                 }
@@ -137,7 +137,7 @@ public class BGPDispatcherImplTest {
                 .setAfi(this.ipv4tt.getAfi()).setSafi(this.ipv4tt.getSafi()).build()).build())
             .setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(new AsNumber(30L)).build()).build()).build());
         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
-        return new BGPSessionPreferences(AS_NUMBER, (short) 4, new Ipv4Address(socketAddress.getAddress().getHostAddress()), tlvs);
+        return new BGPSessionPreferences(AS_NUMBER, (short) 4, new Ipv4Address(socketAddress.getAddress().getHostAddress()), AS_NUMBER, tlvs);
     }
 
 }
index 37396680bd3f41b160d2ecb8fdcefaa57ffee7e6..cb17767ab9cdc280f703b9398ae043d4fdc9aa3f 100644 (file)
@@ -14,7 +14,6 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-
 import com.google.common.collect.Lists;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
@@ -107,7 +106,7 @@ public class FSMTest {
 
 
         tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(capas).build());
-        final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber(30L), (short) 3, new Ipv4Address("1.1.1.1"), tlvs);
+        final BGPSessionPreferences prefs = new BGPSessionPreferences(new AsNumber(30L), (short) 3, new Ipv4Address("1.1.1.1"), new AsNumber(30L), tlvs);
 
         final ChannelFuture f = mock(ChannelFuture.class);
         doReturn(null).when(f).addListener(any(GenericFutureListener.class));
index a4812b3e34a2442edc70e9657f067665c1dbeba1..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,17 +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
@@ -53,9 +70,9 @@ public class StrictBGPPeerRegistryTest {
         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, AS1);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -71,7 +88,7 @@ public class StrictBGPPeerRegistryTest {
         final Ipv4Address to = new Ipv4Address("255.255.255.255");
 
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, AS1);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, this.AS1, createOpen(to, this.AS1));
         } catch (final IllegalStateException e) {
             return;
         }
@@ -92,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, AS1);
+        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, AS1);
+        final BGPSessionListener returnedSession2 = this.droppingBGPSessionRegistry.getPeer(remoteIp2, from, to2, this.AS1, createOpen(to, this.AS1));
         assertSame(session2, returnedSession2);
 
         Mockito.verifyZeroInteractions(session1);
@@ -110,9 +127,9 @@ public class StrictBGPPeerRegistryTest {
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, AS1);
+        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, this.AS1, createOpen(lower, this.AS1));
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, AS1);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, this.AS1, createOpen(higher, this.AS1));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -130,8 +147,8 @@ public class StrictBGPPeerRegistryTest {
         final ReusableBGPPeer session1 = getMockSession();
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, lower, higher, AS1);
-        this.droppingBGPSessionRegistry.getPeer(remoteIp, higher, lower, AS1);
+        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,9 +161,9 @@ public class StrictBGPPeerRegistryTest {
         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, to, to, AS1);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, to, to, this.AS1, createOpen(to, this.AS1));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
@@ -165,8 +182,8 @@ public class StrictBGPPeerRegistryTest {
         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();
     }
 
@@ -180,9 +197,9 @@ public class StrictBGPPeerRegistryTest {
         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;
@@ -202,7 +219,7 @@ public class StrictBGPPeerRegistryTest {
         this.droppingBGPSessionRegistry.addPeer(remoteIp, session1, this.mockPreferences);
 
         try {
-            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2);
+            this.droppingBGPSessionRegistry.getPeer(remoteIp, from, to, as2, createOpen(to, as2));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.BAD_PEER_AS, e.getError());
             return;
@@ -217,7 +234,7 @@ public class StrictBGPPeerRegistryTest {
         return mock;
     }
 
-    public BGPSessionPreferences getMockPreferences() {
-        return new BGPSessionPreferences(AS1, 1,  new Ipv4Address("0.0.0.1"), Collections.<BgpParameters>emptyList());
+    public BGPSessionPreferences getMockPreferences(final AsNumber remoteAs) {
+        return new BGPSessionPreferences(this.AS1, 1, new Ipv4Address("0.0.0.1"), remoteAs, Collections.<BgpParameters> emptyList());
     }
 }
index 369ccbe1f377cccd0089994bcaa02429ae4d9d94..d79f15bc289249a578c5031b4debcaf4b4df8a22 100644 (file)
@@ -101,7 +101,7 @@ public final class Main {
         tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
         tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
 
-        final BGPSessionProposalImpl prop = new BGPSessionProposalImpl(holdTimerValue, as, new Ipv4Address("25.25.25.2"), tables);
+        final BGPSessionProposalImpl prop = new BGPSessionProposalImpl(holdTimerValue, as, new Ipv4Address("25.25.25.2"), tables, as);
 
         final BGPSessionPreferences proposal = prop.getProposal();
 
index 80a8336b1672f777391861f127919a5e586e5d49..3144f896e1cb177834a5cb9cb5181b2e0b2a0f71 100644 (file)
@@ -47,12 +47,12 @@ public class BGPSpeakerMock {
 
     private BGPSpeakerMock(final BGPServerSessionNegotiatorFactory negotiatorFactory, final BGPHandlerFactory factory,
                            final DefaultPromise<BGPSessionImpl> defaultPromise) {
-        disp = new BGPDispatcherImpl(null, new NioEventLoopGroup(), new NioEventLoopGroup());
+        this.disp = new BGPDispatcherImpl(null, new NioEventLoopGroup(), new NioEventLoopGroup());
         this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
         this.factory = Preconditions.checkNotNull(factory);
 
 
-        peerRegistry = new BGPPeerRegistry() {
+        this.peerRegistry = new BGPPeerRegistry() {
             @Override
             public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {
             }
@@ -67,13 +67,13 @@ public class BGPSpeakerMock {
             }
 
             @Override
-            public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final AsNumber asNumber) throws BGPDocumentedException {
+            public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final AsNumber asNumber, final Open open) throws BGPDocumentedException {
                 return new SpeakerSessionListener();
             }
 
             @Override
             public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
-                return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), tables).getProposal();
+                return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), BGPSpeakerMock.this.tables, new AsNumber(72L)).getProposal();
             }
 
             @Override
@@ -86,9 +86,9 @@ public class BGPSpeakerMock {
             }
         };
 
-        tables = new HashMap<>();
-        tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
-        tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
+        this.tables = new HashMap<>();
+        this.tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
+        this.tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
     }
 
     public void main(final String[] args) {
@@ -98,7 +98,7 @@ public class BGPSpeakerMock {
             public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
                 // NOOP
             }
-        }, peerRegistry);
+        }, this.peerRegistry);
 
         final BGPSpeakerMock mock = new BGPSpeakerMock(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
 
@@ -106,7 +106,7 @@ public class BGPSpeakerMock {
     }
 
     private void createServer(final InetSocketAddress address) {
-        disp.createServer(peerRegistry,address, new BGPSessionValidator() {
+        this.disp.createServer(this.peerRegistry,address, new BGPSessionValidator() {
             @Override
             public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
                 // NOOP