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