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.FluentFuture;
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.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.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Implementation of RESTCONF operations on top of a raw NETCONF backend.
31 * @see NetconfDataTreeService
33 public final class NetconfRestconfStrategy extends RestconfStrategy {
34 private static final Logger LOG = LoggerFactory.getLogger(NetconfRestconfStrategy.class);
36 private final NetconfDataTreeService netconfService;
38 public NetconfRestconfStrategy(final NetconfDataTreeService netconfService) {
39 this.netconfService = requireNonNull(netconfService);
43 public RestconfTransaction prepareWriteExecution() {
44 return new NetconfRestconfTransaction(netconfService);
48 public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
49 final YangInstanceIdentifier path) {
52 return netconfService.getConfig(path);
54 return netconfService.get(path);
56 LOG.info("Unknown datastore type: {}.", store);
57 throw new IllegalArgumentException(String.format(
58 "%s, Cannot read data %s for %s datastore, unknown datastore type",
59 netconfService.getDeviceId(), path, store));
64 public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
65 final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
68 return netconfService.getConfig(path, fields);
70 return netconfService.get(path, fields);
72 LOG.info("Unknown datastore type: {}.", store);
73 throw new IllegalArgumentException(String.format(
74 "%s, Cannot read data %s with fields %s for %s datastore, unknown datastore type",
75 netconfService.getDeviceId(), path, fields, store));
80 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
81 return remapException(read(store, path))
82 .transform(optionalNode -> optionalNode != null && optionalNode.isPresent(),
83 MoreExecutors.directExecutor());
86 private static <T> FluentFuture<T> remapException(final ListenableFuture<T> input) {
87 final SettableFuture<T> ret = SettableFuture.create();
88 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());
101 return FluentFuture.from(ret);