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.LogicalDatastoreType;
20 import org.opendaylight.mdsal.common.api.ReadFailedException;
21 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 * Implementation of RESTCONF operations on top of a raw NETCONF backend.
28 * @see NetconfDataTreeService
30 public final class NetconfRestconfStrategy extends RestconfStrategy {
31 private final NetconfDataTreeService netconfService;
33 public NetconfRestconfStrategy(final NetconfDataTreeService netconfService) {
34 this.netconfService = requireNonNull(netconfService);
38 public RestconfTransaction prepareWriteExecution() {
39 return new NetconfRestconfTransaction(netconfService);
43 public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
44 final YangInstanceIdentifier path) {
45 return switch (store) {
46 case CONFIGURATION -> netconfService.getConfig(path);
47 case OPERATIONAL -> netconfService.get(path);
52 public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
53 final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
54 return switch (store) {
55 case CONFIGURATION -> netconfService.getConfig(path, fields);
56 case OPERATIONAL -> netconfService.get(path, fields);
61 public ListenableFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
62 return Futures.transform(remapException(read(store, path)),
63 optionalNode -> optionalNode != null && optionalNode.isPresent(),
64 MoreExecutors.directExecutor());
67 private static <T> ListenableFuture<T> remapException(final ListenableFuture<T> input) {
68 final var ret = SettableFuture.<T>create();
69 Futures.addCallback(input, new FutureCallback<T>() {
71 public void onSuccess(final T result) {
76 public void onFailure(final Throwable cause) {
77 ret.setException(cause instanceof ReadFailedException ? cause
78 : new ReadFailedException("NETCONF operation failed", cause));
80 }, MoreExecutors.directExecutor());