/* * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.restconf.nb.rfc8040.rests.transactions; import static java.util.Objects.requireNonNull; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.SettableFuture; import java.util.List; import java.util.Optional; import org.opendaylight.mdsal.common.api.CommitInfo; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.mdsal.common.api.ReadFailedException; import org.opendaylight.netconf.dom.api.NetconfDataTreeService; import org.opendaylight.restconf.common.errors.SettableRestconfFuture; import org.opendaylight.restconf.nb.rfc8040.rests.utils.TransactionUtil; import org.opendaylight.yangtools.yang.common.Empty; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; /** * Implementation of RESTCONF operations on top of a raw NETCONF backend. * * @see NetconfDataTreeService */ public final class NetconfRestconfStrategy extends RestconfStrategy { private final NetconfDataTreeService netconfService; public NetconfRestconfStrategy(final NetconfDataTreeService netconfService) { this.netconfService = requireNonNull(netconfService); } @Override protected void delete(final SettableRestconfFuture future, final YangInstanceIdentifier path) { final var tx = prepareWriteExecution(); tx.delete(path); Futures.addCallback(tx.commit(), new FutureCallback() { @Override public void onSuccess(final CommitInfo result) { future.set(Empty.value()); } @Override public void onFailure(final Throwable cause) { future.setFailure(TransactionUtil.decodeException(cause, "DELETE", path)); } }, MoreExecutors.directExecutor()); } @Override public RestconfTransaction prepareWriteExecution() { return new NetconfRestconfTransaction(netconfService); } @Override public ListenableFuture> read(final LogicalDatastoreType store, final YangInstanceIdentifier path) { return switch (store) { case CONFIGURATION -> netconfService.getConfig(path); case OPERATIONAL -> netconfService.get(path); }; } @Override public ListenableFuture> read(final LogicalDatastoreType store, final YangInstanceIdentifier path, final List fields) { return switch (store) { case CONFIGURATION -> netconfService.getConfig(path, fields); case OPERATIONAL -> netconfService.get(path, fields); }; } @Override public ListenableFuture exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) { return Futures.transform(remapException(read(store, path)), optionalNode -> optionalNode != null && optionalNode.isPresent(), MoreExecutors.directExecutor()); } private static ListenableFuture remapException(final ListenableFuture input) { final var ret = SettableFuture.create(); Futures.addCallback(input, new FutureCallback() { @Override public void onSuccess(final T result) { ret.set(result); } @Override public void onFailure(final Throwable cause) { ret.setException(cause instanceof ReadFailedException ? cause : new ReadFailedException("NETCONF operation failed", cause)); } }, MoreExecutors.directExecutor()); return ret; } }