2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
10 import static java.util.Objects.requireNonNull;
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.restconf.nb.rfc8040.rests.utils.TransactionUtil;
25 import org.opendaylight.yangtools.yang.common.Empty;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 * Implementation of RESTCONF operations on top of a raw NETCONF backend.
32 * @see NetconfDataTreeService
34 public final class NetconfRestconfStrategy extends RestconfStrategy {
35 private final NetconfDataTreeService netconfService;
37 public NetconfRestconfStrategy(final NetconfDataTreeService netconfService) {
38 this.netconfService = requireNonNull(netconfService);
42 protected void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
43 final var tx = prepareWriteExecution();
45 Futures.addCallback(tx.commit(), new FutureCallback<CommitInfo>() {
47 public void onSuccess(final CommitInfo result) {
48 future.set(Empty.value());
52 public void onFailure(final Throwable cause) {
53 future.setFailure(TransactionUtil.decodeException(cause, "DELETE", path));
55 }, MoreExecutors.directExecutor());
59 public RestconfTransaction prepareWriteExecution() {
60 return new NetconfRestconfTransaction(netconfService);
64 public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
65 final YangInstanceIdentifier path) {
66 return switch (store) {
67 case CONFIGURATION -> netconfService.getConfig(path);
68 case OPERATIONAL -> netconfService.get(path);
73 public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
74 final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
75 return switch (store) {
76 case CONFIGURATION -> netconfService.getConfig(path, fields);
77 case OPERATIONAL -> netconfService.get(path, fields);
82 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
83 return Futures.transform(remapException(netconfService.getConfig(path)),
84 optionalNode -> optionalNode != null && optionalNode.isPresent(),
85 MoreExecutors.directExecutor());
88 private static <T> ListenableFuture<T> remapException(final ListenableFuture<T> input) {
89 final var ret = SettableFuture.<T>create();
90 Futures.addCallback(input, new FutureCallback<T>() {
92 public void onSuccess(final T result) {
97 public void onFailure(final Throwable cause) {
98 ret.setException(cause instanceof ReadFailedException ? cause
99 : new ReadFailedException("NETCONF operation failed", cause));
101 }, MoreExecutors.directExecutor());