Merge "Bug 1029: Remove dead code: sal-schema-repository-api"
[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     protected RpcResult<CompositeNode> getRpcResult(
34                                             Future<RpcResult<CompositeNode>> fromFuture ) {
35         try {
36             return fromFuture.get();
37         }
38         catch( InterruptedException e ) {
39             throw new RestconfDocumentedException(
40                         "The operation was interrupted while executing and did not complete.",
41                         ErrorType.RPC, ErrorTag.PARTIAL_OPERATION );
42         }
43         catch( ExecutionException e ) {
44             Throwable cause = e.getCause();
45             if( cause instanceof CancellationException ) {
46                 throw new RestconfDocumentedException(
47                         "The operation was cancelled while executing.",
48                         ErrorType.RPC, ErrorTag.PARTIAL_OPERATION );
49             }
50             else if( cause != null ){
51                 while( cause.getCause() != null ) {
52                     cause = cause.getCause();
53                 }
54
55                 if( cause instanceof IllegalArgumentException ) {
56                     throw new RestconfDocumentedException(
57                             cause.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
58                 }
59
60                 throw new RestconfDocumentedException(
61                        "The operation encountered an unexpected error while executing.", cause );
62             }
63             else {
64                 throw new RestconfDocumentedException(
65                         "The operation encountered an unexpected error while executing.", e );
66             }
67         }
68     }
69 }