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.nb.rfc8040.rests.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.nb.rfc8040.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;
31 public final class RestconfInvokeOperationsUtil {
33 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
35 private RestconfInvokeOperationsUtil() {
36 throw new UnsupportedOperationException("Util class");
40 * Invoking rpc via mount point.
48 * @return {@link CheckedFuture}
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,
56 return prepareResult(rpc);
58 final String errmsg = "RPC service is missing.";
60 throw new RestconfDocumentedException(errmsg);
70 * @param rpcServiceHandler
71 * rpc service handler to invoke rpc
72 * @return {@link CheckedFuture}
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);
81 final CheckedFuture<DOMRpcResult, DOMRpcException> rpc = rpcService.invokeRpc(schemaPath, data);
82 return prepareResult(rpc);
86 * Check the validity of the result.
90 * @return {@link DOMRpcResult} result
92 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
93 if (response == null) {
97 if (response.getErrors().isEmpty()) {
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, e);
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();