Fix Nested YANG 1.1 Action invocation
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / RestconfInvokeOperationsUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
9
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;
32
33 /**
34  * Util class for rpc.
35  *
36  */
37 public final class RestconfInvokeOperationsUtil {
38
39     private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
40
41     private RestconfInvokeOperationsUtil() {
42         throw new UnsupportedOperationException("Util class");
43     }
44
45     /**
46      * Invoking rpc via mount point.
47      *
48      * @param mountPoint
49      *             mount point
50      * @param data
51      *             input data
52      * @param schemaPath
53      *             schema path of data
54      * @return {@link DOMRpcResult}
55      */
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);
62         }
63         final String errmsg = "RPC service is missing.";
64         LOG.debug(errmsg);
65         throw new RestconfDocumentedException(errmsg);
66     }
67
68     /**
69      * Invoke rpc.
70      *
71      * @param data
72      *             input data
73      * @param schemaPath
74      *             schema path of data
75      * @param rpcServiceHandler
76      *             rpc service handler to invoke rpc
77      * @return {@link DOMRpcResult}
78      */
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);
84         }
85
86         final ListenableFuture<DOMRpcResult> rpc = rpcService.invokeRpc(schemaPath, data);
87         return prepareResult(rpc);
88     }
89
90     /**
91      * Check the validity of the result.
92      *
93      * @param response
94      *             response of rpc
95      * @return {@link DOMRpcResult} result
96      */
97     public static DOMRpcResult checkResponse(final DOMRpcResult response) {
98         if (response == null) {
99             return null;
100         }
101         try {
102             if (response.getErrors().isEmpty()) {
103                 return response;
104             }
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);
111         }
112     }
113
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();
118     }
119
120     /**
121      * Invoking Action via mount point.
122      *
123      * @param mountPoint
124      *             mount point
125      * @param data
126      *             input data
127      * @param schemaPath
128      *             schema path of data
129      * @return {@link DOMActionResult}
130      */
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.");
136         }
137         return prepareActionResult(mountPointService.get().invokeAction(schemaPath, prepareDataTreeId(yangIId), data));
138     }
139
140     /**
141      * Invoke Action via ActionServiceHandler.
142      *
143      * @param data
144      *             input data
145      * @param schemaPath
146      *             schema path of data
147      * @param actionServiceHandler
148      *             action service handler to invoke action
149      * @return {@link DOMActionResult}
150      */
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));
155     }
156
157     /**
158      * Check the validity of the result.
159      *
160      * @param response
161      *             response of Action
162      * @return {@link DOMActionResult} result
163      */
164     public static DOMActionResult checkActionResponse(final DOMActionResult response) {
165         if (response != null) {
166             try {
167                 if (response.getErrors().isEmpty()) {
168                     return response;
169                 }
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);
176             }
177         }
178         return null;
179     }
180
181     /**
182      * Prepare Action Result.
183      *
184      * @param actionResult
185      *            {@link DOMActionResult} - action result
186      * @return {@link DOMActionResult} result
187      */
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();
192     }
193
194     /**
195      * Prepare DOMDataTree Identifier.
196      *
197      * @param yangIId {@link YangInstanceIdentifier}
198      * @return {@link DOMDataTreeIdentifier} domDataTreeIdentifier
199      */
200     private static DOMDataTreeIdentifier prepareDataTreeId(final YangInstanceIdentifier yangIId) {
201         return new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent());
202     }
203 }