Remove RestconfInvokeOperationsUtilTest
[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.base.Throwables;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.concurrent.CancellationException;
16 import java.util.concurrent.ExecutionException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMActionException;
20 import org.opendaylight.mdsal.dom.api.DOMActionResult;
21 import org.opendaylight.mdsal.dom.api.DOMActionService;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
23 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.mdsal.dom.api.DOMRpcException;
25 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
26 import org.opendaylight.mdsal.dom.api.DOMRpcService;
27 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.yangtools.yang.common.ErrorTag;
31 import org.opendaylight.yangtools.yang.common.ErrorType;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.opendaylight.yangtools.yang.common.YangConstants;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Util class for rpc.
46  */
47 public final class RestconfInvokeOperationsUtil {
48     private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
49
50     private RestconfInvokeOperationsUtil() {
51         // Hidden on purpose
52     }
53
54     /**
55      * Invoking rpc via mount point.
56      *
57      * @param mountPoint mount point
58      * @param data input data
59      * @param rpc RPC type
60      * @return {@link DOMRpcResult}
61      */
62     // FIXME: NETCONF-718: we should be returning a future here
63     public static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMMountPoint mountPoint) {
64         return invokeRpc(data, rpc, mountPoint.getService(DOMRpcService.class).orElseThrow(() -> {
65             final String errmsg = "RPC service is missing.";
66             LOG.debug(errmsg);
67             return new RestconfDocumentedException(errmsg);
68         }));
69     }
70
71     /**
72      * Invoke rpc.
73      *
74      * @param data input data
75      * @param rpc RPC type
76      * @param rpcService rpc service to invoke rpc
77      * @return {@link DOMRpcResult}
78      */
79     // FIXME: NETCONF-718: we should be returning a future here
80     public static DOMRpcResult invokeRpc(final NormalizedNode data, final QName rpc, final DOMRpcService rpcService) {
81         return checkedGet(Futures.catching(
82             rpcService.invokeRpc(rpc, nonnullInput(rpc, data)), DOMRpcException.class,
83             cause -> new DefaultDOMRpcResult(ImmutableList.of(RpcResultBuilder.newError(
84                 RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))),
85             MoreExecutors.directExecutor()));
86     }
87
88     private static @NonNull NormalizedNode nonnullInput(final QName type, final NormalizedNode input) {
89         return input != null ? input
90                 : ImmutableNodes.containerNode(YangConstants.operationInputQName(type.getModule()));
91     }
92
93     /**
94      * Check the validity of the result.
95      *
96      * @param response response of rpc
97      * @return {@link DOMRpcResult} result
98      */
99     public static DOMRpcResult checkResponse(final DOMRpcResult response) {
100         if (response == null) {
101             return null;
102         }
103         try {
104             if (response.getErrors().isEmpty()) {
105                 return response;
106             }
107             LOG.debug("RpcError message {}", response.getErrors());
108             throw new RestconfDocumentedException("RPCerror message ", null, response.getErrors());
109         } catch (final CancellationException e) {
110             final String errMsg = "The operation was cancelled while executing.";
111             LOG.debug("Cancel RpcExecution: {}", errMsg, e);
112             throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
113         }
114     }
115
116     /**
117      * Invoking Action via mount point.
118      *
119      * @param mountPoint mount point
120      * @param data input data
121      * @param schemaPath schema path of data
122      * @return {@link DOMActionResult}
123      */
124     public static DOMActionResult invokeAction(final ContainerNode data,
125             final Absolute schemaPath, final YangInstanceIdentifier yangIId, final DOMMountPoint mountPoint) {
126         return invokeAction(data, schemaPath, yangIId, mountPoint.getService(DOMActionService.class)
127             .orElseThrow(() -> new RestconfDocumentedException("DomAction service is missing.")));
128     }
129
130     /**
131      * Invoke Action via ActionServiceHandler.
132      *
133      * @param data input data
134      * @param yangIId invocation context
135      * @param schemaPath schema path of data
136      * @param actionService action service to invoke action
137      * @return {@link DOMActionResult}
138      */
139     // FIXME: NETCONF-718: we should be returning a future here
140     public static DOMActionResult invokeAction(final ContainerNode data, final Absolute schemaPath,
141             final YangInstanceIdentifier yangIId, final DOMActionService actionService) {
142         return checkedGet(Futures.catching(actionService.invokeAction(
143             schemaPath, new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data),
144             DOMActionException.class,
145             cause -> new SimpleDOMActionResult(ImmutableList.of(RpcResultBuilder.newError(
146                 RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))),
147             MoreExecutors.directExecutor()));
148     }
149
150     @Deprecated
151     private static <T> T checkedGet(final ListenableFuture<T> future) {
152         try {
153             return future.get();
154         } catch (InterruptedException e) {
155             throw new RestconfDocumentedException("Interrupted while waiting for result of invocation", e);
156         } catch (ExecutionException e) {
157             final Throwable cause = e.getCause();
158             Throwables.throwIfInstanceOf(cause, RestconfDocumentedException.class);
159             throw new RestconfDocumentedException("Invocation failed", e);
160         }
161     }
162
163     /**
164      * Check the validity of the result.
165      *
166      * @param response
167      *             response of Action
168      * @return {@link DOMActionResult} result
169      */
170     public static DOMActionResult checkActionResponse(final DOMActionResult response) {
171         if (response != null) {
172             try {
173                 if (response.getErrors().isEmpty()) {
174                     return response;
175                 }
176                 LOG.debug("InvokeAction Error Message {}", response.getErrors());
177                 throw new RestconfDocumentedException("InvokeAction Error Message ", null, response.getErrors());
178             } catch (final CancellationException e) {
179                 final String errMsg = "The Action Operation was cancelled while executing.";
180                 LOG.debug("Cancel Execution: {}", errMsg, e);
181                 throw new RestconfDocumentedException(errMsg, ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, e);
182             }
183         }
184         return null;
185     }
186 }