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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.Collections;
18 import java.util.Locale;
20 import java.util.Map.Entry;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import javax.annotation.concurrent.NotThreadSafe;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
28 import org.opendaylight.mdsal.common.api.CommitInfo;
29 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer.RegisterAppPeerListener;
30 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
32 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
33 import org.opendaylight.protocol.bgp.rib.spi.PeerRoleUtil;
34 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibIn;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibOut;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.EffectiveRibIn;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.SupportedTables;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
53 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
56 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
57 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
58 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 * Writer of Adjacency-RIB-In for a single peer. An instance of this object
66 * is attached to each {@link BGPPeer} and {@link ApplicationPeer}.
69 final class AdjRibInWriter {
71 private static final Logger LOG = LoggerFactory.getLogger(AdjRibInWriter.class);
74 static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_FALSE = ImmutableNodes.leafNode(QName.create(Attributes.QNAME,
75 "uptodate"), Boolean.FALSE);
77 static final QName PEER_ID_QNAME = QName.create(Peer.QNAME, "peer-id").intern();
78 private static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_TRUE =
79 ImmutableNodes.leafNode(ATTRIBUTES_UPTODATE_FALSE.getNodeType(), Boolean.TRUE);
80 private static final QName PEER_ROLE_QNAME = QName.create(Peer.QNAME, "peer-role").intern();
81 private static final NodeIdentifier ADJRIBIN = new NodeIdentifier(AdjRibIn.QNAME);
82 private static final NodeIdentifier ADJRIBOUT = new NodeIdentifier(AdjRibOut.QNAME);
83 private static final NodeIdentifier EFFRIBIN = new NodeIdentifier(EffectiveRibIn.QNAME);
84 private static final NodeIdentifier PEER_ID = new NodeIdentifier(PEER_ID_QNAME);
85 private static final NodeIdentifier PEER_ROLE = new NodeIdentifier(PEER_ROLE_QNAME);
86 private static final NodeIdentifier PEER_TABLES = new NodeIdentifier(SupportedTables.QNAME);
87 private static final NodeIdentifier TABLES = new NodeIdentifier(Tables.QNAME);
88 private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
90 // FIXME: is there a utility method to construct this?
91 private static final ContainerNode EMPTY_ADJRIBIN = Builders.containerBuilder()
92 .withNodeIdentifier(ADJRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
93 private static final ContainerNode EMPTY_EFFRIBIN = Builders.containerBuilder()
94 .withNodeIdentifier(EFFRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
95 private static final ContainerNode EMPTY_ADJRIBOUT = Builders.containerBuilder()
96 .withNodeIdentifier(ADJRIBOUT).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
98 private final Map<TablesKey, TableContext> tables;
99 private final YangInstanceIdentifier ribPath;
100 private final DOMTransactionChain chain;
101 private final PeerRole role;
103 private AdjRibInWriter(final YangInstanceIdentifier ribPath, final DOMTransactionChain chain, final PeerRole role,
104 final Map<TablesKey, TableContext> tables) {
105 this.ribPath = requireNonNull(ribPath);
106 this.chain = requireNonNull(chain);
107 this.tables = requireNonNull(tables);
108 this.role = requireNonNull(role);
112 * Create a new writer using a transaction chain.
114 * @param role peer's role
115 * @param chain transaction chain @return A fresh writer instance
117 static AdjRibInWriter create(@Nonnull final YangInstanceIdentifier ribId, @Nonnull final PeerRole role,
118 @Nonnull final DOMTransactionChain chain) {
119 return new AdjRibInWriter(ribId, chain, role, Collections.emptyMap());
123 * Transform this writer to a new writer, which is in charge of specified tables.
124 * Empty tables are created for new entries and old tables are deleted. Once this
125 * method returns, the old instance must not be reasonably used.
127 * @param newPeerId new peer BGP identifier
129 * @param registry RIB extension registry
130 * @param tableTypes New tables, must not be null
131 * @param addPathTablesType
134 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
135 final RIBSupportContextRegistry registry,
136 final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType) {
137 return transform(newPeerId, peerPath, registry, tableTypes, addPathTablesType, null);
140 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
141 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
142 final Map<TablesKey, SendReceive> addPathTablesType,
143 @Nullable final RegisterAppPeerListener registerAppPeerListener) {
144 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
146 createEmptyPeerStructure(newPeerId, peerPath, tx);
147 final ImmutableMap<TablesKey, TableContext> tb = createNewTableInstances(peerPath, registry, tableTypes,
148 addPathTablesType, tx);
150 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
152 public void onSuccess(final CommitInfo result) {
153 if (registerAppPeerListener != null) {
154 LOG.trace("Application Peer Listener registered");
155 registerAppPeerListener.register();
160 public void onFailure(final Throwable throwable) {
161 if (registerAppPeerListener != null) {
162 LOG.error("Failed to create Empty Structure, Application Peer Listener won't be registered",
165 LOG.error("Failed to create Empty Structure", throwable);
168 }, MoreExecutors.directExecutor());
169 return new AdjRibInWriter(this.ribPath, this.chain, this.role, tb);
173 * Create new table instances, potentially creating their empty entries
175 private ImmutableMap<TablesKey, TableContext> createNewTableInstances(final YangInstanceIdentifier newPeerPath,
176 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
177 final Map<TablesKey, SendReceive> addPathTablesType, final DOMDataWriteTransaction tx) {
179 final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
180 for (final TablesKey tableKey : tableTypes) {
181 final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
182 // TODO: Use returned value once Instance Identifier builder allows for it.
183 final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
185 LOG.warn("No support for table type {}, skipping it", tableKey);
188 installAdjRibsOutTables(newPeerPath, rs, instanceIdentifierKey, tableKey, addPathTablesType.get(tableKey), tx);
189 installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
194 private static void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey,
195 final RIBSupportContext rs, final NodeIdentifierWithPredicates instanceIdentifierKey,
196 final DOMDataWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
197 // We will use table keys very often, make sure they are optimized
198 final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath
199 .node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES));
200 idb.nodeWithKey(instanceIdentifierKey.getNodeType(), instanceIdentifierKey.getKeyValues());
202 final TableContext ctx = new TableContext(rs, idb.build());
203 ctx.createEmptyTableStructure(tx);
205 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME)
206 .node(ATTRIBUTES_UPTODATE_FALSE.getNodeType()), ATTRIBUTES_UPTODATE_FALSE);
207 LOG.debug("Created table instance {}", ctx.getTableId());
208 tb.put(tableKey, ctx);
211 private void installAdjRibsOutTables(final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
212 final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey,
213 final SendReceive sendReceive, final DOMDataWriteTransaction tx) {
214 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
215 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> tt =
216 Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
217 for (final Entry<QName, Object> e : supTablesKey.getKeyValues().entrySet()) {
218 tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
220 if (sendReceive != null) {
221 tt.withChild(ImmutableNodes.leafNode(SEND_RECEIVE, sendReceive.toString().toLowerCase(Locale.ENGLISH)));
223 tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
224 rs.createEmptyTableStructure(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier())
225 .node(TABLES).node(instanceIdentifierKey));
228 private void createEmptyPeerStructure(final PeerId newPeerId,
229 final YangInstanceIdentifier peerPath, final DOMDataWriteTransaction tx) {
230 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.domPeerId(newPeerId);
232 tx.put(LogicalDatastoreType.OPERATIONAL, peerPath, peerSkeleton(peerKey, newPeerId.getValue()));
233 LOG.debug("New peer {} structure installed.", peerPath);
237 MapEntryNode peerSkeleton(final NodeIdentifierWithPredicates peerKey, final String peerId) {
238 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> pb = Builders.mapEntryBuilder();
239 pb.withNodeIdentifier(peerKey);
240 pb.withChild(ImmutableNodes.leafNode(PEER_ID, peerId));
241 pb.withChild(ImmutableNodes.leafNode(PEER_ROLE, PeerRoleUtil.roleForString(this.role)));
242 pb.withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(PEER_TABLES).build());
243 pb.withChild(EMPTY_ADJRIBIN);
244 pb.withChild(EMPTY_EFFRIBIN);
245 pb.withChild(EMPTY_ADJRIBOUT);
249 void markTableUptodate(final TablesKey tableTypes) {
250 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
251 final TableContext ctx = this.tables.get(tableTypes);
252 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME)
253 .node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
254 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
256 public void onSuccess(final CommitInfo result) {
257 LOG.trace("Write Attributes uptodate, succeed");
261 public void onFailure(final Throwable throwable) {
262 LOG.error("Write Attributes uptodate failed", throwable);
264 }, MoreExecutors.directExecutor());
267 void updateRoutes(final MpReachNlri nlri, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
268 .bgp.message.rev180329.path.attributes.Attributes attributes) {
269 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
270 final TableContext ctx = this.tables.get(key);
272 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
276 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
277 ctx.writeRoutes(tx, nlri, attributes);
278 LOG.trace("Write routes {}", nlri);
279 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
281 public void onSuccess(final CommitInfo result) {
282 LOG.trace("Write routes {}, succeed", nlri);
286 public void onFailure(final Throwable throwable) {
287 LOG.error("Write routes failed", throwable);
289 }, MoreExecutors.directExecutor());
292 void removeRoutes(final MpUnreachNlri nlri) {
293 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
294 final TableContext ctx = this.tables.get(key);
296 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
299 LOG.trace("Removing routes {}", nlri);
300 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
301 ctx.removeRoutes(tx, nlri);
302 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
304 public void onSuccess(final CommitInfo result) {
305 LOG.trace("Removing routes {}, succeed", nlri);
309 public void onFailure(final Throwable throwable) {
310 LOG.error("Removing routes failed", throwable);
312 }, MoreExecutors.directExecutor());