Groovy scripts header correction
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / UpdateVfModule.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 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
32 import org.openecomp.mso.bpmn.core.WorkflowException
33 import org.openecomp.mso.bpmn.core.json.JsonUtils;
34 import org.openecomp.mso.rest.APIResponse
35 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
36 import org.openecomp.mso.bpmn.core.RollbackData
37
38
39 public class UpdateVfModule extends AbstractServiceTaskProcessor {
40
41         ExceptionUtil exceptionUtil = new ExceptionUtil()
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', 'UPDVfMod_')
50                 execution.setVariable('UPDVfMod_Request', null)
51                 execution.setVariable('UPDVfMod_requestInfo', null)
52                 execution.setVariable('UPDVfMod_requestId', null)
53                 execution.setVariable('UPDVfMod_source', null)
54                 execution.setVariable('UPDVfMod_vnfInputs', null)
55                 execution.setVariable('UPDVfMod_vnfId', null)
56                 execution.setVariable('UPDVfMod_vfModuleId', null)
57                 execution.setVariable('UPDVfMod_tenantId', null)
58                 execution.setVariable('UPDVfMod_volumeGroupId', null)
59                 execution.setVariable('UPDVfMod_vnfParams', null)
60                 execution.setVariable('UPDVfMod_updateInfraRequest', null)
61                 execution.setVariable('UpdateVfModuleSuccessIndicator', false)
62         }
63
64         /**
65          * Check for missing elements in the received 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
76                 try {
77                         initProcessVariables(execution)
78                         String request = validateRequest(execution)
79
80                         utils.logAudit("UpdateVfModule request: " + request)
81                         def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
82                         execution.setVariable('UPDVfMod_requestInfo', requestInfo)
83                         execution.setVariable('UPDVfMod_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
84                         execution.setVariable('UPDVfMod_source', getNodeTextForce(requestInfo, 'source'))
85
86                         def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
87                         execution.setVariable('UPDVfMod_vnfInputs', vnfInputs)
88                         execution.setVariable('UPDVfMod_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
89                         execution.setVariable('UPDVfMod_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
90                         execution.setVariable('UPDVfMod_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
91                         execution.setVariable('UPDVfMod_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
92
93                         def vnfParams = utils.getNodeXml(request, 'vnf-params')
94                         execution.setVariable('UPDVfMod_vnfParams', vnfParams)
95
96                         logDebug('Exited ' + method, isDebugLogEnabled)
97                 } catch (BpmnError e) {
98                         throw e;
99                 } catch (Exception e) {
100                         logError('Caught exception in ' + method, e)
101                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
102                 }
103         }
104
105         /**
106          * Prepare and send the synchronous response for this flow.
107          *
108          * @param execution The flow's execution instance.
109          */
110         public void sendSynchResponse(Execution execution) {
111                 def method = getClass().getSimpleName() + '.sendSynchResponse(' +
112                         'execution=' + execution.getId() +
113                         ')'
114                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
115                 logDebug('Entered ' + method, isDebugLogEnabled)
116
117                 try {
118                         def requestInfo = execution.getVariable('UPDVfMod_requestInfo')
119                         def requestId = execution.getVariable('UPDVfMod_requestId')
120                         def source = execution.getVariable('UPDVfMod_source')
121                         def progress = getNodeTextForce(requestInfo, 'progress')
122                         if (progress.isEmpty()) {
123                                 progress = '0'
124                         }
125                         def startTime = getNodeTextForce(requestInfo, 'start-time')
126                         if (startTime.isEmpty()) {
127                                 startTime = System.currentTimeMillis()
128                         }
129                         def vnfInputs = execution.getVariable('UPDVfMod_vnfInputs')
130
131                         String synchResponse = """
132                                 <vnf-request xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
133                                         <request-info>
134                                                 <request-id>${requestId}</request-id>
135                                                 <action>UPDATE_VF_MODULE</action>
136                                                 <request-status>IN_PROGRESS</request-status>
137                                                 <progress>${progress}</progress>
138                                                 <start-time>${startTime}</start-time>
139                                                 <source>${source}</source>
140                                         </request-info>
141                                         ${vnfInputs}
142                                 </vnf-request>
143                         """
144
145                         synchResponse = utils.formatXml(synchResponse)
146                         sendWorkflowResponse(execution, 200, synchResponse)
147
148                         utils.logAudit("UpdateVfModule Synch Response: " + synchResponse)
149                         logDebug('Exited ' + method, isDebugLogEnabled)
150                 } catch (BpmnError e) {
151                         throw e;
152                 } catch (Exception e) {
153                         logError('Caught exception in ' + method, e)
154                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendSynchResponse(): ' + e.getMessage())
155                 }
156         }
157
158         /**
159          * Prepare the Request for invoking the DoUpdateVfModule subflow.
160          *
161          * NOTE: Currently, the method just logs passing through as the
162          * incoming Request to this main flow is used as the Request to
163          * the DoUpdateVfModule subflow. No preparation processing is
164          * necessary.
165          *
166          * @param execution The flow's execution instance.
167          */
168         public void prepDoUpdateVfModule(Execution execution) {
169                 def method = getClass().getSimpleName() + '.prepDoUpdateVfModule(' +
170                         'execution=' + execution.getId() +
171                         ')'
172                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
173                 logDebug('Entered ' + method, isDebugLogEnabled)
174
175                 try {
176
177                         logDebug('Exited ' + method, isDebugLogEnabled)
178                 } catch (BpmnError e) {
179                         throw e;
180                 } catch (Exception e) {
181                         logError('Caught exception in ' + method, e)
182                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoUpdateVfModule(): ' + e.getMessage())
183                 }
184         }
185
186         /**
187          * Prepare the Request for updating the DB for this Infra Request.
188          *
189          * @param execution The flow's execution instance.
190          */
191         public void prepUpdateInfraRequest(Execution execution) {
192                 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
193                         'execution=' + execution.getId() +
194                         ')'
195                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
196                 logDebug('Entered ' + method, isDebugLogEnabled)
197
198                 try {
199                         def requestId = execution.getVariable('UPDVfMod_requestId')
200                         def vnfId = execution.getVariable('UPDVfMod_vnfId')
201                         def vfModuleId = execution.getVariable('UPDVfMod_vfModuleId')
202                         def tenantId = execution.getVariable('UPDVfMod_tenantId')
203                         def volumeGroupId = execution.getVariable('UPDVfMod_volumeGroupId')
204
205                         String updateInfraRequest = """
206                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
207                                                 xmlns:req="http://org.openecomp.mso/requestsdb">
208                                         <soapenv:Header/>
209                                         <soapenv:Body>
210                                                 <req:updateInfraRequest>
211                                                         <requestId>${requestId}</requestId>
212                                                         <lastModifiedBy>BPEL</lastModifiedBy>
213                                                         <requestStatus>COMPLETE</requestStatus>
214                                                         <progress>100</progress>
215                                                         <vnfOutputs>
216                                                                 <vnf-id>${vnfId}</vnf-id>
217                                                                 <vf-module-id>${vfModuleId}</vf-module-id>
218                                                                 <tenant-id>${tenantId}</tenant-id>
219                                                                 <volume-group-id>${volumeGroupId}</volume-group-id>
220                                                         </vnfOutputs>
221                                                 </req:updateInfraRequest>
222                                         </soapenv:Body>
223                                 </soapenv:Envelope>
224                         """
225
226                         updateInfraRequest = utils.formatXml(updateInfraRequest)
227                         execution.setVariable('UPDVfMod_updateInfraRequest', updateInfraRequest)
228                         logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)
229
230                         utils.logAudit("UpdateVfModule Infra Request: " + updateInfraRequest)
231                         logDebug('Exited ' + method, isDebugLogEnabled)
232                 } catch (BpmnError e) {
233                         throw e;
234                 } catch (Exception e) {
235                         logError('Caught exception in ' + method, e)
236                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateInfraRequest(): ' + e.getMessage())
237                 }
238         }
239
240         /**
241          * Builds a "CompletionHandler" request and stores it in the specified execution variable.
242          *
243          * @param execution the execution
244          * @param resultVar the execution variable in which the result will be stored
245          */
246         public void completionHandlerPrep(Execution execution, String resultVar) {
247                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
248                         'execution=' + execution.getId() +
249                         ', resultVar=' + resultVar +
250                         ')'
251                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
252                 logDebug('Entered ' + method, isDebugLogEnabled)
253
254                 try {
255                         def requestInfo = getVariable(execution, 'UPDVfMod_requestInfo')
256
257                         String content = """
258                                 <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
259                                                 xmlns:reqtype="http://org.openecomp/mso/request/types/v1">
260                                         ${requestInfo}
261                                         <sdncadapterworkflow:mso-bpel-name>MSO_ACTIVATE_BPEL</sdncadapterworkflow:mso-bpel-name>
262                                 </sdncadapterworkflow:MsoCompletionRequest>
263                         """
264
265                         content = utils.formatXml(content)
266                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
267                         execution.setVariable(resultVar, content)
268
269                         utils.logAudit("UpdateVfModule CompletionHandler Request: " + content)
270                         logDebug('Exited ' + method, isDebugLogEnabled)
271                 } catch (BpmnError e) {
272                         throw e;
273                 } catch (Exception e) {
274                         logError('Caught exception in ' + method, e)
275                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')
276                 }
277         }
278
279         /**
280          * Builds a "FalloutHandler" request and stores it in the specified execution variable.
281          *
282          * @param execution the execution
283          * @param resultVar the execution variable in which the result will be stored
284          */
285         public void falloutHandlerPrep(Execution execution, String resultVar) {
286                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
287                         'execution=' + execution.getId() +
288                         ', resultVar=' + resultVar +
289                         ')'
290                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
291                 logDebug('Entered ' + method, isDebugLogEnabled)
292
293                 try {
294                         def prefix = execution.getVariable('prefix')
295                         def request = getVariable(execution, prefix+'Request')
296                         def requestInformation = utils.getNodeXml(request, 'request-information', false)
297
298                         def WorkflowException workflowException = execution.getVariable("WorkflowException")
299                         def errorResponseCode = workflowException.getErrorCode()
300                         def errorResponseMsg = workflowException.getErrorMessage()
301                         def encErrorResponseMsg = ""
302                         if (errorResponseMsg != null) {
303                                 encErrorResponseMsg = errorResponseMsg.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
304                         }
305
306                         String content = """
307                                 <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
308                                                 xmlns:reqtype="http://org.openecomp/mso/request/types/v1"
309                                                 xmlns:msoservtypes="http://org.openecomp/mso/request/types/v1"
310                                                 xmlns:structuredtypes="http://org.openecomp/mso/structured/types/v1">
311                                         ${requestInformation}
312                                         <sdncadapterworkflow:WorkflowException>
313                                                 <sdncadapterworkflow:ErrorMessage>${encErrorResponseMsg}</sdncadapterworkflow:ErrorMessage>
314                                                 <sdncadapterworkflow:ErrorCode>${errorResponseCode}</sdncadapterworkflow:ErrorCode>
315                                         </sdncadapterworkflow:WorkflowException>
316                                 </sdncadapterworkflow:FalloutHandlerRequest>
317                         """
318                         content = utils.formatXml(content)
319                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
320                         execution.setVariable(resultVar, content)
321
322                         utils.logAudit("UpdateVfModule fallOutHandler Request: " + content)
323                         logDebug('Exited ' + method, isDebugLogEnabled)
324                 } catch (BpmnError e) {
325                         throw e;
326                 } catch (Exception e) {
327                         logError('Caught exception in ' + method, e)
328                         exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
329                 }
330         }
331 }