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.base.Optional;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableMap.Builder;
16 import com.google.common.util.concurrent.FluentFuture;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Locale;
26 import java.util.Map.Entry;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.ExecutionException;
30 import java.util.stream.Collectors;
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
33 import javax.annotation.concurrent.GuardedBy;
34 import javax.annotation.concurrent.NotThreadSafe;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
37 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
38 import org.opendaylight.mdsal.common.api.CommitInfo;
39 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer.RegisterAppPeerListener;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerTransactionChain;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
43 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
44 import org.opendaylight.protocol.bgp.rib.spi.PeerRoleUtil;
45 import org.opendaylight.protocol.bgp.rib.spi.RibSupportUtils;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibIn;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.AdjRibOut;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.EffectiveRibIn;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.SupportedTables;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Attributes;
59 import org.opendaylight.yangtools.yang.common.QName;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
64 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
69 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
70 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
71 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
72 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
73 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
78 * Writer of Adjacency-RIB-In for a single peer. An instance of this object
79 * is attached to each {@link BGPPeer} and {@link ApplicationPeer}.
82 final class AdjRibInWriter {
84 private static final Logger LOG = LoggerFactory.getLogger(AdjRibInWriter.class);
87 static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_FALSE = ImmutableNodes.leafNode(QName.create(Attributes.QNAME,
88 "uptodate"), Boolean.FALSE);
90 static final QName PEER_ID_QNAME = QName.create(Peer.QNAME, "peer-id").intern();
91 private static final LeafNode<Boolean> ATTRIBUTES_UPTODATE_TRUE =
92 ImmutableNodes.leafNode(ATTRIBUTES_UPTODATE_FALSE.getNodeType(), Boolean.TRUE);
93 private static final QName PEER_ROLE_QNAME = QName.create(Peer.QNAME, "peer-role").intern();
94 private static final NodeIdentifier ADJRIBIN = new NodeIdentifier(AdjRibIn.QNAME);
95 private static final NodeIdentifier ADJRIBOUT = new NodeIdentifier(AdjRibOut.QNAME);
96 private static final NodeIdentifier EFFRIBIN = new NodeIdentifier(EffectiveRibIn.QNAME);
97 private static final NodeIdentifier PEER_ID = new NodeIdentifier(PEER_ID_QNAME);
98 private static final NodeIdentifier PEER_ROLE = new NodeIdentifier(PEER_ROLE_QNAME);
99 private static final NodeIdentifier PEER_TABLES = new NodeIdentifier(SupportedTables.QNAME);
100 private static final NodeIdentifier TABLES = new NodeIdentifier(Tables.QNAME);
101 private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
103 // FIXME: is there a utility method to construct this?
104 private static final ContainerNode EMPTY_ADJRIBIN = Builders.containerBuilder()
105 .withNodeIdentifier(ADJRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
106 private static final ContainerNode EMPTY_EFFRIBIN = Builders.containerBuilder()
107 .withNodeIdentifier(EFFRIBIN).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
108 private static final ContainerNode EMPTY_ADJRIBOUT = Builders.containerBuilder()
109 .withNodeIdentifier(ADJRIBOUT).addChild(ImmutableNodes.mapNodeBuilder(Tables.QNAME).build()).build();
111 private final Map<TablesKey, TableContext> tables;
112 private final YangInstanceIdentifier ribPath;
113 private final PeerTransactionChain chain;
114 private final PeerRole role;
116 private final Map<TablesKey, Collection<NodeIdentifierWithPredicates>> staleRoutesRegistry = new HashMap<>();
118 private FluentFuture<? extends CommitInfo> submitted;
120 private AdjRibInWriter(final YangInstanceIdentifier ribPath, final PeerTransactionChain chain, final PeerRole role,
121 final Map<TablesKey, TableContext> tables) {
122 this.ribPath = requireNonNull(ribPath);
123 this.chain = requireNonNull(chain);
124 this.tables = requireNonNull(tables);
125 this.role = requireNonNull(role);
129 * Create a new writer using a transaction chain.
131 * @param role peer's role
132 * @param chain transaction chain @return A fresh writer instance
134 static AdjRibInWriter create(@Nonnull final YangInstanceIdentifier ribId, @Nonnull final PeerRole role,
135 @Nonnull final PeerTransactionChain chain) {
136 return new AdjRibInWriter(ribId, chain, role, Collections.emptyMap());
140 * Transform this writer to a new writer, which is in charge of specified tables.
141 * Empty tables are created for new entries and old tables are deleted. Once this
142 * method returns, the old instance must not be reasonably used.
144 * @param newPeerId new peer BGP identifier
145 * @param peerPath path of the peer in the datastore
146 * @param registry RIB extension registry
147 * @param tableTypes New tables, must not be null
148 * @param addPathTablesType supported add path tables
151 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
152 final RIBSupportContextRegistry registry,
153 final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType) {
154 return transform(newPeerId, peerPath, registry, tableTypes, addPathTablesType, null);
157 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
158 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
159 final Map<TablesKey, SendReceive> addPathTablesType,
160 @Nullable final RegisterAppPeerListener registerAppPeerListener) {
161 final DOMDataWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
163 createEmptyPeerStructure(newPeerId, peerPath, tx);
164 final ImmutableMap<TablesKey, TableContext> tb = createNewTableInstances(peerPath, registry, tableTypes,
165 addPathTablesType, tx);
167 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
169 public void onSuccess(final CommitInfo result) {
170 if (registerAppPeerListener != null) {
171 LOG.trace("Application Peer Listener registered");
172 registerAppPeerListener.register();
177 public void onFailure(final Throwable throwable) {
178 if (registerAppPeerListener != null) {
179 LOG.error("Failed to create Empty Structure, Application Peer Listener won't be registered",
182 LOG.error("Failed to create Empty Structure", throwable);
185 }, MoreExecutors.directExecutor());
186 return new AdjRibInWriter(this.ribPath, this.chain, this.role, tb);
190 * Create new table instances, potentially creating their empty entries.
192 private static ImmutableMap<TablesKey, TableContext> createNewTableInstances(
193 final YangInstanceIdentifier newPeerPath, final RIBSupportContextRegistry registry,
194 final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType,
195 final DOMDataWriteTransaction tx) {
197 final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
198 for (final TablesKey tableKey : tableTypes) {
199 final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
200 // TODO: Use returned value once Instance Identifier builder allows for it.
201 final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
203 LOG.warn("No support for table type {}, skipping it", tableKey);
206 installAdjRibsOutTables(newPeerPath, rs, instanceIdentifierKey, tableKey,
207 addPathTablesType.get(tableKey), tx);
208 installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
213 private static void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey,
214 final RIBSupportContext rs, final NodeIdentifierWithPredicates instanceIdentifierKey,
215 final DOMDataWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
216 // We will use table keys very often, make sure they are optimized
217 final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath
218 .node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES));
219 idb.nodeWithKey(instanceIdentifierKey.getNodeType(), instanceIdentifierKey.getKeyValues());
221 final TableContext ctx = new TableContext(rs, idb.build());
222 ctx.createEmptyTableStructure(tx);
224 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME)
225 .node(ATTRIBUTES_UPTODATE_FALSE.getNodeType()), ATTRIBUTES_UPTODATE_FALSE);
226 LOG.debug("Created table instance {}", ctx.getTableId());
227 tb.put(tableKey, ctx);
230 private static void installAdjRibsOutTables(final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
231 final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey,
232 final SendReceive sendReceive, final DOMDataWriteTransaction tx) {
233 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
234 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> tt =
235 Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
236 for (final Entry<QName, Object> e : supTablesKey.getKeyValues().entrySet()) {
237 tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
239 if (sendReceive != null) {
240 tt.withChild(ImmutableNodes.leafNode(SEND_RECEIVE, sendReceive.toString().toLowerCase(Locale.ENGLISH)));
242 tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
243 rs.createEmptyTableStructure(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier())
244 .node(TABLES).node(instanceIdentifierKey));
247 private void createEmptyPeerStructure(final PeerId newPeerId,
248 final YangInstanceIdentifier peerPath, final DOMDataWriteTransaction tx) {
249 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.domPeerId(newPeerId);
251 tx.put(LogicalDatastoreType.OPERATIONAL, peerPath, peerSkeleton(peerKey, newPeerId.getValue()));
252 LOG.debug("New peer {} structure installed.", peerPath);
256 MapEntryNode peerSkeleton(final NodeIdentifierWithPredicates peerKey, final String peerId) {
257 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> pb = Builders.mapEntryBuilder();
258 pb.withNodeIdentifier(peerKey);
259 pb.withChild(ImmutableNodes.leafNode(PEER_ID, peerId));
260 pb.withChild(ImmutableNodes.leafNode(PEER_ROLE, PeerRoleUtil.roleForString(this.role)));
261 pb.withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(PEER_TABLES).build());
262 pb.withChild(EMPTY_ADJRIBIN);
263 pb.withChild(EMPTY_EFFRIBIN);
264 pb.withChild(EMPTY_ADJRIBOUT);
268 void markTableUptodate(final TablesKey tableTypes) {
269 final DOMDataWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
270 final TableContext ctx = this.tables.get(tableTypes);
271 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(Attributes.QNAME)
272 .node(ATTRIBUTES_UPTODATE_TRUE.getNodeType()), ATTRIBUTES_UPTODATE_TRUE);
273 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
275 public void onSuccess(final CommitInfo result) {
276 LOG.trace("Write Attributes uptodate, succeed");
280 public void onFailure(final Throwable throwable) {
281 LOG.error("Write Attributes uptodate failed", throwable);
283 }, MoreExecutors.directExecutor());
286 void updateRoutes(final MpReachNlri nlri, final org.opendaylight.yang.gen.v1.urn.opendaylight.params
287 .xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes attributes) {
288 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
289 final TableContext ctx = this.tables.get(key);
291 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
295 final DOMDataWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
296 final Collection<NodeIdentifierWithPredicates> routeKeys = ctx.writeRoutes(tx, nlri, attributes);
297 final Collection<NodeIdentifierWithPredicates> staleRoutes = this.staleRoutesRegistry.get(key);
298 if (staleRoutes != null) {
299 staleRoutes.removeAll(routeKeys);
301 LOG.trace("Write routes {}", nlri);
302 final FluentFuture<? extends CommitInfo> future = tx.commit();
303 this.submitted = future;
304 future.addCallback(new FutureCallback<CommitInfo>() {
306 public void onSuccess(final CommitInfo result) {
307 LOG.trace("Write routes {}, succeed", nlri);
311 public void onFailure(final Throwable throwable) {
312 LOG.error("Write routes failed", throwable);
314 }, MoreExecutors.directExecutor());
317 void removeRoutes(final MpUnreachNlri nlri) {
318 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
319 final TableContext ctx = this.tables.get(key);
321 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
324 LOG.trace("Removing routes {}", nlri);
325 final DOMDataWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
326 ctx.removeRoutes(tx, nlri);
327 final FluentFuture<? extends CommitInfo> future = tx.commit();
328 this.submitted = future;
329 future.addCallback(new FutureCallback<CommitInfo>() {
331 public void onSuccess(final CommitInfo result) {
332 LOG.trace("Removing routes {}, succeed", nlri);
336 public void onFailure(final Throwable throwable) {
337 LOG.error("Removing routes failed", throwable);
339 }, MoreExecutors.directExecutor());
342 void releaseChain() {
343 if (this.submitted != null) {
345 this.submitted.get();
346 } catch (final InterruptedException | ExecutionException throwable) {
347 LOG.error("Write routes failed", throwable);
352 void storeStaleRoutes(final Set<TablesKey> gracefulTables) {
353 final CountDownLatch latch = new CountDownLatch(gracefulTables.size());
355 try (DOMDataReadOnlyTransaction tx = this.chain.getDomChain().newReadOnlyTransaction()) {
356 for (TablesKey tablesKey : gracefulTables) {
357 final TableContext ctx = this.tables.get(tablesKey);
359 LOG.warn("Missing table for address family {}", tablesKey);
364 Futures.addCallback(tx.read(LogicalDatastoreType.OPERATIONAL, ctx.routesPath()),
365 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
367 public void onSuccess(final Optional<NormalizedNode<?, ?>> routesOptional) {
369 if (routesOptional.isPresent()) {
370 synchronized (AdjRibInWriter.this.staleRoutesRegistry) {
371 final MapNode routesNode = (MapNode) routesOptional.get();
372 final List<NodeIdentifierWithPredicates> routes = routesNode.getValue().stream()
373 .map(MapEntryNode::getIdentifier)
374 .collect(Collectors.toList());
375 if (!routes.isEmpty()) {
376 AdjRibInWriter.this.staleRoutesRegistry.put(tablesKey, routes);
386 public void onFailure(final Throwable throwable) {
387 LOG.warn("Failed to store stale routes for table {}", tablesKey, throwable);
390 }, MoreExecutors.directExecutor());
396 } catch (InterruptedException e) {
397 LOG.warn("Interrupted while waiting to store stale routes with {} tasks of {} to finish", latch.getCount(),
402 void removeStaleRoutes(final TablesKey tableKey) {
403 final TableContext ctx = this.tables.get(tableKey);
405 LOG.debug("No table for {}, not removing any stale routes", tableKey);
408 final Collection<NodeIdentifierWithPredicates> routeKeys = this.staleRoutesRegistry.get(tableKey);
409 if (routeKeys == null || routeKeys.isEmpty()) {
410 LOG.debug("No stale routes present in table {}", tableKey);
413 LOG.trace("Removing routes {}", routeKeys);
414 final DOMDataWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
415 routeKeys.forEach(routeKey -> {
416 tx.delete(LogicalDatastoreType.OPERATIONAL, ctx.routePath(routeKey));
418 final FluentFuture<? extends CommitInfo> future = tx.commit();
419 this.submitted = future;
420 future.addCallback(new FutureCallback<CommitInfo>() {
422 public void onSuccess(final CommitInfo result) {
423 LOG.trace("Removing routes {}, succeed", routeKeys);
424 synchronized (AdjRibInWriter.this.staleRoutesRegistry) {
425 staleRoutesRegistry.remove(tableKey);
430 public void onFailure(final Throwable throwable) {
431 LOG.warn("Removing routes {}, failed", routeKeys, throwable);
433 }, MoreExecutors.directExecutor());
436 FluentFuture<? extends CommitInfo> clearTables(final Set<TablesKey> tablesToClear) {
437 if (tablesToClear == null || tablesToClear.isEmpty()) {
438 return CommitInfo.emptyFluentFuture();
441 final DOMDataWriteTransaction wtx = this.chain.getDomChain().newWriteOnlyTransaction();
442 tablesToClear.forEach(tableKey -> {
443 final TableContext ctx = this.tables.get(tableKey);
444 wtx.delete(LogicalDatastoreType.OPERATIONAL, ctx.routesPath().getParent());