Bug 5528 - Invoke RPC impl
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / 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.restful.utils;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import java.util.concurrent.CancellationException;
13 import javax.ws.rs.core.Response.Status;
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
18 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
21 import org.opendaylight.restconf.handlers.RpcServiceHandler;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Util class for rpc
29  *
30  */
31 public class RestconfInvokeOperationsUtil {
32
33     private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
34
35     private RestconfInvokeOperationsUtil() {
36         throw new UnsupportedOperationException("Util class");
37     }
38
39     /**
40      * Invoking rpc via mount point
41      *
42      * @param mountPoint
43      *            - mount point
44      * @param data
45      *            - input data
46      * @param schemaPath
47      *            - schema path of data
48      * @return {@link CheckedFuture}
49      */
50     public static DOMRpcResult invokeRpcViaMountPoint(final DOMMountPoint mountPoint, final NormalizedNode<?, ?> data,
51             final SchemaPath schemaPath) {
52         final Optional<DOMRpcService> mountPointService = mountPoint.getService(DOMRpcService.class);
53         if (mountPointService.isPresent()) {
54             final CheckedFuture<DOMRpcResult, DOMRpcException> rpc = mountPointService.get().invokeRpc(schemaPath,
55                     data);
56             return prepareResult(rpc);
57         }
58         final String errmsg = "RPC service is missing.";
59         LOG.debug(errmsg);
60         throw new RestconfDocumentedException(errmsg);
61     }
62
63     /**
64      * Invoke rpc
65      *
66      * @param data
67      *            - input data
68      * @param schemaPath
69      *            - schema path of data
70      * @param rpcServiceHandler
71      *            - rpc service handler to invoke rpc
72      * @return {@link CheckedFuture}
73      */
74     public static DOMRpcResult invokeRpc(final NormalizedNode<?, ?> data, final SchemaPath schemaPath,
75             final RpcServiceHandler rpcServiceHandler) {
76         final DOMRpcService rpcService = rpcServiceHandler.get();
77         if (rpcService == null) {
78             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
79         }
80
81         final CheckedFuture<DOMRpcResult, DOMRpcException> rpc = rpcService.invokeRpc(schemaPath, data);
82         return prepareResult(rpc);
83     }
84
85     /**
86      * Check the validity of the result
87      *
88      * @param response
89      *            - response of rpc
90      * @return {@link DOMRpcResult} result
91      */
92     public static DOMRpcResult checkResponse(final DOMRpcResult response) {
93         if (response == null) {
94             return null;
95         }
96         try {
97             if ((response.getErrors() == null) || response.getErrors().isEmpty()) {
98                 return response;
99             }
100             LOG.debug("RpcError message", response.getErrors());
101             throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
102         } catch (final CancellationException e) {
103             final String errMsg = "The operation was cancelled while executing.";
104             LOG.debug("Cancel RpcExecution: " + errMsg, e);
105             throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION);
106         }
107     }
108
109     private static DOMRpcResult prepareResult(final CheckedFuture<DOMRpcResult, DOMRpcException> rpc) {
110         final RpcResultFactory dataFactory = new RpcResultFactory();
111         FutureCallbackTx.addCallback(rpc, RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
112         return dataFactory.build();
113     }
114 }