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.Optional;
12 import java.util.concurrent.CancellationException;
13 import javax.ws.rs.core.Response.Status;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMActionResult;
17 import org.opendaylight.mdsal.dom.api.DOMActionService;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
19 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
23 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
24 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
25 import org.opendaylight.restconf.nb.rfc8040.handlers.ActionServiceHandler;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.YangConstants;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
40 public final class RestconfInvokeOperationsUtil {
41 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
43 private RestconfInvokeOperationsUtil() {
44 throw new UnsupportedOperationException("Util class");
48 * Invoking rpc via mount point.
56 * @return {@link DOMRpcResult}
58 public static DOMRpcResult invokeRpcViaMountPoint(final DOMMountPoint mountPoint, final NormalizedNode<?, ?> data,
59 final QName schemaPath) {
60 final Optional<DOMRpcService> mountPointService = mountPoint.getService(DOMRpcService.class);
61 if (mountPointService.isPresent()) {
62 return prepareResult(mountPointService.get().invokeRpc(schemaPath, nonnullInput(schemaPath, data)));
64 final String errmsg = "RPC service is missing.";
66 throw new RestconfDocumentedException(errmsg);
76 * @param rpcServiceHandler
77 * rpc service handler to invoke rpc
78 * @return {@link DOMRpcResult}
80 public static DOMRpcResult invokeRpc(final NormalizedNode<?, ?> data, final QName rpc,
81 final RpcServiceHandler rpcServiceHandler) {
82 final DOMRpcService rpcService = rpcServiceHandler.get();
83 if (rpcService == null) {
84 throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
87 return prepareResult(rpcService.invokeRpc(rpc, nonnullInput(rpc, data)));
90 private static @NonNull NormalizedNode<?, ?> nonnullInput(final QName type, final NormalizedNode<?, ?> input) {
91 return input != null ? input
92 : ImmutableNodes.containerNode(YangConstants.operationInputQName(type.getModule()));
96 * Check the validity of the result.
100 * @return {@link DOMRpcResult} result
102 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
103 if (response == null) {
107 if (response.getErrors().isEmpty()) {
110 LOG.debug("RpcError message {}", response.getErrors());
111 throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
112 } catch (final CancellationException e) {
113 final String errMsg = "The operation was cancelled while executing.";
114 LOG.debug("Cancel RpcExecution: {}", errMsg, e);
115 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
119 private static DOMRpcResult prepareResult(final ListenableFuture<? extends DOMRpcResult> rpc) {
120 final RpcResultFactory dataFactory = new RpcResultFactory();
121 FutureCallbackTx.addCallback(rpc, PostDataTransactionUtil.POST_TX_TYPE, dataFactory);
122 return dataFactory.build();
126 * Invoking Action via mount point.
133 * schema path of data
134 * @return {@link DOMActionResult}
136 public static DOMActionResult invokeActionViaMountPoint(final DOMMountPoint mountPoint, final ContainerNode data,
137 final Absolute schemaPath, final YangInstanceIdentifier yangIId) {
138 final Optional<DOMActionService> mountPointService = mountPoint.getService(DOMActionService.class);
139 if (mountPointService.isEmpty()) {
140 throw new RestconfDocumentedException("DomAction service is missing.");
142 return prepareActionResult(mountPointService.get().invokeAction(schemaPath, prepareDataTreeId(yangIId), data));
146 * Invoke Action via ActionServiceHandler.
151 * schema path of data
152 * @param actionServiceHandler
153 * action service handler to invoke action
154 * @return {@link DOMActionResult}
156 public static DOMActionResult invokeAction(final ContainerNode data, final Absolute schemaPath,
157 final ActionServiceHandler actionServiceHandler, final YangInstanceIdentifier yangIId) {
158 return prepareActionResult(
159 actionServiceHandler.get().invokeAction(schemaPath, prepareDataTreeId(yangIId), data));
163 * Check the validity of the result.
167 * @return {@link DOMActionResult} result
169 public static DOMActionResult checkActionResponse(final DOMActionResult response) {
170 if (response != null) {
172 if (response.getErrors().isEmpty()) {
175 LOG.debug("InvokeAction Error Message {}", response.getErrors());
176 throw new RestconfDocumentedException("InvokeAction Error Message ", null, response.getErrors());
177 } catch (final CancellationException e) {
178 final String errMsg = "The Action Operation was cancelled while executing.";
179 LOG.debug("Cancel Execution: {}", errMsg, e);
180 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
187 * Prepare Action Result.
189 * @param actionResult
190 * {@link DOMActionResult} - action result
191 * @return {@link DOMActionResult} result
193 private static DOMActionResult prepareActionResult(final ListenableFuture<? extends DOMActionResult> actionResult) {
194 final ActionResultFactory dataFactory = new ActionResultFactory();
195 FutureCallbackTx.addCallback(actionResult, PostDataTransactionUtil.POST_TX_TYPE, dataFactory);
196 return dataFactory.build();
200 * Prepare DOMDataTree Identifier.
202 * @param yangIId {@link YangInstanceIdentifier}
203 * @return {@link DOMDataTreeIdentifier} domDataTreeIdentifier
205 private static DOMDataTreeIdentifier prepareDataTreeId(final YangInstanceIdentifier yangIId) {
206 return new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent());