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;
11 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ADJRIBIN_NID;
12 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ADJRIBOUT_NID;
13 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.ATTRIBUTES_NID;
14 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.EFFRIBIN_NID;
15 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
16 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.UPTODATE_NID;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.ImmutableMap.Builder;
21 import com.google.common.util.concurrent.FluentFuture;
22 import com.google.common.util.concurrent.FutureCallback;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Locale;
30 import java.util.Map.Entry;
31 import java.util.Optional;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.ExecutionException;
35 import java.util.stream.Collectors;
36 import org.checkerframework.checker.lock.qual.GuardedBy;
37 import org.eclipse.jdt.annotation.NonNull;
38 import org.eclipse.jdt.annotation.Nullable;
39 import org.opendaylight.mdsal.common.api.CommitInfo;
40 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
42 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
43 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer.RegisterAppPeerListener;
44 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerTransactionChain;
45 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
46 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
47 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
48 import org.opendaylight.protocol.bgp.rib.spi.PeerRoleUtil;
49 import org.opendaylight.protocol.bgp.rib.spi.RIBNormalizedNodes;
50 import org.opendaylight.protocol.bgp.rib.spi.RIBQNames;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.SupportedTables;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
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.MapEntryNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
68 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
69 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
74 * Writer of Adjacency-RIB-In for a single peer. An instance of this object is attached to each {@link BGPPeer} and
75 * {@link ApplicationPeer}. This class is NOT thread-safe.
77 final class AdjRibInWriter {
79 private static final Logger LOG = LoggerFactory.getLogger(AdjRibInWriter.class);
81 private static final QName PEER_ROLE_QNAME = QName.create(Peer.QNAME, "peer-role").intern();
82 private static final NodeIdentifier PEER_ID = NodeIdentifier.create(RIBQNames.PEER_ID_QNAME);
83 private static final NodeIdentifier PEER_ROLE = NodeIdentifier.create(PEER_ROLE_QNAME);
84 private static final NodeIdentifier PEER_TABLES = NodeIdentifier.create(SupportedTables.QNAME);
85 private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
87 // FIXME: is there a utility method to construct this?
88 private static final MapNode EMPTY_TABLES = ImmutableNodes.mapNodeBuilder(TABLES_NID).build();
89 private static final ContainerNode EMPTY_ADJRIBIN = Builders.containerBuilder()
90 .withNodeIdentifier(ADJRIBIN_NID).addChild(EMPTY_TABLES).build();
91 private static final ContainerNode EMPTY_EFFRIBIN = Builders.containerBuilder()
92 .withNodeIdentifier(EFFRIBIN_NID).addChild(EMPTY_TABLES).build();
93 private static final ContainerNode EMPTY_ADJRIBOUT = Builders.containerBuilder()
94 .withNodeIdentifier(ADJRIBOUT_NID).addChild(EMPTY_TABLES).build();
96 private final Map<TablesKey, TableContext> tables;
97 private final YangInstanceIdentifier ribPath;
98 private final PeerTransactionChain chain;
99 private final PeerRole role;
101 private final Map<TablesKey, Collection<NodeIdentifierWithPredicates>> staleRoutesRegistry = new HashMap<>();
103 private FluentFuture<? extends CommitInfo> submitted;
105 private AdjRibInWriter(final YangInstanceIdentifier ribPath, final PeerTransactionChain chain, final PeerRole role,
106 final Map<TablesKey, TableContext> tables) {
107 this.ribPath = requireNonNull(ribPath);
108 this.chain = requireNonNull(chain);
109 this.tables = requireNonNull(tables);
110 this.role = requireNonNull(role);
114 * Create a new writer using a transaction chain.
116 * @param role peer's role
117 * @param chain transaction chain @return A fresh writer instance
119 static AdjRibInWriter create(final @NonNull YangInstanceIdentifier ribId, final @NonNull PeerRole role,
120 final @NonNull PeerTransactionChain chain) {
121 return new AdjRibInWriter(ribId, chain, role, Collections.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 peerPath path of the peer in the datastore
131 * @param registry RIB extension registry
132 * @param tableTypes New tables, must not be null
133 * @param addPathTablesType supported add path tables
136 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
137 final RIBSupportContextRegistry registry,
138 final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType) {
139 return transform(newPeerId, peerPath, registry, tableTypes, addPathTablesType, null);
142 AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
143 final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
144 final Map<TablesKey, SendReceive> addPathTablesType,
145 final @Nullable RegisterAppPeerListener registerAppPeerListener) {
146 final DOMDataTreeWriteTransaction tx = chain.getDomChain().newWriteOnlyTransaction();
148 createEmptyPeerStructure(newPeerId, peerPath, tx);
149 final ImmutableMap<TablesKey, TableContext> tb = createNewTableInstances(peerPath, registry, tableTypes,
150 addPathTablesType, tx);
152 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
154 public void onSuccess(final CommitInfo result) {
155 if (registerAppPeerListener != null) {
156 LOG.trace("Application Peer Listener registered");
157 registerAppPeerListener.register();
162 public void onFailure(final Throwable throwable) {
163 if (registerAppPeerListener != null) {
164 LOG.error("Failed to create Empty Structure, Application Peer Listener won't be registered",
167 LOG.error("Failed to create Empty Structure", throwable);
170 }, MoreExecutors.directExecutor());
171 return new AdjRibInWriter(ribPath, chain, role, tb);
175 * Create new table instances, potentially creating their empty entries.
177 private static ImmutableMap<TablesKey, TableContext> createNewTableInstances(
178 final YangInstanceIdentifier newPeerPath, final RIBSupportContextRegistry registry,
179 final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType,
180 final DOMDataTreeWriteTransaction tx) {
182 final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
183 for (final TablesKey tableKey : tableTypes) {
184 final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
185 // TODO: Use returned value once Instance Identifier builder allows for it.
186 final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
188 LOG.warn("No support for table type {}, skipping it", tableKey);
191 installAdjRibsOutTables(newPeerPath, rs, instanceIdentifierKey, tableKey,
192 addPathTablesType.get(tableKey), tx);
193 installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
198 private static void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey,
199 final RIBSupportContext rs, final NodeIdentifierWithPredicates instanceIdentifierKey,
200 final DOMDataTreeWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
201 // We will use table keys very often, make sure they are optimized
202 final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath
203 .node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES_NID)).node(instanceIdentifierKey);
205 final TableContext ctx = new TableContext(rs, idb.build());
206 ctx.createEmptyTableStructure(tx);
208 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(ATTRIBUTES_NID).node(UPTODATE_NID),
209 RIBNormalizedNodes.ATTRIBUTES_UPTODATE_FALSE);
210 LOG.debug("Created table instance {}", ctx.getTableId());
211 tb.put(tableKey, ctx);
214 private static void installAdjRibsOutTables(final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
215 final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey,
216 final SendReceive sendReceive, final DOMDataTreeWriteTransaction tx) {
217 final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
218 final var tt = Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
219 for (final Entry<QName, Object> e : supTablesKey.entrySet()) {
220 tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
222 if (sendReceive != null) {
223 tt.withChild(ImmutableNodes.leafNode(SEND_RECEIVE, sendReceive.toString().toLowerCase(Locale.ENGLISH)));
225 tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
226 rs.createEmptyTableStructure(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier())
227 .node(TABLES_NID).node(instanceIdentifierKey));
230 private void createEmptyPeerStructure(final PeerId newPeerId,
231 final YangInstanceIdentifier peerPath, final DOMDataTreeWriteTransaction tx) {
232 final NodeIdentifierWithPredicates peerKey = IdentifierUtils.domPeerId(newPeerId);
234 tx.put(LogicalDatastoreType.OPERATIONAL, peerPath, peerSkeleton(peerKey, newPeerId.getValue()));
235 LOG.debug("New peer {} structure installed.", peerPath);
239 MapEntryNode peerSkeleton(final NodeIdentifierWithPredicates peerKey, final String peerId) {
240 return Builders.mapEntryBuilder()
241 .withNodeIdentifier(peerKey)
242 .withChild(ImmutableNodes.leafNode(PEER_ID, peerId))
243 .withChild(ImmutableNodes.leafNode(PEER_ROLE, PeerRoleUtil.roleForString(role)))
244 .withChild(Builders.mapBuilder().withNodeIdentifier(PEER_TABLES).build())
245 .withChild(EMPTY_ADJRIBIN)
246 .withChild(EMPTY_EFFRIBIN)
247 .withChild(EMPTY_ADJRIBOUT)
251 void markTableUptodate(final TablesKey tableTypes) {
252 final DOMDataTreeWriteTransaction tx = chain.getDomChain().newWriteOnlyTransaction();
253 final TableContext ctx = tables.get(tableTypes);
254 tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(ATTRIBUTES_NID).node(UPTODATE_NID),
255 RIBNormalizedNodes.ATTRIBUTES_UPTODATE_TRUE);
256 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
258 public void onSuccess(final CommitInfo result) {
259 LOG.trace("Write Attributes uptodate, succeed");
263 public void onFailure(final Throwable throwable) {
264 LOG.error("Write Attributes uptodate failed", throwable);
266 }, MoreExecutors.directExecutor());
269 void updateRoutes(final MpReachNlri nlri, final org.opendaylight.yang.gen.v1.urn.opendaylight.params
270 .xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes attributes) {
271 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
272 final TableContext ctx = tables.get(key);
274 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
278 final DOMDataTreeWriteTransaction tx = chain.getDomChain().newWriteOnlyTransaction();
279 final Collection<NodeIdentifierWithPredicates> routeKeys = ctx.writeRoutes(tx, nlri, attributes);
280 final Collection<NodeIdentifierWithPredicates> staleRoutes = staleRoutesRegistry.get(key);
281 if (staleRoutes != null) {
282 staleRoutes.removeAll(routeKeys);
284 LOG.trace("Write routes {}", nlri);
285 final FluentFuture<? extends CommitInfo> future = tx.commit();
287 future.addCallback(new FutureCallback<CommitInfo>() {
289 public void onSuccess(final CommitInfo result) {
290 LOG.trace("Write routes {}, succeed", nlri);
294 public void onFailure(final Throwable throwable) {
295 LOG.error("Write routes failed", throwable);
297 }, MoreExecutors.directExecutor());
300 void removeRoutes(final MpUnreachNlri nlri) {
301 final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
302 final TableContext ctx = tables.get(key);
304 LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
307 LOG.trace("Removing routes {}", nlri);
308 final DOMDataTreeWriteTransaction tx = chain.getDomChain().newWriteOnlyTransaction();
309 ctx.removeRoutes(tx, nlri);
310 final FluentFuture<? extends CommitInfo> future = tx.commit();
312 future.addCallback(new FutureCallback<CommitInfo>() {
314 public void onSuccess(final CommitInfo result) {
315 LOG.trace("Removing routes {}, succeed", nlri);
319 public void onFailure(final Throwable throwable) {
320 LOG.error("Removing routes failed", throwable);
322 }, MoreExecutors.directExecutor());
325 void releaseChain() {
326 if (submitted != null) {
329 } catch (final InterruptedException | ExecutionException throwable) {
330 LOG.error("Write routes failed", throwable);
335 void storeStaleRoutes(final Set<TablesKey> gracefulTables) {
336 final CountDownLatch latch = new CountDownLatch(gracefulTables.size());
338 try (DOMDataTreeReadTransaction tx = chain.getDomChain().newReadOnlyTransaction()) {
339 for (TablesKey tablesKey : gracefulTables) {
340 final TableContext ctx = tables.get(tablesKey);
342 LOG.warn("Missing table for address family {}", tablesKey);
347 tx.read(LogicalDatastoreType.OPERATIONAL, ctx.routesPath()).addCallback(
348 new FutureCallback<Optional<NormalizedNode>>() {
350 public void onSuccess(final Optional<NormalizedNode> routesOptional) {
352 if (routesOptional.isPresent()) {
353 synchronized (staleRoutesRegistry) {
354 final MapNode routesNode = (MapNode) routesOptional.orElseThrow();
355 final List<NodeIdentifierWithPredicates> routes = routesNode.body().stream()
356 .map(MapEntryNode::getIdentifier)
357 .collect(Collectors.toList());
358 if (!routes.isEmpty()) {
359 staleRoutesRegistry.put(tablesKey, routes);
369 public void onFailure(final Throwable throwable) {
370 LOG.warn("Failed to store stale routes for table {}", tablesKey, throwable);
373 }, MoreExecutors.directExecutor());
379 } catch (InterruptedException e) {
380 LOG.warn("Interrupted while waiting to store stale routes with {} tasks of {} to finish", latch.getCount(),
385 void removeStaleRoutes(final TablesKey tableKey) {
386 final TableContext ctx = tables.get(tableKey);
388 LOG.debug("No table for {}, not removing any stale routes", tableKey);
391 final Collection<NodeIdentifierWithPredicates> routeKeys = staleRoutesRegistry.get(tableKey);
392 if (routeKeys == null || routeKeys.isEmpty()) {
393 LOG.debug("No stale routes present in table {}", tableKey);
396 LOG.trace("Removing routes {}", routeKeys);
397 final DOMDataTreeWriteTransaction tx = chain.getDomChain().newWriteOnlyTransaction();
398 routeKeys.forEach(routeKey -> {
399 tx.delete(LogicalDatastoreType.OPERATIONAL, ctx.routePath(routeKey));
401 final FluentFuture<? extends CommitInfo> future = tx.commit();
403 future.addCallback(new FutureCallback<CommitInfo>() {
405 public void onSuccess(final CommitInfo result) {
406 LOG.trace("Removing routes {}, succeed", routeKeys);
407 synchronized (staleRoutesRegistry) {
408 staleRoutesRegistry.remove(tableKey);
413 public void onFailure(final Throwable throwable) {
414 LOG.warn("Removing routes {}, failed", routeKeys, throwable);
416 }, MoreExecutors.directExecutor());
419 FluentFuture<? extends CommitInfo> clearTables(final Set<TablesKey> tablesToClear) {
420 if (tablesToClear == null || tablesToClear.isEmpty()) {
421 return CommitInfo.emptyFluentFuture();
424 final DOMDataTreeWriteTransaction wtx = chain.getDomChain().newWriteOnlyTransaction();
425 tablesToClear.forEach(tableKey -> {
426 final TableContext ctx = tables.get(tableKey);
427 wtx.delete(LogicalDatastoreType.OPERATIONAL, ctx.routesPath().getParent());