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.YangConstants;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
39 public final class RestconfInvokeOperationsUtil {
40 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
42 private RestconfInvokeOperationsUtil() {
43 throw new UnsupportedOperationException("Util class");
47 * Invoking rpc via mount point.
55 * @return {@link DOMRpcResult}
57 public static DOMRpcResult invokeRpcViaMountPoint(final DOMMountPoint mountPoint, final NormalizedNode<?, ?> data,
58 final SchemaPath schemaPath) {
59 final Optional<DOMRpcService> mountPointService = mountPoint.getService(DOMRpcService.class);
60 if (mountPointService.isPresent()) {
61 return prepareResult(mountPointService.get().invokeRpc(schemaPath, nonnullInput(schemaPath, data)));
63 final String errmsg = "RPC service is missing.";
65 throw new RestconfDocumentedException(errmsg);
75 * @param rpcServiceHandler
76 * rpc service handler to invoke rpc
77 * @return {@link DOMRpcResult}
79 public static DOMRpcResult invokeRpc(final NormalizedNode<?, ?> data, final SchemaPath schemaPath,
80 final RpcServiceHandler rpcServiceHandler) {
81 final DOMRpcService rpcService = rpcServiceHandler.get();
82 if (rpcService == null) {
83 throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
86 return prepareResult(rpcService.invokeRpc(schemaPath, nonnullInput(schemaPath, data)));
89 private static @NonNull NormalizedNode<?, ?> nonnullInput(final SchemaPath type, final NormalizedNode<?, ?> input) {
90 return input != null ? input
91 : ImmutableNodes.containerNode(YangConstants.operationInputQName(type.getLastComponent().getModule()));
95 * Check the validity of the result.
99 * @return {@link DOMRpcResult} result
101 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
102 if (response == null) {
106 if (response.getErrors().isEmpty()) {
109 LOG.debug("RpcError message {}", response.getErrors());
110 throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
111 } catch (final CancellationException e) {
112 final String errMsg = "The operation was cancelled while executing.";
113 LOG.debug("Cancel RpcExecution: {}", errMsg, e);
114 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
118 private static DOMRpcResult prepareResult(final ListenableFuture<? extends DOMRpcResult> rpc) {
119 final RpcResultFactory dataFactory = new RpcResultFactory();
120 FutureCallbackTx.addCallback(rpc, PostDataTransactionUtil.POST_TX_TYPE, dataFactory);
121 return dataFactory.build();
125 * Invoking Action via mount point.
132 * schema path of data
133 * @return {@link DOMActionResult}
135 public static DOMActionResult invokeActionViaMountPoint(final DOMMountPoint mountPoint, final ContainerNode data,
136 final SchemaPath schemaPath, final YangInstanceIdentifier yangIId) {
137 final Optional<DOMActionService> mountPointService = mountPoint.getService(DOMActionService.class);
138 if (!mountPointService.isPresent()) {
139 throw new RestconfDocumentedException("DomAction service is missing.");
141 return prepareActionResult(mountPointService.get().invokeAction(schemaPath, prepareDataTreeId(yangIId), data));
145 * Invoke Action via ActionServiceHandler.
150 * schema path of data
151 * @param actionServiceHandler
152 * action service handler to invoke action
153 * @return {@link DOMActionResult}
155 public static DOMActionResult invokeAction(final ContainerNode data, final SchemaPath schemaPath,
156 final ActionServiceHandler actionServiceHandler, final YangInstanceIdentifier yangIId) {
157 return prepareActionResult(
158 actionServiceHandler.get().invokeAction(schemaPath, prepareDataTreeId(yangIId), data));
162 * Check the validity of the result.
166 * @return {@link DOMActionResult} result
168 public static DOMActionResult checkActionResponse(final DOMActionResult response) {
169 if (response != null) {
171 if (response.getErrors().isEmpty()) {
174 LOG.debug("InvokeAction Error Message {}", response.getErrors());
175 throw new RestconfDocumentedException("InvokeAction Error Message ", null, response.getErrors());
176 } catch (final CancellationException e) {
177 final String errMsg = "The Action Operation was cancelled while executing.";
178 LOG.debug("Cancel Execution: {}", errMsg, e);
179 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
186 * Prepare Action Result.
188 * @param actionResult
189 * {@link DOMActionResult} - action result
190 * @return {@link DOMActionResult} result
192 private static DOMActionResult prepareActionResult(final ListenableFuture<? extends DOMActionResult> actionResult) {
193 final ActionResultFactory dataFactory = new ActionResultFactory();
194 FutureCallbackTx.addCallback(actionResult, PostDataTransactionUtil.POST_TX_TYPE, dataFactory);
195 return dataFactory.build();
199 * Prepare DOMDataTree Identifier.
201 * @param yangIId {@link YangInstanceIdentifier}
202 * @return {@link DOMDataTreeIdentifier} domDataTreeIdentifier
204 private static DOMDataTreeIdentifier prepareDataTreeId(final YangInstanceIdentifier yangIId) {
205 return new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent());