BUG-2109 : clear BGP session after it was already initialized
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / StrictBGPPeerRegistry.java
index 3475190fa0462018a716348afe04eac8baf28a5c..d33e8be8b3b440b2753b12b96ff9530aeaab6f05 100644 (file)
@@ -94,47 +94,53 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         checkPeerConfigured(ip);
 
         final BGPSessionId currentConnection = new BGPSessionId(sourceId, remoteId);
+        final BGPSessionListener p = this.peers.get(ip);
 
         if (this.sessionIds.containsKey(ip)) {
-            LOG.warn("Duplicate BGP session established with {}", ip);
-
-            final BGPSessionId previousConnection = this.sessionIds.get(ip);
-
-            // 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);
-                throw new BGPDocumentedException(
-                    String.format("BGP session with %s %s has to be dropped. Same session already present %s",
-                        ip, currentConnection, previousConnection),
-                        BGPError.CONNECTION_COLLISION_RESOLUTION);
-
-                // Session reestablished with lower source bgp id, dropping current
-            } else if (previousConnection.isHigherDirection(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.CONNECTION_COLLISION_RESOLUTION);
-
-                // Session reestablished with higher source bgp id, dropping previous
-            } else if (currentConnection.isHigherDirection(previousConnection)) {
-                LOG.warn("BGP session with {} {} released. Replaced by opposite session", ip, previousConnection);
-                this.peers.get(ip).releaseConnection();
-                return this.peers.get(ip);
-
-                // Session reestablished with same source bgp id, dropping current as duplicate
+            if (p.isSessionActive()) {
+
+                LOG.warn("Duplicate BGP session established with {}", ip);
+
+                final BGPSessionId previousConnection = this.sessionIds.get(ip);
+
+                // 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);
+                    throw new BGPDocumentedException(
+                        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)) {
+                    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);
+
+                    // Session reestablished with higher source bgp id, dropping previous
+                } else if (currentConnection.isHigherDirection(previousConnection)) {
+                    LOG.warn("BGP session with {} {} released. Replaced by opposite session", ip, previousConnection);
+                    this.peers.get(ip).releaseConnection();
+                    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);
+                    throw new BGPDocumentedException(
+                        String.format("BGP session with %s initiated %s has to be dropped. Same session already present",
+                            ip, currentConnection),
+                            BGPError.CEASE);
+                }
             } else {
-                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.CONNECTION_COLLISION_RESOLUTION);
+                removePeerSession(ip);
             }
         }
 
         // Map session id to peer IP address
         this.sessionIds.put(ip, currentConnection);
-        return this.peers.get(ip);
+        return p;
     }
 
     @Override