BGPCEP-764: Use full Ipv6 form under StrictBGPPeerRegistry 16/69816/1
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Mon, 19 Mar 2018 16:18:18 +0000 (17:18 +0100)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Thu, 22 Mar 2018 13:33:14 +0000 (14:33 +0100)
Ipv6 equal is not capable of handle short or full form
of Ipv6, returning false when comparing different versions.
Therefore use full form under registry.

Change-Id: I2127f20e847860792d0f1c18b0ae243825a9d86a
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
(cherry picked from commit 889700a070e725f44ee3d4fe215d93312ac8f0bf)

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

index 18161d630940523b143b91b2c64b671b7571761c..b2c7d69eed4148fe2559399aac83117fe2c47cb4 100644 (file)
@@ -8,6 +8,8 @@
 
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
@@ -37,6 +39,7 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistryListener;
 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistrySessionListener;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
+import org.opendaylight.protocol.util.Ipv6Util;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
@@ -80,56 +83,67 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
     }
 
     @Override
-    public synchronized void addPeer(final IpAddress ip, final BGPSessionListener peer, final BGPSessionPreferences preferences) {
-        Preconditions.checkNotNull(ip);
-        Preconditions.checkArgument(!this.peers.containsKey(ip), "Peer for %s already present", ip);
-        this.peers.put(ip, Preconditions.checkNotNull(peer));
-        Preconditions.checkNotNull(preferences.getMyAs());
-        Preconditions.checkNotNull(preferences.getHoldTime());
-        Preconditions.checkNotNull(preferences.getParams());
-        Preconditions.checkNotNull(preferences.getBgpId());
-        this.peerPreferences.put(ip, preferences);
+    public synchronized void addPeer(final IpAddress oldIp, final BGPSessionListener peer,
+            final BGPSessionPreferences preferences) {
+        IpAddress fullIp = getFullIp(oldIp);
+        Preconditions.checkArgument(!this.peers.containsKey(fullIp),
+                "Peer for %s already present", fullIp);
+        this.peers.put(fullIp, requireNonNull(peer));
+        requireNonNull(preferences.getMyAs());
+        requireNonNull(preferences.getParams());
+        requireNonNull(preferences.getBgpId());
+        this.peerPreferences.put(fullIp, preferences);
         for (final PeerRegistryListener peerRegistryListener : this.listeners) {
-            peerRegistryListener.onPeerAdded(ip, preferences);
+            peerRegistryListener.onPeerAdded(fullIp, preferences);
+        }
+    }
+
+    private IpAddress getFullIp(final IpAddress ip) {
+        requireNonNull(ip);
+        if (ip.getIpv6Address() != null) {
+            return new IpAddress(Ipv6Util.getFullForm(ip.getIpv6Address()));
         }
+        return ip;
     }
 
     @Override
-    public synchronized void removePeer(final IpAddress ip) {
-        Preconditions.checkNotNull(ip);
-        this.peers.remove(ip);
+    public synchronized void removePeer(final IpAddress oldIp) {
+        IpAddress fullIp = getFullIp(oldIp);
+        this.peers.remove(fullIp);
         for (final PeerRegistryListener peerRegistryListener : this.listeners) {
-            peerRegistryListener.onPeerRemoved(ip);
+            peerRegistryListener.onPeerRemoved(fullIp);
         }
     }
 
     @Override
-    public synchronized void removePeerSession(final IpAddress ip) {
-        Preconditions.checkNotNull(ip);
-        this.sessionIds.remove(ip);
+    public synchronized void removePeerSession(final IpAddress oldIp) {
+        IpAddress fullIp = getFullIp(oldIp);
+        this.sessionIds.remove(fullIp);
         for (final PeerRegistrySessionListener peerRegistrySessionListener : this.sessionListeners) {
-            peerRegistrySessionListener.onSessionRemoved(ip);
+            peerRegistrySessionListener.onSessionRemoved(fullIp);
         }
     }
 
     @Override
-    public boolean isPeerConfigured(final IpAddress ip) {
-        Preconditions.checkNotNull(ip);
-        return this.peers.containsKey(ip);
+    public boolean isPeerConfigured(final IpAddress oldIp) {
+        IpAddress fullIp = getFullIp(oldIp);
+        return this.peers.containsKey(fullIp);
     }
 
     private void checkPeerConfigured(final IpAddress ip) {
-        Preconditions.checkState(isPeerConfigured(ip), "BGP peer with ip: %s not configured, configured peers are: %s", ip, this.peers.keySet());
+        Preconditions.checkState(isPeerConfigured(ip),
+                "BGP peer with ip: %s not configured, configured peers are: %s",
+                ip, this.peers.keySet());
     }
 
     @Override
     public synchronized BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId,
         final Ipv4Address remoteId, final Open openObj) throws BGPDocumentedException {
-        Preconditions.checkNotNull(ip);
-        Preconditions.checkNotNull(sourceId);
-        Preconditions.checkNotNull(remoteId);
+        requireNonNull(ip);
+        requireNonNull(sourceId);
+        requireNonNull(remoteId);
         final AsNumber remoteAsNumber = AsNumberUtil.advertizedAsNumber(openObj);
-        Preconditions.checkNotNull(remoteAsNumber);
+        requireNonNull(remoteAsNumber);
 
         final BGPSessionPreferences prefs = getPeerPreferences(ip);
 
@@ -146,20 +160,21 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
 
             // Session reestablished with different ids
             if (!previousConnection.equals(currentConnection)) {
-                LOG.warn("BGP session with {} {} has to be dropped. Same session already present {}", ip, currentConnection, previousConnection);
+                LOG.warn("BGP session with {} {} has to be dropped. Same session already present {}", ip,
+                        currentConnection, previousConnection);
                 throw new BGPDocumentedException(
-                    String.format("BGP session with %s %s has to be dropped. Same session already present %s",
-                        ip, currentConnection, previousConnection),
+                        String.format("BGP session with %s %s has to be dropped. Same session already present %s",
+                                ip, currentConnection, previousConnection),
                         BGPError.CEASE);
 
                 // Session reestablished with lower source bgp id, dropping current
             } else if (previousConnection.isHigherDirection(currentConnection) ||
                     previousConnection.hasHigherAsNumber(currentConnection)) {
-                LOG.warn("BGP session with {} {} has to be dropped. Opposite session already present", ip, currentConnection);
+                LOG.warn("BGP session with {} {} has to be dropped. Opposite session already present",
+                        ip, currentConnection);
                 throw new BGPDocumentedException(
-                    String.format("BGP session with %s initiated %s has to be dropped. Opposite session already present",
-                        ip, currentConnection),
-                        BGPError.CEASE);
+                        String.format("BGP session with %s initiated %s has to be dropped. "
+                                + "Opposite session already present", ip, currentConnection), BGPError.CEASE);
 
                 // Session reestablished with higher source bgp id, dropping previous
             } else if (currentConnection.isHigherDirection(previousConnection) ||
@@ -169,11 +184,11 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
                 return this.peers.get(ip);
                 // Session reestablished with same source bgp id, dropping current as duplicate
             } else {
-                LOG.warn("BGP session with %s initiated from %s to %s has to be dropped. Same session already present", ip, sourceId, remoteId);
+                LOG.warn("BGP session with %s initiated from %s to %s has to be dropped. Same session already present",
+                        ip, sourceId, remoteId);
                 throw new BGPDocumentedException(
-                    String.format("BGP session with %s initiated %s has to be dropped. Same session already present",
-                        ip, currentConnection),
-                        BGPError.CEASE);
+                        String.format("BGP session with %s initiated %s has to be dropped. "
+                                        + "Same session already present", ip, currentConnection), BGPError.CEASE);
             }
         }
         validateAs(remoteAsNumber, openObj, prefs);
@@ -186,27 +201,32 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         return p;
     }
 
