[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / ExceptionUtil.groovy
  * ============LICENSE_END=========================================================
  */
 
-package com.att.bpm.scripts
+package org.openecomp.mso.bpmn.common.scripts
 
-import org.openecomp.mso.bpmn.core.WorkflowException
+import static org.apache.commons.lang3.StringUtils.*
 
+import org.apache.commons.lang3.*
 import org.camunda.bpm.engine.delegate.BpmnError
 import org.camunda.bpm.engine.runtime.Execution
-import org.apache.commons.lang3.*
-
-import static org.apache.commons.lang3.StringUtils.*;
+import org.openecomp.mso.bpmn.core.WorkflowException
 
 /**
  * @version 1.0
@@ -74,7 +73,7 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
                        String modifiedErrorMessage = 'Received error from A&AI (' + text +')'
                        utils.log("DEBUG", "ModifiedErrorMessage " + modifiedErrorMessage, isDebugEnabled)
                        // let $ModifiedErrorMessage := concat( 'Received error from A',$exceptionaai:ampersand,'AI (' ,functx:replace-multi($ErrorMessage,$from,$Variables ),')')
-                       String message = """<aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
+                       String message = """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
                                <aetgt:ErrorMessage>$modifiedErrorMessage</aetgt:ErrorMessage>
                                <aetgt:ErrorCode>$errorCode</aetgt:ErrorCode>
                        </aetgt:WorkflowException>"""
@@ -86,7 +85,7 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
                                errorCode = MapErrorCode(errorMsg)
                                String mappedErrorMessage = MapErrorMessage(errorMsg, errorCode)
 
-                               String message = """<aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
+                               String message = """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
                                        <aetgt:ErrorMessage>$mappedErrorMessage</aetgt:ErrorMessage>
                                        <aetgt:ErrorCode>$errorCode</aetgt:ErrorCode>
                                </aetgt:WorkflowException>"""
@@ -253,7 +252,7 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
         */
        @Deprecated
        String buildWorkflowExceptionXml(String errorCode, String errorMessage) {
-               return """<aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
+               return """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
                                        <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
                                        <aetgt:ErrorCode>${errorCode}</aetgt:ErrorCode>
                                  </aetgt:WorkflowException>"""
@@ -276,14 +275,14 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
                        String mes = wfex.getErrorMessage()
                        int code = wfex.getErrorCode()
                        xml =
-                       """<aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
+                       """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
                                        <aetgt:ErrorMessage>${mes}</aetgt:ErrorMessage>
                                        <aetgt:ErrorCode>${code}</aetgt:ErrorCode>
                                  </aetgt:WorkflowException>"""
 
                }else{
                        xml =
-                       """<aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
+                       """<aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
                                        <aetgt:ErrorMessage>Internal Error</aetgt:ErrorMessage>
                                        <aetgt:ErrorCode>2500</aetgt:ErrorCode>
                                  </aetgt:WorkflowException>"""
@@ -315,12 +314,14 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
                if(errorMessage == null){
                        errorMessage=""
                }
-               if( errorCode.equals('5010'))
-                                       return 'Could not communicate with A&amp;AI'
-               else if (errorCode.equals('5020'))
+               if( errorCode.equals('5010')) {
+                       return 'Could not communicate with A&amp;AI'
+               } else if (errorCode.equals('5020')) {
                        return 'No response from A&amp;AI'
-               else
+               } else {
+                       errorMessage = errorMessage.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
                        return 'Received error from A&amp;AI (' +errorMessage +')'
+               }
        }
 
        String MapCategoryToErrorCode(String errorCategory)
@@ -451,11 +452,11 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
                        int errorCode = wfex.getErrorCode()
 
                        String falloutHandlerRequest =
-                       """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1"
-                                                                    xmlns:ns="http://ecomp.att.com/mso/request/types/v1"
-                                                                    xmlns:wfsch="http://ecomp.att.com/mso/workflow/schema/v1">
+                       """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
+                                                                    xmlns:ns="http://org.openecomp/mso/request/types/v1"
+                                                                    xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">
                                           ${requestInfo}
-                                               <aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
+                                               <aetgt:WorkflowException xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1">
                                                        <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
                                                        <aetgt:ErrorCode>${errorCode}</aetgt:ErrorCode>
                                                </aetgt:WorkflowException>
@@ -473,6 +474,73 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
                utils.log("DEBUG", "Completed ProcessMainflowBPMNException Method", isDebugEnabled)
        }
 
+       /**
+        *
+        * This method should only be used by DHV Flows. This method is
+        * executed after an MSOWorkflowException is caught by a Mainflow.
+        * It builds and returns a FalloutHandler Request. It also
+        * verifies the WorkflowException variable is populated.
+        *
+        * @param - execution
+        * @param - requestInfo
+        *
+        * @return - falloutHandlerRequest
+        *
+        */
+       public String processMainflowsBPMNExceptionDHV(Execution execution, String requestId, def source, def action, def notificationUrl, def serviceInstanceId, def startTime){
+               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
+               String processKey = getProcessKey(execution)
+               try{
+                       utils.log("DEBUG", "Started ProcessMainflowBPMNExceptionDHV Method", isDebugEnabled)
+
+                       def WorkflowException workflowException = execution.getVariable("WorkflowException")
+                       int errorResponseCode = 0
+                       def errorResponseMsg = ""
+                       if(workflowException != null){
+                               errorResponseCode = workflowException.getErrorCode()
+                               def errorMsg = workflowException.getErrorMessage()
+                               if (errorMsg != null) {
+                                       errorResponseMsg = errorMsg.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
+                               }
+                       }else{
+                               errorResponseCode = 2500
+                               errorResponseMsg = "Internal Error occured during " + action + ", unable to retrieve error information."
+                       }
+
+
+                       String request =
+                       """<FalloutHandlerRequest xmlns="http://org.openecomp/mso/workflow/schema/v1">
+                                       <request-information xmlns="http://org.openecomp/mso/request/types/v1">
+                                               <request-id>${requestId}</request-id>
+                                               <request-action>${action}</request-action>
+                                               <source>${source}</source>
+                                               <notification-url>${notificationUrl}</notification-url>
+                                       </request-information>
+                                       <mso-bpel-name>BPMN</mso-bpel-name>
+                                       <is-srv-inst-req>true</is-srv-inst-req>
+                                       <resp-content-type>JSON</resp-content-type>
+                                       <service-instance-id>${serviceInstanceId}</service-instance-id>
+                                       <start-time>${startTime}</start-time>
+                                       <WorkflowException>
+                                               <ErrorMessage>${errorResponseMsg}</ErrorMessage>
+                                               <ErrorCode>${errorResponseCode}</ErrorCode>
+                                       </WorkflowException>
+                               </FalloutHandlerRequest>"""
+
+
+                       request = utils.formatXml(request)
+                       utils.log("DEBUG", processKey + " Outgoing WorkflowException is: " + execution.getVariable("WorkflowException"), isDebugEnabled)
+                       utils.log("DEBUG", processKey + " Outgoing FalloutHandler Request is: " + request, isDebugEnabled)
+
+                       return request
+
+               }catch(Exception e){
+                       utils.log("DEBUG", "Caught Exception during ProcessMainflowBPMNException Method: " + e, isDebugEnabled)
+                       return null
+               }
+               utils.log("DEBUG", "Completed ProcessMainflowBPMNExceptionDHV Method", isDebugEnabled)
+       }
+
        /**
         * This method is executed after an Java Exception is caught.
         * It sets the WorkflowException variable and throws an MSOWorkflowException.
@@ -480,6 +548,7 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
         * @param - execution
         *
         */
+       @Deprecated
        public void processSubflowsJavaException(Execution execution){
                def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
                String processKey = getProcessKey(execution)
@@ -497,13 +566,34 @@ class ExceptionUtil extends AbstractServiceTaskProcessor {
                utils.log("DEBUG", "Completed processJavaException Method", isDebugEnabled)
        }
 
+       /**
+        *
+        * This method is executed after an Java Exception is caught
+        * It sets the WorkflowException variable. The method can be used in either mainflow or subflows.
+        *
+        * @param - execution
+        *
+        */
+       public void processJavaException(Execution execution){
+               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
+               String processKey = getProcessKey(execution)
+               try{
+                       utils.log("DEBUG", "Caught a Java Exception in " + processKey, isDebugEnabled)
+                       utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)
+                       buildWorkflowException(execution, 2500, "Catch a Java Lang Exception in " + processKey)
 
-       public void preProcessRequest(Execution execution) {
-               // TODO Auto-generated method stub
-
+               }catch(BpmnError b){
+                       throw b
+               }catch(Exception e){
+                       utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)
+                       buildWorkflowException(execution, 2500, "Internal Error - During Process Java Exception")
+               }
+               utils.log("DEBUG", "Completed processJavaException Method", isDebugEnabled)
        }
 
 
+       public void preProcessRequest(Execution execution) {
+               // TODO Auto-generated method stub
 
-
+       }
 }