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;
14 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
15 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
16 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
21 public abstract class AbstractRpcExecutor implements RpcExecutor {
22 private final RpcDefinition rpcDef;
24 public AbstractRpcExecutor( RpcDefinition rpcDef ){
29 public RpcDefinition getRpcDefinition() {
34 public RpcResult<CompositeNode> invokeRpc( CompositeNode rpcRequest )
35 throws RestconfDocumentedException {
37 return getRpcResult( invokeRpcUnchecked( rpcRequest ) );
39 catch( IllegalArgumentException e ) {
40 throw new RestconfDocumentedException(
41 e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
43 catch( UnsupportedOperationException e ) {
44 throw new RestconfDocumentedException(
45 e.getMessage(), ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED );
47 catch( Exception e ) {
48 throw new RestconfDocumentedException(
49 "The operation encountered an unexpected error while executing.", e );
53 protected abstract Future<RpcResult<CompositeNode>> invokeRpcUnchecked( CompositeNode rpcRequest );
55 protected RpcResult<CompositeNode> getRpcResult(
56 Future<RpcResult<CompositeNode>> fromFuture ) {
58 return fromFuture.get();
60 catch( InterruptedException e ) {
61 throw new RestconfDocumentedException(
62 "The operation was interrupted while executing and did not complete.",
63 ErrorType.RPC, ErrorTag.PARTIAL_OPERATION );
65 catch( ExecutionException e ) {
66 Throwable cause = e.getCause();
67 if( cause instanceof CancellationException ) {
68 throw new RestconfDocumentedException(
69 "The operation was cancelled while executing.",
70 ErrorType.RPC, ErrorTag.PARTIAL_OPERATION );
72 else if( cause != null ){
73 while( cause.getCause() != null ) {
74 cause = cause.getCause();
77 if( cause instanceof IllegalArgumentException ) {
78 throw new RestconfDocumentedException(
79 cause.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
82 throw new RestconfDocumentedException(
83 "The operation encountered an unexpected error while executing.", cause );
86 throw new RestconfDocumentedException(
87 "The operation encountered an unexpected error while executing.", e );