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