2 * Copyright (c) 2016 Cisco Systems, Inc. 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.utils;
10 import com.google.common.base.Throwables;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.concurrent.CancellationException;
16 import java.util.concurrent.ExecutionException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMActionException;
20 import org.opendaylight.mdsal.dom.api.DOMActionResult;
21 import org.opendaylight.mdsal.dom.api.DOMActionService;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
23 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.mdsal.dom.api.DOMRpcException;
25 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
26 import org.opendaylight.mdsal.dom.api.DOMRpcService;
27 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.yangtools.yang.common.ErrorTag;
31 import org.opendaylight.yangtools.yang.common.ErrorType;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.opendaylight.yangtools.yang.common.YangConstants;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
47 public final class RestconfInvokeOperationsUtil {
48 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
50 private RestconfInvokeOperationsUtil() {
55 * Invoking rpc via mount point.
57 * @param mountPoint mount point
58 * @param data input data
60 * @return {@link DOMRpcResult}
62 // FIXME: NETCONF-718: we should be returning a future here
63 public static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMMountPoint mountPoint) {
64 return invokeRpc(data, rpc, mountPoint.getService(DOMRpcService.class).orElseThrow(() -> {
65 final String errmsg = "RPC service is missing.";
67 return new RestconfDocumentedException(errmsg);
74 * @param data input data
76 * @param rpcService rpc service to invoke rpc
77 * @return {@link DOMRpcResult}
79 // FIXME: NETCONF-718: we should be returning a future here
80 public static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMRpcService rpcService) {
81 return checkedGet(Futures.catching(
82 rpcService.invokeRpc(rpc, nonnullInput(rpc, data)), DOMRpcException.class,
83 cause -> new DefaultDOMRpcResult(ImmutableList.of(RpcResultBuilder.newError(
84 RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))),
85 MoreExecutors.directExecutor()));
88 private static @NonNull NormalizedNode nonnullInput(final QName type, final NormalizedNode input) {
89 return input != null ? input
90 : ImmutableNodes.containerNode(YangConstants.operationInputQName(type.getModule()));
94 * Check the validity of the result.
96 * @param response response of rpc
97 * @return {@link DOMRpcResult} result
99 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
100 if (response == null) {
104 if (response.getErrors().isEmpty()) {
107 LOG.debug("RpcError message {}", response.getErrors());
108 throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
109 } catch (final CancellationException e) {
110 final String errMsg = "The operation was cancelled while executing.";
111 LOG.debug("Cancel RpcExecution: {}", errMsg, e);
112 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
117 * Invoking Action via mount point.
119 * @param mountPoint mount point
120 * @param data input data
121 * @param schemaPath schema path of data
122 * @return {@link DOMActionResult}
124 public static DOMActionResult invokeAction(final ContainerNode data,
125 final Absolute schemaPath, final YangInstanceIdentifier yangIId, final DOMMountPoint mountPoint) {
126 return invokeAction(data, schemaPath, yangIId, mountPoint.getService(DOMActionService.class)
127 .orElseThrow(() -> new RestconfDocumentedException("DomAction service is missing.")));
131 * Invoke Action via ActionServiceHandler.
133 * @param data input data
134 * @param yangIId invocation context
135 * @param schemaPath schema path of data
136 * @param actionService action service to invoke action
137 * @return {@link DOMActionResult}
139 // FIXME: NETCONF-718: we should be returning a future here
140 public static DOMActionResult invokeAction(final ContainerNode data, final Absolute schemaPath,
141 final YangInstanceIdentifier yangIId, final DOMActionService actionService) {
142 return checkedGet(Futures.catching(actionService.invokeAction(
143 schemaPath, new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data),
144 DOMActionException.class,
145 cause -> new SimpleDOMActionResult(ImmutableList.of(RpcResultBuilder.newError(
146 RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))),
147 MoreExecutors.directExecutor()));
151 private static <T> T checkedGet(final ListenableFuture<T> future) {
154 } catch (InterruptedException e) {
155 throw new RestconfDocumentedException("Interrupted while waiting for result of invocation", e);
156 } catch (ExecutionException e) {
157 final Throwable cause = e.getCause();
158 Throwables.throwIfInstanceOf(cause, RestconfDocumentedException.class);
159 throw new RestconfDocumentedException("Invocation failed", e);
164 * Check the validity of the result.
168 * @return {@link DOMActionResult} result
170 public static DOMActionResult checkActionResponse(final DOMActionResult response) {
171 if (response != null) {
173 if (response.getErrors().isEmpty()) {
176 LOG.debug("InvokeAction Error Message {}", response.getErrors());
177 throw new RestconfDocumentedException("InvokeAction Error Message ", null, response.getErrors());
178 } catch (final CancellationException e) {
179 final String errMsg = "The Action Operation was cancelled while executing.";
180 LOG.debug("Cancel Execution: {}", errMsg, e);
181 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);