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);
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 (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 = routeEntries.get(routeId);
72 if (tc.getRootNode().getDataAfter().isPresent()) {
74 entry = new RouteEntry();
75 routeEntries.put(routeId, entry);
78 entry.addRoute(routerId, (ContainerNode) tc.getRootNode().getDataAfter().get());
79 } else if (entry != null) {
80 if (entry.removeRoute(routerId)) {
81 routeEntries.remove(routeId);
86 toUpdate.put(new RouteUpdateKey(peerId, routeId), entry);
89 final DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
91 // Now walk all updated entries
92 for (Entry<RouteUpdateKey, RouteEntry> e : toUpdate.entrySet()) {
93 final RouteEntry entry = e.getValue();
94 final NormalizedNode<?, ?> value;
97 if (!entry.selectBest(ourAs)) {
98 // Best path has not changed, no need to do anything else. Proceed to next route.
102 value = entry.bestValue(e.getKey().getRouteId());
108 tx.put(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()), value);
110 tx.delete(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()));
114 * We need to keep track of routers and populate adj-ribs-out, too. If we do not, we need to
115 * expose from which client a particular route was learned from in the local RIB, and have
116 * the listener perform filtering.
118 * We walk the policy set in order to minimize the amount of work we do for multiple peers:
119 * if we have two eBGP peers, for example, there is no reason why we should perform the translation
122 for (PeerRole role : PeerRole.values()) {
123 final PeerExportGroup peerGroup = peerPolicyTracker.getPeerGroup(role);
124 if (peerGroup != null) {
125 final ContainerNode attributes = null;
126 final PeerId peerId = e.getKey().getPeerId();
127 final ContainerNode effectiveAttributes = peerGroup.effectiveAttributes(peerId, attributes);
129 for (Entry<PeerId, YangInstanceIdentifier> pid : peerGroup.getPeers()) {
130 // This points to adj-rib-out for a particular peer/table combination
131 final YangInstanceIdentifier routeTarget = pid.getValue().node(e.getKey().getRouteId());
133 if (effectiveAttributes != null && value != null && !peerId.equals(pid.getKey())) {
134 tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget, value);
135 tx.put(LogicalDatastoreType.OPERATIONAL, routeTarget.node(attributesIdentifier), effectiveAttributes);
137 tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);