Migrate to MD-SAL APIs
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / AdjRibInWriter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
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;
17
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;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Optional;
32 import java.util.Set;
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.protocol.bgp.rib.spi.RibSupportUtils;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.SendReceive;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.peer.SupportedTables;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
65 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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.DataContainerNodeBuilder;
72 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75
76 /**
77  * Writer of Adjacency-RIB-In for a single peer. An instance of this object is attached to each {@link BGPPeer} and
78  * {@link ApplicationPeer}. This class is NOT thread-safe.
79  */
80 final class AdjRibInWriter {
81
82     private static final Logger LOG = LoggerFactory.getLogger(AdjRibInWriter.class);
83
84     private static final QName PEER_ROLE_QNAME = QName.create(Peer.QNAME, "peer-role").intern();
85     private static final NodeIdentifier PEER_ID = NodeIdentifier.create(RIBQNames.PEER_ID_QNAME);
86     private static final NodeIdentifier PEER_ROLE = NodeIdentifier.create(PEER_ROLE_QNAME);
87     private static final NodeIdentifier PEER_TABLES = NodeIdentifier.create(SupportedTables.QNAME);
88     private static final QName SEND_RECEIVE = QName.create(SupportedTables.QNAME, "send-receive").intern();
89
90     // FIXME: is there a utility method to construct this?
91     private static final MapNode EMPTY_TABLES = ImmutableNodes.mapNodeBuilder(TABLES_NID).build();
92     private static final ContainerNode EMPTY_ADJRIBIN = Builders.containerBuilder()
93             .withNodeIdentifier(ADJRIBIN_NID).addChild(EMPTY_TABLES).build();
94     private static final ContainerNode EMPTY_EFFRIBIN = Builders.containerBuilder()
95             .withNodeIdentifier(EFFRIBIN_NID).addChild(EMPTY_TABLES).build();
96     private static final ContainerNode EMPTY_ADJRIBOUT = Builders.containerBuilder()
97             .withNodeIdentifier(ADJRIBOUT_NID).addChild(EMPTY_TABLES).build();
98
99     private final Map<TablesKey, TableContext> tables;
100     private final YangInstanceIdentifier ribPath;
101     private final PeerTransactionChain chain;
102     private final PeerRole role;
103     @GuardedBy("this")
104     private final Map<TablesKey, Collection<NodeIdentifierWithPredicates>> staleRoutesRegistry = new HashMap<>();
105     @GuardedBy("this")
106     private FluentFuture<? extends CommitInfo> submitted;
107
108     private AdjRibInWriter(final YangInstanceIdentifier ribPath, final PeerTransactionChain chain, final PeerRole role,
109             final Map<TablesKey, TableContext> tables) {
110         this.ribPath = requireNonNull(ribPath);
111         this.chain = requireNonNull(chain);
112         this.tables = requireNonNull(tables);
113         this.role = requireNonNull(role);
114     }
115
116     /**
117      * Create a new writer using a transaction chain.
118      *
119      * @param role                peer's role
120      * @param chain               transaction chain  @return A fresh writer instance
121      */
122     static AdjRibInWriter create(final @NonNull YangInstanceIdentifier ribId, final @NonNull PeerRole role,
123             final @NonNull PeerTransactionChain chain) {
124         return new AdjRibInWriter(ribId, chain, role, Collections.emptyMap());
125     }
126
127     /**
128      * Transform this writer to a new writer, which is in charge of specified tables.
129      * Empty tables are created for new entries and old tables are deleted. Once this
130      * method returns, the old instance must not be reasonably used.
131      *
132      * @param newPeerId         new peer BGP identifier
133      * @param peerPath          path of the peer in the datastore
134      * @param registry          RIB extension registry
135      * @param tableTypes        New tables, must not be null
136      * @param addPathTablesType supported add path tables
137      * @return New writer
138      */
139     AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
140             final RIBSupportContextRegistry registry,
141             final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType) {
142         return transform(newPeerId, peerPath, registry, tableTypes, addPathTablesType, null);
143     }
144
145     AdjRibInWriter transform(final PeerId newPeerId, final YangInstanceIdentifier peerPath,
146             final RIBSupportContextRegistry registry, final Set<TablesKey> tableTypes,
147             final Map<TablesKey, SendReceive> addPathTablesType,
148             final @Nullable RegisterAppPeerListener registerAppPeerListener) {
149         final DOMDataTreeWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
150
151         createEmptyPeerStructure(newPeerId, peerPath, tx);
152         final ImmutableMap<TablesKey, TableContext> tb = createNewTableInstances(peerPath, registry, tableTypes,
153                 addPathTablesType, tx);
154
155         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
156             @Override
157             public void onSuccess(final CommitInfo result) {
158                 if (registerAppPeerListener != null) {
159                     LOG.trace("Application Peer Listener registered");
160                     registerAppPeerListener.register();
161                 }
162             }
163
164             @Override
165             public void onFailure(final Throwable throwable) {
166                 if (registerAppPeerListener != null) {
167                     LOG.error("Failed to create Empty Structure, Application Peer Listener won't be registered",
168                             throwable);
169                 } else {
170                     LOG.error("Failed to create Empty Structure", throwable);
171                 }
172             }
173         }, MoreExecutors.directExecutor());
174         return new AdjRibInWriter(this.ribPath, this.chain, this.role, tb);
175     }
176
177     /**
178      * Create new table instances, potentially creating their empty entries.
179      */
180     private static ImmutableMap<TablesKey, TableContext> createNewTableInstances(
181             final YangInstanceIdentifier newPeerPath, final RIBSupportContextRegistry registry,
182             final Set<TablesKey> tableTypes, final Map<TablesKey, SendReceive> addPathTablesType,
183             final DOMDataTreeWriteTransaction tx) {
184
185         final Builder<TablesKey, TableContext> tb = ImmutableMap.builder();
186         for (final TablesKey tableKey : tableTypes) {
187             final RIBSupportContext rs = registry.getRIBSupportContext(tableKey);
188             // TODO: Use returned value once Instance Identifier builder allows for it.
189             final NodeIdentifierWithPredicates instanceIdentifierKey = RibSupportUtils.toYangTablesKey(tableKey);
190             if (rs == null) {
191                 LOG.warn("No support for table type {}, skipping it", tableKey);
192                 continue;
193             }
194             installAdjRibsOutTables(newPeerPath, rs, instanceIdentifierKey, tableKey,
195                     addPathTablesType.get(tableKey), tx);
196             installAdjRibInTables(newPeerPath, tableKey, rs, instanceIdentifierKey, tx, tb);
197         }
198         return tb.build();
199     }
200
201     private static void installAdjRibInTables(final YangInstanceIdentifier newPeerPath, final TablesKey tableKey,
202             final RIBSupportContext rs, final NodeIdentifierWithPredicates instanceIdentifierKey,
203             final DOMDataTreeWriteTransaction tx, final Builder<TablesKey, TableContext> tb) {
204         // We will use table keys very often, make sure they are optimized
205         final InstanceIdentifierBuilder idb = YangInstanceIdentifier.builder(newPeerPath
206                 .node(EMPTY_ADJRIBIN.getIdentifier()).node(TABLES_NID));
207         idb.nodeWithKey(instanceIdentifierKey.getNodeType(), instanceIdentifierKey.getKeyValues());
208
209         final TableContext ctx = new TableContext(rs, idb.build());
210         ctx.createEmptyTableStructure(tx);
211
212         tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(ATTRIBUTES_NID).node(UPTODATE_NID),
213             RIBNormalizedNodes.ATTRIBUTES_UPTODATE_FALSE);
214         LOG.debug("Created table instance {}", ctx.getTableId());
215         tb.put(tableKey, ctx);
216     }
217
218     private static void installAdjRibsOutTables(final YangInstanceIdentifier newPeerPath, final RIBSupportContext rs,
219             final NodeIdentifierWithPredicates instanceIdentifierKey, final TablesKey tableKey,
220             final SendReceive sendReceive, final DOMDataTreeWriteTransaction tx) {
221         final NodeIdentifierWithPredicates supTablesKey = RibSupportUtils.toYangKey(SupportedTables.QNAME, tableKey);
222         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> tt =
223                 Builders.mapEntryBuilder().withNodeIdentifier(supTablesKey);
224         for (final Entry<QName, Object> e : supTablesKey.getKeyValues().entrySet()) {
225             tt.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
226         }
227         if (sendReceive != null) {
228             tt.withChild(ImmutableNodes.leafNode(SEND_RECEIVE, sendReceive.toString().toLowerCase(Locale.ENGLISH)));
229         }
230         tx.put(LogicalDatastoreType.OPERATIONAL, newPeerPath.node(PEER_TABLES).node(supTablesKey), tt.build());
231         rs.createEmptyTableStructure(tx, newPeerPath.node(EMPTY_ADJRIBOUT.getIdentifier())
232                 .node(TABLES_NID).node(instanceIdentifierKey));
233     }
234
235     private void createEmptyPeerStructure(final PeerId newPeerId,
236             final YangInstanceIdentifier peerPath, final DOMDataTreeWriteTransaction tx) {
237         final NodeIdentifierWithPredicates peerKey = IdentifierUtils.domPeerId(newPeerId);
238
239         tx.put(LogicalDatastoreType.OPERATIONAL, peerPath, peerSkeleton(peerKey, newPeerId.getValue()));
240         LOG.debug("New peer {} structure installed.", peerPath);
241     }
242
243     @VisibleForTesting
244     MapEntryNode peerSkeleton(final NodeIdentifierWithPredicates peerKey, final String peerId) {
245         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> pb = Builders.mapEntryBuilder();
246         pb.withNodeIdentifier(peerKey);
247         pb.withChild(ImmutableNodes.leafNode(PEER_ID, peerId));
248         pb.withChild(ImmutableNodes.leafNode(PEER_ROLE, PeerRoleUtil.roleForString(this.role)));
249         pb.withChild(ImmutableMapNodeBuilder.create().withNodeIdentifier(PEER_TABLES).build());
250         pb.withChild(EMPTY_ADJRIBIN);
251         pb.withChild(EMPTY_EFFRIBIN);
252         pb.withChild(EMPTY_ADJRIBOUT);
253         return pb.build();
254     }
255
256     void markTableUptodate(final TablesKey tableTypes) {
257         final DOMDataTreeWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
258         final TableContext ctx = this.tables.get(tableTypes);
259         tx.merge(LogicalDatastoreType.OPERATIONAL, ctx.getTableId().node(ATTRIBUTES_NID).node(UPTODATE_NID),
260             RIBNormalizedNodes.ATTRIBUTES_UPTODATE_TRUE);
261         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
262             @Override
263             public void onSuccess(final CommitInfo result) {
264                 LOG.trace("Write Attributes uptodate, succeed");
265             }
266
267             @Override
268             public void onFailure(final Throwable throwable) {
269                 LOG.error("Write Attributes uptodate failed", throwable);
270             }
271         }, MoreExecutors.directExecutor());
272     }
273
274     void updateRoutes(final MpReachNlri nlri, final org.opendaylight.yang.gen.v1.urn.opendaylight.params
275             .xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes attributes) {
276         final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
277         final TableContext ctx = this.tables.get(key);
278         if (ctx == null) {
279             LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
280             return;
281         }
282
283         final DOMDataTreeWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
284         final Collection<NodeIdentifierWithPredicates> routeKeys = ctx.writeRoutes(tx, nlri, attributes);
285         final Collection<NodeIdentifierWithPredicates> staleRoutes = this.staleRoutesRegistry.get(key);
286         if (staleRoutes != null) {
287             staleRoutes.removeAll(routeKeys);
288         }
289         LOG.trace("Write routes {}", nlri);
290         final FluentFuture<? extends CommitInfo> future = tx.commit();
291         this.submitted = future;
292         future.addCallback(new FutureCallback<CommitInfo>() {
293             @Override
294             public void onSuccess(final CommitInfo result) {
295                 LOG.trace("Write routes {}, succeed", nlri);
296             }
297
298             @Override
299             public void onFailure(final Throwable throwable) {
300                 LOG.error("Write routes failed", throwable);
301             }
302         }, MoreExecutors.directExecutor());
303     }
304
305     void removeRoutes(final MpUnreachNlri nlri) {
306         final TablesKey key = new TablesKey(nlri.getAfi(), nlri.getSafi());
307         final TableContext ctx = this.tables.get(key);
308         if (ctx == null) {
309             LOG.debug("No table for {}, not accepting NLRI {}", key, nlri);
310             return;
311         }
312         LOG.trace("Removing routes {}", nlri);
313         final DOMDataTreeWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
314         ctx.removeRoutes(tx, nlri);
315         final FluentFuture<? extends CommitInfo> future = tx.commit();
316         this.submitted = future;
317         future.addCallback(new FutureCallback<CommitInfo>() {
318             @Override
319             public void onSuccess(final CommitInfo result) {
320                 LOG.trace("Removing routes {}, succeed", nlri);
321             }
322
323             @Override
324             public void onFailure(final Throwable throwable) {
325                 LOG.error("Removing routes failed", throwable);
326             }
327         }, MoreExecutors.directExecutor());
328     }
329
330     void releaseChain() {
331         if (this.submitted != null) {
332             try {
333                 this.submitted.get();
334             } catch (final InterruptedException | ExecutionException throwable) {
335                 LOG.error("Write routes failed", throwable);
336             }
337         }
338     }
339
340     void storeStaleRoutes(final Set<TablesKey> gracefulTables) {
341         final CountDownLatch latch = new CountDownLatch(gracefulTables.size());
342
343         try (DOMDataTreeReadTransaction tx = this.chain.getDomChain().newReadOnlyTransaction()) {
344             for (TablesKey tablesKey : gracefulTables) {
345                 final TableContext ctx = this.tables.get(tablesKey);
346                 if (ctx == null) {
347                     LOG.warn("Missing table for address family {}", tablesKey);
348                     latch.countDown();
349                     continue;
350                 }
351
352                 tx.read(LogicalDatastoreType.OPERATIONAL, ctx.routesPath()).addCallback(
353                     new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
354                         @Override
355                         public void onSuccess(final Optional<NormalizedNode<?, ?>> routesOptional) {
356                             try {
357                                 if (routesOptional.isPresent()) {
358                                     synchronized (AdjRibInWriter.this.staleRoutesRegistry) {
359                                         final MapNode routesNode = (MapNode) routesOptional.get();
360                                         final List<NodeIdentifierWithPredicates> routes = routesNode.getValue().stream()
361                                                 .map(MapEntryNode::getIdentifier)
362                                                 .collect(Collectors.toList());
363                                         if (!routes.isEmpty()) {
364                                             AdjRibInWriter.this.staleRoutesRegistry.put(tablesKey, routes);
365                                         }
366                                     }
367                                 }
368                             } finally {
369                                 latch.countDown();
370                             }
371                         }
372
373                         @Override
374                         public void onFailure(final Throwable throwable) {
375                             LOG.warn("Failed to store stale routes for table {}", tablesKey, throwable);
376                             latch.countDown();
377                         }
378                     }, MoreExecutors.directExecutor());
379             }
380         }
381
382         try {
383             latch.await();
384         } catch (InterruptedException e) {
385             LOG.warn("Interrupted while waiting to store stale routes with {} tasks of {} to finish", latch.getCount(),
386                 gracefulTables, e);
387         }
388     }
389
390     void removeStaleRoutes(final TablesKey tableKey) {
391         final TableContext ctx = this.tables.get(tableKey);
392         if (ctx == null) {
393             LOG.debug("No table for {}, not removing any stale routes", tableKey);
394             return;
395         }
396         final Collection<NodeIdentifierWithPredicates> routeKeys = this.staleRoutesRegistry.get(tableKey);
397         if (routeKeys == null || routeKeys.isEmpty()) {
398             LOG.debug("No stale routes present in table {}", tableKey);
399             return;
400         }
401         LOG.trace("Removing routes {}", routeKeys);
402         final DOMDataTreeWriteTransaction tx = this.chain.getDomChain().newWriteOnlyTransaction();
403         routeKeys.forEach(routeKey -> {
404             tx.delete(LogicalDatastoreType.OPERATIONAL, ctx.routePath(routeKey));
405         });
406         final FluentFuture<? extends CommitInfo> future = tx.commit();
407         this.submitted = future;
408         future.addCallback(new FutureCallback<CommitInfo>() {
409             @Override
410             public void onSuccess(final CommitInfo result) {
411                 LOG.trace("Removing routes {}, succeed", routeKeys);
412                 synchronized (AdjRibInWriter.this.staleRoutesRegistry) {
413                     staleRoutesRegistry.remove(tableKey);
414                 }
415             }
416
417             @Override
418             public void onFailure(final Throwable throwable) {
419                 LOG.warn("Removing routes {}, failed", routeKeys, throwable);
420             }
421         }, MoreExecutors.directExecutor());
422     }
423
424     FluentFuture<? extends CommitInfo> clearTables(final Set<TablesKey> tablesToClear) {
425         if (tablesToClear == null || tablesToClear.isEmpty()) {
426             return CommitInfo.emptyFluentFuture();
427         }
428
429         final DOMDataTreeWriteTransaction wtx = this.chain.getDomChain().newWriteOnlyTransaction();
430         tablesToClear.forEach(tableKey -> {
431             final TableContext ctx = this.tables.get(tableKey);
432             wtx.delete(LogicalDatastoreType.OPERATIONAL, ctx.routesPath().getParent());
433         });
434         return wtx.commit();
435     }
436 }