Bug 1010: Implement restconf error responses
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / rpc / impl / AbstractRpcExecutor.java
index 446d07d42639969eec1f999fb8713b96d0a3f307..0bc8428d76f85fae184b577040ee4d52f9cc682d 100644 (file)
@@ -7,6 +7,15 @@
 */
 package org.opendaylight.controller.sal.restconf.rpc.impl;
 
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
+import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
+import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 
 public abstract class AbstractRpcExecutor implements RpcExecutor {
@@ -20,4 +29,41 @@ public abstract class AbstractRpcExecutor implements RpcExecutor {
     public RpcDefinition getRpcDefinition() {
         return rpcDef;
     }
+
+    protected RpcResult<CompositeNode> getRpcResult(
+                                            Future<RpcResult<CompositeNode>> fromFuture ) {
+        try {
+            return fromFuture.get();
+        }
+        catch( InterruptedException e ) {
+            throw new RestconfDocumentedException(
+                        "The operation was interrupted while executing and did not complete.",
+                        ErrorType.RPC, ErrorTag.PARTIAL_OPERATION );
+        }
+        catch( ExecutionException e ) {
+            Throwable cause = e.getCause();
+            if( cause instanceof CancellationException ) {
+                throw new RestconfDocumentedException(
+                        "The operation was cancelled while executing.",
+                        ErrorType.RPC, ErrorTag.PARTIAL_OPERATION );
+            }
+            else if( cause != null ){
+                while( cause.getCause() != null ) {
+                    cause = cause.getCause();
+                }
+
+                if( cause instanceof IllegalArgumentException ) {
+                    throw new RestconfDocumentedException(
+                            cause.getMessage(), ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE );
+                }
+
+                throw new RestconfDocumentedException(
+                       "The operation encountered an unexpected error while executing.", cause );
+            }
+            else {
+                throw new RestconfDocumentedException(
+                        "The operation encountered an unexpected error while executing.", e );
+            }
+        }
+    }
 }
\ No newline at end of file