Groovy scripts header correction
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DeleteVfModuleInfra.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 import groovy.util.Node
24 import groovy.util.XmlParser;
25 import groovy.xml.QName
26
27 import java.io.Serializable;
28
29 import org.camunda.bpm.engine.delegate.BpmnError
30 import org.camunda.bpm.engine.runtime.Execution
31
32 import groovy.json.JsonSlurper
33
34 import org.openecomp.mso.rest.APIResponse
35 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
36 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
37 import org.openecomp.mso.bpmn.common.scripts.VidUtils;
38 import org.openecomp.mso.bpmn.core.RollbackData
39 import org.openecomp.mso.bpmn.core.WorkflowException
40 import org.openecomp.mso.bpmn.core.json.JsonUtils;
41
42
43 public class DeleteVfModuleInfra extends AbstractServiceTaskProcessor {
44
45         ExceptionUtil exceptionUtil = new ExceptionUtil()
46         JsonUtils jsonUtil = new JsonUtils()
47         /**
48          * Initialize the flow's variables.
49          *
50          * @param execution The flow's execution instance.
51          */
52         public void initProcessVariables(Execution execution) {
53                 execution.setVariable('prefix', 'DELVfModI_')
54                 execution.setVariable('DELVfModI_requestInfo', null)
55                 execution.setVariable('DELVfModI_requestId', null)
56                 execution.setVariable('DELVfModI_source', null)
57                 execution.setVariable('DELVfModI_vnfInputs', null)
58                 execution.setVariable('DELVfModI_vnfId', null)
59                 execution.setVariable('DELVfModI_vfModuleId', null)
60                 execution.setVariable('DELVfModI_tenantId', null)
61                 execution.setVariable('DELVfModI_volumeGroupId', null)
62                 execution.setVariable('DELVfModI_vnfParams', null)
63                 execution.setVariable('DELVfModI_updateInfraRequest', null)
64                 execution.setVariable('DeleteVfModuleRequest', null)
65                 execution.setVariable('DeleteVfModuleSuccessIndicator', false)
66         }
67
68         /**
69          * Process the incoming DELETE_VF_MODULE vnf-request.
70          *
71          * @param execution The flow's execution instance.
72          */
73         public void preProcessRequest(Execution execution) {
74                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
75                         'execution=' + execution.getId() +
76                         ')'
77                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
78                 logDebug('Entered ' + method, isDebugLogEnabled)
79                 execution.setVariable("isVidRequest", "false")
80                 initProcessVariables(execution)
81
82                 def prefix = execution.getVariable('prefix')
83
84                 def incomingRequest = execution.getVariable('bpmnRequest')
85
86                 utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)
87                 utils.logAudit("DeleteVfModule Infra incoming Request: " + incomingRequest)
88
89                 // check if request is xml or json
90                 try {
91                         def jsonSlurper = new JsonSlurper()
92                         Map reqMap = jsonSlurper.parseText(incomingRequest)
93                         utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
94
95                         def serviceInstanceId = execution.getVariable('serviceInstanceId')
96                         utils.log("DEBUG", "serviceInstanceId is: " + serviceInstanceId, isDebugLogEnabled)
97                         def vnfId = execution.getVariable('vnfId')
98                         utils.log("DEBUG", "vnfId is: " + vnfId, isDebugLogEnabled)
99                         def cloudConfiguration = jsonUtil.getJsonValue(incomingRequest, "requestDetails.cloudConfiguration")
100                         execution.setVariable("cloudConfiguration", cloudConfiguration)
101                         utils.log("DEBUG", "CloudConfiguration is: " + cloudConfiguration, isDebugLogEnabled)
102                         def vfModuleModelInfo = jsonUtil.getJsonValue(incomingRequest, "requestDetails.modelInfo")
103
104                         execution.setVariable("vfModuleModelInfo", vfModuleModelInfo)
105                         utils.log("DEBUG", "VfModuleModelInfo is: " + vfModuleModelInfo, isDebugLogEnabled)
106
107                         def vidUtils = new VidUtils(this)
108
109                         String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'DELETE_VF_MODULE', serviceInstanceId)
110
111                         utils.log("DEBUG", " Request in XML format: " + requestInXmlFormat, isDebugLogEnabled)
112
113                         setBasicDBAuthHeader(execution, isDebugLogEnabled)
114                         
115                         execution.setVariable(prefix + 'Request', requestInXmlFormat)
116                         execution.setVariable(prefix+'vnfId', vnfId)
117                         execution.setVariable("isVidRequest", "true")
118
119                 }
120                 catch(groovy.json.JsonException je) {
121                         utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
122                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Internal Error - During PreProcess Request")
123
124                 }
125                 catch(Exception e) {
126                         String restFaultMessage = e.getMessage()
127                         utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
128                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Internal Error - During PreProcess Request")
129                 }
130
131
132                 try {
133
134                         String request = validateRequest(execution)
135                         execution.setVariable('DeleteVfModuleRequest', request)
136                         utils.logAudit("DeleteVfModuleInfra Request: " + request)
137
138                         def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
139                         execution.setVariable('DELVfModI_requestInfo', requestInfo)
140                         execution.setVariable('DELVfModI_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
141                         execution.setVariable('DELVfModI_source', getNodeTextForce(requestInfo, 'source'))
142
143                         def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
144                         execution.setVariable('DELVfModI_vnfInputs', vnfInputs)
145                         execution.setVariable('DELVfModI_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
146                         execution.setVariable('DELVfModI_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
147                         execution.setVariable('DELVfModI_vfModuleName', getNodeTextForce(vnfInputs, 'vf-module-name'))
148                         execution.setVariable('DELVfModI_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
149                         execution.setVariable('DELVfModI_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
150
151                         def vnfParams = utils.getNodeXml(request, 'vnf-params')
152                         execution.setVariable('DELVfModI_vnfParams', vnfParams)
153
154                         logDebug('Exited ' + method, isDebugLogEnabled)
155                 } catch (BpmnError e) {
156                         throw e;
157                 } catch (Exception e) {
158                         logError('Caught exception in ' + method, e)
159                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
160                 }
161         }
162
163         /**
164          * Sends the 'IN_PROGRESS' synchronous response.
165          *
166          * @param execution The flow's execution instance.
167          */
168         public void sendSynchResponse(Execution execution) {
169                 def method = getClass().getSimpleName() + '.sendResponse(' +
170                         'execution=' + execution.getId() +
171                         ')'
172                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
173                 logDebug('Entered ' + method, isDebugLogEnabled)
174
175                 try {
176                         def requestInfo = execution.getVariable('DELVfModI_requestInfo')
177                         def requestId = execution.getVariable('DELVfModI_requestId')
178                         def source = execution.getVariable('DELVfModI_source')
179                         def progress = getNodeTextForce(requestInfo, 'progress')
180                         if (progress.isEmpty()) {
181                                 progress = '0'
182                         }
183                         def startTime = getNodeTextForce(requestInfo, 'start-time')
184                         if (startTime.isEmpty()) {
185                                 startTime = System.currentTimeMillis()
186                         }
187
188                         // RESTResponse (for API Handler (APIH) Reply Task)
189                         def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
190                         String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()
191
192                         utils.logAudit("DeleteVfModuleInfra Synch Response: " + synchResponse)
193                         sendWorkflowResponse(execution, 200, synchResponse)
194
195                         logDebug('Exited ' + method, isDebugLogEnabled)
196                 } catch (BpmnError e) {
197                         throw e;
198                 } catch (Exception e) {
199                         logError('Caught exception in ' + method, e)
200                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
201                 }
202         }
203
204         /**
205          * Currently passing the entire DELETE_VF_MODULE vnf-request to DoDeleteVfModule.
206          * 'DeleteVfModuleRequest' is now being set in preProcessRequest().
207          * TBD: may want to eventually create a specific request that only contains the needed fields.
208          *
209          * @param execution The flow's execution instance.
210          */
211         public void prepDoDeleteVfModule(Execution execution) {
212                 def method = getClass().getSimpleName() + '.prepDoDeleteVfModule(' +
213                         'execution=' + execution.getId() +
214                         ')'
215                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
216                 logDebug('Entered ' + method, isDebugLogEnabled)
217
218                 try {
219
220                         logDebug('Exited ' + method, isDebugLogEnabled)
221                 } catch (BpmnError e) {
222                         throw e;
223                 } catch (Exception e) {
224                         logError('Caught exception in ' + method, e)
225                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoDeleteVfModule(): ' + e.getMessage())
226                 }
227         }
228
229         /**
230          * Prepare the DB update to add an entry for the Vf Module request.
231          *
232          * @param execution The flow's execution instance.
233          */
234         public void prepUpdateInfraRequest(Execution execution) {
235                 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
236                         'execution=' + execution.getId() +
237                         ')'
238                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
239                 logDebug('Entered ' + method, isDebugLogEnabled)
240
241                 try {
242                         def requestId = execution.getVariable('DELVfModI_requestId')
243                         def vnfId = execution.getVariable('DELVfModI_vnfId')
244                         def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
245                         def tenantId = execution.getVariable('DELVfModI_tenantId')
246                         def volumeGroupId = execution.getVariable('DELVfModI_volumeGroupId')
247
248                         String updateInfraRequest = """
249                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
250                                         xmlns:req="http://org.openecomp.mso/requestsdb">
251                                         <soapenv:Header/>
252                                         <soapenv:Body>
253                                                 <req:updateInfraRequest>
254                                                         <requestId>${requestId}</requestId>
255                                                         <lastModifiedBy>BPMN</lastModifiedBy>
256                                                         <requestStatus>COMPLETED</requestStatus>
257                                                         <progress>100</progress>
258                                                 </req:updateInfraRequest>
259                                         </soapenv:Body>
260                                 </soapenv:Envelope>
261                         """
262
263                         updateInfraRequest = utils.formatXml(updateInfraRequest)
264                         execution.setVariable('DELVfModI_updateInfraRequest', updateInfraRequest)
265                         utils.logAudit("DeleteAAIVfModuleInfra Update Request: " + updateInfraRequest)
266                         logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)
267
268                         logDebug('Exited ' + method, isDebugLogEnabled)
269                 } catch (BpmnError e) {
270                         throw e;
271                 } catch (Exception e) {
272                         logError('Caught exception in ' + method, e)
273                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepInfraRequest(): ' + e.getMessage())
274                 }
275         }
276
277         /**
278          * Builds a "CompletionHandler" request and stores it in the specified execution variable.
279          *
280          * @param execution the execution
281          * @param resultVar the execution variable in which the result will be stored
282          */
283         public void completionHandlerPrep(Execution execution, String resultVar) {
284                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
285                         'execution=' + execution.getId() +
286                         ', resultVar=' + resultVar +
287                         ')'
288                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
289                 logDebug('Entered ' + method, isDebugLogEnabled)
290
291                 try {
292                         def request = execution.getVariable("DeleteVfModuleRequest")
293                         def requestInfo = utils.getNodeXml(request, 'request-info', false)
294                         def action = utils.getNodeText1(requestInfo, "action")
295
296                         String content =
297                                         """  <aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
298                                                    xmlns:ns="http://org.openecomp/mso/request/types/v1"
299                                                    xmlns:ns8="http://org.openecomp/mso/workflow/schema/v1">
300                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
301                         ${requestInfo}
302                         </request-info>
303                         <ns8:status-message>Vf Module has been deleted successfully.</ns8:status-message>
304                         <ns8:mso-bpel-name>BPMN</ns8:mso-bpel-name>
305                         </aetgt:MsoCompletionRequest>"""
306
307                         content = utils.formatXml(content)
308                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
309                         utils.logAudit("DeleteVfModule Infra Completion Handler Request: " + content)
310                         execution.setVariable(resultVar, content)
311
312                         logDebug('Exited ' + method, isDebugLogEnabled)
313                 } catch (BpmnError e) {
314                         throw e;
315                 } catch (Exception e) {
316                         logError('Caught exception in ' + method, e)
317                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')
318                 }
319         }
320
321         /**
322          * Builds a "FalloutHandler" request and stores it in the specified execution variable.
323          *
324          * @param execution the execution
325          * @param resultVar the execution variable in which the result will be stored
326          */
327         public void falloutHandlerPrep(Execution execution, String resultVar) {
328                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
329                         'execution=' + execution.getId() +
330                         ', resultVar=' + resultVar +
331                         ')'
332                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
333                 logDebug('Entered ' + method, isDebugLogEnabled)
334
335                 try {
336                         def prefix = execution.getVariable('prefix')
337                         def request = execution.getVariable("DeleteVfModuleRequest")
338                         def requestInfo = utils.getNodeXml(request, 'request-info', false)
339                         def WorkflowException workflowException = execution.getVariable("WorkflowException")
340                         def errorResponseCode = workflowException.getErrorCode()
341                         def errorResponseMsg = workflowException.getErrorMessage()
342                         def encErrorResponseMsg = ""
343                         if (errorResponseMsg != null) {
344                                 encErrorResponseMsg = errorResponseMsg.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
345                         }
346
347                         String content = """
348                         <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
349                                         xmlns:reqtype="http://org.openecomp/mso/request/types/v1"
350                                         xmlns:msoservtypes="http://org.openecomp/mso/request/types/v1"
351                                         xmlns:structuredtypes="http://org.openecomp/mso/structured/types/v1">
352                                         ${requestInfo}
353                                         <sdncadapterworkflow:WorkflowException>
354                                                 <sdncadapterworkflow:ErrorMessage>${encErrorResponseMsg}</sdncadapterworkflow:ErrorMessage>
355                                                 <sdncadapterworkflow:ErrorCode>${errorResponseCode}</sdncadapterworkflow:ErrorCode>
356                                         </sdncadapterworkflow:WorkflowException>
357                                 </sdncadapterworkflow:FalloutHandlerRequest>
358                         """
359                         content = utils.formatXml(content)
360                         utils.logAudit("DeleteVfModuleInfra Fallout Handler Request: " + content)
361                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
362                         execution.setVariable(resultVar, content)
363
364                         logDebug('Exited ' + method, isDebugLogEnabled)
365                 } catch (BpmnError e) {
366                         throw e;
367                 } catch (Exception e) {
368                         logError('Caught exception in ' + method, e)
369                         exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
370                 }
371         }
372 }