Use DatabindContext in InstanceIdentifierContext
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / MdsalRestconfStrategy.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
12
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;
38
39 /**
40  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
41  *
42  * @see DOMTransactionChain
43  * @see DOMDataTreeReadWriteTransaction
44  */
45 public final class MdsalRestconfStrategy extends RestconfStrategy {
46     private final DOMDataBroker dataBroker;
47
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);
53     }
54
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());
58     }
59
60     @Override
61     RestconfTransaction prepareWriteExecution() {
62         return new MdsalRestconfTransaction(modelContext(), dataBroker);
63     }
64
65     @Override
66     void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
67         final var tx = dataBroker.newReadWriteTransaction();
68         tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
69             @Override
70             public void onSuccess(final Boolean result) {
71                 if (!result) {
72                     cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
73                         ErrorTag.DATA_MISSING, path));
74                     return;
75                 }
76
77                 tx.delete(CONFIGURATION, path);
78                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
79                     @Override
80                     public void onSuccess(final CommitInfo result) {
81                         future.set(Empty.value());
82                     }
83
84                     @Override
85                     public void onFailure(final Throwable cause) {
86                         future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
87                             cause));
88                     }
89                 }, MoreExecutors.directExecutor());
90             }
91
92             @Override
93             public void onFailure(final Throwable cause) {
94                 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
95             }
96
97             private void cancelTx(final RestconfDocumentedException ex) {
98                 tx.cancel();
99                 future.setFailure(ex);
100             }
101         }, MoreExecutors.directExecutor());
102     }
103
104     @Override
105     ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
106             final YangInstanceIdentifier path) {
107         try (var tx = dataBroker.newReadOnlyTransaction()) {
108             return tx.read(store, path);
109         }
110     }
111
112     @Override
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));
117     }
118
119     @Override
120     ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
121         try (var tx = dataBroker.newReadOnlyTransaction()) {
122             return tx.exists(LogicalDatastoreType.CONFIGURATION, path);
123         }
124     }
125 }