Expose streams with all supported encodings
[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.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;
34
35 /**
36  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
37  *
38  * @see DOMTransactionChain
39  * @see DOMDataTreeReadWriteTransaction
40  */
41 public final class MdsalRestconfStrategy extends RestconfStrategy {
42     private final DOMDataBroker dataBroker;
43
44     public MdsalRestconfStrategy(final EffectiveModelContext modelContext, final DOMDataBroker dataBroker,
45             final @Nullable DOMRpcService rpcService) {
46         super(modelContext, rpcService);
47         this.dataBroker = requireNonNull(dataBroker);
48     }
49
50     @Override
51     RestconfTransaction prepareWriteExecution() {
52         return new MdsalRestconfTransaction(modelContext(), dataBroker);
53     }
54
55     @Override
56     void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
57         final var tx = dataBroker.newReadWriteTransaction();
58         tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
59             @Override
60             public void onSuccess(final Boolean result) {
61                 if (!result) {
62                     cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
63                         ErrorTag.DATA_MISSING, path));
64                     return;
65                 }
66
67                 tx.delete(CONFIGURATION, path);
68                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
69                     @Override
70                     public void onSuccess(final CommitInfo result) {
71                         future.set(Empty.value());
72                     }
73
74                     @Override
75                     public void onFailure(final Throwable cause) {
76                         future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
77                             cause));
78                     }
79                 }, MoreExecutors.directExecutor());
80             }
81
82             @Override
83             public void onFailure(final Throwable cause) {
84                 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
85             }
86
87             private void cancelTx(final RestconfDocumentedException ex) {
88                 tx.cancel();
89                 future.setFailure(ex);
90             }
91         }, MoreExecutors.directExecutor());
92     }
93
94     @Override
95     ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
96             final YangInstanceIdentifier path) {
97         try (var tx = dataBroker.newReadOnlyTransaction()) {
98             return tx.read(store, path);
99         }
100     }
101
102     @Override
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));
107     }
108
109     @Override
110     ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
111         try (var tx = dataBroker.newReadOnlyTransaction()) {
112             return tx.exists(LogicalDatastoreType.CONFIGURATION, path);
113         }
114     }
115 }