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