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.yangtools.yang.common.Empty;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 * Implementation of RESTCONF operations on top of a raw NETCONF backend.
31 * @see NetconfDataTreeService
33 public final class NetconfRestconfStrategy extends RestconfStrategy {
34 private final NetconfDataTreeService netconfService;
36 public NetconfRestconfStrategy(final NetconfDataTreeService netconfService) {
37 this.netconfService = requireNonNull(netconfService);
41 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
42 final var tx = prepareWriteExecution();
44 Futures.addCallback(tx.commit(), new FutureCallback<CommitInfo>() {
46 public void onSuccess(final CommitInfo result) {
47 future.set(Empty.value());
51 public void onFailure(final Throwable cause) {
52 future.setFailure(TransactionUtil.decodeException(cause, "DELETE", path));
54 }, MoreExecutors.directExecutor());
58 RestconfTransaction prepareWriteExecution() {
59 return new NetconfRestconfTransaction(netconfService);
63 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
64 final YangInstanceIdentifier path) {
65 return switch (store) {
66 case CONFIGURATION -> netconfService.getConfig(path);
67 case OPERATIONAL -> netconfService.get(path);
72 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
73 final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
74 return switch (store) {
75 case CONFIGURATION -> netconfService.getConfig(path, fields);
76 case OPERATIONAL -> netconfService.get(path, fields);
81 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
82 return Futures.transform(remapException(netconfService.getConfig(path)),
83 optionalNode -> optionalNode != null && optionalNode.isPresent(),
84 MoreExecutors.directExecutor());
87 private static <T> ListenableFuture<T> remapException(final ListenableFuture<T> input) {
88 final var ret = SettableFuture.<T>create();
89 Futures.addCallback(input, new FutureCallback<T>() {
91 public void onSuccess(final T result) {
96 public void onFailure(final Throwable cause) {
97 ret.setException(cause instanceof ReadFailedException ? cause
98 : new ReadFailedException("NETCONF operation failed", cause));
100 }, MoreExecutors.directExecutor());