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.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;
30 public final class RestconfInvokeOperationsUtil {
32 private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsUtil.class);
34 private RestconfInvokeOperationsUtil() {
35 throw new UnsupportedOperationException("Util class");
39 * Invoking rpc via mount point.
47 * @return {@link DOMRpcResult}
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);
56 final String errmsg = "RPC service is missing.";
58 throw new RestconfDocumentedException(errmsg);
68 * @param rpcServiceHandler
69 * rpc service handler to invoke rpc
70 * @return {@link DOMRpcResult}
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);
79 final ListenableFuture<DOMRpcResult> rpc = rpcService.invokeRpc(schemaPath, data);
80 return prepareResult(rpc);
84 * Check the validity of the result.
88 * @return {@link DOMRpcResult} result
90 public static DOMRpcResult checkResponse(final DOMRpcResult response) {
91 if (response == null) {
95 if (response.getErrors().isEmpty()) {
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);
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();