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;
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.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
26 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibIn;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.AdjRibOut;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.EffectiveRibIn;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.peer.SupportedTables;
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.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
48 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * Writer of Adjacency-RIB-In for a single peer. An instance of this object
57 * is attached to each {@link BGPPeer} and {@link ApplicationPeer}.
60 final class AdjRibInWriter {
61 private static final Logger LOG = LoggerFactory.getLogger(AdjRibInWriter.class);
63 static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_FALSE = ImmutableNodes.leafNode(QName.create(Attributes.QNAME, "uptodate"), Boolean.FALSE);
64 private static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_TRUE = ImmutableNodes.leafNode(ATTRIBUTES_UPTODATE_FALSE.getNodeType(), Boolean.TRUE);
66 static final QName PEER_ID_QNAME = QName.create(Peer.QNAME, "peer-id").intern();
67 private static final QName PEER_ROLE_QNAME = QName.create(Peer.QNAME, "peer-role").intern();
68 private static final NodeIdentifier ADJRIBIN = new NodeIdentifier(AdjRibIn.QNAME);
69 private static final NodeIdentifier ADJRIBOUT = new NodeIdentifier(AdjRibOut.QNAME);
70 private static final NodeIdentifier EFFRIBIN = new NodeIdentifier(EffectiveRibIn.QNAME);
71 private static final NodeIdentifier PEER_ID = new NodeIdentifier(PEER_ID_QNAME);
72 private static final NodeIdentifier PEER_ROLE = new NodeIdentifier(PEER_ROLE_QNAME);
73 private static final NodeIdentifier PEER_TABLES = new NodeIdentifier(SupportedTables.QNAME);
74 private static final NodeIdentifier TABLES = new NodeIdentifier(Tables.QNAME);
76 // FIXME: is there a utility method to construct this?
77 private static final ContainerNode EMPTY_ADJRIBIN = Builders.containerBuilder().withNodeIdentifier(ADJRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
78 private static final ContainerNode EMPTY_EFFRIBIN = Builders.containerBuilder().withNodeIdentifier(EFFRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
79 private static final ContainerNode EMPTY_ADJRIBOUT = Builders.containerBuilder().withNodeIdentifier(ADJRIBOUT).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
81 private final Map<TablesKey, TableContext> tables;
82 private final YangInstanceIdentifier peerPath;
83 private final YangInstanceIdentifier ribPath;
84 private final DOMTransactionChain chain;
85 private final PeerId peerId;
86 private final String role;
88 private AdjRibInWriter(final YangInstanceIdentifier ribPath, final DOMTransactionChain chain, final PeerId peerId, final String role, final YangInstanceIdentifier peerPath, final Map<TablesKey, TableContext> tables) {
89 this.ribPath = Preconditions.checkNotNull(ribPath);
90 this.chain = Preconditions.checkNotNull(chain);
91 this.tables = Preconditions.checkNotNull(tables);
92 this.role = Preconditions.checkNotNull(role);
93 this.peerPath = peerPath;
97 // We could use a codec, but this should be fine, too
98 private static String roleString(final PeerRole role) {
109 throw new IllegalArgumentException("Unhandled role " + role);
114 * Create a new writer using a transaction chain.
116 * @param role peer's role
117 * @param chain transaction chain
118 * @return A fresh writer instance
120 static AdjRibInWriter create(@Nonnull final YangInstanceIdentifier ribId, @Nonnull final PeerRole role, @Nonnull final DOMTransactionChain chain) {
121 return new AdjRibInWriter(ribId, chain, null, roleString(role), null, Collections.<TablesKey, TableContext>emptyMap());
125 * Transform this writer to a new writer, which is in charge of specified tables.
126 * Empty tables are created for new entries and old tables are deleted. Once this
127 * method returns, the old instance must not be reasonably used.
129 * @param newPeerId new peer BGP identifier
130 * @param registry RIB extension registry
131 * @param tableTypes New tables, must not be null
134 AdjRibInWriter transform(final PeerId newPeerId, final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes, final boolean isAppPeer) {
135 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
137 final YangInstanceIdentifier newPeerPath;
138 newPeerPath = createEmptyPeerStructure(newPeerId, isAppPeer, tx);
139 final ImmutableMap<TablesKey, TableContext> tb = createNewTableInstances(newPeerPath, isAppPeer, registry, tableTypes, tx);
142 return new AdjRibInWriter(this.ribPath, this.chain, newPeerId, this.role, newPeerPath, tb);
146 * Create new table instances, potentially creating their empty entries
154 private ImmutableMap<TablesKey, TableContext> createNewTableInstances(final YangInstanceIdentifier newPeerPath, final boolean isAppPeer,
155 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes, final DOMDataWriteTransaction tx) {
157 final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
158 for (final TablesKey tableKey : tableTypes) {
159 final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
160 // TODO: Use returned value once Instance Identifier builder allows for it.
161 final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
163 LOG.warn("No support for table type {}, skipping it", tableKey);
166 installAdjRibsOutTables(isAppPeer, newPeerPath, rs, instanceIdentifierKey, tableKey, tx);
167 installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
172 private void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey, final RIBSupportContext rs,
173 final NodeIdentifierWithPredicates instanceIdentifierKey, final DOMDataWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
174 // We will use table keys very often, make sure they are optimized
175 final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath.node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES));
176 idb.nodeWithKey(instanceIdentifierKey.getNodeType(), instanceIdentifierKey.getKeyValues());
178 TableContext ctx = new TableContext(rs, idb.build());
181 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME).node(ATTRIBUTES_UPTODATE_FALSE.getNodeType()), ATTRIBUTES_UPTODATE_FALSE);
182 LOG.debug("Created table instance {}", ctx.getTableId());
183 tb.put(tableKey, ctx);
186 private void installAdjRibsOutTables(final boolean isAppPeer, final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
187 final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey, final DOMDataWriteTransaction tx) {
189 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
190 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> tt = Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
191 for (final Entry<QName, Object> e : supTablesKey.getKeyValues().entrySet()) {
192 tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
194 tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
195 rs.clearTable(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier()).node(TABLES).node(instanceIdentifierKey));
199 private YangInstanceIdentifier createEmptyPeerStructure(final PeerId newPeerId, final boolean isAppPeer, 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(), isAppPeer));
204 LOG.debug("New peer {} structure installed.", newPeerPath);
209 MapEntryNode peerSkeleton(final NodeIdentifierWithPredicates peerKey, final String peerId, final boolean isAppPeer) {
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, this.role));
214 pb.withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(PEER_TABLES).build());
215 pb.withChild(EMPTY_ADJRIBIN);
216 pb.withChild(EMPTY_EFFRIBIN);
218 pb.withChild(EMPTY_ADJRIBOUT);
224 if(this.peerPath != null) {
225 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
226 tx.delete(LogicalDatastoreType.OPERATIONAL, this.peerPath);
229 tx.submit().checkedGet();
230 } catch (final TransactionCommitFailedException e) {
231 LOG.debug("Failed to remove Peer {}", this.peerPath, e);
236 void markTableUptodate(final TablesKey tableTypes) {
237 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
239 final TableContext ctx = this.tables.get(tableTypes);
240 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME).node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
245 void updateRoutes(final MpReachNlri nlri, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes attributes) {
246 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
247 final TableContext ctx = this.tables.get(key);
249 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
253 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
254 ctx.writeRoutes(tx, nlri, attributes);
255 LOG.trace("Write routes {}", nlri);
259 void removeRoutes(final MpUnreachNlri nlri) {
260 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
261 final TableContext ctx = this.tables.get(key);
263 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
266 LOG.trace("Removing routes {}", nlri);
267 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
268 ctx.removeRoutes(tx, nlri);