-    private static void validateAs(final AsNumber remoteAs, final Open openObj, final BGPSessionPreferences localPref) throws BGPDocumentedException {
+    private static void validateAs(final AsNumber remoteAs, final Open openObj, final BGPSessionPreferences localPref)
+            throws BGPDocumentedException {
         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().getValue().equals(localPref.getBgpId().getValue())) {
+        if (openObj.getBgpIdentifier() != null
+                && openObj.getBgpIdentifier().getValue().equals(localPref.getBgpId().getValue())) {
             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()));
+                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);
+            throw new BGPDocumentedException("Open message unacceptable. Check the configuration of BGP speaker.",
+                    BGPError.UNSPECIFIC_OPEN_ERROR);
         }
     }
 
@@ -223,7 +243,8 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
     }
 
     private static byte[] serializeAs4BytesCapability(final As4BytesCapability as4Capability) {
-        final ByteBuf buffer = Unpooled.buffer(1 /*CODE*/ + 1 /*LENGTH*/ + Integer.SIZE / Byte.SIZE /*4 byte value*/);
+        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();
@@ -231,24 +252,28 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
 
     @Override
     public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
-        Preconditions.checkNotNull(ip);
+        requireNonNull(ip);
         checkPeerConfigured(ip);
         return this.peerPreferences.get(ip);
     }
 
     /**
-     * Creates IpAddress from SocketAddress. Only InetSocketAddress is accepted with inner address: Inet4Address and Inet6Address.
+     * Creates IpAddress from SocketAddress. Only InetSocketAddress
+     * is accepted with inner address: Inet4Address and Inet6Address.
      *
      * @param socketAddress socket address to transform
      * @return IpAddress equivalent to given socket address
      * @throws IllegalArgumentException if submitted socket address is not InetSocketAddress[ipv4 | ipv6]
      */
     public static IpAddress getIpAddress(final SocketAddress socketAddress) {
-        Preconditions.checkNotNull(socketAddress);
-        Preconditions.checkArgument(socketAddress instanceof InetSocketAddress, "Expecting InetSocketAddress but was %s", socketAddress.getClass());
+        requireNonNull(socketAddress);
+        Preconditions.checkArgument(socketAddress instanceof InetSocketAddress,
+                "Expecting InetSocketAddress but was %s", socketAddress.getClass());
         final InetAddress inetAddress = ((InetSocketAddress) socketAddress).getAddress();
 
-        Preconditions.checkArgument(inetAddress instanceof Inet4Address || inetAddress instanceof Inet6Address, "Expecting %s or %s but was %s", Inet4Address.class, Inet6Address.class, inetAddress.getClass());
+        Preconditions.checkArgument(inetAddress instanceof Inet4Address
+                || inetAddress instanceof Inet6Address, "Expecting %s or %s but was %s",
+                Inet4Address.class, Inet6Address.class, inetAddress.getClass());
         return IetfInetUtil.INSTANCE.ipAddressFor(inetAddress);
     }
 
