Eliminate PostDataTransactionUtil
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / NetconfRestconfStrategy.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
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.List;
18 import java.util.Optional;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.common.api.ReadFailedException;
22 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
23 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
24 import org.opendaylight.restconf.nb.rfc8040.rests.utils.TransactionUtil;
25 import org.opendaylight.yangtools.yang.common.Empty;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28
29 /**
30  * Implementation of RESTCONF operations on top of a raw NETCONF backend.
31  *
32  * @see NetconfDataTreeService
33  */
34 public final class NetconfRestconfStrategy extends RestconfStrategy {
35     private final NetconfDataTreeService netconfService;
36
37     public NetconfRestconfStrategy(final NetconfDataTreeService netconfService) {
38         this.netconfService = requireNonNull(netconfService);
39     }
40
41     @Override
42     protected void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
43         final var tx = prepareWriteExecution();
44         tx.delete(path);
45         Futures.addCallback(tx.commit(), new FutureCallback<CommitInfo>() {
46             @Override
47             public void onSuccess(final CommitInfo result) {
48                 future.set(Empty.value());
49             }
50
51             @Override
52             public void onFailure(final Throwable cause) {
53                 future.setFailure(TransactionUtil.decodeException(cause, "DELETE", path));
54             }
55         }, MoreExecutors.directExecutor());
56     }
57
58     @Override
59     public RestconfTransaction prepareWriteExecution() {
60         return new NetconfRestconfTransaction(netconfService);
61     }
62
63     @Override
64     public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
65             final YangInstanceIdentifier path) {
66         return switch (store) {
67             case CONFIGURATION -> netconfService.getConfig(path);
68             case OPERATIONAL -> netconfService.get(path);
69         };
70     }
71
72     @Override
73     public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
74             final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
75         return switch (store) {
76             case CONFIGURATION -> netconfService.getConfig(path, fields);
77             case OPERATIONAL -> netconfService.get(path, fields);
78         };
79     }
80
81     @Override
82     ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
83         return Futures.transform(remapException(netconfService.getConfig(path)),
84             optionalNode -> optionalNode != null && optionalNode.isPresent(),
85             MoreExecutors.directExecutor());
86     }
87
88     private static <T> ListenableFuture<T> remapException(final ListenableFuture<T> input) {
89         final var ret = SettableFuture.<T>create();
90         Futures.addCallback(input, new FutureCallback<T>() {
91             @Override
92             public void onSuccess(final T result) {
93                 ret.set(result);
94             }
95
96             @Override
97             public void onFailure(final Throwable cause) {
98                 ret.setException(cause instanceof ReadFailedException ? cause
99                     : new ReadFailedException("NETCONF operation failed", cause));
100             }
101         }, MoreExecutors.directExecutor());
102         return ret;
103     }
104 }