2 * Copyright (c) 2014 Brocade Communications 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.controller.sal.restconf.rpc.impl;
10 import java.util.concurrent.CancellationException;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
14 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
15 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
18 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
20 public abstract class AbstractRpcExecutor implements RpcExecutor {
21 private final RpcDefinition rpcDef;
23 public AbstractRpcExecutor(RpcDefinition rpcDef) {
28 public RpcDefinition getRpcDefinition() {
33 public RpcResult<CompositeNode> invokeRpc(CompositeNode rpcRequest) throws RestconfDocumentedException {
35 return getRpcResult(invokeRpcUnchecked(rpcRequest));
36 } catch (IllegalArgumentException e) {
37 throw new RestconfDocumentedException(e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
38 } catch (UnsupportedOperationException e) {
39 throw new RestconfDocumentedException(e.getMessage(), ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED);
40 } catch (RestconfDocumentedException e) {
42 } catch (Exception e) {
43 throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.", e);
47 protected abstract Future<RpcResult<CompositeNode>> invokeRpcUnchecked(CompositeNode rpcRequest);
49 protected RpcResult<CompositeNode> getRpcResult(Future<RpcResult<CompositeNode>> fromFuture) {
51 return fromFuture.get();
52 } catch (InterruptedException e) {
53 throw new RestconfDocumentedException(
54 "The operation was interrupted while executing and did not complete.", ErrorType.RPC,
55 ErrorTag.PARTIAL_OPERATION);
56 } catch (ExecutionException e) {
57 Throwable cause = e.getCause();
58 if (cause instanceof CancellationException) {
59 throw new RestconfDocumentedException("The operation was cancelled while executing.", ErrorType.RPC,
60 ErrorTag.PARTIAL_OPERATION);
61 } else if (cause != null) {
62 while (cause.getCause() != null) {
63 cause = cause.getCause();
66 if (cause instanceof IllegalArgumentException) {
67 throw new RestconfDocumentedException(cause.getMessage(), ErrorType.PROTOCOL,
68 ErrorTag.INVALID_VALUE);
71 throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.",
74 throw new RestconfDocumentedException("The operation encountered an unexpected error while executing.",