Improve RestconfStrategy API surface
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / MdsalRestconfStrategy.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 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
12
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.List;
18 import java.util.Optional;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
23 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
26 import org.opendaylight.yangtools.yang.common.Empty;
27 import org.opendaylight.yangtools.yang.common.ErrorTag;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31
32 /**
33  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
34  *
35  * @see DOMTransactionChain
36  * @see DOMDataTreeReadWriteTransaction
37  */
38 public final class MdsalRestconfStrategy extends RestconfStrategy {
39     private final DOMDataBroker dataBroker;
40
41     public MdsalRestconfStrategy(final DOMDataBroker dataBroker) {
42         this.dataBroker = requireNonNull(dataBroker);
43     }
44
45     @Override
46     RestconfTransaction prepareWriteExecution() {
47         return new MdsalRestconfTransaction(dataBroker);
48     }
49
50     @Override
51     void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
52         final var tx = dataBroker.newReadWriteTransaction();
53         tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
54             @Override
55             public void onSuccess(final Boolean result) {
56                 if (!result) {
57                     cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
58                         ErrorTag.DATA_MISSING, path));
59                     return;
60                 }
61
62                 tx.delete(CONFIGURATION, path);
63                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
64                     @Override
65                     public void onSuccess(final CommitInfo result) {
66                         future.set(Empty.value());
67                     }
68
69                     @Override
70                     public void onFailure(final Throwable cause) {
71                         future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
72                             cause));
73                     }
74                 }, MoreExecutors.directExecutor());
75             }
76
77             @Override
78             public void onFailure(final Throwable cause) {
79                 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
80             }
81
82             private void cancelTx(final RestconfDocumentedException ex) {
83                 tx.cancel();
84                 future.setFailure(ex);
85             }
86         }, MoreExecutors.directExecutor());
87     }
88
89     @Override
90     ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
91             final YangInstanceIdentifier path) {
92         try (var tx = dataBroker.newReadOnlyTransaction()) {
93             return tx.read(store, path);
94         }
95     }
96
97     @Override
98     ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store, final YangInstanceIdentifier path,
99             final List<YangInstanceIdentifier> fields) {
100         return Futures.immediateFailedFuture(new UnsupportedOperationException(
101                 "Reading of selected subtrees is currently not supported in: " + MdsalRestconfStrategy.class));
102     }
103
104     @Override
105     ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
106         try (var tx = dataBroker.newReadOnlyTransaction()) {
107             return tx.exists(LogicalDatastoreType.CONFIGURATION, path);
108         }
109     }
110 }