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.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.mdsal.dom.api.DOMYangTextSourceProvider;
26 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
27 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
28 import org.opendaylight.restconf.server.api.DatabindContext;
29 import org.opendaylight.yangtools.yang.common.Empty;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 * Implementation of RESTCONF operations on top of a raw NETCONF backend.
36 * @see NetconfDataTreeService
38 public final class NetconfRestconfStrategy extends RestconfStrategy {
39 private final NetconfDataTreeService netconfService;
41 public NetconfRestconfStrategy(final DatabindContext databind, final NetconfDataTreeService netconfService,
42 final @Nullable DOMRpcService rpcService, final @Nullable DOMYangTextSourceProvider sourceProvider) {
43 super(databind, ImmutableMap.of(), rpcService, sourceProvider);
44 this.netconfService = requireNonNull(netconfService);
48 void delete(final SettableRestconfFuture<Empty> future, final YangInstanceIdentifier path) {
49 final var tx = prepareWriteExecution();
51 Futures.addCallback(tx.commit(), new FutureCallback<CommitInfo>() {
53 public void onSuccess(final CommitInfo result) {
54 future.set(Empty.value());
58 public void onFailure(final Throwable cause) {
59 future.setFailure(TransactionUtil.decodeException(cause, "DELETE", path, modelContext()));
61 }, MoreExecutors.directExecutor());
65 RestconfTransaction prepareWriteExecution() {
66 return new NetconfRestconfTransaction(modelContext(), netconfService);
70 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
71 final YangInstanceIdentifier path) {
72 return switch (store) {
73 case CONFIGURATION -> netconfService.getConfig(path);
74 case OPERATIONAL -> netconfService.get(path);
79 ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
80 final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
81 return switch (store) {
82 case CONFIGURATION -> netconfService.getConfig(path, fields);
83 case OPERATIONAL -> netconfService.get(path, fields);
88 ListenableFuture<Boolean> exists(final YangInstanceIdentifier path) {
89 return Futures.transform(remapException(netconfService.getConfig(path)),
90 optionalNode -> optionalNode != null && optionalNode.isPresent(),
91 MoreExecutors.directExecutor());
94 private static <T> ListenableFuture<T> remapException(final ListenableFuture<T> input) {
95 final var ret = SettableFuture.<T>create();
96 Futures.addCallback(input, new FutureCallback<T>() {
98 public void onSuccess(final T result) {
103 public void onFailure(final Throwable cause) {
104 ret.setException(cause instanceof ReadFailedException ? cause
105 : new ReadFailedException("NETCONF operation failed", cause));
107 }, MoreExecutors.directExecutor());