Bump versions by x.y.(z+1)
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / StrictBGPPeerRegistry.java
index 0a57de9204df27ec66440a0aacf3ca898ea5f4cf..541da34782198d94fcf165aac09fc67091f64d50 100644 (file)
@@ -21,8 +21,11 @@ import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 import javax.annotation.concurrent.GuardedBy;
 import javax.annotation.concurrent.ThreadSafe;
 import org.opendaylight.protocol.bgp.parser.AsNumberUtil;
@@ -31,17 +34,20 @@ 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.PeerRegistryListener;
+import org.opendaylight.protocol.bgp.rib.impl.spi.PeerRegistrySessionListener;
 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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
+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;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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.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.opendaylight.yangtools.concepts.AbstractRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,6 +70,14 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
     private final Map<IpAddress, BGPSessionId> sessionIds = Maps.newHashMap();
     @GuardedBy("this")
     private final Map<IpAddress, BGPSessionPreferences> peerPreferences = Maps.newHashMap();
+    @GuardedBy("this")
+    private final Set<PeerRegistryListener> listeners = new HashSet<>();
+    @GuardedBy("this")
+    private final Set<PeerRegistrySessionListener> sessionListeners = new HashSet<>();
+
+    public static BGPPeerRegistry instance() {
+        return GLOBAL;
+    }
 
     @Override
     public synchronized void addPeer(final IpAddress ip, final BGPSessionListener peer, final BGPSessionPreferences preferences) {
@@ -75,18 +89,27 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         Preconditions.checkNotNull(preferences.getParams());
         Preconditions.checkNotNull(preferences.getBgpId());
         this.peerPreferences.put(ip, preferences);
+        for (final PeerRegistryListener peerRegistryListener : this.listeners) {
+            peerRegistryListener.onPeerAdded(ip, preferences);
+        }
     }
 
     @Override
     public synchronized void removePeer(final IpAddress ip) {
         Preconditions.checkNotNull(ip);
         this.peers.remove(ip);
+        for (final PeerRegistryListener peerRegistryListener : this.listeners) {
+            peerRegistryListener.onPeerRemoved(ip);
+        }
     }
 
     @Override
     public synchronized void removePeerSession(final IpAddress ip) {
         Preconditions.checkNotNull(ip);
         this.sessionIds.remove(ip);
+        for (final PeerRegistrySessionListener peerRegistrySessionListener : this.sessionListeners) {
+            peerRegistrySessionListener.onSessionRemoved(ip);
+        }
     }
 
     @Override
@@ -166,17 +189,20 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
 
         // Map session id to peer IP address
         this.sessionIds.put(ip, currentConnection);
+        for (final PeerRegistrySessionListener peerRegistrySessionListener : this.sessionListeners) {
+            peerRegistrySessionListener.onSessionCreated(ip);
+        }
         return p;
     }
 
-    private 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().equals(localPref.getBgpId())) {
+        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);
         }
@@ -232,10 +258,7 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         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());
-        if(inetAddress instanceof Inet4Address) {
-            return new IpAddress(new Ipv4Address(inetAddress.getHostAddress()));
-        }
-        return new IpAddress(new Ipv6Address(inetAddress.getHostAddress()));
+        return IetfInetUtil.INSTANCE.ipAddressFor(inetAddress);
     }
 
     @Override
@@ -322,4 +345,36 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
                 .toString();
         }
     }
-}
\ No newline at end of file
+
+    @Override
+    public synchronized AutoCloseable registerPeerRegisterListener(final PeerRegistryListener listener) {
+        this.listeners.add(listener);
+        for (final Entry<IpAddress, BGPSessionPreferences> entry : this.peerPreferences.entrySet()) {
+            listener.onPeerAdded(entry.getKey(), entry.getValue());
+        }
+        return new AbstractRegistration() {
+            @Override
+            protected void removeRegistration() {
+                synchronized (StrictBGPPeerRegistry.this) {
+                    StrictBGPPeerRegistry.this.listeners.remove(listener);
+                }
+            }
+        };
+    }
+
+    @Override
+    public synchronized AutoCloseable registerPeerSessionListener(final PeerRegistrySessionListener listener) {
+        this.sessionListeners.add(listener);
+        for (final IpAddress ipAddress : this.sessionIds.keySet()) {
+            listener.onSessionCreated(ipAddress);
+        }
+        return new AbstractRegistration() {
+            @Override
+            protected void removeRegistration() {
+                synchronized (StrictBGPPeerRegistry.this) {
+                    StrictBGPPeerRegistry.this.sessionListeners.remove(listener);
+                }
+            }
+        };
+    }
+}