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.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;
32 * Implementation of RESTCONF operations on top of a raw NETCONF backend.
34 * @see NetconfDataTreeService
36 public final class NetconfRestconfStrategy extends RestconfStrategy {
37 private final NetconfDataTreeService netconfService;
39 public NetconfRestconfStrategy(final EffectiveModelContext modelContext,
40 final NetconfDataTreeService netconfService, final @Nullable DOMRpcService rpcService) {
41 super(modelContext, rpcService);
42 this.netconfService = requireNonNull(netconfService);
46 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
47 final var tx = prepareWriteExecution();
49 Futures.addCallback(tx.commit(), new FutureCallback<CommitInfo>() {
51 public void onSuccess(final CommitInfo result) {
52 future.set(Empty.value());
56 public void onFailure(final Throwable cause) {
57 future.setFailure(TransactionUtil.decodeException(cause, "DELETE", path, modelContext()));
59 }, MoreExecutors.directExecutor());
63 RestconfTransaction prepareWriteExecution() {
64 return new NetconfRestconfTransaction(modelContext(), netconfService);
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);
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);
86 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
87 return Futures.transform(remapException(netconfService.getConfig(path)),
88 optionalNode -> optionalNode != null && optionalNode.isPresent(),
89 MoreExecutors.directExecutor());
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>() {
96 public void onSuccess(final T result) {
101 public void onFailure(final Throwable cause) {
102 ret.setException(cause instanceof ReadFailedException ? cause
103 : new ReadFailedException("NETCONF operation failed", cause));
105 }, MoreExecutors.directExecutor());