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