2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.restful.utils;
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;
30 * @deprecated move to splitted module restconf-nb-rfc8040
33 public class RestconfInvokeOperationsUtil {
35 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
37 private RestconfInvokeOperationsUtil() {
38 throw new UnsupportedOperationException("Util class");
42 * Invoking rpc via mount point.
50 * @return {@link CheckedFuture}
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,
58 return prepareResult(rpc);
60 final String errmsg = "RPC service is missing.";
62 throw new RestconfDocumentedException(errmsg);
72 * @param rpcServiceHandler
73 * rpc service handler to invoke rpc
74 * @return {@link CheckedFuture}
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);
83 final CheckedFuture<DOMRpcResult, DOMRpcException> rpc = rpcService.invokeRpc(schemaPath, data);
84 return prepareResult(rpc);
88 * Check the validity of the result.
92 * @return {@link DOMRpcResult} result
94 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
95 if (response == null) {
99 if (response.getErrors().isEmpty()) {
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);
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();