2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import java.util.Collection;
13 import javax.annotation.Nonnull;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
23 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.graceful.restart._case.graceful.restart.capability.Tables;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.EffectiveRibIn;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Implementation of the BGP import policy. Listens on all Adj-RIB-In, inspects all inbound
43 * routes in the context of the advertising peer's role and applies the inbound policy.
45 * Inbound policy is applied as follows:
47 * 1) if the peer is an eBGP peer, perform attribute replacement and filtering
48 * 2) check if a route is admissible based on attributes attached to it, as well as the
49 * advertising peer's role
50 * 3) output admitting routes with edited attributes into /bgp-rib/rib/peer/effective-rib-in/tables/routes
52 * Note that we maintain the peer roles using a DCL, even if we could look up our internal
53 * structures. This is done so we maintain causality and loose coupling.
56 final class EffectiveRibInWriter implements AutoCloseable {
57 private static final Logger LOG = LoggerFactory.getLogger(EffectiveRibInWriter.class);
58 private static final NodeIdentifier TABLE_ROUTES = new NodeIdentifier(Routes.QNAME);
61 * Maintains {@link TableRouteListener} instances.
63 private final class AdjInTracker implements AutoCloseable, DOMDataTreeChangeListener {
64 private final RIBSupportContextRegistry registry;
65 private final YangInstanceIdentifier ribId;
66 private final ListenerRegistration<?> reg;
67 private final DOMTransactionChain chain;
69 AdjInTracker(final DOMDataTreeChangeService service, final RIBSupportContextRegistry registry, final DOMTransactionChain chain, final YangInstanceIdentifier ribId) {
70 this.registry = Preconditions.checkNotNull(registry);
71 this.chain = Preconditions.checkNotNull(chain);
72 this.ribId = Preconditions.checkNotNull(ribId);
74 final YangInstanceIdentifier tableId = ribId.node(Peer.QNAME).node(AdjRibIn.QNAME).node(Tables.QNAME);
75 final DOMDataTreeIdentifier treeId = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, tableId);
76 this.reg = service.registerDataTreeChangeListener(treeId, this);
79 private void processRoute(final DOMDataWriteTransaction tx, final RIBSupport ribSupport, final AbstractImportPolicy policy, final YangInstanceIdentifier routesPath, final DataTreeCandidateNode route) {
80 switch (route.getModificationType()) {
82 // Delete has already been affected by the store in caller, so this is a no-op.
85 LOG.info("Merge on {} reported, this should never have happened, ignoring", route);
90 case SUBTREE_MODIFIED:
92 // Lookup per-table attributes from RIBSupport
93 final ContainerNode adverisedAttrs = (ContainerNode) NormalizedNodes.findNode(route.getDataAfter(), ribSupport.routeAttributesIdentifier()).orNull();
94 final ContainerNode effectiveAttrs;
96 if (adverisedAttrs != null) {
97 effectiveAttrs = policy.effectiveAttributes(adverisedAttrs);
100 * Speed hack: if we determine that the policy has passed the attributes
101 * back unmodified, the corresponding change has already been written in
102 * our caller. There is no need to perform any further processing.
104 * We also use direct object comparison to make the check very fast, as
105 * it may not be that common, in which case it does not make sense to pay
106 * the full equals price.
108 if (effectiveAttrs == adverisedAttrs) {
112 effectiveAttrs = null;
115 final YangInstanceIdentifier routeId = ribSupport.routePath(routesPath, route.getIdentifier());
116 LOG.debug("Route {} effective attributes {} towards {}", route.getIdentifier(), effectiveAttrs, routeId);
118 if (effectiveAttrs != null) {
119 tx.put(LogicalDatastoreType.OPERATIONAL, routeId.node(ribSupport.routeAttributesIdentifier()), effectiveAttrs);
121 LOG.warn("Route {} advertised empty attributes", route.getDataAfter());
122 tx.delete(LogicalDatastoreType.OPERATIONAL, routeId);
126 LOG.warn("Ignoring unhandled route {}", route);
131 private void processTableChildren(final DOMDataWriteTransaction tx, final RIBSupport ribSupport, final NodeIdentifierWithPredicates peerKey, final YangInstanceIdentifier tablePath, final Collection<DataTreeCandidateNode> children) {
132 final AbstractImportPolicy policy = EffectiveRibInWriter.this.peerPolicyTracker.policyFor(IdentifierUtils.peerId(peerKey));
134 for (final DataTreeCandidateNode child : children) {
135 switch (child.getModificationType()) {
137 tx.delete(LogicalDatastoreType.OPERATIONAL, tablePath.node(child.getIdentifier()));
140 LOG.info("Merge on {} reported, this should never have happened, ignoring", child);
145 case SUBTREE_MODIFIED:
147 tx.put(LogicalDatastoreType.OPERATIONAL, tablePath.node(child.getIdentifier()), child.getDataAfter().get());
149 // Routes are special, as they may end up being filtered. The previous put conveniently
150 // ensured that we have them in at target, so a subsequent delete will not fail :)
151 if (TABLE_ROUTES.equals(child.getIdentifier())) {
152 final YangInstanceIdentifier routesPath = tablePath.node(Routes.QNAME);
153 for (final DataTreeCandidateNode route : ribSupport.changedRoutes(child)) {
154 processRoute(tx, ribSupport, policy, routesPath, route);
159 LOG.warn("Ignoring unhandled child {}", child);
165 private RIBSupportContext getRibSupport(final NodeIdentifierWithPredicates tableKey) {
166 // FIXME: use codec to translate tableKey
167 return this.registry.getRIBSupportContext(null);
170 private YangInstanceIdentifier effectiveTablePath(final NodeIdentifierWithPredicates peerKey, final NodeIdentifierWithPredicates tableKey) {
171 return this.ribId.node(Peer.QNAME).node(peerKey).node(EffectiveRibIn.QNAME).node(tableKey);
174 private void modifyTable(final DOMDataWriteTransaction tx, final NodeIdentifierWithPredicates peerKey, final NodeIdentifierWithPredicates tableKey, final DataTreeCandidateNode table) {
175 final RIBSupportContext ribSupport = getRibSupport(tableKey);
176 final YangInstanceIdentifier tablePath = effectiveTablePath(peerKey, tableKey);
178 processTableChildren(tx, ribSupport.getRibSupport(), peerKey, tablePath, table.getChildNodes());
181 private void writeTable(final DOMDataWriteTransaction tx, final NodeIdentifierWithPredicates peerKey, final NodeIdentifierWithPredicates tableKey, final DataTreeCandidateNode table) {
182 final RIBSupportContext ribSupport = getRibSupport(tableKey);
183 final YangInstanceIdentifier tablePath = effectiveTablePath(peerKey, tableKey);
185 // Create an empty table
186 ribSupport.clearTable(tx,tablePath);
188 processTableChildren(tx, ribSupport.getRibSupport(), peerKey, tablePath, table.getChildNodes());
192 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
193 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
195 for (final DataTreeCandidate tc : changes) {
196 final YangInstanceIdentifier rootPath = tc.getRootPath();
198 // Obtain the peer's key
199 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.peerKey(rootPath);
201 // Extract the table key, this should be safe based on the path where we subscribed,
202 // but let's verify explicitly.
203 final PathArgument lastArg = rootPath.getLastPathArgument();
204 Verify.verify(lastArg instanceof NodeIdentifierWithPredicates, "Unexpected type %s in path %s", lastArg.getClass(), rootPath);
205 final NodeIdentifierWithPredicates tableKey = (NodeIdentifierWithPredicates) lastArg;
207 final DataTreeCandidateNode root = tc.getRootNode();
208 switch (root.getModificationType()) {
210 // delete the corresponding effective table
211 tx.delete(LogicalDatastoreType.OPERATIONAL, effectiveTablePath(peerKey, tableKey));
214 // TODO: upstream API should never give us this, as it leaks how the delta was created.
215 LOG.info("Merge on {} reported, this should never have happened, but attempting to cope", rootPath);
216 modifyTable(tx, peerKey, tableKey, root);
218 case SUBTREE_MODIFIED:
219 modifyTable(tx, peerKey, tableKey, root);
222 LOG.info("Ignoring spurious notification on {} data {}", rootPath, root);
225 writeTable(tx, peerKey, tableKey, root);
228 LOG.warn("Ignoring unhandled root {}", root);
237 public void close() {
238 // FIXME: wipe all effective routes?
243 private final ImportPolicyPeerTracker peerPolicyTracker;
244 private final AdjInTracker adjInTracker;
246 static EffectiveRibInWriter create(@Nonnull final DOMDataTreeChangeService service, @Nonnull final DOMTransactionChain chain,
247 @Nonnull final YangInstanceIdentifier ribId, @Nonnull final PolicyDatabase pd, @Nonnull final RIBSupportContextRegistry registry) {
248 return new EffectiveRibInWriter(service, chain, ribId, pd, registry);
251 private EffectiveRibInWriter(final DOMDataTreeChangeService service, final DOMTransactionChain chain, final YangInstanceIdentifier ribId,
252 final PolicyDatabase pd, final RIBSupportContextRegistry registry) {
254 this.peerPolicyTracker = new ImportPolicyPeerTracker(service, ribId, pd);
255 this.adjInTracker = new AdjInTracker(service, registry, chain, ribId);
259 public void close() {
260 this.adjInTracker.close();
261 this.peerPolicyTracker.close();