AT&T 1712 and 1802 release code
[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.delegate.DelegateExecution
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(DelegateExecution 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(DelegateExecution 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                         // This is aLaCarte flow, so aLaCarte flag is always on
108                         execution.setVariable('aLaCarte', true)
109                         
110                         def vidUtils = new VidUtils(this)
111
112                         String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'DELETE_VF_MODULE', serviceInstanceId)
113
114                         utils.log("DEBUG", " Request in XML format: " + requestInXmlFormat, isDebugLogEnabled)
115
116                         setBasicDBAuthHeader(execution, isDebugLogEnabled)
117                         
118                         execution.setVariable(prefix + 'Request', requestInXmlFormat)
119                         execution.setVariable(prefix+'vnfId', vnfId)
120                         execution.setVariable("isVidRequest", "true")
121
122                 }
123                 catch(groovy.json.JsonException je) {
124                         utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
125                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Internal Error - During PreProcess Request")
126
127                 }
128                 catch(Exception e) {
129                         String restFaultMessage = e.getMessage()
130                         utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
131                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Internal Error - During PreProcess Request")
132                 }
133
134
135                 try {
136
137                         String request = validateRequest(execution)
138                         execution.setVariable('DeleteVfModuleRequest', request)
139                         utils.logAudit("DeleteVfModuleInfra Request: " + request)
140
141                         def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
142                         execution.setVariable('DELVfModI_requestInfo', requestInfo)
143                         execution.setVariable('DELVfModI_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
144                         execution.setVariable('DELVfModI_source', getNodeTextForce(requestInfo, 'source'))
145
146                         def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
147                         execution.setVariable('DELVfModI_vnfInputs', vnfInputs)
148                         execution.setVariable('DELVfModI_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
149                         execution.setVariable('DELVfModI_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
150                         execution.setVariable('DELVfModI_vfModuleName', getNodeTextForce(vnfInputs, 'vf-module-name'))
151                         execution.setVariable('DELVfModI_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
152                         execution.setVariable('DELVfModI_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
153
154                         def vnfParams = utils.getNodeXml(request, 'vnf-params')
155                         execution.setVariable('DELVfModI_vnfParams', vnfParams)
156
157                         logDebug('Exited ' + method, isDebugLogEnabled)
158                 } catch (BpmnError e) {
159                         throw e;
160                 } catch (Exception e) {
161                         logError('Caught exception in ' + method, e)
162                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
163                 }
164         }
165
166         /**
167          * Sends the 'IN_PROGRESS' synchronous response.
168          *
169          * @param execution The flow's execution instance.
170          */
171         public void sendSynchResponse(DelegateExecution execution) {
172                 def method = getClass().getSimpleName() + '.sendResponse(' +
173                         'execution=' + execution.getId() +
174                         ')'
175                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
176                 logDebug('Entered ' + method, isDebugLogEnabled)
177
178                 try {
179                         def requestInfo = execution.getVariable('DELVfModI_requestInfo')
180                         def requestId = execution.getVariable('DELVfModI_requestId')
181                         def source = execution.getVariable('DELVfModI_source')
182                         def progress = getNodeTextForce(requestInfo, 'progress')
183                         if (progress.isEmpty()) {
184                                 progress = '0'
185                         }
186                         def startTime = getNodeTextForce(requestInfo, 'start-time')
187                         if (startTime.isEmpty()) {
188                                 startTime = System.currentTimeMillis()
189                         }
190
191                         // RESTResponse (for API Handler (APIH) Reply Task)
192                         def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
193                         String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()
194
195                         utils.logAudit("DeleteVfModuleInfra Synch Response: " + synchResponse)
196                         sendWorkflowResponse(execution, 200, synchResponse)
197
198                         logDebug('Exited ' + method, isDebugLogEnabled)
199                 } catch (BpmnError e) {
200                         throw e;
201                 } catch (Exception e) {
202                         logError('Caught exception in ' + method, e)
203                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
204                 }
205         }
206
207         /**
208          * Currently passing the entire DELETE_VF_MODULE vnf-request to DoDeleteVfModule.
209          * 'DeleteVfModuleRequest' is now being set in preProcessRequest().
210          * TBD: may want to eventually create a specific request that only contains the needed fields.
211          *
212          * @param execution The flow's execution instance.
213          */
214         public void prepDoDeleteVfModule(DelegateExecution execution) {
215                 def method = getClass().getSimpleName() + '.prepDoDeleteVfModule(' +
216                         'execution=' + execution.getId() +
217                         ')'
218                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
219                 logDebug('Entered ' + method, isDebugLogEnabled)
220
221                 try {
222
223                         logDebug('Exited ' + method, isDebugLogEnabled)
224                 } catch (BpmnError e) {
225                         throw e;
226                 } catch (Exception e) {
227                         logError('Caught exception in ' + method, e)
228                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoDeleteVfModule(): ' + e.getMessage())
229                 }
230         }
231
232         /**
233          * Prepare the DB update to add an entry for the Vf Module request.
234          *
235          * @param execution The flow's execution instance.
236          */
237         public void prepUpdateInfraRequest(DelegateExecution execution) {
238                 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
239                         'execution=' + execution.getId() +
240                         ')'
241                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
242                 logDebug('Entered ' + method, isDebugLogEnabled)
243
244                 try {
245                         def requestId = execution.getVariable('DELVfModI_requestId')
246                         def vnfId = execution.getVariable('DELVfModI_vnfId')
247                         def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
248                         def tenantId = execution.getVariable('DELVfModI_tenantId')
249                         def volumeGroupId = execution.getVariable('DELVfModI_volumeGroupId')
250
251                         String updateInfraRequest = """
252                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
253                                         xmlns:req="http://org.openecomp.mso/requestsdb">
254                                         <soapenv:Header/>
255                                         <soapenv:Body>
256                                                 <req:updateInfraRequest>
257                                                         <requestId>${requestId}</requestId>
258                                                         <lastModifiedBy>BPMN</lastModifiedBy>
259                                                         <requestStatus>COMPLETED</requestStatus>
260                                                         <progress>100</progress>
261                                                 </req:updateInfraRequest>
262                                         </soapenv:Body>
263                                 </soapenv:Envelope>
264                         """
265
266                         updateInfraRequest = utils.formatXml(updateInfraRequest)
267                         execution.setVariable('DELVfModI_updateInfraRequest', updateInfraRequest)
268                         utils.logAudit("DeleteAAIVfModuleInfra Update Request: " + updateInfraRequest)
269                         logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)
270
271                         logDebug('Exited ' + method, isDebugLogEnabled)
272                 } catch (BpmnError e) {
273                         throw e;
274                 } catch (Exception e) {
275                         logError('Caught exception in ' + method, e)
276                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepInfraRequest(): ' + e.getMessage())
277                 }
278         }
279
280         /**
281          * Builds a "CompletionHandler" request and stores it in the specified execution variable.
282          *
283          * @param execution the execution
284          * @param resultVar the execution variable in which the result will be stored
285          */
286         public void completionHandlerPrep(DelegateExecution execution, String resultVar) {
287                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
288                         'execution=' + execution.getId() +
289                         ', resultVar=' + resultVar +
290                         ')'
291                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
292                 logDebug('Entered ' + method, isDebugLogEnabled)
293
294                 try {
295                         def request = execution.getVariable("DeleteVfModuleRequest")
296                         def requestInfo = utils.getNodeXml(request, 'request-info', false)
297                         def action = utils.getNodeText1(requestInfo, "action")
298
299                         String content =
300                                         """  <aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
301                                                    xmlns:ns="http://org.openecomp/mso/request/types/v1"
302                                                    xmlns:ns8="http://org.openecomp/mso/workflow/schema/v1">
303                 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
304                         ${requestInfo}
305                         </request-info>
306                         <ns8:status-message>Vf Module has been deleted successfully.</ns8:status-message>
307                         <ns8:mso-bpel-name>BPMN</ns8:mso-bpel-name>
308                         </aetgt:MsoCompletionRequest>"""
309
310                         content = utils.formatXml(content)
311                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
312                         utils.logAudit("DeleteVfModule Infra Completion Handler Request: " + content)
313                         execution.setVariable(resultVar, content)
314
315                         logDebug('Exited ' + method, isDebugLogEnabled)
316                 } catch (BpmnError e) {
317                         throw e;
318                 } catch (Exception e) {
319                         logError('Caught exception in ' + method, e)
320                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')
321                 }
322         }
323
324         /**
325          * Builds a "FalloutHandler" request and stores it in the specified execution variable.
326          *
327          * @param execution the execution
328          * @param resultVar the execution variable in which the result will be stored
329          */
330         public void falloutHandlerPrep(DelegateExecution execution, String resultVar) {
331                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
332                         'execution=' + execution.getId() +
333                         ', resultVar=' + resultVar +
334                         ')'
335                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
336                 logDebug('Entered ' + method, isDebugLogEnabled)
337
338                 try {
339                         def prefix = execution.getVariable('prefix')
340                         def request = execution.getVariable("DeleteVfModuleRequest")
341                         def requestInfo = utils.getNodeXml(request, 'request-info', false)
342                         def WorkflowException workflowException = execution.getVariable("WorkflowException")
343                         def errorResponseCode = workflowException.getErrorCode()
344                         def errorResponseMsg = workflowException.getErrorMessage()
345                         def encErrorResponseMsg = ""
346                         if (errorResponseMsg != null) {
347                                 encErrorResponseMsg = errorResponseMsg.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
348                         }
349
350                         String content = """
351                         <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
352                                         xmlns:reqtype="http://org.openecomp/mso/request/types/v1"
353                                         xmlns:msoservtypes="http://org.openecomp/mso/request/types/v1"
354                                         xmlns:structuredtypes="http://org.openecomp/mso/structured/types/v1">
355                                         ${requestInfo}
356                                         <sdncadapterworkflow:WorkflowException>
357                                                 <sdncadapterworkflow:ErrorMessage>${encErrorResponseMsg}</sdncadapterworkflow:ErrorMessage>
358                                                 <sdncadapterworkflow:ErrorCode>${errorResponseCode}</sdncadapterworkflow:ErrorCode>
359                                         </sdncadapterworkflow:WorkflowException>
360                                 </sdncadapterworkflow:FalloutHandlerRequest>
361                         """
362                         content = utils.formatXml(content)
363                         utils.logAudit("DeleteVfModuleInfra Fallout Handler Request: " + content)
364                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
365                         execution.setVariable(resultVar, content)
366
367                         logDebug('Exited ' + method, isDebugLogEnabled)
368                 } catch (BpmnError e) {
369                         throw e;
370                 } catch (Exception e) {
371                         logError('Caught exception in ' + method, e)
372                         exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
373                 }
374         }
375 }