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.mdsal.dom.api.DOMYangTextSourceProvider;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
30 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
31 import org.opendaylight.restconf.server.spi.RpcImplementation;
32 import org.opendaylight.yangtools.yang.common.Empty;
33 import org.opendaylight.yangtools.yang.common.ErrorTag;
34 import org.opendaylight.yangtools.yang.common.ErrorType;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
42 * @see DOMTransactionChain
43 * @see DOMDataTreeReadWriteTransaction
45 public final class MdsalRestconfStrategy extends RestconfStrategy {
46 private final DOMDataBroker dataBroker;
48 public MdsalRestconfStrategy(final DatabindContext databind, final DOMDataBroker dataBroker,
49 final @Nullable DOMRpcService rpcService, final @Nullable DOMYangTextSourceProvider sourceProvider,
50 final ImmutableMap<QName, RpcImplementation> localRpcs) {
51 super(databind, localRpcs, rpcService, sourceProvider);
52 this.dataBroker = requireNonNull(dataBroker);
55 public MdsalRestconfStrategy(final DatabindContext databind, final DOMDataBroker dataBroker,
56 final @Nullable DOMRpcService rpcService, final @Nullable DOMYangTextSourceProvider sourceProvider) {
57 this(databind, dataBroker, rpcService, sourceProvider, ImmutableMap.of());
61 RestconfTransaction prepareWriteExecution() {
62 return new MdsalRestconfTransaction(modelContext(), dataBroker);
66 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
67 final var tx = dataBroker.newReadWriteTransaction();
68 tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
70 public void onSuccess(final Boolean result) {
72 cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
73 ErrorTag.DATA_MISSING, path));
77 tx.delete(CONFIGURATION, path);
78 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
80 public void onSuccess(final CommitInfo result) {
81 future.set(Empty.value());
85 public void onFailure(final Throwable cause) {
86 future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
89 }, MoreExecutors.directExecutor());
93 public void onFailure(final Throwable cause) {
94 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
97 private void cancelTx(final RestconfDocumentedException ex) {
99 future.setFailure(ex);
101 }, MoreExecutors.directExecutor());
105 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
106 final YangInstanceIdentifier path) {
107 try (var tx = dataBroker.newReadOnlyTransaction()) {
108 return tx.read(store, path);
113 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store, final YangInstanceIdentifier path,
114 final List<YangInstanceIdentifier> fields) {
115 return Futures.immediateFailedFuture(new UnsupportedOperationException(
116 "Reading of selected subtrees is currently not supported in: " + MdsalRestconfStrategy.class));
120 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
121 try (var tx = dataBroker.newReadOnlyTransaction()) {
122 return tx.exists(LogicalDatastoreType.CONFIGURATION, path);