BUG-2109 : cleaned BGP sessions on session closed.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeer.java
index 05f03738bcbdfd54344e34342363ced143073ac3..266a32d2fff2284c0c5665aa5319704dd37c3def 100644 (file)
@@ -11,16 +11,16 @@ package org.opendaylight.protocol.bgp.rib.impl;
 import com.google.common.base.Objects;
 import com.google.common.base.Objects.ToStringHelper;
 import com.google.common.base.Preconditions;
-import java.util.Comparator;
+import com.google.common.net.InetAddresses;
 import java.util.HashSet;
 import java.util.Set;
 import javax.annotation.concurrent.GuardedBy;
-import org.opendaylight.protocol.bgp.parser.BGPSession;
-import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
+import org.opendaylight.protocol.bgp.rib.impl.spi.AdjRIBsOutRegistration;
 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
+import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
+import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
 import org.opendaylight.protocol.bgp.rib.spi.Peer;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
@@ -41,8 +41,12 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
     private final RIB rib;
     private final String name;
 
-    private Comparator<PathAttributes> comparator;
+    @GuardedBy("this")
     private BGPSession session;
+    @GuardedBy("this")
+    private byte[] rawIdentifier;
+    @GuardedBy("this")
+    private AdjRIBsOutRegistration reg;
 
     public BGPPeer(final String name, final RIB rib) {
         this.rib = Preconditions.checkNotNull(rib);
@@ -69,7 +73,7 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
         LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
 
         this.session = session;
-        this.comparator = new BGPObjectComparator(this.rib.getLocalAs(), rib.getBgpIdentifier(), session.getBgpId());
+        this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
 
         for (final BgpTableType t : session.getAdvertisedTableTypes()) {
             final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
@@ -77,6 +81,11 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
             this.tables.add(key);
             this.rib.initTable(this, key);
         }
+
+        // Not particularly nice, but what can
+        if (session instanceof BGPSessionImpl) {
+            reg = rib.registerRIBsOut(this, new SessionRIBsOut((BGPSessionImpl) session));
+        }
     }
 
     private synchronized void cleanup() {
@@ -85,21 +94,24 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
             this.rib.clearTable(this, key);
         }
 
+        if (this.reg != null) {
+            this.reg.close();
+            this.reg = null;
+        }
+
         this.tables.clear();
-        this.session = null;
-        this.comparator = null;
     }
 
     @Override
     public void onSessionDown(final BGPSession session, final Exception e) {
         LOG.info("Session with peer {} went down", this.name, e);
-        cleanup();
+        releaseConnection();
     }
 
     @Override
     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
         LOG.info("Session with peer {} terminated: {}", this.name, cause);
-        cleanup();
+        releaseConnection();
     }
 
     @Override
@@ -118,13 +130,8 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
         return this.name;
     }
 
-    @Override
-    public Comparator<PathAttributes> getComparator() {
-        return this.comparator;
-    }
-
     protected RIB getRib() {
-        return rib;
+        return this.rib;
     }
 
     @Override
@@ -133,10 +140,16 @@ public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable {
         cleanup();
     }
 
+    @GuardedBy("this")
     private void dropConnection() {
         if (this.session != null) {
             this.session.close();
             this.session = null;
         }
     }
+
+    @Override
+    public synchronized byte[] getRawIdentifier() {
+        return rawIdentifier;
+    }
 }