@@ -275,9 +300,9 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         private final AsNumber asNumber;
 
         BGPSessionId(final Ipv4Address from, final Ipv4Address to, final AsNumber asNumber) {
-            this.from = Preconditions.checkNotNull(from);
-            this.to = Preconditions.checkNotNull(to);
-            this.asNumber = Preconditions.checkNotNull(asNumber);
+            this.from = requireNonNull(from);
+            this.to = requireNonNull(to);
+            this.asNumber = requireNonNull(asNumber);
         }
 
         /**
index f71ce640a666dd9605167a5465d5f80f38e4af67..6957a689fecf22a7cde76e13eb619bb8643e88e6 100644 (file)
@@ -35,14 +35,14 @@ public interface BGPPeerRegistry extends AutoCloseable {
      *
      * @param ip address of remote peer
      */
-    void removePeer(IpAddress ip);
+    void removePeer(@Nonnull IpAddress ip);
 
     /**
      * Remove peer session from registry.
      *
      * @param ip address of remote peer
      */
-    void removePeerSession(IpAddress ip);
+    void removePeerSession(@Nonnull IpAddress ip);
 
     /**
      * Check whether peer on provided IP address is present in this registry.
@@ -50,7 +50,7 @@ public interface BGPPeerRegistry extends AutoCloseable {
      * @param ip address of remote peer
      * @return true if peer is present false otherwise
      */
-    boolean isPeerConfigured(IpAddress ip);
+    boolean isPeerConfigured(@Nonnull IpAddress ip);
 
     /**
      * Get configured peer after BGP session was successfully established. Called by negotiators.