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.EnumMap;
14 import java.util.HashMap;
16 import java.util.Map.Entry;
17 import javax.annotation.concurrent.NotThreadSafe;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
22 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 DOMDataTreeChangeListener {
35 private final Map<PathArgument, RouteEntry> routeEntries = new HashMap<>();
36 private final YangInstanceIdentifier target;
37 private final DOMTransactionChain chain;
38 private final RIBSupport ribSupport;
39 private final Long ourAs;
41 // FIXME: these maps need to be populated
42 private final Map<PeerRole, Map<PeerId, YangInstanceIdentifier>> peersToUpdate = new EnumMap<>(PeerRole.class);
43 private final Map<PeerId, PeerRole> peers = new HashMap<>();
45 LocRibWriter(final RIBSupport ribSupport, final DOMTransactionChain chain, final YangInstanceIdentifier target, final Long ourAs) {
46 this.chain = Preconditions.checkNotNull(chain);
47 this.target = Preconditions.checkNotNull(target);
48 this.ourAs = Preconditions.checkNotNull(ourAs);
49 this.ribSupport = Preconditions.checkNotNull(ribSupport);
53 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
55 * We use two-stage processing here in hopes that we avoid duplicate
56 * calculations when multiple peers have changed a particular entry.
58 final Map<RouteUpdateKey, RouteEntry> toUpdate = new HashMap<>();
59 for (DataTreeCandidate tc : changes) {
60 final YangInstanceIdentifier path = tc.getRootPath();
61 final PathArgument routeId = path.getLastPathArgument();
62 final NodeIdentifierWithPredicates peerKey = EffectiveRibInWriter.peerKey(path);
63 final PeerId peerId = EffectiveRibInWriter.peerId(peerKey);
64 final UnsignedInteger routerId = RouterIds.routerIdForPeerId(peerId);
66 RouteEntry entry = routeEntries.get(routeId);
67 if (tc.getRootNode().getDataAfter().isPresent()) {
69 entry = new RouteEntry();
70 routeEntries.put(routeId, entry);
73 entry.addRoute(routerId, (ContainerNode) tc.getRootNode().getDataAfter().get());
74 } else if (entry != null) {
75 if (entry.removeRoute(routerId)) {
76 routeEntries.remove(routeId);
81 toUpdate.put(new RouteUpdateKey(peerId, routeId), entry);
84 final DOMDataWriteTransaction tx = chain.newWriteOnlyTransaction();
86 // Now walk all updated entries
87 for (Entry<RouteUpdateKey, RouteEntry> e : toUpdate.entrySet()) {
88 final RouteEntry entry = e.getValue();
89 final NormalizedNode<?, ?> value;
92 if (!entry.selectBest(ourAs)) {
93 // Best path has not changed, no need to do anything else. Proceed to next route.
97 value = entry.bestValue(e.getKey().getRouteId());
103 tx.put(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()), value);
105 tx.delete(LogicalDatastoreType.OPERATIONAL, target.node(e.getKey().getRouteId()));
109 * We need to keep track of routers and populate adj-ribs-out, too. If we do not, we need to
110 * expose from which client a particular route was learned from in the local RIB, and have
111 * the listener perform filtering.
113 * We walk the policy set in order to minimize the amount of work we do for multiple peers:
114 * if we have two eBGP peers, for example, there is no reason why we should perform the translation
117 for (Entry<PeerRole, AbstractExportPolicy> pe : AbstractExportPolicy.POLICIES.entrySet()) {
118 final Map<PeerId, YangInstanceIdentifier> toPeers = peersToUpdate.get(pe.getKey());
119 if (toPeers == null || toPeers.isEmpty()) {
123 final ContainerNode attributes = null;
124 final PeerId peerId = e.getKey().getPeerId();
125 final ContainerNode effectiveAttributes = pe.getValue().effectiveAttributes(peers.get(peerId), attributes);
127 for (Entry<PeerId, YangInstanceIdentifier> pid : toPeers.entrySet()) {
128 // This points to adj-rib-out for a particlar 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(ribSupport.routeAttributes()), effectiveAttributes);
135 tx.delete(LogicalDatastoreType.OPERATIONAL, routeTarget);