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.util.concurrent.ListenableFuture;
11 import java.util.concurrent.CancellationException;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.mdsal.dom.api.DOMActionResult;
15 import org.opendaylight.mdsal.dom.api.DOMActionService;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.mdsal.dom.api.DOMRpcService;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.common.ErrorType;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.YangConstants;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
36 public final class RestconfInvokeOperationsUtil {
37 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
39 private RestconfInvokeOperationsUtil() {
44 * Invoking rpc via mount point.
52 * @return {@link DOMRpcResult}
54 public static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMMountPoint mountPoint) {
55 return invokeRpc(data, rpc, mountPoint.getService(DOMRpcService.class).orElseThrow(() -> {
56 final String errmsg = "RPC service is missing.";
58 return new RestconfDocumentedException(errmsg);
70 * rpc service to invoke rpc
71 * @return {@link DOMRpcResult}
73 public static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMRpcService rpcService) {
74 final ListenableFuture<? extends DOMRpcResult> future = rpcService.invokeRpc(rpc, nonnullInput(rpc, data));
75 final RpcResultFactory dataFactory = new RpcResultFactory();
76 FutureCallbackTx.addCallback(future, PostDataTransactionUtil.POST_TX_TYPE, dataFactory);
77 return dataFactory.build();
80 private static @NonNull NormalizedNode nonnullInput(final QName type, final NormalizedNode input) {
81 return input != null ? input
82 : ImmutableNodes.containerNode(YangConstants.operationInputQName(type.getModule()));
86 * Check the validity of the result.
90 * @return {@link DOMRpcResult} result
92 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
93 if (response == null) {
97 if (response.getErrors().isEmpty()) {
100 LOG.debug("RpcError message {}", response.getErrors());
101 throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
102 } catch (final CancellationException e) {
103 final String errMsg = "The operation was cancelled while executing.";
104 LOG.debug("Cancel RpcExecution: {}", errMsg, e);
105 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
110 * Invoking Action via mount point.
117 * schema path of data
118 * @return {@link DOMActionResult}
120 public static DOMActionResult invokeAction(final ContainerNode data,
121 final Absolute schemaPath, final YangInstanceIdentifier yangIId, final DOMMountPoint mountPoint) {
122 return invokeAction(data, schemaPath, yangIId, mountPoint.getService(DOMActionService.class)
123 .orElseThrow(() -> new RestconfDocumentedException("DomAction service is missing.")));
127 * Invoke Action via ActionServiceHandler.
132 * schema path of data
133 * @param actionService
134 * action service to invoke action
135 * @return {@link DOMActionResult}
137 public static DOMActionResult invokeAction(final ContainerNode data, final Absolute schemaPath,
138 final YangInstanceIdentifier yangIId, final DOMActionService actionService) {
139 final ListenableFuture<? extends DOMActionResult> future = actionService.invokeAction(schemaPath,
140 new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data);
141 final ActionResultFactory dataFactory = new ActionResultFactory();
142 FutureCallbackTx.addCallback(future, PostDataTransactionUtil.POST_TX_TYPE, dataFactory);
143 return dataFactory.build();
147 * Check the validity of the result.
151 * @return {@link DOMActionResult} result
153 public static DOMActionResult checkActionResponse(final DOMActionResult response) {
154 if (response != null) {
156 if (response.getErrors().isEmpty()) {
159 LOG.debug("InvokeAction Error Message {}", response.getErrors());
160 throw new RestconfDocumentedException("InvokeAction Error Message ", null, response.getErrors());
161 } catch (final CancellationException e) {
162 final String errMsg = "The Action Operation was cancelled while executing.";
163 LOG.debug("Cancel Execution: {}", errMsg, e);
164 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);