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.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMRpcService;
25 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
26 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
27 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
28 import org.opendaylight.yangtools.yang.common.Empty;
29 import org.opendaylight.yangtools.yang.common.ErrorTag;
30 import org.opendaylight.yangtools.yang.common.ErrorType;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
38 * @see DOMTransactionChain
39 * @see DOMDataTreeReadWriteTransaction
41 public final class MdsalRestconfStrategy extends RestconfStrategy {
42 private final DOMDataBroker dataBroker;
44 public MdsalRestconfStrategy(final EffectiveModelContext modelContext, final DOMDataBroker dataBroker,
45 final @Nullable DOMRpcService rpcService) {
46 super(modelContext, rpcService);
47 this.dataBroker = requireNonNull(dataBroker);
51 RestconfTransaction prepareWriteExecution() {
52 return new MdsalRestconfTransaction(modelContext(), dataBroker);
56 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
57 final var tx = dataBroker.newReadWriteTransaction();
58 tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
60 public void onSuccess(final Boolean result) {
62 cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
63 ErrorTag.DATA_MISSING, path));
67 tx.delete(CONFIGURATION, path);
68 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
70 public void onSuccess(final CommitInfo result) {
71 future.set(Empty.value());
75 public void onFailure(final Throwable cause) {
76 future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
79 }, MoreExecutors.directExecutor());
83 public void onFailure(final Throwable cause) {
84 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
87 private void cancelTx(final RestconfDocumentedException ex) {
89 future.setFailure(ex);
91 }, MoreExecutors.directExecutor());
95 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
96 final YangInstanceIdentifier path) {
97 try (var tx = dataBroker.newReadOnlyTransaction()) {
98 return tx.read(store, path);
103 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store, final YangInstanceIdentifier path,
104 final List<YangInstanceIdentifier> fields) {
105 return Futures.immediateFailedFuture(new UnsupportedOperationException(
106 "Reading of selected subtrees is currently not supported in: " + MdsalRestconfStrategy.class));
110 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
111 try (var tx = dataBroker.newReadOnlyTransaction()) {
112 return tx.exists(LogicalDatastoreType.CONFIGURATION, path);