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