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