*/
package org.opendaylight.protocol.bgp.rib.impl;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
+import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ADJRIBIN_ATTRIBUTES_AID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ADJRIBIN_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ATTRIBUTES_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.EFFRIBIN_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.LLGR_STALE_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ROUTES_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
+import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.UPTODATE_NID;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.MoreExecutors;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collection;
-import javax.annotation.Nonnull;
-import javax.annotation.concurrent.NotThreadSafe;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.LongAdder;
+import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.checkerframework.checker.lock.qual.Holding;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
+import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
+import org.opendaylight.protocol.bgp.parser.impl.message.update.CommunityUtil;
+import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
+import org.opendaylight.protocol.bgp.rib.impl.spi.RibOutRefresh;
+import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesInstalledCounters;
+import org.opendaylight.protocol.bgp.rib.impl.state.peer.PrefixesReceivedCounters;
+import org.opendaylight.protocol.bgp.rib.spi.RIBNormalizedNodes;
import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.EffectiveRibIn;
-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.tables.Routes;
+import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRibRoutingPolicy;
+import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
+import org.opendaylight.protocol.bgp.route.targetcontrain.spi.ClientRouteTargetContrainCache;
+import org.opendaylight.protocol.bgp.route.targetcontrain.spi.RouteTargetMembeshipUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.Communities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.RouteTargetConstrainSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRoute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.MplsLabeledVpnSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteTarget;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
+import org.opendaylight.yangtools.yang.data.tree.api.ModificationType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Implementation of the BGP import policy. Listens on all Adj-RIB-In, inspects all inbound
+ * Implementation of the BGP import policy. Listens on peer's Adj-RIB-In, inspects all inbound
* routes in the context of the advertising peer's role and applies the inbound policy.
*
+ * <p>
* Inbound policy is applied as follows:
*
+ * <p>
* 1) if the peer is an eBGP peer, perform attribute replacement and filtering
* 2) check if a route is admissible based on attributes attached to it, as well as the
- * advertising peer's role
+ * advertising peer's role
* 3) output admitting routes with edited attributes into /bgp-rib/rib/peer/effective-rib-in/tables/routes
*
- * Note that we maintain the peer roles using a DCL, even if we could look up our internal
- * structures. This is done so we maintain causality and loose coupling.
+ * <p>
+ * This class is NOT thread-safe.
*/
-@NotThreadSafe
-final class EffectiveRibInWriter implements AutoCloseable {
+final class EffectiveRibInWriter implements PrefixesReceivedCounters, PrefixesInstalledCounters,
+ AutoCloseable, ClusteredDOMDataTreeChangeListener {
+
private static final Logger LOG = LoggerFactory.getLogger(EffectiveRibInWriter.class);
- private static final NodeIdentifier TABLE_ROUTES = new NodeIdentifier(Routes.QNAME);
-
- /**
- * Maintains {@link TableRouteListener} instances.
- */
- private final class AdjInTracker implements AutoCloseable, DOMDataTreeChangeListener {
- private final RIBSupportContextRegistry registry;
- private final YangInstanceIdentifier ribId;
- private final ListenerRegistration<?> reg;
- private final DOMTransactionChain chain;
-
- AdjInTracker(final DOMDataTreeChangeService service, final RIBSupportContextRegistry registry, final DOMTransactionChain chain, final YangInstanceIdentifier ribId) {
- this.registry = Preconditions.checkNotNull(registry);
- this.chain = Preconditions.checkNotNull(chain);
- this.ribId = Preconditions.checkNotNull(ribId);
-
- final YangInstanceIdentifier tableId = ribId.node(Peer.QNAME).node(Peer.QNAME).node(AdjRibIn.QNAME).node(Tables.QNAME).node(Tables.QNAME);
- final DOMDataTreeIdentifier treeId = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, tableId);
- LOG.debug("Registered Effective RIB on {}", tableId);
- this.reg = service.registerDataTreeChangeListener(treeId, this);
+ private static final TablesKey IVP4_VPN_TABLE_KEY =
+ new TablesKey(Ipv4AddressFamily.VALUE, MplsLabeledVpnSubsequentAddressFamily.VALUE);
+ private static final TablesKey IVP6_VPN_TABLE_KEY =
+ new TablesKey(Ipv6AddressFamily.VALUE, MplsLabeledVpnSubsequentAddressFamily.VALUE);
+ private static final ImmutableList<Communities> STALE_LLGR_COMMUNUTIES =
+ ImmutableList.of(StaleCommunities.STALE_LLGR);
+ private static final Attributes STALE_LLGR_ATTRIBUTES = new org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder()
+ .setCommunities(STALE_LLGR_COMMUNUTIES)
+ .build();
+ private static final ChoiceNode EMPTY_ROUTES = Builders.choiceBuilder().withNodeIdentifier(ROUTES_NID).build();
+
+ private final RIBSupportContextRegistry registry;
+ private final YangInstanceIdentifier peerIId;
+ private final YangInstanceIdentifier effRibTables;
+ private final DOMDataTreeChangeService service;
+ private final List<RouteTarget> rtMemberships;
+ private final RibOutRefresh vpnTableRefresher;
+ private final ClientRouteTargetContrainCache rtCache;
+ private ListenerRegistration<?> reg;
+ private DOMTransactionChain chain;
+ private final Map<TablesKey, LongAdder> prefixesReceived;
+ private final Map<TablesKey, LongAdder> prefixesInstalled;
+ private final BGPRibRoutingPolicy ribPolicies;
+ private final BGPRouteEntryImportParameters peerImportParameters;
+ private final BGPTableTypeRegistryConsumer tableTypeRegistry;
+ @GuardedBy("this")
+ private FluentFuture<? extends CommitInfo> submitted;
+ private boolean rtMembershipsUpdated;
+
+ EffectiveRibInWriter(
+ final BGPRouteEntryImportParameters peer,
+ final RIB rib,
+ final DOMTransactionChain chain,
+ final YangInstanceIdentifier peerIId,
+ final Set<TablesKey> tables,
+ final BGPTableTypeRegistryConsumer tableTypeRegistry,
+ final List<RouteTarget> rtMemberships,
+ final ClientRouteTargetContrainCache rtCache) {
+ registry = requireNonNull(rib.getRibSupportContext());
+ this.chain = requireNonNull(chain);
+ this.peerIId = requireNonNull(peerIId);
+ effRibTables = this.peerIId.node(EFFRIBIN_NID);
+ prefixesInstalled = buildPrefixesTables(tables);
+ prefixesReceived = buildPrefixesTables(tables);
+ ribPolicies = requireNonNull(rib.getRibPolicies());
+ service = requireNonNull(rib.getService());
+ this.tableTypeRegistry = requireNonNull(tableTypeRegistry);
+ peerImportParameters = peer;
+ this.rtMemberships = rtMemberships;
+ this.rtCache = rtCache;
+ vpnTableRefresher = rib;
+ }
+
+ public void init() {
+ final DOMDataTreeIdentifier treeId = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
+ peerIId.node(ADJRIBIN_NID).node(TABLES_NID));
+ LOG.debug("Registered Effective RIB on {}", peerIId);
+ reg = requireNonNull(service).registerDataTreeChangeListener(treeId, this);
+ }
+
+ private static Map<TablesKey, LongAdder> buildPrefixesTables(final Set<TablesKey> tables) {
+ final ImmutableMap.Builder<TablesKey, LongAdder> b = ImmutableMap.builder();
+ tables.forEach(table -> b.put(table, new LongAdder()));
+ return b.build();
+ }
+
+ @Override
+ public synchronized void onInitialData() {
+ // FIXME: update as if root was deleted
+ }
+
+ @Override
+ public synchronized void onDataTreeChanged(final List<DataTreeCandidate> changes) {
+ if (chain == null) {
+ LOG.trace("Chain closed. Ignoring Changes : {}", changes);
+ return;
}
- private void processRoute(final DOMDataWriteTransaction tx, final RIBSupport ribSupport, final AbstractImportPolicy policy, final YangInstanceIdentifier routesPath, final DataTreeCandidateNode route) {
- LOG.debug("Process route {}", route);
- switch (route.getModificationType()) {
- case DELETE:
- // Delete has already been affected by the store in caller, so this is a no-op.
- break;
- case MERGE:
- LOG.info("Merge on {} reported, this should never have happened, ignoring", route);
- break;
- case UNMODIFIED:
- // No-op
- break;
- case SUBTREE_MODIFIED:
- case WRITE:
- // Lookup per-table attributes from RIBSupport
- final ContainerNode advertisedAttrs = (ContainerNode) NormalizedNodes.findNode(route.getDataAfter(), ribSupport.routeAttributesIdentifier()).orNull();
- final ContainerNode effectiveAttrs;
-
- if (advertisedAttrs != null) {
- effectiveAttrs = policy.effectiveAttributes(advertisedAttrs);
-
- /*
- * Speed hack: if we determine that the policy has passed the attributes
- * back unmodified, the corresponding change has already been written in
- * our caller. There is no need to perform any further processing.
- *
- * We also use direct object comparison to make the check very fast, as
- * it may not be that common, in which case it does not make sense to pay
- * the full equals price.
- */
- if (effectiveAttrs == advertisedAttrs) {
- LOG.trace("Effective and local attributes are equal. Quit processing route {}", route);
- return;
- }
- } else {
- effectiveAttrs = null;
+ LOG.trace("Data changed called to effective RIB. Change : {}", changes);
+ DOMDataTreeWriteTransaction tx = null;
+ for (final DataTreeCandidate tc : changes) {
+ final YangInstanceIdentifier rootPath = tc.getRootPath();
+ final DataTreeCandidateNode root = tc.getRootNode();
+ for (final DataTreeCandidateNode table : root.getChildNodes()) {
+ if (tx == null) {
+ tx = chain.newWriteOnlyTransaction();
}
+ changeDataTree(tx, rootPath, root, table);
+ }
+ }
- final YangInstanceIdentifier routeId = ribSupport.routePath(routesPath, route.getIdentifier());
- LOG.debug("Route {} effective attributes {} towards {}", route.getIdentifier(), effectiveAttrs, routeId);
+ if (tx != null) {
+ final FluentFuture<? extends CommitInfo> future = tx.commit();
+ submitted = future;
+ future.addCallback(new FutureCallback<CommitInfo>() {
+ @Override
+ public void onSuccess(final CommitInfo result) {
+ LOG.trace("Successful commit");
+ }
- if (effectiveAttrs != null) {
- tx.put(LogicalDatastoreType.OPERATIONAL, routeId.node(ribSupport.routeAttributesIdentifier()), effectiveAttrs);
- } else {
- LOG.warn("Route {} advertised empty attributes", routeId);
- tx.delete(LogicalDatastoreType.OPERATIONAL, routeId);
+ @Override
+ public void onFailure(final Throwable trw) {
+ LOG.error("Failed commit", trw);
}
+ }, MoreExecutors.directExecutor());
+ }
+
+ //Refresh VPN Table if RT Memberships were updated
+ if (rtMembershipsUpdated) {
+ vpnTableRefresher.refreshTable(IVP4_VPN_TABLE_KEY, peerImportParameters.getFromPeerId());
+ vpnTableRefresher.refreshTable(IVP6_VPN_TABLE_KEY, peerImportParameters.getFromPeerId());
+ rtMembershipsUpdated = false;
+ }
+ }
+
+ @Override
+ public synchronized void close() {
+ if (reg != null) {
+ reg.close();
+ reg = null;
+ }
+ if (submitted != null) {
+ try {
+ submitted.get();
+ } catch (final InterruptedException | ExecutionException throwable) {
+ LOG.error("Write routes failed", throwable);
+ }
+ }
+ if (chain != null) {
+ chain.close();
+ chain = null;
+ }
+ prefixesReceived.values().forEach(LongAdder::reset);
+ prefixesInstalled.values().forEach(LongAdder::reset);
+ }
+
+ @Override
+ public long getPrefixedReceivedCount(final TablesKey tablesKey) {
+ final LongAdder counter = prefixesReceived.get(tablesKey);
+ if (counter == null) {
+ return 0;
+ }
+ return counter.longValue();
+ }
+
+ @Override
+ public Set<TablesKey> getTableKeys() {
+ return ImmutableSet.copyOf(prefixesReceived.keySet());
+ }
+
+ @Override
+ public boolean isSupported(final TablesKey tablesKey) {
+ return prefixesReceived.containsKey(tablesKey);
+ }
+
+ @Override
+ public long getPrefixedInstalledCount(final TablesKey tablesKey) {
+ final LongAdder counter = prefixesInstalled.get(tablesKey);
+ if (counter == null) {
+ return 0;
+ }
+ return counter.longValue();
+ }
+
+ @Override
+ public long getTotalPrefixesInstalled() {
+ return prefixesInstalled.values().stream().mapToLong(LongAdder::longValue).sum();
+ }
+
+ @Holding("this")
+ private void changeDataTree(final DOMDataTreeWriteTransaction tx, final YangInstanceIdentifier rootPath,
+ final DataTreeCandidateNode root, final DataTreeCandidateNode table) {
+ final PathArgument lastArg = table.getIdentifier();
+ verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(),
+ rootPath);
+ final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
+ final RIBSupportContext ribContext = registry.getRIBSupportContext(tableKey);
+ if (ribContext == null) {
+ LOG.warn("Table {} is not supported, ignoring event", tableKey);
+ return;
+ }
+
+ final YangInstanceIdentifier effectiveTablePath = effectiveTablePath(tableKey);
+ final ModificationType modificationType = root.getModificationType();
+ LOG.debug("Effective table {} modification type {}", effectiveTablePath, modificationType);
+ switch (modificationType) {
+ case DISAPPEARED:
+ case DELETE:
+ deleteTable(tx, ribContext, effectiveTablePath, table);
+ break;
+ case APPEARED:
+ case WRITE:
+ writeTable(tx, ribContext, effectiveTablePath, table);
+ break;
+ case SUBTREE_MODIFIED:
+ modifyTable(tx, ribContext, effectiveTablePath, table);
+ break;
+ case UNMODIFIED:
+ LOG.info("Ignoring spurious notification on {} data {}", rootPath, table);
break;
default:
- LOG.warn("Ignoring unhandled route {}", route);
+ LOG.warn("Ignoring unhandled root {}", table);
break;
- }
}
+ }
- private void processTableChildren(final DOMDataWriteTransaction tx, final RIBSupport ribSupport, final NodeIdentifierWithPredicates peerKey, final YangInstanceIdentifier tablePath, final Collection<DataTreeCandidateNode> children) {
- final AbstractImportPolicy policy = EffectiveRibInWriter.this.peerPolicyTracker.policyFor(IdentifierUtils.peerId(peerKey));
+ private void deleteTable(final DOMDataTreeWriteTransaction tx, final RIBSupportContext ribContext,
+ final YangInstanceIdentifier effectiveTablePath, final DataTreeCandidateNode table) {
+ LOG.debug("Delete Effective Table {}", effectiveTablePath);
+ onDeleteTable(ribContext.getRibSupport(), effectiveTablePath, table.getDataBefore());
+ tx.delete(LogicalDatastoreType.OPERATIONAL, effectiveTablePath);
+ }
- for (final DataTreeCandidateNode child : children) {
- LOG.debug("Process table children {}", child);
- switch (child.getModificationType()) {
- case DELETE:
- tx.delete(LogicalDatastoreType.OPERATIONAL, tablePath.node(child.getIdentifier()));
- break;
- case MERGE:
- LOG.info("Merge on {} reported, this should never have happened, ignoring", child);
+ private void modifyTable(final DOMDataTreeWriteTransaction tx, final RIBSupportContext ribContext,
+ final YangInstanceIdentifier effectiveTablePath, final DataTreeCandidateNode table) {
+ LOG.debug("Modify Effective Table {}", effectiveTablePath);
+
+ final boolean wasLongLivedStale = isLongLivedStaleTable(table.getDataBefore());
+ final boolean longLivedStale = isLongLivedStaleTable(table.getDataAfter());
+ if (wasLongLivedStale != longLivedStale) {
+ LOG.debug("LLGR_STALE flag flipped {}, overwriting table {}", longLivedStale ? "ON" : "OFF",
+ effectiveTablePath);
+ writeTable(tx, ribContext, effectiveTablePath, table);
+ return;
+ }
+
+ table.getModifiedChild(ATTRIBUTES_NID).ifPresent(modifiedAttrs -> {
+ final YangInstanceIdentifier effAttrsPath = effectiveTablePath.node(ATTRIBUTES_NID);
+ final Optional<NormalizedNode> optAttrsAfter = modifiedAttrs.getDataAfter();
+ if (optAttrsAfter.isPresent()) {
+ tx.put(LogicalDatastoreType.OPERATIONAL, effAttrsPath,
+ effectiveAttributes(extractContainer(optAttrsAfter.orElseThrow())));
+ } else {
+ tx.delete(LogicalDatastoreType.OPERATIONAL, effAttrsPath);
+ }
+ });
+
+ table.getModifiedChild(ROUTES_NID).ifPresent(modifiedRoutes -> {
+ final RIBSupport<?, ?> ribSupport = ribContext.getRibSupport();
+ switch (modifiedRoutes.getModificationType()) {
+ case APPEARED:
+ case WRITE:
+ deleteRoutesBefore(tx, ribSupport, effectiveTablePath, modifiedRoutes);
+ // XXX: YANG Tools seems to have an issue stacking DELETE with child WRITE
+ tx.put(LogicalDatastoreType.OPERATIONAL, effectiveTablePath.node(ROUTES_NID), EMPTY_ROUTES);
+ writeRoutesAfter(tx, ribSupport, effectiveTablePath, modifiedRoutes.getDataAfter(), longLivedStale);
break;
- case UNMODIFIED:
- // No-op
+ case DELETE:
+ case DISAPPEARED:
+ deleteRoutesBefore(tx, ribSupport, effectiveTablePath, modifiedRoutes);
+ tx.delete(LogicalDatastoreType.OPERATIONAL, effectiveTablePath.node(ROUTES_NID));
break;
case SUBTREE_MODIFIED:
- case WRITE:
- tx.put(LogicalDatastoreType.OPERATIONAL, tablePath.node(child.getIdentifier()), child.getDataAfter().get());
-
- // Routes are special, as they may end up being filtered. The previous put conveniently
- // ensured that we have them in at target, so a subsequent delete will not fail :)
- if (TABLE_ROUTES.equals(child.getIdentifier())) {
- final YangInstanceIdentifier routesPath = tablePath.node(Routes.QNAME);
- for (final DataTreeCandidateNode route : ribSupport.changedRoutes(child)) {
- processRoute(tx, ribSupport, policy, routesPath, route);
- }
+ for (DataTreeCandidateNode modifiedRoute : ribSupport.changedRoutes(modifiedRoutes)) {
+ processRoute(tx, ribSupport, effectiveTablePath, modifiedRoute, longLivedStale);
}
break;
+ case UNMODIFIED:
+ // No-op
+ return;
default:
- LOG.warn("Ignoring unhandled child {}", child);
+ LOG.warn("Ignoring modified routes {}", modifiedRoutes);
break;
- }
}
+ });
+ }
+
+ private void writeTable(final DOMDataTreeWriteTransaction tx, final RIBSupportContext ribContext,
+ final YangInstanceIdentifier effectiveTablePath, final DataTreeCandidateNode table) {
+ LOG.debug("Write Effective Table {}", effectiveTablePath);
+ onDeleteTable(ribContext.getRibSupport(), effectiveTablePath, table.getDataBefore());
+
+ final Optional<NormalizedNode> maybeTableAfter = table.getDataAfter();
+ if (maybeTableAfter.isPresent()) {
+ final NormalizedNode node = maybeTableAfter.orElseThrow();
+ verify(node instanceof MapEntryNode, "Expected MapEntryNode, got %s", node);
+ final MapEntryNode tableAfter = (MapEntryNode) node;
+ ribContext.createEmptyTableStructure(tx, effectiveTablePath);
+
+ final DataContainerChild maybeAttrsAfter = tableAfter.childByArg(ATTRIBUTES_NID);
+ final boolean longLivedStale;
+ if (maybeAttrsAfter != null) {
+ final ContainerNode attrsAfter = extractContainer(maybeAttrsAfter);
+ longLivedStale = isLongLivedStale(attrsAfter);
+ tx.put(LogicalDatastoreType.OPERATIONAL, effectiveTablePath.node(ATTRIBUTES_NID),
+ effectiveAttributes(attrsAfter));
+ } else {
+ longLivedStale = false;
+ }
+
+ writeRoutesAfter(tx, ribContext.getRibSupport(), effectiveTablePath,
+ NormalizedNodes.findNode(tableAfter, ROUTES_NID), longLivedStale);
}
+ }
- private RIBSupportContext getRibSupport(final NodeIdentifierWithPredicates tableKey) {
- return this.registry.getRIBSupportContext(tableKey);
+ // Performs house-keeping when the contents of a table is deleted
+ private void onDeleteTable(final RIBSupport<?, ?> ribSupport, final YangInstanceIdentifier effectiveTablePath,
+ final Optional<NormalizedNode> tableBefore) {
+ // Routes are special in that we need to process the to keep our counters accurate
+ final Optional<NormalizedNode> maybeRoutesBefore = findRoutesMap(ribSupport,
+ NormalizedNodes.findNode(tableBefore, ROUTES_NID));
+ if (maybeRoutesBefore.isPresent()) {
+ onRoutesDeleted(ribSupport, effectiveTablePath, extractMap(maybeRoutesBefore).body());
}
+ }
- private YangInstanceIdentifier effectiveTablePath(final NodeIdentifierWithPredicates peerKey, final NodeIdentifierWithPredicates tableKey) {
- return this.ribId.node(Peer.QNAME).node(peerKey).node(EffectiveRibIn.QNAME).node(Tables.QNAME).node(tableKey);
+ private void deleteRoutesBefore(final DOMDataTreeWriteTransaction tx, final RIBSupport<?, ?> ribSupport,
+ final YangInstanceIdentifier effectiveTablePath, final DataTreeCandidateNode modifiedRoutes) {
+ final Optional<NormalizedNode> maybeRoutesBefore = NormalizedNodes.findNode(
+ modifiedRoutes.getDataBefore(), ribSupport.relativeRoutesPath());
+ if (maybeRoutesBefore.isPresent()) {
+ onRoutesDeleted(ribSupport, effectiveTablePath, extractMap(maybeRoutesBefore).body());
}
+ }
- private void modifyTable(final DOMDataWriteTransaction tx, final NodeIdentifierWithPredicates peerKey, final NodeIdentifierWithPredicates tableKey, final DataTreeCandidateNode table) {
- final RIBSupportContext ribSupport = getRibSupport(tableKey);
- final YangInstanceIdentifier tablePath = effectiveTablePath(peerKey, tableKey);
+ private void writeRoutesAfter(final DOMDataTreeWriteTransaction tx, final RIBSupport<?, ?> ribSupport,
+ final YangInstanceIdentifier effectiveTablePath, final Optional<NormalizedNode> routesAfter,
+ final boolean longLivedStale) {
+ final Optional<NormalizedNode> maybeRoutesAfter = NormalizedNodes.findNode(routesAfter,
+ ribSupport.relativeRoutesPath());
+ if (maybeRoutesAfter.isPresent()) {
+ final YangInstanceIdentifier routesPath = routeMapPath(ribSupport, effectiveTablePath);
+ for (MapEntryNode routeAfter : extractMap(maybeRoutesAfter).body()) {
+ writeRoute(tx, ribSupport, routesPath.node(routeAfter.getIdentifier()), Optional.empty(), routeAfter,
+ longLivedStale);
+ }
+ }
+ }
- processTableChildren(tx, ribSupport.getRibSupport(), peerKey, tablePath, table.getChildNodes());
+ private void onRoutesDeleted(final RIBSupport<?, ?> ribSupport, final YangInstanceIdentifier effectiveTablePath,
+ final Collection<MapEntryNode> deletedRoutes) {
+ if (RouteTargetConstrainSubsequentAddressFamily.VALUE.equals(ribSupport.getTablesKey().getSafi())) {
+ final YangInstanceIdentifier routesPath = routeMapPath(ribSupport, effectiveTablePath);
+ for (final MapEntryNode routeBefore : deletedRoutes) {
+ deleteRouteTarget(ribSupport, routesPath.node(routeBefore.getIdentifier()), routeBefore);
+ }
+ rtMembershipsUpdated = true;
}
- private void writeTable(final DOMDataWriteTransaction tx, final NodeIdentifierWithPredicates peerKey, final NodeIdentifierWithPredicates tableKey, final DataTreeCandidateNode table) {
- final RIBSupportContext ribSupport = getRibSupport(tableKey);
- final YangInstanceIdentifier tablePath = effectiveTablePath(peerKey, tableKey);
+ final TablesKey tablesKey = ribSupport.getTablesKey();
+ CountersUtil.add(prefixesInstalled.get(tablesKey), tablesKey, -deletedRoutes.size());
+ }
- // Create an empty table
- ribSupport.clearTable(tx,tablePath);
+ private void processRoute(final DOMDataTreeWriteTransaction tx, final RIBSupport<?, ?> ribSupport,
+ final YangInstanceIdentifier routesPath, final DataTreeCandidateNode route, final boolean longLivedStale) {
+ LOG.debug("Process route {}", route.getIdentifier());
+ final YangInstanceIdentifier routePath = ribSupport.routePath(routesPath, route.getIdentifier());
+ switch (route.getModificationType()) {
+ case DELETE:
+ case DISAPPEARED:
+ deleteRoute(tx, ribSupport, routePath, route.getDataBefore().orElse(null));
+ break;
+ case UNMODIFIED:
+ // No-op
+ break;
+ case APPEARED:
+ case SUBTREE_MODIFIED:
+ case WRITE:
+ writeRoute(tx, ribSupport, routePath, route.getDataBefore(), route.getDataAfter().orElseThrow(),
+ longLivedStale);
+ break;
+ default:
+ LOG.warn("Ignoring unhandled route {}", route);
+ break;
+ }
+ }
- processTableChildren(tx, ribSupport.getRibSupport(), peerKey, tablePath, table.getChildNodes());
+ private void deleteRoute(final DOMDataTreeWriteTransaction tx, final RIBSupport<?, ?> ribSupport,
+ final YangInstanceIdentifier routeIdPath, final NormalizedNode route) {
+ handleRouteTarget(ModificationType.DELETE, ribSupport, routeIdPath, route);
+ tx.delete(LogicalDatastoreType.OPERATIONAL, routeIdPath);
+ LOG.debug("Route deleted. routeId={}", routeIdPath);
+ final TablesKey tablesKey = ribSupport.getTablesKey();
+ CountersUtil.decrement(prefixesInstalled.get(tablesKey), tablesKey);
+ }
+
+ private void writeRoute(final DOMDataTreeWriteTransaction tx, final RIBSupport<?, ?> ribSupport,
+ final YangInstanceIdentifier routePath, final Optional<NormalizedNode> routeBefore,
+ final NormalizedNode routeAfter, final boolean longLivedStale) {
+ final TablesKey tablesKey = ribSupport.getTablesKey();
+ CountersUtil.increment(prefixesReceived.get(tablesKey), tablesKey);
+ // Lookup per-table attributes from RIBSupport
+ final ContainerNode advertisedAttrs = (ContainerNode) NormalizedNodes.findNode(routeAfter,
+ ribSupport.routeAttributesIdentifier()).orElse(null);
+ final Attributes routeAttrs = ribSupport.attributeFromContainerNode(advertisedAttrs);
+ final Optional<Attributes> optEffAtt;
+ // In case we want to add LLGR_STALE we do not process route through policies since it may be
+ // considered as received with LLGR_STALE from peer which is not true.
+ if (longLivedStale) {
+ // LLGR procedures are in effect. If the route is tagged with NO_LLGR, it needs to be removed.
+ final List<Communities> effCommunities = routeAttrs.getCommunities();
+ if (effCommunities != null && effCommunities.contains(CommunityUtil.NO_LLGR)) {
+ deleteRoute(tx, ribSupport, routePath, routeBefore.orElse(null));
+ return;
+ }
+ optEffAtt = Optional.of(wrapLongLivedStale(routeAttrs));
+ } else {
+ optEffAtt = ribPolicies.applyImportPolicies(peerImportParameters, routeAttrs,
+ verifyNotNull(tableTypeRegistry.getAfiSafiType(ribSupport.getTablesKey())));
+ }
+ if (optEffAtt.isEmpty()) {
+ deleteRoute(tx, ribSupport, routePath, routeBefore.orElse(null));
+ return;
}
+ handleRouteTarget(ModificationType.WRITE, ribSupport, routePath, routeAfter);
+ tx.put(LogicalDatastoreType.OPERATIONAL, routePath, routeAfter);
+ CountersUtil.increment(prefixesInstalled.get(tablesKey), tablesKey);
- @Override
- public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
- LOG.trace("Data changed called to effective RIB. Change : {}", changes);
- final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
+ final Attributes attToStore = optEffAtt.orElseThrow();
+ if (!attToStore.equals(routeAttrs)) {
+ final YangInstanceIdentifier attPath = routePath.node(ribSupport.routeAttributesIdentifier());
+ final ContainerNode finalAttribute = ribSupport.attributeToContainerNode(attPath, attToStore);
+ tx.put(LogicalDatastoreType.OPERATIONAL, attPath, finalAttribute);
+ }
+ }
- for (final DataTreeCandidate tc : changes) {
- final YangInstanceIdentifier rootPath = tc.getRootPath();
+ private void addRouteTarget(final RouteTargetConstrainRoute rtc) {
+ final RouteTarget rtMembership = RouteTargetMembeshipUtil.getRT(rtc);
+ if (PeerRole.Ebgp != peerImportParameters.getFromPeerRole()) {
+ rtCache.cacheRoute(rtc);
+ }
+ rtMemberships.add(rtMembership);
+ }
- // Obtain the peer's key
- final NodeIdentifierWithPredicates peerKey = IdentifierUtils.peerKey(rootPath);
+ private void deleteRouteTarget(final RIBSupport<?, ?> ribSupport, final YangInstanceIdentifier routeIdPath,
+ final NormalizedNode route) {
+ deleteRouteTarget((RouteTargetConstrainRoute) ribSupport.fromNormalizedNode(routeIdPath, route));
+ }
- // Extract the table key, this should be safe based on the path where we subscribed,
- // but let's verify explicitly.
- final PathArgument lastArg = rootPath.getLastPathArgument();
- Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), rootPath);
- final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
+ private void deleteRouteTarget(final RouteTargetConstrainRoute rtc) {
+ final RouteTarget rtMembership = RouteTargetMembeshipUtil.getRT(rtc);
+ if (PeerRole.Ebgp != peerImportParameters.getFromPeerRole()) {
+ rtCache.uncacheRoute(rtc);
+ }
+ rtMemberships.remove(rtMembership);
+ }
- final DataTreeCandidateNode root = tc.getRootNode();
- switch (root.getModificationType()) {
- case DELETE:
- // delete the corresponding effective table
- tx.delete(LogicalDatastoreType.OPERATIONAL, effectiveTablePath(peerKey, tableKey));
- break;
- case MERGE:
- // TODO: upstream API should never give us this, as it leaks how the delta was created.
- LOG.info("Merge on {} reported, this should never have happened, but attempting to cope", rootPath);
- modifyTable(tx, peerKey, tableKey, root);
- break;
- case SUBTREE_MODIFIED:
- modifyTable(tx, peerKey, tableKey, root);
- break;
- case UNMODIFIED:
- LOG.info("Ignoring spurious notification on {} data {}", rootPath, root);
- break;
- case WRITE:
- writeTable(tx, peerKey, tableKey, root);
- break;
- default:
- LOG.warn("Ignoring unhandled root {}", root);
- break;
- }
+ private void handleRouteTarget(final ModificationType modificationType, final RIBSupport<?, ?> ribSupport,
+ final YangInstanceIdentifier routeIdPath, final NormalizedNode route) {
+ if (RouteTargetConstrainSubsequentAddressFamily.VALUE.equals(ribSupport.getTablesKey().getSafi())) {
+ final var rtc = (RouteTargetConstrainRoute) ribSupport.fromNormalizedNode(routeIdPath, route);
+ if (ModificationType.DELETE == modificationType) {
+ deleteRouteTarget(rtc);
+ } else {
+ addRouteTarget(rtc);
}
+ rtMembershipsUpdated = true;
+ }
+ }
- tx.submit();
+ @SuppressFBWarnings("UPM_UNCALLED_PRIVATE_METHOD")
+ private static Attributes wrapLongLivedStale(final Attributes attrs) {
+ if (attrs == null) {
+ return STALE_LLGR_ATTRIBUTES;
+ }
+
+ final List<Communities> oldCommunities = attrs.getCommunities();
+ final List<Communities> newCommunities;
+ if (oldCommunities != null) {
+ if (oldCommunities.contains(StaleCommunities.STALE_LLGR)) {
+ return attrs;
+ }
+ newCommunities = StaleCommunities.create(oldCommunities);
+ } else {
+ newCommunities = STALE_LLGR_COMMUNUTIES;
}
- @Override
- public void close() {
- // FIXME: wipe all effective routes?
- this.reg.close();
+ return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120
+ .path.attributes.AttributesBuilder(attrs).setCommunities(newCommunities).build();
+ }
+
+ // XXX: this should be moved to YangInstanceIdentifier at some point
+ private static YangInstanceIdentifier concat(final YangInstanceIdentifier parent, final List<PathArgument> args) {
+ YangInstanceIdentifier ret = parent;
+ for (PathArgument arg : args) {
+ ret = ret.node(arg);
}
+ return ret;
}
- private final ImportPolicyPeerTracker peerPolicyTracker;
- private final AdjInTracker adjInTracker;
+ private YangInstanceIdentifier effectiveTablePath(final NodeIdentifierWithPredicates tableKey) {
+ return effRibTables.node(TABLES_NID).node(tableKey);
+ }
- static EffectiveRibInWriter create(@Nonnull final DOMDataTreeChangeService service, @Nonnull final DOMTransactionChain chain,
- @Nonnull final YangInstanceIdentifier ribId, @Nonnull final PolicyDatabase pd, @Nonnull final RIBSupportContextRegistry registry) {
- return new EffectiveRibInWriter(service, chain, ribId, pd, registry);
+ private static YangInstanceIdentifier routeMapPath(final RIBSupport<?, ?> ribSupport,
+ final YangInstanceIdentifier tablePath) {
+ return concat(tablePath.node(ROUTES_NID), ribSupport.relativeRoutesPath());
}
- private EffectiveRibInWriter(final DOMDataTreeChangeService service, final DOMTransactionChain chain, final YangInstanceIdentifier ribId,
- final PolicyDatabase pd, final RIBSupportContextRegistry registry) {
- this.peerPolicyTracker = new ImportPolicyPeerTracker(service, ribId, pd);
- this.adjInTracker = new AdjInTracker(service, registry, chain, ribId);
+ private static Optional<NormalizedNode> findRoutesMap(final RIBSupport<?, ?> ribSupport,
+ final Optional<NormalizedNode> optRoutes) {
+ return NormalizedNodes.findNode(optRoutes, ribSupport.relativeRoutesPath());
}
- @Override
- public void close() {
- this.adjInTracker.close();
- this.peerPolicyTracker.close();
+ private static ContainerNode extractContainer(final NormalizedNode node) {
+ verify(node instanceof ContainerNode, "Expected ContainerNode, got %s", node);
+ return (ContainerNode) node;
+ }
+
+ private static MapNode extractMap(final Optional<? extends NormalizedNode> optNode) {
+ final NormalizedNode node = optNode.orElseThrow();
+ verify(node instanceof MapNode, "Expected MapNode, got %s", node);
+ return (MapNode) node;
+ }
+
+ private static boolean isLongLivedStale(final ContainerNode attributes) {
+ return NormalizedNodes.findNode(attributes, ADJRIBIN_ATTRIBUTES_AID, LLGR_STALE_NID).isPresent();
+ }
+
+ private static boolean isLongLivedStaleTable(final Optional<NormalizedNode> optTable) {
+ final Optional<NormalizedNode> optAttributes = NormalizedNodes.findNode(optTable, ATTRIBUTES_NID);
+ return optAttributes.isPresent() && isLongLivedStale(extractContainer(optAttributes.orElseThrow()));
+ }
+
+ private static ContainerNode effectiveAttributes(final ContainerNode attrs) {
+ final var upToDate = attrs.childByArg(UPTODATE_NID);
+ if (upToDate != null) {
+ final Object value = upToDate.body();
+ verify(value instanceof Boolean, "Expected boolean uptodate, got %s", value);
+ if ((Boolean) value) {
+ return RIBNormalizedNodes.UPTODATE_ATTRIBUTES;
+ }
+ }
+ return RIBNormalizedNodes.NOT_UPTODATE_ATTRIBUTES;
}
}