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.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.Collections;
20 import java.util.Locale;
22 import java.util.Map.Entry;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import javax.annotation.concurrent.NotThreadSafe;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
30 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer.RegisterAppPeerListener;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
33 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
34 import org.opendaylight.protocol.bgp.rib.spi.PeerRoleUtil;
35 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.SendReceive;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlri;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlri;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibIn;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibOut;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.EffectiveRibIn;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.SupportedTables;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
54 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
57 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
58 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
59 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
61 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * Writer of Adjacency-RIB-In for a single peer. An instance of this object
67 * is attached to each {@link BGPPeer} and {@link ApplicationPeer}.
70 final class AdjRibInWriter {
72 private static final Logger LOG = LoggerFactory.getLogger(AdjRibInWriter.class);
75 static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_FALSE = ImmutableNodes.leafNode(QName.create(Attributes.QNAME,
76 "uptodate"), Boolean.FALSE);
78 static final QName PEER_ID_QNAME = QName.create(Peer.QNAME, "peer-id").intern();
79 private static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_TRUE =
80 ImmutableNodes.leafNode(ATTRIBUTES_UPTODATE_FALSE.getNodeType(), Boolean.TRUE);
81 private static final QName PEER_ROLE_QNAME = QName.create(Peer.QNAME, "peer-role").intern();
82 private static final NodeIdentifier ADJRIBIN = new NodeIdentifier(AdjRibIn.QNAME);
83 private static final NodeIdentifier ADJRIBOUT = new NodeIdentifier(AdjRibOut.QNAME);
84 private static final NodeIdentifier EFFRIBIN = new NodeIdentifier(EffectiveRibIn.QNAME);
85 private static final NodeIdentifier PEER_ID = new NodeIdentifier(PEER_ID_QNAME);
86 private static final NodeIdentifier PEER_ROLE = new NodeIdentifier(PEER_ROLE_QNAME);
87 private static final NodeIdentifier PEER_TABLES = new NodeIdentifier(SupportedTables.QNAME);
88 private static final NodeIdentifier TABLES = new NodeIdentifier(Tables.QNAME);
89 private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
91 // FIXME: is there a utility method to construct this?
92 private static final ContainerNode EMPTY_ADJRIBIN = Builders.containerBuilder()
93 .withNodeIdentifier(ADJRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
94 private static final ContainerNode EMPTY_EFFRIBIN = Builders.containerBuilder()
95 .withNodeIdentifier(EFFRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
96 private static final ContainerNode EMPTY_ADJRIBOUT = Builders.containerBuilder()
97 .withNodeIdentifier(ADJRIBOUT).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
99 private final Map<TablesKey, TableContext> tables;
100 private final YangInstanceIdentifier ribPath;
101 private final DOMTransactionChain chain;
102 private final PeerRole role;
104 private AdjRibInWriter(final YangInstanceIdentifier ribPath, final DOMTransactionChain chain, final PeerRole role,
105 final Map<TablesKey, TableContext> tables) {
106 this.ribPath = requireNonNull(ribPath);
107 this.chain = requireNonNull(chain);
108 this.tables = requireNonNull(tables);
109 this.role = requireNonNull(role);
113 * Create a new writer using a transaction chain.
115 * @param role peer's role
116 * @param chain transaction chain @return A fresh writer instance
118 static AdjRibInWriter create(@Nonnull final YangInstanceIdentifier ribId, @Nonnull final PeerRole role,
119 @Nonnull final DOMTransactionChain chain) {
120 return new AdjRibInWriter(ribId, chain, role, Collections.emptyMap());
124 * Transform this writer to a new writer, which is in charge of specified tables.
125 * Empty tables are created for new entries and old tables are deleted. Once this
126 * method returns, the old instance must not be reasonably used.
128 * @param newPeerId new peer BGP identifier
130 * @param registry RIB extension registry
131 * @param tableTypes New tables, must not be null
132 * @param addPathTablesType
135 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
136 final RIBSupportContextRegistry registry,
137 final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType) {
138 return transform(newPeerId, peerPath, registry, tableTypes, addPathTablesType, null);
141 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
142 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
143 final Map<TablesKey, SendReceive> addPathTablesType,
144 @Nullable final RegisterAppPeerListener registerAppPeerListener) {
145 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
147 createEmptyPeerStructure(newPeerId, peerPath, tx);
148 final ImmutableMap<TablesKey, TableContext> tb = createNewTableInstances(peerPath, registry, tableTypes,
149 addPathTablesType, tx);
151 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
153 public void onSuccess(final Void result) {
154 if (registerAppPeerListener != null) {
155 LOG.trace("Application Peer Listener registered");
156 registerAppPeerListener.register();
161 public void onFailure(final Throwable throwable) {
162 if (registerAppPeerListener != null) {
163 LOG.error("Failed to create Empty Structure, Application Peer Listener won't be registered",
166 LOG.error("Failed to create Empty Structure", throwable);
169 }, MoreExecutors.directExecutor());
170 return new AdjRibInWriter(this.ribPath, this.chain, this.role, tb);
174 * Create new table instances, potentially creating their empty entries
176 private ImmutableMap<TablesKey, TableContext> createNewTableInstances(final YangInstanceIdentifier newPeerPath,
177 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
178 final Map<TablesKey, SendReceive> addPathTablesType, final DOMDataWriteTransaction tx) {
180 final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
181 for (final TablesKey tableKey : tableTypes) {
182 final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
183 // TODO: Use returned value once Instance Identifier builder allows for it.
184 final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
186 LOG.warn("No support for table type {}, skipping it", tableKey);
189 installAdjRibsOutTables(newPeerPath, rs, instanceIdentifierKey, tableKey, addPathTablesType.get(tableKey), tx);
190 installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
195 private static void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey,
196 final RIBSupportContext rs, final NodeIdentifierWithPredicates instanceIdentifierKey,
197 final DOMDataWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
198 // We will use table keys very often, make sure they are optimized
199 final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath
200 .node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES));
201 idb.nodeWithKey(instanceIdentifierKey.getNodeType(), instanceIdentifierKey.getKeyValues());
203 final TableContext ctx = new TableContext(rs, idb.build());
204 ctx.createEmptyTableStructure(tx);
206 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME)
207 .node(ATTRIBUTES_UPTODATE_FALSE.getNodeType()), ATTRIBUTES_UPTODATE_FALSE);
208 LOG.debug("Created table instance {}", ctx.getTableId());
209 tb.put(tableKey, ctx);
212 private void installAdjRibsOutTables(final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
213 final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey,
214 final SendReceive sendReceive, final DOMDataWriteTransaction tx) {
215 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
216 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> tt =
217 Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
218 for (final Entry<QName, Object> e : supTablesKey.getKeyValues().entrySet()) {
219 tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
221 if (sendReceive != null) {
222 tt.withChild(ImmutableNodes.leafNode(SEND_RECEIVE, sendReceive.toString().toLowerCase(Locale.ENGLISH)));
224 tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
225 rs.createEmptyTableStructure(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier())
226 .node(TABLES).node(instanceIdentifierKey));
229 private void createEmptyPeerStructure(final PeerId newPeerId,
230 final YangInstanceIdentifier peerPath, final DOMDataWriteTransaction tx) {
231 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.domPeerId(newPeerId);
233 tx.put(LogicalDatastoreType.OPERATIONAL, peerPath, peerSkeleton(peerKey, newPeerId.getValue()));
234 LOG.debug("New peer {} structure installed.", peerPath);
238 MapEntryNode peerSkeleton(final NodeIdentifierWithPredicates peerKey, final String peerId) {
239 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> pb = Builders.mapEntryBuilder();
240 pb.withNodeIdentifier(peerKey);
241 pb.withChild(ImmutableNodes.leafNode(PEER_ID, peerId));
242 pb.withChild(ImmutableNodes.leafNode(PEER_ROLE, PeerRoleUtil.roleForString(this.role)));
243 pb.withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(PEER_TABLES).build());
244 pb.withChild(EMPTY_ADJRIBIN);
245 pb.withChild(EMPTY_EFFRIBIN);
246 pb.withChild(EMPTY_ADJRIBOUT);
250 void markTableUptodate(final TablesKey tableTypes) {
251 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
252 final TableContext ctx = this.tables.get(tableTypes);
253 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME)
254 .node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
255 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
257 public void onSuccess(final Void result) {
258 LOG.trace("Write Attributes uptodate, succeed");
262 public void onFailure(final Throwable throwable) {
263 LOG.error("Write Attributes uptodate failed", throwable);
265 }, MoreExecutors.directExecutor());
268 void updateRoutes(final MpReachNlri nlri, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
269 .bgp.message.rev171207.path.attributes.Attributes attributes) {
270 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
271 final TableContext ctx = this.tables.get(key);
273 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
277 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
278 ctx.writeRoutes(tx, nlri, attributes);
279 LOG.trace("Write routes {}", nlri);
280 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
282 public void onSuccess(final Void result) {
283 LOG.trace("Write routes {}, succeed", nlri);
287 public void onFailure(final Throwable throwable) {
288 LOG.error("Write routes failed", throwable);
290 }, MoreExecutors.directExecutor());
293 void removeRoutes(final MpUnreachNlri nlri) {
294 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
295 final TableContext ctx = this.tables.get(key);
297 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
300 LOG.trace("Removing routes {}", nlri);
301 final DOMDataWriteTransaction tx = this.chain.newWriteOnlyTransaction();
302 ctx.removeRoutes(tx, nlri);
303 Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
305 public void onSuccess(final Void result) {
306 LOG.trace("Removing routes {}, succeed", nlri);
310 public void onFailure(final Throwable throwable) {
311 LOG.error("Removing routes failed", throwable);
313 }, MoreExecutors.directExecutor());