Bug 1125: Added regression test
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / rpc / impl / AbstractRpcExecutor.java
1 /*
2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3 *
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
7 */
8 package org.opendaylight.controller.sal.restconf.rpc.impl;
9
10 import java.util.concurrent.CancellationException;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13
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;
20
21 public abstract class AbstractRpcExecutor implements RpcExecutor {
22     private final RpcDefinition rpcDef;
23
24     public AbstractRpcExecutor( RpcDefinition rpcDef ){
25         this.rpcDef = rpcDef;
26     }
27
28     @Override
29     public RpcDefinition getRpcDefinition() {
30         return rpcDef;
31     }
32
33     @Override
34     public RpcResult<CompositeNode> invokeRpc( CompositeNode rpcRequest )
35                                                    throws RestconfDocumentedException {
36         try {
37             return getRpcResult( invokeRpcUnchecked( rpcRequest ) );
38         }
39         catch( IllegalArgumentException e ) {
40             throw new RestconfDocumentedException(
41                                 e.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
42         }
43         catch( UnsupportedOperationException e ) {
44             throw new RestconfDocumentedException(
45                                 e.getMessage(), ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED );
46         }
47         catch( Exception e ) {
48             throw new RestconfDocumentedException(
49                     "The operation encountered an unexpected error while executing.", e );
50         }
51     }
52
53     protected abstract Future<RpcResult<CompositeNode>> invokeRpcUnchecked( CompositeNode rpcRequest );
54
55     protected RpcResult<CompositeNode> getRpcResult(
56                                             Future<RpcResult<CompositeNode>> fromFuture ) {
57         try {
58             return fromFuture.get();
59         }
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 );
64         }
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 );
71             }
72             else if( cause != null ){
73                 while( cause.getCause() != null ) {
74                     cause = cause.getCause();
75                 }
76
77                 if( cause instanceof IllegalArgumentException ) {
78                     throw new RestconfDocumentedException(
79                             cause.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
80                 }
81
82                 throw new RestconfDocumentedException(
83                        "The operation encountered an unexpected error while executing.", cause );
84             }
85             else {
86                 throw new RestconfDocumentedException(
87                         "The operation encountered an unexpected error while executing.", e );
88             }
89         }
90     }
91 }