Rename restconf-nb-rfc8040 to restconf-nb
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / RestconfStrategy.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.rests.transactions;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
16 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
17 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
21 /**
22  * Baseline execution strategy for various RESTCONF operations.
23  *
24  * @see NetconfRestconfStrategy
25  * @see MdsalRestconfStrategy
26  */
27 // FIXME: it seems the first three operations deal with lifecycle of a transaction, while others invoke various
28 //        operations. This should be handled through proper allocation indirection.
29 public abstract class RestconfStrategy {
30     RestconfStrategy() {
31         // Hidden on purpose
32     }
33
34     /**
35      * Look up the appropriate strategy for a particular mount point.
36      *
37      * @param mountPoint Target mount point
38      * @return A strategy, or null if the mount point does not expose a supported interface
39      * @throws NullPointerException if {@code mountPoint} is null
40      */
41     public static Optional<RestconfStrategy> forMountPoint(final DOMMountPoint mountPoint) {
42         final Optional<RestconfStrategy> netconf = mountPoint.getService(NetconfDataTreeService.class)
43             .map(NetconfRestconfStrategy::new);
44         if (netconf.isPresent()) {
45             return netconf;
46         }
47
48         return mountPoint.getService(DOMDataBroker.class).map(MdsalRestconfStrategy::new);
49     }
50
51     /**
52      * Lock the entire datastore.
53      *
54      * @return A {@link RestconfTransaction}. This transaction needs to be either committed or canceled before doing
55      *         anything else.
56      */
57     public abstract RestconfTransaction prepareWriteExecution();
58
59     /**
60      * Read data from the datastore.
61      *
62      * @param store the logical data store which should be modified
63      * @param path the data object path
64      * @return a ListenableFuture containing the result of the read
65      */
66     public abstract ListenableFuture<Optional<NormalizedNode>> read(LogicalDatastoreType store,
67         YangInstanceIdentifier path);
68
69     /**
70      * Read data selected using fields from the datastore.
71      *
72      * @param store the logical data store which should be modified
73      * @param path the parent data object path
74      * @param fields paths to selected fields relative to parent path
75      * @return a ListenableFuture containing the result of the read
76      */
77     public abstract ListenableFuture<Optional<NormalizedNode>> read(LogicalDatastoreType store,
78             YangInstanceIdentifier path, List<YangInstanceIdentifier> fields);
79
80     /**
81      * Check if data already exists in the datastore.
82      *
83      * @param store the logical data store which should be modified
84      * @param path the data object path
85      * @return a FluentFuture containing the result of the check
86      */
87     public abstract FluentFuture<Boolean> exists(LogicalDatastoreType store, YangInstanceIdentifier path);
88 }