Handle end-of-RIB messages
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBImpl.java
index 1c5ea526ca19c11fc678de2af8f5cccb0f921017..a7382ec7a848090c89ce51fedc288a14151b0512 100644 (file)
@@ -7,21 +7,30 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
-import java.util.concurrent.Future;
-
 import javax.annotation.concurrent.ThreadSafe;
 
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
-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.message.rev130918.update.PathAttributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes2;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpReachNlri;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpUnreachNlri;
+import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
+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.message.rev130919.UpdateBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.Nlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -29,56 +38,97 @@ 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.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.JdkFutureAdapters;
 
 @ThreadSafe
 public class RIBImpl {
-       private static final Logger logger = LoggerFactory.getLogger(RIBImpl.class);
+       private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class);
+       private static final Update EOR = new UpdateBuilder().build();
        private final DataProviderService dps;
        private final RIBTables tables;
 
-       public RIBImpl(final DataProviderService dps) {
+       public RIBImpl(final RIBExtensionConsumerContext extensions, final DataProviderService dps) {
                this.dps = Preconditions.checkNotNull(dps);
-               this.tables = new RIBTables(BGPObjectComparator.INSTANCE, AdjRIBsInFactoryRegistryImpl.INSTANCE);
+               this.tables = new RIBTables(BGPObjectComparator.INSTANCE, extensions);
        }
 
        synchronized void updateTables(final BGPPeer peer, final Update message) {
                final DataModificationTransaction trans = this.dps.beginTransaction();
 
-               // FIXME: detect and handle end-of-RIB markers
+               if (EOR.equals(message)) {
+                       final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
+                       if (ari != null) {
+                               ari.markUptodate(trans, peer);
+                       } else {
+                               LOG.debug("End-of-RIB for IPv4 Unicast ignored");
+                       }
+                       return;
+               }
 
-               // remove(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
-               // trans, peer, message.getWithdrawnRoutes().getWithdrawnRoutes().iterator());
+               final WithdrawnRoutes wr = message.getWithdrawnRoutes();
+               if (wr != null) {
+                       final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
+                       if (ari != null) {
+                               ari.removeRoutes(trans, peer, new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
+                                               new WithdrawnRoutesBuilder().setDestinationType(new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build());
+                       } else {
+                               LOG.debug("Not removing objects from unhandled IPv4 Unicast");
+                       }
+               }
 
                final PathAttributes attrs = message.getPathAttributes();
                final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class);
                if (mpu != null) {
                        final MpUnreachNlri nlri = mpu.getMpUnreachNlri();
 
-                       final AdjRIBsIn ari = this.tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
+                       final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
                        if (ari != null) {
                                ari.removeRoutes(trans, peer, nlri);
                        } else {
-                               logger.debug("Not removing objects from unhandled NLRI {}", nlri);
+                               LOG.debug("Not removing objects from unhandled NLRI {}", nlri);
                        }
                }
 
-               // add(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
-               // trans, peer, message.getNlri().getNlri().iterator(), attrs);
+               final Nlri ar = message.getNlri();
+               if (ar != null) {
+                       final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
+                       if (ari != null) {
+                               ari.addRoutes(trans, peer, new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).
+                                               setCNextHop(attrs.getCNextHop()).setAdvertizedRoutes(
+                                                               new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build(), attrs);
+                       } else {
+                               LOG.debug("Not adding objects from unhandled IPv4 Unicast");
+                       }
+               }
 
                final PathAttributes1 mpr = message.getPathAttributes().getAugmentation(PathAttributes1.class);
                if (mpr != null) {
                        final MpReachNlri nlri = mpr.getMpReachNlri();
 
-                       final AdjRIBsIn ari = this.tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
+                       final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
                        if (ari != null) {
                                ari.addRoutes(trans, peer, nlri, attrs);
+                               if (message.equals(ari.endOfRib())) {
+                                       ari.markUptodate(trans, peer);
+                               }
                        } else {
-                               logger.debug("Not adding objects from unhandled NLRI {}", nlri);
+                               LOG.debug("Not adding objects from unhandled NLRI {}", nlri);
                        }
                }
 
-               // FIXME: we need to attach to this future for failures
-               final Future<RpcResult<TransactionStatus>> f = trans.commit();
+               Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
+                       @Override
+                       public void onSuccess(final RpcResult<TransactionStatus> result) {
+                               LOG.debug("RIB modification successfully committed.");
+                       }
+
+                       @Override
+                       public void onFailure(final Throwable t) {
+                               LOG.error("Failed to commit RIB modification", t);
+                       }
+               });
        }
 
        synchronized void clearTable(final BGPPeer peer, final TablesKey key) {
@@ -87,13 +137,22 @@ public class RIBImpl {
                        final DataModificationTransaction trans = this.dps.beginTransaction();
                        ari.clear(trans, peer);
 
-                       // FIXME: we need to attach to this future for failures
-                       final Future<RpcResult<TransactionStatus>> f = trans.commit();
+                       Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
+                               @Override
+                               public void onSuccess(final RpcResult<TransactionStatus> result) {
+                                       // Nothing to do
+                               }
+
+                               @Override
+                               public void onFailure(final Throwable t) {
+                                       LOG.error("Failed to commit RIB modification", t);
+                               }
+                       });
                }
        }
 
        @Override
-       public String toString() {
+       public final String toString() {
                return addToStringAttributes(Objects.toStringHelper(this)).toString();
        }