dc1454a09d38e36d6441cf359b1a611361bc4b7f
[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.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
15 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
16 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19
20 /**
21  * Baseline execution strategy for various RESTCONF operations.
22  *
23  * @see NetconfRestconfStrategy
24  * @see MdsalRestconfStrategy
25  */
26 // FIXME: it seems the first three operations deal with lifecycle of a transaction, while others invoke various
27 //        operations. This should be handled through proper allocation indirection.
28 public abstract class RestconfStrategy implements AutoCloseable {
29     RestconfStrategy() {
30         // Hidden on purpose
31     }
32
33     /**
34      * Look up the appropriate strategy for a particular mount point.
35      *
36      * @param mountPoint Target mount point
37      * @return A strategy, or null if the mount point does not expose a supported interface
38      * @throws NullPointerException if {@code mountPoint} is null
39      */
40     public static Optional<RestconfStrategy> forMountPoint(final DOMMountPoint mountPoint) {
41         final Optional<RestconfStrategy> netconf = mountPoint.getService(NetconfDataTreeService.class)
42             .map(NetconfRestconfStrategy::new);
43         if (netconf.isPresent()) {
44             return netconf;
45         }
46
47         return mountPoint.getService(DOMDataBroker.class).map(MdsalRestconfStrategy::new);
48     }
49
50     /**
51      * Lock the entire datastore.
52      *
53      * @return A {@link RestconfTransaction}. This transaction needs to be either committed or canceled before doing
54      *         anything else.
55      */
56     public abstract RestconfTransaction prepareWriteExecution();
57
58     /**
59      * Read data from the datastore.
60      *
61      * @param store the logical data store which should be modified
62      * @param path the data object path
63      * @return a ListenableFuture containing the result of the read
64      */
65     public abstract ListenableFuture<Optional<NormalizedNode<?, ?>>> read(LogicalDatastoreType store,
66         YangInstanceIdentifier path);
67
68     /**
69      * Check if data already exists in the datastore.
70      *
71      * @param store the logical data store which should be modified
72      * @param path the data object path
73      * @return a FluentFuture containing the result of the check
74      */
75     public abstract FluentFuture<Boolean> exists(LogicalDatastoreType store, YangInstanceIdentifier path);
76
77     @Override
78     public abstract void close();
79 }