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;
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.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.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;
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) {
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
// Map session id to peer IP address
this.sessionIds.put(ip, currentConnection);
+ for (final PeerRegistrySessionListener peerRegistrySessionListener : this.sessionListeners) {
+ peerRegistrySessionListener.onSessionCreated(ip);
+ }
return p;
}
}
// 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);
}
.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);
+ }
+ }
+ };
+ }
+}