Groovy scripts header correction
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / 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  * 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.openecomp.mso.bpmn.infrastructure.scripts
22
23
24 import javax.xml.parsers.DocumentBuilder
25 import javax.xml.parsers.DocumentBuilderFactory
26
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.runtime.Execution
30 import org.w3c.dom.Document
31 import org.w3c.dom.Element
32 import org.w3c.dom.Node
33 import org.w3c.dom.NodeList
34 import org.xml.sax.InputSource
35
36
37 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
38 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
39 import org.openecomp.mso.bpmn.common.scripts.VidUtils;
40 import org.openecomp.mso.bpmn.core.WorkflowException
41 import org.openecomp.mso.bpmn.core.json.JsonUtils;
42
43
44 /**
45  * This class supports the DeleteVnfInfra Flow
46  * with the Deletion of a generic vnf for
47  * infrastructure.
48  */
49 class DeleteVnfInfra extends AbstractServiceTaskProcessor {
50
51         String Prefix="DELVI_"
52         ExceptionUtil exceptionUtil = new ExceptionUtil()
53         JsonUtils jsonUtil = new JsonUtils()
54         VidUtils vidUtils = new VidUtils(this)
55
56         /**
57          * This method gets and validates the incoming
58          * request.
59          *
60          * @param - execution
61          */
62         public void preProcessRequest(Execution execution) {
63                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
64                 execution.setVariable("prefix",Prefix)
65                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra PreProcessRequest Process*** ", isDebugEnabled)
66
67                 execution.setVariable("DELVI_SuccessIndicator", false)
68                 execution.setVariable("DELVI_vnfInUse", false)
69
70                 try{
71                         // Get Variables
72                         String deleteVnfRequest = execution.getVariable("bpmnRequest")
73                         execution.setVariable("DELVI_DeleteVnfRequest", deleteVnfRequest)
74                         utils.logAudit("Incoming DeleteVnfInfra Request is: \n" + deleteVnfRequest)
75
76                         if(deleteVnfRequest != null){
77
78                                 String requestId = execution.getVariable("mso-request-id")
79                                 execution.setVariable("DELVI_requestId", requestId)
80
81                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
82                                 execution.setVariable("DELVI_serviceInstanceId", serviceInstanceId)
83                                 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
84
85                                 String vnfId = execution.getVariable("vnfId")
86                                 execution.setVariable("DELVI_vnfId", vnfId)
87                                 utils.log("DEBUG", "Incoming Vnf(Instance) Id is: " + vnfId, isDebugEnabled)
88
89                                 String source = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.requestInfo.source")
90                                 execution.setVariable("DELVI_source", source)
91                                 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
92                                 
93                                 def cloudConfiguration = jsonUtil.getJsonValue(deleteVnfRequest, "requestDetails.cloudConfiguration")
94                                 execution.setVariable("DELVI_cloudConfiguration", cloudConfiguration)
95                                 
96                                 boolean cascadeDelete = false
97                                 Boolean cascadeDeleteObj = jsonUtil.getJsonRawValue(deleteVnfRequest, "requestDetails.requestParameters.cascadeDelete")
98                                 if(cascadeDeleteObj!=null){
99                                         cascadeDelete = cascadeDeleteObj.booleanValue()
100                                 }
101                                 execution.setVariable("DELVI_cascadeDelete", cascadeDelete)
102                                 utils.log("DEBUG", "Incoming cascadeDelete is: " + cascadeDelete, isDebugEnabled)
103
104                                 //For Completion Handler & Fallout Handler
105                                 String requestInfo =
106                                 """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
107                                         <request-id>${requestId}</request-id>
108                                         <action>DELETE</action>
109                                         <source>${source}</source>
110                                    </request-info>"""
111
112                                 execution.setVariable("DELVI_requestInfo", requestInfo)
113
114                                 // Setting for sub flow calls
115                                 execution.setVariable("DELVI_type", "generic-vnf")
116
117                         }else{
118                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
119                         }
120
121                 }catch(BpmnError b){
122                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
123                         throw b
124                 }catch(Exception e){
125                         utils.log("DEBUG", " Error Occured in DeleteVnfInfra PreProcessRequest method!" + e, isDebugEnabled)
126                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PreProcessRequest")
127
128                 }
129                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra PreProcessRequest Process ***", isDebugEnabled)
130         }
131
132         public void sendSyncResponse (Execution execution) {
133                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
134                 execution.setVariable("prefix",Prefix)
135
136                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra SendSyncResponse Process *** ", isDebugEnabled)
137
138                 try {
139                         String requestId = execution.getVariable("DELVI_requestId")
140                         String vnfId = execution.getVariable("DELVI_vnfId")
141
142                         String DeleteVnfResponse = """{"requestReferences":{"instanceId":"${vnfId}","requestId":"${requestId}"}}""".trim()
143
144                         utils.log("DEBUG", " DeleteVnfInfra Sync Response is: \n"  + DeleteVnfResponse, isDebugEnabled)
145                         execution.setVariable("DELVI_sentSyncResponse", true)
146
147                         sendWorkflowResponse(execution, 202, DeleteVnfResponse)
148
149                 } catch (Exception ex) {
150                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra SendSyncResponse Process " + ex.getMessage(), isDebugEnabled)
151                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra SendSyncResponse Process")
152
153                 }
154                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra SendSyncResponse Process ***", isDebugEnabled)
155         }
156
157         public void prepareCompletionHandlerRequest(Execution execution){
158                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
159                 execution.setVariable("prefix",Prefix)
160
161                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra PrepareCompletionHandlerRequest Process *** ", isDebugEnabled)
162
163                 try {
164                         String requestInfo = execution.getVariable("DELVI_requestInfo")
165                         requestInfo = utils.removeXmlPreamble(requestInfo)
166                         String vnfId = execution.getVariable("DELVI_vnfId")
167
168                         String request =
169                                 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
170                                                                         xmlns:ns="http://org.openecomp/mso/request/types/v1">
171                                                         ${requestInfo}
172                                                         <status-message>Vnf has been deleted successfully.</status-message>
173                                                         <vnfId>${vnfId}</vnfId>
174                                                         <mso-bpel-name>DeleteVnfInfra</mso-bpel-name>
175                                                 </aetgt:MsoCompletionRequest>"""
176
177                         execution.setVariable("DELVI_completionHandlerRequest", request)
178                         utils.log("DEBUG", "Completion Handler Request is: " + request, isDebugEnabled)
179
180                         execution.setVariable("WorkflowResponse", "Success") // for junits
181
182                 } catch (Exception ex) {
183                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process " + ex.getMessage(), isDebugEnabled)
184                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra PrepareCompletionHandlerRequest Process")
185
186                 }
187                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra PrepareCompletionHandlerRequest Process ***", isDebugEnabled)
188         }
189
190         public void sendErrorResponse(Execution execution){
191                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
192                 execution.setVariable("prefix",Prefix)
193
194                 utils.log("DEBUG", " *** STARTED DeleteVnfInfra sendErrorResponse Process *** ", isDebugEnabled)
195                 try {
196                         def sentSyncResponse = execution.getVariable("DELVI_sentSyncResponse")
197                         if(sentSyncResponse == false){
198                                 utils.log("DEBUG", "Sending a Sync Error Response", isDebugEnabled)
199                                 WorkflowException wfex = execution.getVariable("WorkflowException")
200                                 String response = exceptionUtil.buildErrorResponseXml(wfex)
201
202                                 utils.logAudit(response)
203                                 sendWorkflowResponse(execution, 500, response)
204                         }else{
205                                 utils.log("DEBUG", "A Sync Response has already been sent. Skipping Send Sync Error Response.", isDebugEnabled)
206                         }
207
208                 } catch(Exception ex) {
209                         utils.log("DEBUG", "Error Occured in DeleteVnfInfra sendErrorResponse Process " + ex.getMessage(), isDebugEnabled)
210                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DeleteVnfInfra sendErrorResponse Process")
211                 }
212                 utils.log("DEBUG", "*** COMPLETED DeleteVnfInfra sendErrorResponse Process ***", isDebugEnabled)
213         }
214
215
216
217 }