BUG-3888 : fix comparing ASnumbers 56/24756/1
authorDana Kutenicsova <dkutenic@cisco.com>
Wed, 22 Jul 2015 12:56:46 +0000 (14:56 +0200)
committerDana Kutenicsova <dkutenic@cisco.com>
Tue, 4 Aug 2015 11:22:38 +0000 (13:22 +0200)
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>
(cherry picked from commit d99e924ff10bb7652ef12e7989540b9ffecb2dd3)

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 9ace4ae15b64619fe766aa90d6aa939c5f623a58..d7a89403a1abbccd0b2ab301a1ea7ff5eb24f44b 100644 (file)
@@ -108,7 +108,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 0539e5d4bc733c648e70045fb1a39e406b70ad69..ea40da2cbedb6eb2b45405d3f7397f0129743b36 100644 (file)
@@ -191,9 +191,8 @@ public abstract class AbstractBGPSessionNegotiator extends AbstractSessionNegoti
         }
 
         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 cbae35b5164e29c4ee3aef4ddf8ec619d2580194..d7fcaceaab36d71dc6364f3f6b90ebe731ddb0e1 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 GracefulRestartCaseBuilder().setGracefulRestartCapability(
                     new GracefulRestartCapabilityBuilder().build()).build()).build());
-        this.prefs = new BGPSessionPreferences(as, holdTimer, bgpId, Lists.newArrayList(new BgpParametersBuilder().setOptionalCapabilities(caps).build()));
+        this.prefs = new BGPSessionPreferences(as, holdTimer, bgpId, this.remoteAs, Lists.newArrayList(new BgpParametersBuilder().setOptionalCapabilities(caps).build()));
     }
 
     @Override
index 57afa27e02ada4643bffa69a0e8299caf8cb485a..1a3ae36ad64c210441e58b2e9f0f7cfe0039b49a 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,13 @@ 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.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.CParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.as4.bytes._case.As4BytesCapability;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -90,15 +103,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 +162,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 instanceof As4BytesCase) {
+                    return Optional.of(((As4BytesCase)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 As4BytesCaseBuilder().setAs4BytesCapability(as4Capability).build(), buffer);
+        return buffer.array();
+    }
+
     @Override
     public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
         Preconditions.checkNotNull(ip);
@@ -200,7 +255,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 ccd500801235a009cdfeff6bbe569901cec0491e..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.
@@ -57,13 +58,14 @@ public interface BGPPeerRegistry extends AutoCloseable {
      * @param ip address of remote peer
      * @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 id of the AS
-     * @return configured Peer as BGP listener
+     * @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 aa67f2e491e918cd6e85dfa9223bfdf3bc0c9c5c..9a546bf03a4f03d364493531b0d4d92c06d5517a 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 4ef6ca1ac2c46b33db4acbc794ab030ca0ec450b..3450ca9ea32554e5f8fdf5714d31d73c4326912f 100644 (file)
@@ -27,7 +27,7 @@ public class ApiTest {
         final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> map = new HashMap<>();
         map.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
 
-        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 1bbaf276f8b84c459659053ba4b64654000c7f9f..ad5a80f6e8c10adca63e9f513cbad4627bb1afde 100644 (file)
@@ -80,7 +80,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 {
         capas.add(new OptionalCapabilitiesBuilder().setCParameters(new As4BytesCaseBuilder().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 9ecbc613f01e79b3d8bf822ecbeb93864e2d4b3e..55ea30b2bd3672ba03785cd7c758fead1dd37984 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;
@@ -106,7 +105,7 @@ public class FSMTest {
             new GracefulRestartCaseBuilder().setGracefulRestartCapability(
                 new GracefulRestartCapabilityBuilder().build()).build()).build());
         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 360331e50de32fe34ee2ff00aa48d0aef4251814..40633540f51f8fc396c5752c9d932988b3ed4158 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,31 @@ 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.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.As4BytesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.as4.bytes._case.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 As4BytesCaseBuilder().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 +69,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 +87,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 +108,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 +126,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 +146,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 +160,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 +181,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 +196,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 +218,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 +233,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());
     }
-}
\ No newline at end of file
+}
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 280788c53b03f89a571579f4b648fa1b480aa28a..d4159c526d2905ddd2f4d23036909bae948c5b65 100644 (file)
@@ -86,13 +86,13 @@ public class BGPSpeakerMock<M, S extends ProtocolSession<M>, L extends SessionLi
             }
 
             @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"), tables, new AsNumber(72L)).getProposal();
             }
 
             @Override