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