BUG-185 : implemented Best Path Selection Algorithm.
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeer.java
index 42a8db6d4a906867b7f30f79db785cb00229c19a..c729b8384b6cf6d54cec85f413971f87342565bd 100644 (file)
@@ -13,8 +13,8 @@ import org.opendaylight.protocol.bgp.parser.BGPSession;
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
 import org.opendaylight.protocol.bgp.rib.spi.Peer;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Update;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.BgpTableType;
+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;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
@@ -23,14 +23,15 @@ import org.slf4j.LoggerFactory;
 import com.google.common.base.Objects;
 import com.google.common.base.Objects.ToStringHelper;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
 
 /**
  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
  * RIB actions.
  */
 public final class BGPPeer implements BGPSessionListener, Peer {
-       private static final Logger logger = LoggerFactory.getLogger(BGPPeer.class);
-       private Set<TablesKey> tables;
+       private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
+       private final Set<TablesKey> tables = Sets.newHashSet();
        private final String name;
        private final RIBImpl rib;
 
@@ -42,38 +43,46 @@ public final class BGPPeer implements BGPSessionListener, Peer {
        @Override
        public void onMessage(final BGPSession session, final Notification message) {
                if (message instanceof Update) {
-                       this.rib.updateTables(this, (Update)message);
+                       this.rib.updateTables(this, (Update) message);
                } else {
-                       logger.info("Ignoring unhandled message class " + message.getClass());
+                       LOG.info("Ignoring unhandled message class " + message.getClass());
                }
        }
 
        @Override
        public void onSessionUp(final BGPSession session) {
-               logger.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
+               LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
 
-               for (BgpTableType t : session.getAdvertisedTableTypes()) {
-                       tables.add(new TablesKey(t.getAfi(), t.getSafi()));
+               for (final BgpTableType t : session.getAdvertisedTableTypes()) {
+                       this.tables.add(new TablesKey(t.getAfi(), t.getSafi()));
                }
+               this.rib.initTables(session.getBgpId());
+
        }
 
-       @Override
-       public void onSessionDown(final BGPSession session, final Exception e) {
-               // FIXME: support graceful restart
+       private void cleanup() {
+               // FIXME: BUG-196: support graceful restart
                for (final TablesKey key : this.tables) {
                        this.rib.clearTable(this, key);
                }
 
-               tables.clear();
+               this.tables.clear();
+       }
+
+       @Override
+       public void onSessionDown(final BGPSession session, final Exception e) {
+               LOG.info("Session with peer {} went down", this.name, e);
+               cleanup();
        }
 
        @Override
        public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
-               logger.info("Session with peer {} terminated: {}", this.name, cause);
+               LOG.info("Session with peer {} terminated: {}", this.name, cause);
+               cleanup();
        }
 
        @Override
-       public final String toString() {
+       public String toString() {
                return addToStringAttributes(Objects.toStringHelper(this)).toString();
        }