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.primitives.UnsignedInteger;
12 import java.util.Collection;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
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.spi.RIBSupport;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 // FIXME: instantiate for each table, listen on wildcard peer and routes
34 final class LocRibWriter implements AutoCloseable, DOMDataTreeChangeListener {
35 private final Map<PathArgument, RouteEntry> routeEntries = new HashMap<>();
36 private final YangInstanceIdentifier target;
37 private final DOMTransactionChain chain;
38 private final ExportPolicyPeerTracker peerPolicyTracker;
39 private final NodeIdentifier attributesIdentifier;
40 private final Long ourAs;
42 LocRibWriter(final RIBSupport ribSupport, final DOMTransactionChain chain, final YangInstanceIdentifier target, final Long ourAs) {
43 this.chain = Preconditions.checkNotNull(chain);
44 this.target = Preconditions.checkNotNull(target);
45 this.ourAs = Preconditions.checkNotNull(ourAs);
46 this.attributesIdentifier = ribSupport.routeAttributesIdentifier();
48 // FIXME: proper values
49 this.peerPolicyTracker = new ExportPolicyPeerTracker(null, null, null);
54 peerPolicyTracker.close();
58 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
60 * We use two-stage processing here in hopes that we avoid duplicate
61 * calculations when multiple peers have changed a particular entry.
63 final Map<RouteUpdateKey, RouteEntry> toUpdate = new HashMap<>();
64 for (final DataTreeCandidate tc : changes) {
65 final YangInstanceIdentifier path = tc.getRootPath();
66 final PathArgument routeId = path.getLastPathArgument();
67 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.peerKey(path);
68 final PeerId peerId = IdentifierUtils.peerId(peerKey);
69 final UnsignedInteger routerId = RouterIds.routerIdForPeerId(peerId);
71 RouteEntry entry = this.routeEntries.get(routeId);
72 if (tc.getRootNode().getDataAfter().isPresent()) {
74 entry = new RouteEntry();
75 this.routeEntries.put(routeId, entry);
78 entry.addRoute(routerId, (ContainerNode) tc.getRootNode().getDataAfter().get());
79 } else if (entry != null && entry.removeRoute(routerId)) {
80 this.routeEntries.remove(routeId);
84 toUpdate.put(new RouteUpdateKey(peerId, routeId), entry);
87 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
89 // Now walk all updated entries
90 for (final Entry<RouteUpdateKey, RouteEntry> e : toUpdate.entrySet()) {
91 final RouteEntry entry = e.getValue();
92 final NormalizedNode<?, ?> value;
95 if (!entry.selectBest(this.ourAs)) {
96 // Best path has not changed, no need to do anything else. Proceed to next route.
100 value = entry.bestValue(e.getKey().getRouteId());
106 tx.put(LogicalDatastoreType.OPERATIONAL, this.target.node(e.getKey().getRouteId()), value);
108 tx.delete(LogicalDatastoreType.OPERATIONAL, this.target.node(e.getKey().getRouteId()));
112 * We need to keep track of routers and populate adj-ribs-out, too. If we do not, we need to
113 * expose from which client a particular route was learned from in the local RIB, and have
114 * the listener perform filtering.
116 * We walk the policy set in order to minimize the amount of work we do for multiple peers:
117 * if we have two eBGP peers, for example, there is no reason why we should perform the translation
120 for (PeerRole role : PeerRole.values()) {
121 final PeerExportGroup peerGroup = peerPolicyTracker.getPeerGroup(role);
122 if (peerGroup != null) {
123 final ContainerNode attributes = null;
124 final PeerId peerId = e.getKey().getPeerId();
125 final ContainerNode effectiveAttributes = peerGroup.effectiveAttributes(peerId, attributes);
127 for (final Entry<PeerId, YangInstanceIdentifier> pid : peerGroup.getPeers()) {
128 // This points to adj-rib-out for a particular peer/table combination
129 final YangInstanceIdentifier routeTarget = pid.getValue().node(e.getKey().getRouteId());
131 if (effectiveAttributes != null && value != null && !peerId.equals(pid.getKey())) {
132 tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
133 tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(attributesIdentifier), effectiveAttributes);
135 tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);