Remove DeleteDataTransactionUtil
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
18 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
19 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
20 import org.opendaylight.restconf.common.errors.RestconfFuture;
21 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 /**
27  * Baseline execution strategy for various RESTCONF operations.
28  *
29  * @see NetconfRestconfStrategy
30  * @see MdsalRestconfStrategy
31  */
32 // FIXME: it seems the first three operations deal with lifecycle of a transaction, while others invoke various
33 //        operations. This should be handled through proper allocation indirection.
34 public abstract class RestconfStrategy {
35     RestconfStrategy() {
36         // Hidden on purpose
37     }
38
39     /**
40      * Look up the appropriate strategy for a particular mount point.
41      *
42      * @param mountPoint Target mount point
43      * @return A strategy, or null if the mount point does not expose a supported interface
44      * @throws NullPointerException if {@code mountPoint} is null
45      */
46     public static Optional<RestconfStrategy> forMountPoint(final DOMMountPoint mountPoint) {
47         final Optional<RestconfStrategy> netconf = mountPoint.getService(NetconfDataTreeService.class)
48             .map(NetconfRestconfStrategy::new);
49         if (netconf.isPresent()) {
50             return netconf;
51         }
52
53         return mountPoint.getService(DOMDataBroker.class).map(MdsalRestconfStrategy::new);
54     }
55
56     /**
57      * Lock the entire datastore.
58      *
59      * @return A {@link RestconfTransaction}. This transaction needs to be either committed or canceled before doing
60      *         anything else.
61      */
62     public abstract RestconfTransaction prepareWriteExecution();
63
64     /**
65      * Read data from the datastore.
66      *
67      * @param store the logical data store which should be modified
68      * @param path the data object path
69      * @return a ListenableFuture containing the result of the read
70      */
71     public abstract ListenableFuture<Optional<NormalizedNode>> read(LogicalDatastoreType store,
72         YangInstanceIdentifier path);
73
74     /**
75      * Read data selected using fields from the datastore.
76      *
77      * @param store the logical data store which should be modified
78      * @param path the parent data object path
79      * @param fields paths to selected fields relative to parent path
80      * @return a ListenableFuture containing the result of the read
81      */
82     public abstract ListenableFuture<Optional<NormalizedNode>> read(LogicalDatastoreType store,
83             YangInstanceIdentifier path, List<YangInstanceIdentifier> fields);
84
85     /**
86      * Check if data already exists in the datastore.
87      *
88      * @param store the logical data store which should be modified
89      * @param path the data object path
90      * @return a FluentFuture containing the result of the check
91      */
92     public abstract ListenableFuture<Boolean> exists(LogicalDatastoreType store, YangInstanceIdentifier path);
93
94     /**
95      * Delete data from the configuration datastore. If the data does not exist, this operation will fail, as outlined
96      * in <a href="https://www.rfc-editor.org/rfc/rfc8040#section-4.7">RFC8040 section 4.7</a>
97      *
98      * @param path Path to delete
99      * @return A {@link RestconfFuture}
100      * @throws NullPointerException if {@code path} is {@code null}
101      */
102     public final @NonNull RestconfFuture<Empty> delete(final YangInstanceIdentifier path) {
103         final var ret = new SettableRestconfFuture<Empty>();
104         delete(ret, requireNonNull(path));
105         return ret;
106     }
107
108     protected abstract void delete(@NonNull SettableRestconfFuture<Empty> future, @NonNull YangInstanceIdentifier path);
109 }