X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=bgp%2Frib-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Fbgp%2Frib%2Fimpl%2FRIBImpl.java;h=432869c178d25299a21209183a47d4f860841b32;hb=9696218c43efa3b515001123d855668249fa4938;hp=3d4e522ca808adb13361333380025469adf85f00;hpb=3a4d19d96be80779f8e8317e1fe0fda85bb12ca6;p=bgpcep.git diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBImpl.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBImpl.java index 3d4e522ca8..432869c178 100644 --- a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBImpl.java +++ b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBImpl.java @@ -11,24 +11,24 @@ import com.google.common.base.Objects; import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; - import java.util.Collections; import java.util.List; +import java.util.Set; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutionException; - +import java.util.concurrent.LinkedBlockingQueue; import javax.annotation.concurrent.ThreadSafe; - import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.md.sal.common.api.data.TransactionChain; import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener; import org.opendaylight.protocol.bgp.rib.DefaultRibReference; @@ -36,6 +36,7 @@ import org.opendaylight.protocol.bgp.rib.impl.spi.AdjRIBsOut; import org.opendaylight.protocol.bgp.rib.impl.spi.AdjRIBsOutRegistration; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher; import org.opendaylight.protocol.bgp.rib.impl.spi.RIB; +import org.opendaylight.protocol.bgp.rib.spi.AbstractAdjRIBs; import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn; import org.opendaylight.protocol.bgp.rib.spi.BGPObjectComparator; import org.opendaylight.protocol.bgp.rib.spi.Peer; @@ -60,14 +61,17 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mult 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.BgpRib; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRibBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRibBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables; 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.rib.rev130925.rib.tables.routes.Ipv4RoutesCase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.routes.Ipv6RoutesCase; 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.binding.InstanceIdentifier; @@ -79,21 +83,69 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class); private static final Update EOR = new UpdateBuilder().build(); private static final TablesKey IPV4_UNICAST_TABLE = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class); + + /* + * FIXME: performance: this needs to be turned into a Peer->offset map. + * The offset is used to locate a the per-peer state entry in the + * RIB tables. + * + * For the first release, that map is updated whenever configuration + * changes and remains constant on peer flaps. On re-configuration + * a resize task is scheduled, so large tables may take some time + * before they continue reacting to updates. + * + * For subsequent releases, if we make the reformat process concurrent, + * we can trigger reformats when Graceful Restart Time expires for a + * particular peer. + */ private final ConcurrentMap ribOuts = new ConcurrentHashMap<>(); private final ReconnectStrategyFactory tcpStrategyFactory; private final ReconnectStrategyFactory sessionStrategyFactory; + + /** + * BGP Best Path selection comparator for ingress best path selection. + */ private final BGPObjectComparator comparator; private final BGPDispatcher dispatcher; private final BindingTransactionChain chain; private final AsNumber localAs; private final Ipv4Address bgpIdentifier; - private final List localTables; + private final Set localTables; private final RIBTables tables; + private final BlockingQueue peers; + private final DataBroker dataBroker; + + private final Runnable scheduler = new Runnable() { + @Override + public void run() { + try { + final Peer peer = RIBImpl.this.peers.take(); + LOG.debug("Advertizing loc-rib to new peer {}.", peer); + for (final BgpTableType key : RIBImpl.this.localTables) { + final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(RIBImpl.this.ribOuts, RIBImpl.this.comparator, RIBImpl.this.chain.newWriteOnlyTransaction()); + final AbstractAdjRIBs adj = (AbstractAdjRIBs) RIBImpl.this.tables.get(new TablesKey(key.getAfi(), key.getSafi())); + adj.addAllEntries(trans); + Futures.addCallback(trans.commit(), new FutureCallback() { + @Override + public void onSuccess(final Void result) { + LOG.trace("Advertizing {} to peer {} committed successfully", key.getAfi(), peer); + } + @Override + public void onFailure(final Throwable t) { + LOG.error("Failed to update peer {} with RIB {}", peer, t); + } + }); + } + } catch (final InterruptedException e) { + LOG.info("Scheduler thread was interrupted.", e); + } + } + }; public RIBImpl(final RibId ribId, final AsNumber localAs, final Ipv4Address localBgpId, final RIBExtensionConsumerContext extensions, - final BGPDispatcher dispatcher, final ReconnectStrategyFactory tcpStrategyFactory, - final ReconnectStrategyFactory sessionStrategyFactory, final DataBroker dps, final List localTables) { - super(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(ribId))).toInstance()); + final BGPDispatcher dispatcher, final ReconnectStrategyFactory tcpStrategyFactory, + final ReconnectStrategyFactory sessionStrategyFactory, final DataBroker dps, final List localTables) { + super(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(ribId))).build()); this.chain = dps.createTransactionChain(this); this.localAs = Preconditions.checkNotNull(localAs); this.comparator = new BGPObjectComparator(localAs); @@ -101,26 +153,20 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, this.dispatcher = Preconditions.checkNotNull(dispatcher); this.sessionStrategyFactory = Preconditions.checkNotNull(sessionStrategyFactory); this.tcpStrategyFactory = Preconditions.checkNotNull(tcpStrategyFactory); - this.localTables = ImmutableList.copyOf(localTables); + this.localTables = ImmutableSet.copyOf(localTables); this.tables = new RIBTables(extensions); + this.peers = new LinkedBlockingQueue<>(); + this.dataBroker = dps; LOG.debug("Instantiating RIB table {} at {}", ribId, getInstanceIdentifier()); - final ReadWriteTransaction trans = chain.newReadWriteTransaction(); - Optional o; - try { - o = trans.read(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier()).get(); - } catch (InterruptedException | ExecutionException e) { - throw new IllegalStateException("Failed to read topology", e); - } - Preconditions.checkState(!o.isPresent(), "Data provider conflict detected on object {}", getInstanceIdentifier()); + final WriteTransaction trans = this.chain.newWriteOnlyTransaction(); // put empty BgpRib if not exists - trans.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(BgpRib.class).build(), new BgpRibBuilder().build()); trans.put(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier(), new RibBuilder().setKey(new RibKey(ribId)).setId(ribId).setLocRib( - new LocRibBuilder().setTables(Collections. emptyList()).build()).build()); + new LocRibBuilder().setTables(Collections. emptyList()).build()).build(), true); - for (BgpTableType t : localTables) { + for (final BgpTableType t : localTables) { final TablesKey key = new TablesKey(t.getAfi(), t.getSafi()); if (this.tables.create(trans, this, key) == null) { LOG.debug("Did not create local table for unhandled table type {}", t); @@ -145,20 +191,23 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, @Override public synchronized void updateTables(final Peer peer, final Update message) { - final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(ribOuts, this.comparator, this.chain.newWriteOnlyTransaction()); + final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(this.ribOuts, this.comparator, this.chain.newWriteOnlyTransaction()); if (!EOR.equals(message)) { final WithdrawnRoutes wr = message.getWithdrawnRoutes(); if (wr != null) { final AdjRIBsIn ari = this.tables.get(IPV4_UNICAST_TABLE); if (ari != null) { + /* + * create MPUnreach for the routes to be handled in the same way as linkstate routes + */ ari.removeRoutes( - trans, - peer, - new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes( - new WithdrawnRoutesBuilder().setDestinationType( - new DestinationIpv4CaseBuilder().setDestinationIpv4( - new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build()).build()); + trans, + peer, + new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes( + new WithdrawnRoutesBuilder().setDestinationType( + new DestinationIpv4CaseBuilder().setDestinationIpv4( + new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build()).build()); } else { LOG.debug("Not removing objects from unhandled IPv4 Unicast"); } @@ -169,12 +218,14 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class); if (mpu != null) { final MpUnreachNlri nlri = mpu.getMpUnreachNlri(); - - final AdjRIBsIn ari = this.tables.get(new TablesKey(nlri.getAfi(), nlri.getSafi())); - if (ari != null) { - ari.removeRoutes(trans, peer, nlri); - } else { - LOG.debug("Not removing objects from unhandled NLRI {}", nlri); + // EOR messages do not contain withdrawn routes + if (nlri.getWithdrawnRoutes() != null) { + final AdjRIBsIn ari = this.tables.get(new TablesKey(nlri.getAfi(), nlri.getSafi())); + if (ari != null) { + ari.removeRoutes(trans, peer, nlri); + } else { + LOG.debug("Not removing objects from unhandled NLRI {}", nlri); + } } } } @@ -183,11 +234,14 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, if (ar != null) { final AdjRIBsIn ari = this.tables.get(IPV4_UNICAST_TABLE); if (ari != null) { + /* + * create MPReach for the routes to be handled in the same way as linkstate routes + */ final MpReachNlriBuilder b = new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi( - UnicastSubsequentAddressFamily.class).setAdvertizedRoutes( - new AdvertizedRoutesBuilder().setDestinationType( - new DestinationIpv4CaseBuilder().setDestinationIpv4( - new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build()); + UnicastSubsequentAddressFamily.class).setAdvertizedRoutes( + new AdvertizedRoutesBuilder().setDestinationType( + new DestinationIpv4CaseBuilder().setDestinationIpv4( + new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build()); if (attrs != null) { b.setCNextHop(attrs.getCNextHop()); } @@ -241,7 +295,7 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, public synchronized void clearTable(final Peer peer, final TablesKey key) { final AdjRIBsIn ari = this.tables.get(key); if (ari != null) { - final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(ribOuts, comparator, this.chain.newWriteOnlyTransaction()); + final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(this.ribOuts, this.comparator, this.chain.newWriteOnlyTransaction()); ari.clear(trans, peer); Futures.addCallback(trans.commit(), new FutureCallback() { @@ -267,12 +321,17 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, return toStringHelper; } + @SuppressWarnings("unchecked") + protected AdjRIBsIn getTable(final TablesKey key) { + return (AdjRIBsIn) this.tables.get(key); + } + @Override public void close() throws InterruptedException, ExecutionException { final WriteTransaction t = this.chain.newWriteOnlyTransaction(); t.delete(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier()); t.submit().get(); - chain.close(); + this.chain.close(); } @Override @@ -286,7 +345,7 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, } @Override - public List getLocalTables() { + public Set getLocalTables() { return this.localTables; } @@ -315,12 +374,18 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, final AdjRIBsOutRegistration reg = new AdjRIBsOutRegistration(aro) { @Override protected void removeRegistration() { - ribOuts.remove(peer, aro); + RIBImpl.this.ribOuts.remove(peer, aro); } }; - ribOuts.put(peer, aro); - // FIXME: schedule a walk over all the tables + this.ribOuts.put(peer, aro); + LOG.debug("Registering this peer {} to RIB-Out {}", peer, this.ribOuts); + try { + this.peers.put(peer); + new Thread(this.scheduler).start(); + } catch (final InterruptedException e) { + // + } return reg; } @@ -333,4 +398,29 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable, public void onTransactionChainSuccessful(final TransactionChain chain) { LOG.info("RIB {} closed successfully", getInstanceIdentifier()); } + + @Override + public long getRoutesCount(final TablesKey key) { + try { + final Optional tableMaybe = this.dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, + getInstanceIdentifier().child(LocRib.class).child(Tables.class, key)).checkedGet(); + if (tableMaybe.isPresent()) { + final Tables table = tableMaybe.get(); + if (table.getRoutes() instanceof Ipv4RoutesCase) { + final Ipv4RoutesCase routesCase = (Ipv4RoutesCase) table.getRoutes(); + if (routesCase.getIpv4Routes() != null && routesCase.getIpv4Routes().getIpv4Route() != null) { + return routesCase.getIpv4Routes().getIpv4Route().size(); + } + } else if (table.getRoutes() instanceof Ipv6RoutesCase) { + final Ipv6RoutesCase routesCase = (Ipv6RoutesCase) table.getRoutes(); + if (routesCase.getIpv6Routes() != null && routesCase.getIpv6Routes().getIpv6Route() != null) { + return routesCase.getIpv6Routes().getIpv6Route().size(); + } + } + } + } catch (final ReadFailedException e) { + //no-op + } + return 0; + } }