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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.primitives.UnsignedInteger;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.HashMap;
17 import java.util.Map.Entry;
18 import javax.annotation.Nonnull;
19 import javax.annotation.concurrent.NotThreadSafe;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
27 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
28 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.LocRib;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibOut;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.EffectiveRibIn;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
46 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
50 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
51 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 final class LocRibWriter implements AutoCloseable, DOMDataTreeChangeListener {
58 private static final Logger LOG = LoggerFactory.getLogger(LocRibWriter.class);
60 private static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_TRUE = ImmutableNodes.leafNode(QName.create(Attributes.QNAME, "uptodate"), Boolean.TRUE);
61 private static final NodeIdentifier ROUTES_IDENTIFIER = new NodeIdentifier(Routes.QNAME);
62 private static final NodeIdentifier EFFRIBIN_NID = new NodeIdentifier(EffectiveRibIn.QNAME);
63 private static final NodeIdentifier TABLES_NID = new NodeIdentifier(Tables.QNAME);
65 private final Map<PathArgument, AbstractRouteEntry> routeEntries = new HashMap<>();
66 private final YangInstanceIdentifier locRibTarget;
67 private final DOMTransactionChain chain;
68 private final ExportPolicyPeerTracker peerPolicyTracker;
69 private final NodeIdentifier attributesIdentifier;
70 private final Long ourAs;
71 private final RIBSupport ribSupport;
72 private final NodeIdentifierWithPredicates tableKey;
73 private final RIBSupportContextRegistry registry;
74 private final ListenerRegistration<LocRibWriter> reg;
76 LocRibWriter(final RIBSupportContextRegistry registry, final DOMTransactionChain chain, final YangInstanceIdentifier target, final Long ourAs,
77 final DOMDataTreeChangeService service, final PolicyDatabase pd, final TablesKey tablesKey) {
78 this.chain = Preconditions.checkNotNull(chain);
79 this.tableKey = RibSupportUtils.toYangTablesKey(tablesKey);
80 this.locRibTarget = YangInstanceIdentifier.create(target.node(LocRib.QNAME).node(Tables.QNAME).node(this.tableKey).getPathArguments());
81 this.ourAs = Preconditions.checkNotNull(ourAs);
82 this.registry = registry;
83 this.ribSupport = this.registry.getRIBSupportContext(tablesKey).getRibSupport();
84 this.attributesIdentifier = this.ribSupport.routeAttributesIdentifier();
85 this.peerPolicyTracker = new ExportPolicyPeerTracker(pd);
87 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
88 tx.merge(LogicalDatastoreType.OPERATIONAL, this.locRibTarget.node(Routes.QNAME), this.ribSupport.emptyRoutes());
89 tx.merge(LogicalDatastoreType.OPERATIONAL, this.locRibTarget.node(Attributes.QNAME).node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
92 final YangInstanceIdentifier tableId = target.node(Peer.QNAME).node(Peer.QNAME);
94 this.reg = service.registerDataTreeChangeListener(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, tableId), this);
97 public static LocRibWriter create(@Nonnull final RIBSupportContextRegistry registry, @Nonnull final TablesKey tablesKey, @Nonnull final DOMTransactionChain chain, @Nonnull final YangInstanceIdentifier target,
98 @Nonnull final AsNumber ourAs, @Nonnull final DOMDataTreeChangeService service, @Nonnull final PolicyDatabase pd) {
99 return new LocRibWriter(registry, chain, target, ourAs.getValue(), service, pd, tablesKey);
103 public void close() {
105 // FIXME: wipe the local rib
106 // FIXME: wait for the chain to close? unfortunately RIBImpl is the listener, so that may require some work
110 @Nonnull private AbstractRouteEntry createEntry(final PathArgument routeId) {
111 final AbstractRouteEntry ret = this.ribSupport.isComplexRoute() ? new ComplexRouteEntry() : new SimpleRouteEntry();
113 this.routeEntries.put(routeId, ret);
114 LOG.trace("Created new entry for {}", routeId);
119 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
120 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
121 if (LOG.isTraceEnabled()) {
122 LOG.trace("Received data change to LocRib {}", Arrays.toString(changes.toArray()));
126 * We use two-stage processing here in hopes that we avoid duplicate
127 * calculations when multiple peers have changed a particular entry.
129 final Map<RouteUpdateKey, AbstractRouteEntry> toUpdate = new HashMap<>();
130 update(tx, changes, toUpdate);
132 // Now walk all updated entries
133 walkThrough(tx, toUpdate);
138 private void update(final DOMDataWriteTransaction tx, final Collection<DataTreeCandidate> changes,
139 final Map<RouteUpdateKey, AbstractRouteEntry> toUpdate) {
141 for (final DataTreeCandidate tc : changes) {
142 // call out peer-role has changed
143 final YangInstanceIdentifier rootPath = tc.getRootPath();
144 final DataTreeCandidateNode rootNode = tc.getRootNode();
145 final DataTreeCandidateNode roleChange = rootNode.getModifiedChild(AbstractPeerRoleTracker.PEER_ROLE_NID);
146 if (roleChange != null) {
147 this.peerPolicyTracker.onDataTreeChanged(roleChange, IdentifierUtils.peerPath(rootPath));
149 // filter out any change outside EffRibsIn
150 final DataTreeCandidateNode ribIn = rootNode.getModifiedChild(EFFRIBIN_NID);
152 LOG.debug("Skipping change {}", tc.getRootNode());
155 final DataTreeCandidateNode table = ribIn.getModifiedChild(TABLES_NID).getModifiedChild(this.tableKey);
157 LOG.debug("Skipping change {}", tc.getRootNode());
160 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.peerKey(rootPath);
161 final PeerId peerId = IdentifierUtils.peerId(peerKey);
162 final UnsignedInteger routerId = RouterIds.routerIdForPeerId(peerId);
163 for (final DataTreeCandidateNode child : table.getChildNodes()) {
164 if ((Attributes.QNAME).equals(child.getIdentifier().getNodeType())) {
165 if (child.getDataAfter().isPresent()) {
166 // putting uptodate attribute in
167 LOG.trace("Uptodate found for {}", child.getDataAfter());
168 tx.put(LogicalDatastoreType.OPERATIONAL, this.locRibTarget.node(child.getIdentifier()), child.getDataAfter().get());
172 for (final DataTreeCandidateNode route : this.ribSupport.changedRoutes(child)) {
173 final PathArgument routeId = route.getIdentifier();
174 AbstractRouteEntry entry = this.routeEntries.get(routeId);
176 final Optional<NormalizedNode<?, ?>> maybeData = route.getDataAfter();
177 if (maybeData.isPresent()) {
179 entry = createEntry(routeId);
182 entry.addRoute(routerId, this.attributesIdentifier, maybeData.get());
183 } else if (entry != null && entry.removeRoute(routerId)) {
184 this.routeEntries.remove(routeId);
186 LOG.trace("Removed route from {}", routerId);
188 LOG.debug("Updated route {} entry {}", routeId, entry);
189 toUpdate.put(new RouteUpdateKey(peerId, routeId), entry);
195 private void walkThrough(final DOMDataWriteTransaction tx, final Map<RouteUpdateKey, AbstractRouteEntry> toUpdate) {
196 for (final Entry<RouteUpdateKey, AbstractRouteEntry> e : toUpdate.entrySet()) {
197 LOG.trace("Walking through {}", e);
198 final AbstractRouteEntry entry = e.getValue();
199 final RouteUpdateKey key = e.getKey();
200 final NormalizedNode<?, ?> value;
203 if (!entry.selectBest(this.ourAs)) {
204 // Best path has not changed, no need to do anything else. Proceed to next route.
205 LOG.trace("Continuing");
208 value = entry.createValue(key.getRouteId());
209 LOG.trace("Selected best value {}", value);
214 final YangInstanceIdentifier writePath = this.ribSupport.routePath(this.locRibTarget.node(ROUTES_IDENTIFIER), key.getRouteId());
216 LOG.debug("Write route to LocRib {}", value);
217 tx.put(LogicalDatastoreType.OPERATIONAL, writePath, value);
219 LOG.debug("Delete route from LocRib {}", entry);
220 tx.delete(LogicalDatastoreType.OPERATIONAL, writePath);
222 fillAdjRibsOut(tx, entry, value, key);
226 private void fillAdjRibsOut(final DOMDataWriteTransaction tx, final AbstractRouteEntry entry, final NormalizedNode<?, ?> value, final RouteUpdateKey key) {
228 * We need to keep track of routers and populate adj-ribs-out, too. If we do not, we need to
229 * expose from which client a particular route was learned from in the local RIB, and have
230 * the listener perform filtering.
232 * We walk the policy set in order to minimize the amount of work we do for multiple peers:
233 * if we have two eBGP peers, for example, there is no reason why we should perform the translation
236 for (final PeerRole role : PeerRole.values()) {
237 final PeerExportGroup peerGroup = this.peerPolicyTracker.getPeerGroup(role);
238 if (peerGroup != null) {
239 final ContainerNode attributes = entry == null ? null : entry.attributes();
240 final PeerId peerId = key.getPeerId();
241 final ContainerNode effectiveAttributes = peerGroup.effectiveAttributes(peerId, attributes);
242 for (final Entry<PeerId, YangInstanceIdentifier> pid : peerGroup.getPeers()) {
243 final YangInstanceIdentifier routeTarget = this.ribSupport.routePath(pid.getValue().node(AdjRibOut.QNAME).node(Tables.QNAME).node(this.tableKey).node(ROUTES_IDENTIFIER), key.getRouteId());
244 if (effectiveAttributes != null && value != null && !peerId.equals(pid.getKey())) {
245 LOG.debug("Write route to AdjRibsOut {}", value);
246 tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
247 tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(this.attributesIdentifier), effectiveAttributes);
249 LOG.trace("Removing {} from transaction", routeTarget);
250 tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);