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.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.mdsal.dom.api.DOMActionResult;
16 import org.opendaylight.mdsal.dom.api.DOMActionService;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
19 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
20 import org.opendaylight.mdsal.dom.api.DOMRpcService;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
23 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
24 import org.opendaylight.restconf.nb.rfc8040.handlers.ActionServiceHandler;
25 import org.opendaylight.restconf.nb.rfc8040.handlers.RpcServiceHandler;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
37 public final class RestconfInvokeOperationsUtil {
39 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
41 private RestconfInvokeOperationsUtil() {
42 throw new UnsupportedOperationException("Util class");
46 * Invoking rpc via mount point.
54 * @return {@link DOMRpcResult}
56 public static DOMRpcResult invokeRpcViaMountPoint(final DOMMountPoint mountPoint, final NormalizedNode<?, ?> data,
57 final SchemaPath schemaPath) {
58 final Optional<DOMRpcService> mountPointService = mountPoint.getService(DOMRpcService.class);
59 if (mountPointService.isPresent()) {
60 final ListenableFuture<DOMRpcResult> rpc = mountPointService.get().invokeRpc(schemaPath, data);
61 return prepareResult(rpc);
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 final ListenableFuture<DOMRpcResult> rpc = rpcService.invokeRpc(schemaPath, data);
87 return prepareResult(rpc);
91 * Check the validity of the result.
95 * @return {@link DOMRpcResult} result
97 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
98 if (response == null) {
102 if (response.getErrors().isEmpty()) {
105 LOG.debug("RpcError message {}", response.getErrors());
106 throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
107 } catch (final CancellationException e) {
108 final String errMsg = "The operation was cancelled while executing.";
109 LOG.debug("Cancel RpcExecution: {}", errMsg, e);
110 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
114 private static DOMRpcResult prepareResult(final ListenableFuture<DOMRpcResult> rpc) {
115 final RpcResultFactory dataFactory = new RpcResultFactory();
116 FutureCallbackTx.addCallback(rpc, RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
117 return dataFactory.build();
121 * Invoking Action via mount point.
128 * schema path of data
129 * @return {@link DOMActionResult}
131 public static DOMActionResult invokeActionViaMountPoint(final DOMMountPoint mountPoint, final ContainerNode data,
132 final SchemaPath schemaPath, final YangInstanceIdentifier yangIId) {
133 final Optional<DOMActionService> mountPointService = mountPoint.getService(DOMActionService.class);
134 if (!mountPointService.isPresent()) {
135 throw new RestconfDocumentedException("DomAction service is missing.");
137 return prepareActionResult(mountPointService.get().invokeAction(schemaPath, prepareDataTreeId(yangIId), data));
141 * Invoke Action via ActionServiceHandler.
146 * schema path of data
147 * @param actionServiceHandler
148 * action service handler to invoke action
149 * @return {@link DOMActionResult}
151 public static DOMActionResult invokeAction(final ContainerNode data, final SchemaPath schemaPath,
152 final ActionServiceHandler actionServiceHandler, final YangInstanceIdentifier yangIId) {
153 return prepareActionResult(
154 actionServiceHandler.get().invokeAction(schemaPath, prepareDataTreeId(yangIId), data));
158 * Check the validity of the result.
162 * @return {@link DOMActionResult} result
164 public static DOMActionResult checkActionResponse(final DOMActionResult response) {
165 if (response != null) {
167 if (response.getErrors().isEmpty()) {
170 LOG.debug("InvokeAction Error Message {}", response.getErrors());
171 throw new RestconfDocumentedException("InvokeAction Error Message ", null, response.getErrors());
172 } catch (final CancellationException e) {
173 final String errMsg = "The Action Operation was cancelled while executing.";
174 LOG.debug("Cancel Execution: {}", errMsg, e);
175 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
182 * Prepare Action Result.
184 * @param actionResult
185 * {@link DOMActionResult} - action result
186 * @return {@link DOMActionResult} result
188 private static DOMActionResult prepareActionResult(final ListenableFuture<? extends DOMActionResult> actionResult) {
189 final ActionResultFactory dataFactory = new ActionResultFactory();
190 FutureCallbackTx.addCallback(actionResult, RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
191 return dataFactory.build();
195 * Prepare DOMDataTree Identifier.
197 * @param yangIId {@link YangInstanceIdentifier}
198 * @return {@link DOMDataTreeIdentifier} domDataTreeIdentifier
200 private static DOMDataTreeIdentifier prepareDataTreeId(final YangInstanceIdentifier yangIId) {
201 return new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent());