25594a778235df92c4480a548472aade462ea8be
[netconf.git] / restconf / restconf-nb-bierman02 / 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.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
20 import org.opendaylight.restconf.common.errors.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  * @deprecated move to splitted module restconf-nb-rfc8040
31  */
32 @Deprecated
33 public class RestconfInvokeOperationsUtil {
34
35     private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
36
37     private RestconfInvokeOperationsUtil() {
38         throw new UnsupportedOperationException("Util class");
39     }
40
41     /**
42      * Invoking rpc via mount point.
43      *
44      * @param mountPoint
45      *             mount point
46      * @param data
47      *             input data
48      * @param schemaPath
49      *             schema path of data
50      * @return {@link CheckedFuture}
51      */
52     public static DOMRpcResult invokeRpcViaMountPoint(final DOMMountPoint mountPoint, final NormalizedNode<?, ?> data,
53             final SchemaPath schemaPath) {
54         final Optional<DOMRpcService> mountPointService = mountPoint.getService(DOMRpcService.class);
55         if (mountPointService.isPresent()) {
56             final CheckedFuture<DOMRpcResult, DOMRpcException> rpc = mountPointService.get().invokeRpc(schemaPath,
57                     data);
58             return prepareResult(rpc);
59         }
60         final String errmsg = "RPC service is missing.";
61         LOG.debug(errmsg);
62         throw new RestconfDocumentedException(errmsg);
63     }
64
65     /**
66      * Invoke rpc.
67      *
68      * @param data
69      *             input data
70      * @param schemaPath
71      *             schema path of data
72      * @param rpcServiceHandler
73      *             rpc service handler to invoke rpc
74      * @return {@link CheckedFuture}
75      */
76     public static DOMRpcResult invokeRpc(final NormalizedNode<?, ?> data, final SchemaPath schemaPath,
77             final RpcServiceHandler rpcServiceHandler) {
78         final DOMRpcService rpcService = rpcServiceHandler.get();
79         if (rpcService == null) {
80             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
81         }
82
83         final CheckedFuture<DOMRpcResult, DOMRpcException> rpc = rpcService.invokeRpc(schemaPath, data);
84         return prepareResult(rpc);
85     }
86
87     /**
88      * Check the validity of the result.
89      *
90      * @param response
91      *             response of rpc
92      * @return {@link DOMRpcResult} result
93      */
94     public static DOMRpcResult checkResponse(final DOMRpcResult response) {
95         if (response == null) {
96             return null;
97         }
98         try {
99             if (response.getErrors().isEmpty()) {
100                 return response;
101             }
102             LOG.debug("RpcError message", response.getErrors());
103             throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
104         } catch (final CancellationException e) {
105             final String errMsg = "The operation was cancelled while executing.";
106             LOG.debug("Cancel RpcExecution: " + errMsg, e);
107             throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION);
108         }
109     }
110
111     private static DOMRpcResult prepareResult(final CheckedFuture<DOMRpcResult, DOMRpcException> rpc) {
112         final RpcResultFactory dataFactory = new RpcResultFactory();
113         FutureCallbackTx.addCallback(rpc, RestconfDataServiceConstant.PostData.POST_TX_TYPE, dataFactory);
114         return dataFactory.build();
115     }
116 }