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.collect.ImmutableMap;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.List;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.mdsal.common.api.CommitInfo;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
25 import org.opendaylight.mdsal.dom.api.DOMRpcService;
26 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
29 import org.opendaylight.restconf.server.spi.RpcImplementation;
30 import org.opendaylight.yangtools.yang.common.Empty;
31 import org.opendaylight.yangtools.yang.common.ErrorTag;
32 import org.opendaylight.yangtools.yang.common.ErrorType;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
39 * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
41 * @see DOMTransactionChain
42 * @see DOMDataTreeReadWriteTransaction
44 public final class MdsalRestconfStrategy extends RestconfStrategy {
45 private final DOMDataBroker dataBroker;
47 public MdsalRestconfStrategy(final EffectiveModelContext modelContext, final DOMDataBroker dataBroker,
48 final @Nullable DOMRpcService rpcService, final ImmutableMap<QName, RpcImplementation> localRpcs) {
49 super(modelContext, localRpcs, rpcService);
50 this.dataBroker = requireNonNull(dataBroker);
53 public MdsalRestconfStrategy(final EffectiveModelContext modelContext, final DOMDataBroker dataBroker,
54 final @Nullable DOMRpcService rpcService) {
55 this(modelContext, dataBroker, rpcService, ImmutableMap.of());
59 RestconfTransaction prepareWriteExecution() {
60 return new MdsalRestconfTransaction(modelContext(), dataBroker);
64 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
65 final var tx = dataBroker.newReadWriteTransaction();
66 tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
68 public void onSuccess(final Boolean result) {
70 cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
71 ErrorTag.DATA_MISSING, path));
75 tx.delete(CONFIGURATION, path);
76 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
78 public void onSuccess(final CommitInfo result) {
79 future.set(Empty.value());
83 public void onFailure(final Throwable cause) {
84 future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
87 }, MoreExecutors.directExecutor());
91 public void onFailure(final Throwable cause) {
92 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
95 private void cancelTx(final RestconfDocumentedException ex) {
97 future.setFailure(ex);
99 }, MoreExecutors.directExecutor());
103 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
104 final YangInstanceIdentifier path) {
105 try (var tx = dataBroker.newReadOnlyTransaction()) {
106 return tx.read(store, path);
111 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store, final YangInstanceIdentifier path,
112 final List<YangInstanceIdentifier> fields) {
113 return Futures.immediateFailedFuture(new UnsupportedOperationException(
114 "Reading of selected subtrees is currently not supported in: " + MdsalRestconfStrategy.class));
118 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
119 try (var tx = dataBroker.newReadOnlyTransaction()) {
120 return tx.exists(LogicalDatastoreType.CONFIGURATION, path);