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