Fix runtime bugs in bpmn groovy
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DeleteVnfInfra.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.scripts
24
25
26 import org.apache.commons.lang3.*
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor;
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
31 import org.onap.so.bpmn.common.scripts.MsoUtils
32 import org.onap.so.bpmn.common.scripts.VidUtils;
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.core.json.JsonUtils;
35 import org.slf4j.Logger
36 import org.slf4j.LoggerFactory
37
38
39 /**
40  * This class supports the DeleteVnfInfra Flow
41  * with the Deletion of a generic vnf for
42  * infrastructure.
43  */
44 class DeleteVnfInfra extends AbstractServiceTaskProcessor {
45
46     private static final Logger logger = LoggerFactory.getLogger( DeleteVnfInfra.class);
47         
48         String Prefix="DELVI_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51         VidUtils vidUtils = new VidUtils(this)
52
53         /**
54          * This method gets and validates the incoming
55          * request.
56          *
57          * @param - execution
58          */
59         public void preProcessRequest(DelegateExecution execution) {
60                 execution.setVariable("prefix",Prefix)
61                 logger.trace("STARTED DeleteVnfInfra PreProcessRequest Process")
62
63                 execution.setVariable("DELVI_SuccessIndicator", false)
64                 execution.setVariable("DELVI_vnfInUse", false)
65
66                 try{
67                         // Get Variables
68                         String deleteVnfRequest = execution.getVariable("bpmnRequest")
69                         execution.setVariable("DELVI_DeleteVnfRequest", deleteVnfRequest)
70                         logger.debug("Incoming DeleteVnfInfra Request is: \n" + deleteVnfRequest)
71
72                         if(deleteVnfRequest != null){
73
74                                 String requestId = execution.getVariable("mso-request-id")
75                                 execution.setVariable("DELVI_requestId", requestId)
76
77                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
78                                 execution.setVariable("DELVI_serviceInstanceId", serviceInstanceId)
79                                 logger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
80
81                                 String vnfId = execution.getVariable("vnfId")
82                                 execution.setVariable("DELVI_vnfId", vnfId)
83                                 logger.debug("Incoming Vnf(Instance) Id is: " + vnfId)
84
85                                 String source = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.requestInfo.source")
86                                 execution.setVariable("DELVI_source", source)
87                                 logger.debug("Incoming Source is: " + source)
88                                 
89                                 def cloudConfiguration = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.cloudConfiguration")
90                                 execution.setVariable("DELVI_cloudConfiguration", cloudConfiguration)
91                                 
92                                 boolean cascadeDelete = false
93                                 Boolean cascadeDeleteObj = jsonUtil.getJsonRawValue(deleteVnfRequest, "requestDetails.requestParameters.cascadeDelete")
94                                 if(cascadeDeleteObj!=null){
95                                         cascadeDelete = cascadeDeleteObj.booleanValue()
96                                 }
97                                 execution.setVariable("DELVI_cascadeDelete", cascadeDelete)
98                                 logger.debug("Incoming cascadeDelete is: " + cascadeDelete)
99
100                                 //For Completion Handler & Fallout Handler
101                                 String requestInfo =
102                                 """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
103                                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
104                                         <action>DELETE</action>
105                                         <source>${MsoUtils.xmlEscape(source)}</source>
106                                    </request-info>"""
107
108                                 execution.setVariable("DELVI_requestInfo", requestInfo)
109
110                                 // Setting for sub flow calls
111                                 execution.setVariable("DELVI_type", "generic-vnf")
112
113                         }else{
114                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
115                         }
116
117                 }catch(BpmnError b){
118                         logger.debug("Rethrowing MSOWorkflowException")
119                         throw b
120                 }catch(Exception e){
121                         logger.debug(" Error Occured in DeleteVnfInfra PreProcessRequest method!" + e)
122                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PreProcessRequest")
123
124                 }
125                 logger.trace("COMPLETED DeleteVnfInfra PreProcessRequest Process")
126         }
127
128         public void sendSyncResponse (DelegateExecution execution) {
129                 execution.setVariable("prefix",Prefix)
130
131                 logger.trace("STARTED DeleteVnfInfra SendSyncResponse Process")
132
133                 try {
134                         String requestId = execution.getVariable("DELVI_requestId")
135                         String vnfId = execution.getVariable("DELVI_vnfId")
136
137                         String DeleteVnfResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
138
139                         logger.debug("DeleteVnfInfra Sync Response is: \n"  + DeleteVnfResponse)
140                         execution.setVariable("DELVI_sentSyncResponse", true)
141
142                         sendWorkflowResponse(execution, 202, DeleteVnfResponse)
143
144                 } catch (Exception ex) {
145                         logger.debug("Error Occured in DeleteVnfInfra SendSyncResponse Process " + ex.getMessage())
146                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra SendSyncResponse Process")
147
148                 }
149                 logger.trace("COMPLETED DeleteVnfInfra SendSyncResponse Process")
150         }
151
152         public void prepareCompletionHandlerRequest(DelegateExecution execution){
153                 execution.setVariable("prefix",Prefix)
154
155                 logger.trace("STARTED DeleteVnfInfra PrepareCompletionHandlerRequest Process")
156
157                 try {
158                         String requestInfo = execution.getVariable("DELVI_requestInfo")
159                         requestInfo = utils.removeXmlPreamble(requestInfo)
160                         String vnfId = execution.getVariable("DELVI_vnfId")
161
162                         String request =
163                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
164                                                                         xmlns:ns="http://org.onap/so/request/types/v1">
165                                                         ${requestInfo}
166                                                         <status-message>Vnf has been deleted successfully.</status-message>
167                                                         <vnfId>${MsoUtils.xmlEscape(vnfId)}</vnfId>
168                                                         <mso-bpel-name>DeleteVnfInfra</mso-bpel-name>
169                                                 </aetgt:MsoCompletionRequest>"""
170
171                         execution.setVariable("DELVI_completionHandlerRequest", request)
172                         logger.debug("Completion Handler Request is: " + request)
173
174                         execution.setVariable("WorkflowResponse", "Success") // for junits
175
176                 } catch (Exception ex) {
177                         logger.debug("Error Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process " + ex.getMessage())
178                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process")
179
180                 }
181                 logger.trace("COMPLETED DeleteVnfInfra PrepareCompletionHandlerRequest Process")
182         }
183
184         public void sendErrorResponse(DelegateExecution execution){
185                 execution.setVariable("prefix",Prefix)
186
187                 logger.trace("STARTED DeleteVnfInfra sendErrorResponse Process")
188                 try {
189                         def sentSyncResponse = execution.getVariable("DELVI_sentSyncResponse")
190                         if(sentSyncResponse == false){
191                                 logger.debug("Sending a Sync Error Response")
192                                 WorkflowException wfex = execution.getVariable("WorkflowException")
193                                 String response = exceptionUtil.buildErrorResponseXml(wfex)
194
195                                 logger.debug(response)
196                                 sendWorkflowResponse(execution, 500, response)
197                         }else{
198                                 logger.debug("A Sync Response has already been sent. Skipping Send Sync Error Response.")
199                         }
200
201                 } catch(Exception ex) {
202                         logger.debug("Error Occured in DeleteVnfInfra sendErrorResponse Process " + ex.getMessage())
203                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra sendErrorResponse Process")
204                 }
205                 logger.trace("COMPLETED DeleteVnfInfra sendErrorResponse Process")
206         }
207
208
209
210 }