82b67ce3bfbe3b55860696cf924c0e3497c4db0d
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / VnfAdapterUtils.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.common.scripts\r
21 \r
22 import org.camunda.bpm.engine.delegate.BpmnError\r
23 import org.camunda.bpm.engine.runtime.Execution;\r
24 import org.openecomp.mso.bpmn.core.WorkflowException\r
25 \r
26 class VnfAdapterUtils {\r
27 \r
28         private AbstractServiceTaskProcessor taskProcessor\r
29 \r
30         public VnfAdapterUtils(AbstractServiceTaskProcessor taskProcessor) {\r
31                 this.taskProcessor = taskProcessor\r
32         }\r
33 \r
34         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
35 \r
36         public void validateVnfResponse(Execution execution, String responseVar, String responseCodeVar, String errorResponseVar) {\r
37                 def method = getClass().getSimpleName() + '.validateVnfResponse(' +\r
38                         'execution=' + execution.getId() +\r
39                         ', responseVar=' + responseVar +\r
40                         ', responseCodeVar=' + responseCodeVar +\r
41                         ', errorResponseVar=' + errorResponseVar +\r
42                         ')'\r
43                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
44                 taskProcessor.logDebug('Entered ' + method, isDebugLogEnabled)\r
45 \r
46                 try {\r
47                         def prefix = execution.getVariable('prefix')\r
48 \r
49                         def response = execution.getVariable(responseVar)\r
50                         def responseCode = execution.getVariable(responseCodeVar)\r
51                         def errorResponse = execution.getVariable(errorResponseVar)\r
52 \r
53                         // The following if statement never appears to be true as any VNF Adapter error seems to be stored in 'errorResponse'.\r
54                         // Also, the value is stored as a WorkflowException object, not a String. Added the else if to provide the proper\r
55                         // functionality but leaving the original code in case it is hit under some circumstances.\r
56                         if (response.contains("WorkflowException")) {\r
57                                 execution.setVariable(prefix + "ErrorResponse", response)\r
58                                 //execution.setVariable(prefix + "ResponseCode", responseCode)\r
59                                 taskProcessor.logDebug(" Sub Vnf flow Error WorkflowException Response - " + "\n" + response, isDebugLogEnabled)\r
60                                 throw new BpmnError("MSOWorkflowException")\r
61                         } else if (errorResponse != null && errorResponse instanceof WorkflowException) {\r
62                                 // Not sure the variables with the associated prefix are still used\r
63                                 execution.setVariable(prefix + "ErrorResponse", errorResponse.getErrorMessage())\r
64                                 execution.setVariable(prefix + "ResponseCode", errorResponse.getErrorCode())\r
65                                 taskProcessor.logDebug("Sub Vnf flow Error WorkflowException " + prefix + "ErrorResponse" + " - " +\r
66                                         errorResponse.getErrorMessage(), isDebugLogEnabled)\r
67                                 // this is the important part to ensure we hit the Fallout Handler\r
68                                 throw new BpmnError("MSOWorkflowException")\r
69                         } else if (errorResponse != null && errorResponse instanceof WorkflowException) {\r
70                                 // Not sure the variables with the associated prefix are still used\r
71                                 execution.setVariable(prefix + "ErrorResponse", errorResponse.getErrorMessage())\r
72                                 execution.setVariable(prefix + "ResponseCode", errorResponse.getErrorCode())\r
73                                 taskProcessor.logDebug("Sub Vnf flow Error WorkflowException " + prefix + "ErrorResponse" + " - " +\r
74                                         errorResponse.getErrorMessage(), isDebugLogEnabled)\r
75                                 // this is the important part to ensure we hit the Fallout Handler\r
76                                 throw new BpmnError("MSOWorkflowException")\r
77                         }\r
78                 } catch (BpmnError e) {\r
79                         throw e;\r
80                 } catch (Exception e) {\r
81                         taskProcessor.logError('Caught exception in ' + method, e)\r
82                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, 'Internal Error- Unable to validate VNF Response ' + e.getMessage())\r
83                 }\r
84         }\r
85 \r
86 }\r