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.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.DOMActionService;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMRpcService;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
27 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfFuture;
30 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
31 import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
32 import org.opendaylight.restconf.nb.rfc8040.utils.parser.WriterFieldsTranslator;
33 import org.opendaylight.restconf.server.api.DataGetParams;
34 import org.opendaylight.restconf.server.api.DataGetResult;
35 import org.opendaylight.restconf.server.api.DatabindContext;
36 import org.opendaylight.restconf.server.spi.ApiPathNormalizer.DataPath;
37 import org.opendaylight.restconf.server.spi.RpcImplementation;
38 import org.opendaylight.yangtools.yang.common.Empty;
39 import org.opendaylight.yangtools.yang.common.ErrorTag;
40 import org.opendaylight.yangtools.yang.common.ErrorType;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
48 * @see DOMTransactionChain
49 * @see DOMDataTreeReadWriteTransaction
51 public final class MdsalRestconfStrategy extends RestconfStrategy {
52 private final DOMDataBroker dataBroker;
54 public MdsalRestconfStrategy(final DatabindContext databind, final DOMDataBroker dataBroker,
55 final @Nullable DOMRpcService rpcService, final @Nullable DOMActionService actionService,
56 final @Nullable YangTextSourceExtension sourceProvider,
57 final @Nullable DOMMountPointService mountPointService,
58 final ImmutableMap<QName, RpcImplementation> localRpcs) {
59 super(databind, localRpcs, rpcService, actionService, sourceProvider, mountPointService);
60 this.dataBroker = requireNonNull(dataBroker);
63 public MdsalRestconfStrategy(final DatabindContext databind, final DOMDataBroker dataBroker,
64 final @Nullable DOMRpcService rpcService, final @Nullable DOMActionService actionService,
65 final @Nullable YangTextSourceExtension sourceProvider,
66 final @Nullable DOMMountPointService mountPointService) {
67 this(databind, dataBroker, rpcService, actionService, sourceProvider, mountPointService, ImmutableMap.of());
71 RestconfTransaction prepareWriteExecution() {
72 return new MdsalRestconfTransaction(modelContext(), dataBroker);
76 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
77 final var tx = dataBroker.newReadWriteTransaction();
78 tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
80 public void onSuccess(final Boolean result) {
82 cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
83 ErrorTag.DATA_MISSING, path));
87 tx.delete(CONFIGURATION, path);
88 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
90 public void onSuccess(final CommitInfo result) {
91 future.set(Empty.value());
95 public void onFailure(final Throwable cause) {
96 future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
99 }, MoreExecutors.directExecutor());
103 public void onFailure(final Throwable cause) {
104 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
107 private void cancelTx(final RestconfDocumentedException ex) {
109 future.setFailure(ex);
111 }, MoreExecutors.directExecutor());
115 RestconfFuture<DataGetResult> dataGET(final DataPath path, final DataGetParams params) {
116 final var inference = path.inference();
117 final var fields = params.fields();
118 final var translatedFields = fields == null ? null
119 : WriterFieldsTranslator.translate(inference.modelContext(), path.schema(), fields);
120 return completeDataGET(inference, QueryParameters.of(params, translatedFields),
121 readData(params.content(), path.instance(), params.withDefaults()), null);
125 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
126 final YangInstanceIdentifier path) {
127 try (var tx = dataBroker.newReadOnlyTransaction()) {
128 return tx.read(store, path);
133 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
134 try (var tx = dataBroker.newReadOnlyTransaction()) {
135 return tx.exists(LogicalDatastoreType.CONFIGURATION, path);