Bind RestconfStrategy to EffectiveModelContext
[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 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
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 EffectiveModelContext modelContext,
38             final NetconfDataTreeService netconfService) {
39         super(modelContext);
40         this.netconfService = requireNonNull(netconfService);
41     }
42
43     @Override
44     void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
45         final var tx = prepareWriteExecution();
46         tx.delete(path);
47         Futures.addCallback(tx.commit(), new FutureCallback<CommitInfo>() {
48             @Override
49             public void onSuccess(final CommitInfo result) {
50                 future.set(Empty.value());
51             }
52
53             @Override
54             public void onFailure(final Throwable cause) {
55                 future.setFailure(TransactionUtil.decodeException(cause, "DELETE", path));
56             }
57         }, MoreExecutors.directExecutor());
58     }
59
60     @Override
61     RestconfTransaction prepareWriteExecution() {
62         return new NetconfRestconfTransaction(modelContext(), netconfService);
63     }
64
65     @Override
66     ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
67             final YangInstanceIdentifier path) {
68         return switch (store) {
69             case CONFIGURATION -> netconfService.getConfig(path);
70             case OPERATIONAL -> netconfService.get(path);
71         };
72     }
73
74     @Override
75     ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
76             final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
77         return switch (store) {
78             case CONFIGURATION -> netconfService.getConfig(path, fields);
79             case OPERATIONAL -> netconfService.get(path, fields);
80         };
81     }
82
83     @Override
84     ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
85         return Futures.transform(remapException(netconfService.getConfig(path)),
86             optionalNode -> optionalNode != null && optionalNode.isPresent(),
87             MoreExecutors.directExecutor());
88     }
89
90     private static <T> ListenableFuture<T> remapException(final ListenableFuture<T> input) {
91         final var ret = SettableFuture.<T>create();
92         Futures.addCallback(input, new FutureCallback<T>() {
93             @Override
94             public void onSuccess(final T result) {
95                 ret.set(result);
96             }
97
98             @Override
99             public void onFailure(final Throwable cause) {
100                 ret.setException(cause instanceof ReadFailedException ? cause
101                     : new ReadFailedException("NETCONF operation failed", cause));
102             }
103         }, MoreExecutors.directExecutor());
104         return ret;
105     }
106 }