Shift ETag/Last-Modified generation to RestconfStrategy
[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.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.DOMTransactionChain;
27 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
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;
44
45 /**
46  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
47  *
48  * @see DOMTransactionChain
49  * @see DOMDataTreeReadWriteTransaction
50  */
51 public final class MdsalRestconfStrategy extends RestconfStrategy {
52     private final DOMDataBroker dataBroker;
53
54     public MdsalRestconfStrategy(final DatabindContext databind, final DOMDataBroker dataBroker,
55             final @Nullable DOMRpcService rpcService, final @Nullable DOMActionService actionService,
56             final @Nullable DOMYangTextSourceProvider 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);
61     }
62
63     public MdsalRestconfStrategy(final DatabindContext databind, final DOMDataBroker dataBroker,
64             final @Nullable DOMRpcService rpcService, final @Nullable DOMActionService actionService,
65             final @Nullable DOMYangTextSourceProvider sourceProvider,
66             final @Nullable DOMMountPointService mountPointService) {
67         this(databind, dataBroker, rpcService, actionService, sourceProvider, mountPointService, ImmutableMap.of());
68     }
69
70     @Override
71     RestconfTransaction prepareWriteExecution() {
72         return new MdsalRestconfTransaction(modelContext(), dataBroker);
73     }
74
75     @Override
76     void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
77         final var tx = dataBroker.newReadWriteTransaction();
78         tx.exists(CONFIGURATION, path).addCallback(new FutureCallback<>() {
79             @Override
80             public void onSuccess(final Boolean result) {
81                 if (!result) {
82                     cancelTx(new RestconfDocumentedException("Data does not exist", ErrorType.PROTOCOL,
83                         ErrorTag.DATA_MISSING, path));
84                     return;
85                 }
86
87                 tx.delete(CONFIGURATION, path);
88                 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
89                     @Override
90                     public void onSuccess(final CommitInfo result) {
91                         future.set(Empty.value());
92                     }
93
94                     @Override
95                     public void onFailure(final Throwable cause) {
96                         future.setFailure(new RestconfDocumentedException("Transaction to delete " + path + " failed",
97                             cause));
98                     }
99                 }, MoreExecutors.directExecutor());
100             }
101
102             @Override
103             public void onFailure(final Throwable cause) {
104                 cancelTx(new RestconfDocumentedException("Failed to access " + path, cause));
105             }
106
107             private void cancelTx(final RestconfDocumentedException ex) {
108                 tx.cancel();
109                 future.setFailure(ex);
110             }
111         }, MoreExecutors.directExecutor());
112     }
113
114     @Override
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.getEffectiveModelContext(), path.schema(), fields);
120         return completeDataGET(inference, QueryParameters.of(params, translatedFields),
121             readData(params.content(), path.instance(), params.withDefaults()), null);
122     }
123
124     @Override
125     ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
126             final YangInstanceIdentifier path) {
127         try (var tx = dataBroker.newReadOnlyTransaction()) {
128             return tx.read(store, path);
129         }
130     }
131
132     @Override
133     ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
134         try (var tx = dataBroker.newReadOnlyTransaction()) {
135             return tx.exists(LogicalDatastoreType.CONFIGURATION, path);
136         }
137     }
138 }