Eliminate unnecessary blocking checks
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.mdsal.common.api.CommitInfo;
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.mdsal.dom.api.DOMTransactionChain;
20 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
21 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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     public abstract void prepareReadWriteExecution();
60
61     /**
62      * Confirm previous operations.
63      *
64      * @return a FluentFuture containing the result of the commit information
65      */
66     public abstract FluentFuture<? extends @NonNull CommitInfo> commit();
67
68     /**
69      * Rollback changes and unlock the datastore.
70      */
71     public abstract void cancel();
72
73     /**
74      * Read data from the datastore.
75      *
76      * @param store the logical data store which should be modified
77      * @param path the data object path
78      * @return a ListenableFuture containing the result of the read
79      */
80     public abstract ListenableFuture<Optional<NormalizedNode<?, ?>>> read(LogicalDatastoreType store,
81         YangInstanceIdentifier path);
82
83     /**
84      * Check if data already exists in the datastore.
85      *
86      * @param store the logical data store which should be modified
87      * @param path the data object path
88      * @return a FluentFuture containing the result of the check
89      */
90     public abstract FluentFuture<Boolean> exists(LogicalDatastoreType store, YangInstanceIdentifier path);
91
92     /**
93      * Delete data from the datastore.
94      *
95      * @param store the logical data store which should be modified
96      * @param path the data object path
97      */
98     public abstract void delete(LogicalDatastoreType store, YangInstanceIdentifier path);
99
100     /**
101      * Remove data from the datastore.
102      *
103      * @param store the logical data store which should be modified
104      * @param path  the data object path
105      */
106     public abstract void remove(LogicalDatastoreType store, YangInstanceIdentifier path);
107
108     /**
109      * Merges a piece of data with the existing data at a specified path.
110      *
111      * @param store the logical data store which should be modified
112      * @param path the data object path
113      * @param data the data object to be merged to the specified path
114      */
115     public abstract void merge(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data);
116
117     /**
118      * Stores a piece of data at the specified path.
119      *
120      * @param store the logical data store which should be modified
121      * @param path  the data object path
122      * @param data  the data object to be merged to the specified path
123      * @param schemaContext  static view of compiled yang files
124      */
125     public abstract void create(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data,
126                 SchemaContext schemaContext);
127
128     /**
129      * Replace a piece of data at the specified path.
130      *
131      * @param store        the logical data store which should be modified
132      * @param path         the data object path
133      * @param data         the data object to be merged to the specified path
134      * @param schemaContext  static view of compiled yang files
135      */
136     public abstract void replace(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data,
137                  SchemaContext schemaContext);
138
139
140     /**
141      * Get transaction chain for creating specific transaction for specific operation.
142      *
143      * @return transaction chain or null
144      */
145     // FIXME: these look like an implementation detail
146     public abstract @Nullable DOMTransactionChain getTransactionChain();
147
148     /**
149      * Get transaction chain handler for creating new transaction chain.
150      *
151      * @return {@link TransactionChainHandler} or null
152      */
153     // FIXME: these look like an implementation detail
154     public abstract @Nullable TransactionChainHandler getTransactionChainHandler();
155 }