2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
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;
33 * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
35 * @see DOMTransactionChain
36 * @see DOMDataTreeReadWriteTransaction
38 public final class MdsalRestconfStrategy extends RestconfStrategy {
39 private final DOMDataBroker dataBroker;
41 public MdsalRestconfStrategy(final DOMDataBroker dataBroker) {
42 this.dataBroker = requireNonNull(dataBroker);
46 RestconfTransaction prepareWriteExecution() {
47 return new MdsalRestconfTransaction(dataBroker);
51 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
52 final var tx = dataBroker.newReadWriteTransaction();
53 tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
55 public void onSuccess(final Boolean result) {
57 cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
58 ErrorTag.DATA_MISSING, path));
62 tx.delete(CONFIGURATION, path);
63 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
65 public void onSuccess(final CommitInfo result) {
66 future.set(Empty.value());
70 public void onFailure(final Throwable cause) {
71 future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
74 }, MoreExecutors.directExecutor());
78 public void onFailure(final Throwable cause) {
79 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
82 private void cancelTx(final RestconfDocumentedException ex) {
84 future.setFailure(ex);
86 }, MoreExecutors.directExecutor());
90 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
91 final YangInstanceIdentifier path) {
92 try (var tx = dataBroker.newReadOnlyTransaction()) {
93 return tx.read(store, path);
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));
105 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
106 try (var tx = dataBroker.newReadOnlyTransaction()) {
107 return tx.exists(LogicalDatastoreType.CONFIGURATION, path);