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