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.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.concurrent.CancellationException;
15 import javax.ws.rs.core.Response.Status;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMActionResult;
18 import org.opendaylight.mdsal.dom.api.DOMActionService;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
20 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
21 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
22 import org.opendaylight.mdsal.dom.api.DOMRpcService;
23 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
24 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
25 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.ActionServiceHandler;
27 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.model.api.SchemaPath;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
40 public final class RestconfInvokeOperationsUtil {
42 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
44 private RestconfInvokeOperationsUtil() {
45 throw new UnsupportedOperationException("Util class");
49 * Invoking rpc via mount point.
57 * @return {@link DOMRpcResult}
59 public static DOMRpcResult invokeRpcViaMountPoint(final DOMMountPoint mountPoint, final NormalizedNode<?, ?> data,
60 final SchemaPath schemaPath) {
61 final Optional<DOMRpcService> mountPointService = mountPoint.getService(DOMRpcService.class);
62 if (mountPointService.isPresent()) {
63 final ListenableFuture<DOMRpcResult> rpc = mountPointService.get().invokeRpc(schemaPath, data);
64 return prepareResult(rpc);
66 final String errmsg = "RPC service is missing.";
68 throw new RestconfDocumentedException(errmsg);
78 * @param rpcServiceHandler
79 * rpc service handler to invoke rpc
80 * @return {@link DOMRpcResult}
82 public static DOMRpcResult invokeRpc(final NormalizedNode<?, ?> data, final SchemaPath schemaPath,
83 final RpcServiceHandler rpcServiceHandler) {
84 final DOMRpcService rpcService = rpcServiceHandler.get();
85 if (rpcService == null) {
86 throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
89 final ListenableFuture<DOMRpcResult> rpc = rpcService.invokeRpc(schemaPath, data);
90 return prepareResult(rpc);
94 * Check the validity of the result.
98 * @return {@link DOMRpcResult} result
100 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
101 if (response == null) {
105 if (response.getErrors().isEmpty()) {
108 LOG.debug("RpcError message {}", response.getErrors());
109 throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
110 } catch (final CancellationException e) {
111 final String errMsg = "The operation was cancelled while executing.";
112 LOG.debug("Cancel RpcExecution: {}", errMsg, e);
113 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
117 private static DOMRpcResult prepareResult(final ListenableFuture<DOMRpcResult> rpc) {
118 final RpcResultFactory dataFactory = new RpcResultFactory();
119 FutureCallbackTx.addCallback(rpc, RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
120 return dataFactory.build();
124 * Invoking Action via mount point.
131 * schema path of data
132 * @return {@link DOMActionResult}
134 public static DOMActionResult invokeActionViaMountPoint(final DOMMountPoint mountPoint, final ContainerNode data,
135 final SchemaPath schemaPath, final YangInstanceIdentifier yangIId) {
136 final Optional<DOMActionService> mountPointService = mountPoint.getService(DOMActionService.class);
137 if (!mountPointService.isPresent()) {
138 throw new RestconfDocumentedException("DomAction service is missing.");
141 return prepareActionResult(mountPointService.get().invokeAction(schemaPath,
142 prepareDataTreeId(yangIId, schemaPath), 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 SchemaPath schemaPath,
157 final ActionServiceHandler actionServiceHandler, final YangInstanceIdentifier yangIId) {
158 return prepareActionResult(actionServiceHandler.get().invokeAction(schemaPath,
159 prepareDataTreeId(yangIId, schemaPath), 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, RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
196 return dataFactory.build();
200 * Prepare DOMDataTree Identifier.
203 * {@link YangInstanceIdentifier}
206 * @return {@link DOMDataTreeIdentifier} domDataTreeIdentifier
208 private static DOMDataTreeIdentifier prepareDataTreeId(final YangInstanceIdentifier yangIId,
209 final SchemaPath schemaPath) {
210 final List<PathArgument> pathArg = new ArrayList<>();
211 for (PathArgument path : yangIId.getPathArguments()) {
212 if (path.getNodeType().getLocalName().equals(schemaPath.getLastComponent().getLocalName())) {
217 YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.builder().append(pathArg).build();
218 DOMDataTreeIdentifier domDataTreeIdentifier = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
219 yangInstanceIdentifier);
220 return domDataTreeIdentifier;