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.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import java.util.Collections;
16 import java.util.Map.Entry;
17 import java.util.Optional;
19 import javax.annotation.Nonnull;
20 import javax.annotation.concurrent.NotThreadSafe;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
27 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
28 import org.opendaylight.protocol.bgp.rib.spi.PeerRoleUtil;
29 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibOut;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.EffectiveRibIn;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.SupportedTables;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Attributes;
44 import org.opendaylight.yangtools.yang.common.QName;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
52 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
53 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 * Writer of Adjacency-RIB-In for a single peer. An instance of this object
62 * is attached to each {@link BGPPeer} and {@link ApplicationPeer}.
65 final class AdjRibInWriter {
66 private static final Logger LOG = LoggerFactory.getLogger(AdjRibInWriter.class);
68 static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_FALSE = ImmutableNodes.leafNode(QName.create(Attributes.QNAME, "uptodate"), Boolean.FALSE);
69 private static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_TRUE = ImmutableNodes.leafNode(ATTRIBUTES_UPTODATE_FALSE.getNodeType(), Boolean.TRUE);
71 static final QName PEER_ID_QNAME = QName.create(Peer.QNAME, "peer-id").intern();
72 private static final QName PEER_ROLE_QNAME = QName.create(Peer.QNAME, "peer-role").intern();
73 private static final NodeIdentifier ADJRIBIN = new NodeIdentifier(AdjRibIn.QNAME);
74 private static final NodeIdentifier ADJRIBOUT = new NodeIdentifier(AdjRibOut.QNAME);
75 private static final NodeIdentifier EFFRIBIN = new NodeIdentifier(EffectiveRibIn.QNAME);
76 private static final NodeIdentifier PEER_ID = new NodeIdentifier(PEER_ID_QNAME);
77 private static final NodeIdentifier PEER_ROLE = new NodeIdentifier(PEER_ROLE_QNAME);
78 private static final NodeIdentifier PEER_TABLES = new NodeIdentifier(SupportedTables.QNAME);
79 private static final NodeIdentifier TABLES = new NodeIdentifier(Tables.QNAME);
80 private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
81 static final NodeIdentifier SIMPLE_ROUTING_POLICY_NID = new NodeIdentifier(QName.create(Peer.QNAME, "simple-routing-policy").intern());
83 // FIXME: is there a utility method to construct this?
84 private static final ContainerNode EMPTY_ADJRIBIN = Builders.containerBuilder().withNodeIdentifier(ADJRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
85 private static final ContainerNode EMPTY_EFFRIBIN = Builders.containerBuilder().withNodeIdentifier(EFFRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
86 private static final ContainerNode EMPTY_ADJRIBOUT = Builders.containerBuilder().withNodeIdentifier(ADJRIBOUT).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
88 private final Map<TablesKey, TableContext> tables;
89 private final YangInstanceIdentifier peerPath;
90 private final YangInstanceIdentifier ribPath;
91 private final DOMTransactionChain chain;
92 private final PeerRole role;
93 private final Optional<SimpleRoutingPolicy> simpleRoutingPolicy;
95 private AdjRibInWriter(final YangInstanceIdentifier ribPath, final DOMTransactionChain chain, final PeerRole role,
96 final Optional<SimpleRoutingPolicy> simpleRoutingPolicy, final YangInstanceIdentifier peerPath, final Map<TablesKey, TableContext> tables) {
97 this.ribPath = Preconditions.checkNotNull(ribPath);
98 this.chain = Preconditions.checkNotNull(chain);
99 this.tables = Preconditions.checkNotNull(tables);
100 this.role = Preconditions.checkNotNull(role);
101 this.simpleRoutingPolicy = simpleRoutingPolicy;
102 this.peerPath = peerPath;
106 * Create a new writer using a transaction chain.
108 * @param role peer's role
109 * @param simpleRoutingPolicy simple Routing Policy {@link SimpleRoutingPolicy}
110 *@param chain transaction chain @return A fresh writer instance
112 static AdjRibInWriter create(@Nonnull final YangInstanceIdentifier ribId, @Nonnull final PeerRole role, final Optional<SimpleRoutingPolicy> simpleRoutingPolicy,
113 @Nonnull final DOMTransactionChain chain) {
114 return new AdjRibInWriter(ribId, chain, role, simpleRoutingPolicy, null, Collections.emptyMap());
118 * Transform this writer to a new writer, which is in charge of specified tables.
119 * Empty tables are created for new entries and old tables are deleted. Once this
120 * method returns, the old instance must not be reasonably used.
122 * @param newPeerId new peer BGP identifier
123 * @param registry RIB extension registry
124 * @param tableTypes New tables, must not be null
125 * @param addPathTablesType
128 AdjRibInWriter transform(final PeerId newPeerId, final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
129 final Map<TablesKey, SendReceive> addPathTablesType) {
130 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
132 final YangInstanceIdentifier newPeerPath;
133 newPeerPath = createEmptyPeerStructure(newPeerId, tx);
134 final ImmutableMap<TablesKey, TableContext> tb = createNewTableInstances(newPeerPath, registry, tableTypes, addPathTablesType, tx);
137 return new AdjRibInWriter(this.ribPath, this.chain, this.role, this.simpleRoutingPolicy, newPeerPath, tb);
141 * Create new table instances, potentially creating their empty entries
145 * @param addPathTablesType
149 private ImmutableMap<TablesKey, TableContext> createNewTableInstances(final YangInstanceIdentifier newPeerPath,
150 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType,
151 final DOMDataWriteTransaction tx) {
153 final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
154 for (final TablesKey tableKey : tableTypes) {
155 final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
156 // TODO: Use returned value once Instance Identifier builder allows for it.
157 final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
159 LOG.warn("No support for table type {}, skipping it", tableKey);
162 installAdjRibsOutTables(newPeerPath, rs, instanceIdentifierKey, tableKey, addPathTablesType.get(tableKey), tx);
163 installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
168 private void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey, final RIBSupportContext rs,
169 final NodeIdentifierWithPredicates instanceIdentifierKey, final DOMDataWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
170 // We will use table keys very often, make sure they are optimized
171 final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath.node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES));
172 idb.nodeWithKey(instanceIdentifierKey.getNodeType(), instanceIdentifierKey.getKeyValues());
174 final TableContext ctx = new TableContext(rs, idb.build());
175 ctx.createEmptyTableStructure(tx);
177 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME).node(ATTRIBUTES_UPTODATE_FALSE.getNodeType()), ATTRIBUTES_UPTODATE_FALSE);
178 LOG.debug("Created table instance {}", ctx.getTableId());
179 tb.put(tableKey, ctx);
182 private void installAdjRibsOutTables(final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
183 final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey, final SendReceive sendReceive,
184 final DOMDataWriteTransaction tx) {
185 if (!isAnnounceNone(this.simpleRoutingPolicy)) {
186 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
187 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> tt = Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
188 for (final Entry<QName, Object> e : supTablesKey.getKeyValues().entrySet()) {
189 tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
191 if(sendReceive != null) {
192 tt.withChild(ImmutableNodes.leafNode(SEND_RECEIVE, sendReceive.toString().toLowerCase()));
194 tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
195 rs.createEmptyTableStructure(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier()).node(TABLES).node(instanceIdentifierKey));
199 private YangInstanceIdentifier createEmptyPeerStructure(final PeerId newPeerId, final DOMDataWriteTransaction tx) {
200 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.domPeerId(newPeerId);
201 final YangInstanceIdentifier newPeerPath = this.ribPath.node(Peer.QNAME).node(peerKey);
203 tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath, peerSkeleton(peerKey, newPeerId.getValue()));
204 LOG.debug("New peer {} structure installed.", newPeerPath);
209 MapEntryNode peerSkeleton(final NodeIdentifierWithPredicates peerKey, final String peerId) {
210 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> pb = Builders.mapEntryBuilder();
211 pb.withNodeIdentifier(peerKey);
212 pb.withChild(ImmutableNodes.leafNode(PEER_ID, peerId));
213 pb.withChild(ImmutableNodes.leafNode(PEER_ROLE, PeerRoleUtil.roleForString(this.role)));
214 if (this.simpleRoutingPolicy.isPresent() && this.role != PeerRole.Internal) {
215 pb.withChild(ImmutableNodes.leafNode(SIMPLE_ROUTING_POLICY_NID, simpleRoutingPolicyString(this.simpleRoutingPolicy.get())));
217 pb.withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(PEER_TABLES).build());
218 pb.withChild(EMPTY_ADJRIBIN);
219 if(!isLearnNone(this.simpleRoutingPolicy)) {
220 pb.withChild(EMPTY_EFFRIBIN);
222 if (!isAnnounceNone(this.simpleRoutingPolicy)) {
223 pb.withChild(EMPTY_ADJRIBOUT);
229 if(this.peerPath != null) {
230 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
231 tx.delete(LogicalDatastoreType.OPERATIONAL, this.peerPath);
234 tx.submit().checkedGet();
235 } catch (final TransactionCommitFailedException e) {
236 LOG.debug("Failed to remove Peer {}", this.peerPath, e);
241 void markTableUptodate(final TablesKey tableTypes) {
242 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
243 final TableContext ctx = this.tables.get(tableTypes);
244 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME).node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
248 void updateRoutes(final MpReachNlri nlri, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes attributes) {
249 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
250 final TableContext ctx = this.tables.get(key);
252 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
256 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
257 ctx.writeRoutes(tx, nlri, attributes);
258 LOG.trace("Write routes {}", nlri);
262 void removeRoutes(final MpUnreachNlri nlri) {
263 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
264 final TableContext ctx = this.tables.get(key);
266 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
269 LOG.trace("Removing routes {}", nlri);
270 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
271 ctx.removeRoutes(tx, nlri);
275 static boolean isAnnounceNone(final java.util.Optional<SimpleRoutingPolicy> peerStatus) {
276 return peerStatus.isPresent() && peerStatus.get() == SimpleRoutingPolicy.AnnounceNone;
279 static boolean isLearnNone(final java.util.Optional<SimpleRoutingPolicy> peerStatus) {
280 return peerStatus.isPresent() && peerStatus.get() == SimpleRoutingPolicy.LearnNone;
283 private static String simpleRoutingPolicyString(final SimpleRoutingPolicy simpleRoutingPolicy) {
284 switch (simpleRoutingPolicy) {
286 return "announce-none";
290 throw new IllegalArgumentException("Unhandled Simple Routing Policy " + simpleRoutingPolicy);