[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / UpdateVfModuleInfra.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.json.JsonSlurper
24 import groovy.util.Node
25 import groovy.util.XmlParser;
26 import groovy.xml.QName
27
28 import java.io.Serializable;
29
30 import org.camunda.bpm.engine.delegate.BpmnError
31 import org.camunda.bpm.engine.runtime.Execution
32 import org.openecomp.mso.rest.APIResponse
33 import org.openecomp.mso.rest.RESTClient
34 import org.openecomp.mso.rest.RESTConfig
35 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
36 import org.openecomp.mso.bpmn.common.scripts.VidUtils;
37 import org.openecomp.mso.bpmn.core.RollbackData
38 import org.openecomp.mso.bpmn.core.WorkflowException
39
40
41 public class UpdateVfModuleInfra 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', 'UPDVfModI_')
50                 execution.setVariable('UPDVfModI_Request', null)
51                 execution.setVariable('UPDVfModI_requestInfo', null)
52                 execution.setVariable('UPDVfModI_requestId', null)
53                 execution.setVariable('UPDVfModI_source', null)
54                 execution.setVariable('UPDVfModI_vnfInputs', null)
55                 execution.setVariable('UPDVfModI_vnfId', null)
56                 execution.setVariable('UPDVfModI_vfModuleId', null)
57                 execution.setVariable('UPDVfModI_tenantId', null)
58                 execution.setVariable('UPDVfModI_volumeGroupId', null)
59                 execution.setVariable('UPDVfModI_vnfParams', null)
60                 execution.setVariable('UPDVfModI_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                 initProcessVariables(execution)
77                 
78                 def prefix = "UPDVfModI_"
79                 
80                 execution.setVariable("isVidRequest", "false")
81                 
82                 def incomingRequest = execution.getVariable('bpmnRequest')
83                 
84                 utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)
85                 
86                 // check if request is xml or json
87                 try {
88                         def jsonSlurper = new JsonSlurper()
89                         Map reqMap = jsonSlurper.parseText(incomingRequest)
90                         utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
91                         
92                         def serviceInstanceId = execution.getVariable('serviceInstanceId')
93                         def vnfId = execution.getVariable('vnfId')
94                         
95                         def vidUtils = new VidUtils(this)
96                         
97                         String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'UPDATE_VF_MODULE', serviceInstanceId)
98                         
99                         utils.log("DEBUG", " Request in XML format: " + requestInXmlFormat, isDebugLogEnabled)
100                         
101                         execution.setVariable(prefix + 'Request', requestInXmlFormat)
102                         execution.setVariable(prefix+'vnfId', vnfId)
103                         execution.setVariable("isVidRequest", "true")                   
104                         
105                 }
106                 catch(groovy.json.JsonException je) {
107                         utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
108                         workflowException(execution, "Invalid request format", 400)
109                         
110                 }
111                 catch(Exception e) {
112                         String restFaultMessage = e.getMessage()                        
113                         utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
114                         workflowException(execution, restFaultMessage, 400)
115                 }
116                                 
117
118                 try {
119                         
120                         String request = validateInfraRequest(execution)
121                         
122                         def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
123                         execution.setVariable('UPDVfModI_requestInfo', requestInfo)
124                         execution.setVariable('UPDVfModI_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
125                         execution.setVariable('UPDVfModI_source', getNodeTextForce(requestInfo, 'source'))
126                         
127                         def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
128                         execution.setVariable('UPDVfModI_vnfInputs', vnfInputs)
129                         execution.setVariable('UPDVfModI_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
130                         execution.setVariable('UPDVfModI_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
131                         execution.setVariable('UPDVfModI_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
132                         execution.setVariable('UPDVfModI_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
133
134                         def vnfParams = utils.getNodeXml(request, 'vnf-params')
135                         execution.setVariable('UPDVfModI_vnfParams', vnfParams)
136
137                         logDebug('Exited ' + method, isDebugLogEnabled)
138                 } catch (BpmnError e) {
139                         throw e;
140                 } catch (Exception e) {
141                         logError('Caught exception in ' + method, e)
142                         createWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
143                 }
144         }
145         
146         /**
147          * Prepare and send the sychronous response for this flow.
148          * 
149          * @param execution The flow's execution instance.
150          */
151         public void sendSynchResponse(Execution execution) {
152                 def method = getClass().getSimpleName() + '.sendSynchResponse(' +
153                         'execution=' + execution.getId() +
154                         ')'
155                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
156                 logDebug('Entered ' + method, isDebugLogEnabled)
157
158                 
159                 try {
160                         def requestInfo = execution.getVariable('UPDVfModI_requestInfo')
161                         def requestId = execution.getVariable('UPDVfModI_requestId')
162                         def source = execution.getVariable('UPDVfModI_source')
163                         def progress = getNodeTextForce(requestInfo, 'progress')
164                         if (progress.isEmpty()) {
165                                 progress = '0'
166                         }
167                         def startTime = getNodeTextForce(requestInfo, 'start-time')
168                         if (startTime.isEmpty()) {
169                                 startTime = System.currentTimeMillis()
170                         }                       
171                                 
172                         // RESTResponse (for API Handler (APIH) Reply Task)
173                         def vfModuleId = execution.getVariable("vfModuleId")
174                         String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()
175                         
176                         sendWorkflowResponse(execution, 200, synchResponse)
177
178                         logDebug('Exited ' + method, isDebugLogEnabled)
179                 } catch (BpmnError e) {
180                         throw e;
181                 } catch (Exception e) {
182                         logError('Caught exception in ' + method, e)
183                         createWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
184                 }
185         }
186         
187         /**
188          * Prepare the Request for invoking the DoUpdateVfModule subflow.
189          * 
190          * NOTE: Currently, the method just logs passing through as the
191          * incoming Request to this main flow is used as the Request to
192          * the DoUpdateVfModule subflow. No preparation processing is
193          * necessary.
194          * 
195          * @param execution The flow's execution instance.
196          */
197         public void prepDoUpdateVfModule(Execution execution) {
198                 def method = getClass().getSimpleName() + '.prepDoUpdateVfModule(' +
199                         'execution=' + execution.getId() +
200                         ')'
201                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
202                 logDebug('Entered ' + method, isDebugLogEnabled)
203
204                 try {
205                         
206                         logDebug('Exited ' + method, isDebugLogEnabled)
207                 } catch (BpmnError e) {
208                         throw e;
209                 } catch (Exception e) {
210                         logError('Caught exception in ' + method, e)
211                         createWorkflowException(execution, 1002, 'Error in prepDoUpdateVfModule(): ' + e.getMessage())
212                 }
213         }
214         
215         /**
216          * Prepare the Request for updating the DB for this Infra Request.
217          * 
218          * @param execution The flow's execution instance.
219          */
220         public void prepUpdateInfraRequest(Execution execution) {
221                 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
222                         'execution=' + execution.getId() +
223                         ')'
224                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
225                 logDebug('Entered ' + method, isDebugLogEnabled)
226
227                 try {
228                         def requestId = execution.getVariable('UPDVfModI_requestId')
229                         def vnfId = execution.getVariable('UPDVfModI_vnfId')
230                         def vfModuleId = execution.getVariable('UPDVfModI_vfModuleId')
231                         def tenantId = execution.getVariable('UPDVfModI_tenantId')
232                         def volumeGroupId = execution.getVariable('UPDVfModI_volumeGroupId')
233                         
234                         String updateInfraRequest = """
235                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
236                                                 xmlns:req="http://org.openecomp.mso/requestsdb">
237                                         <soapenv:Header/>
238                                         <soapenv:Body>
239                                                 <req:updateInfraRequest>
240                                                         <requestId>${requestId}</requestId>
241                                                         <lastModifiedBy>BPEL</lastModifiedBy>
242                                                         <requestStatus>COMPLETE</requestStatus>
243                                                         <progress>100</progress>
244                                                         <vnfOutputs>
245                                                                 <vnf-id>${vnfId}</vnf-id>
246                                                                 <vf-module-id>${vfModuleId}</vf-module-id>
247                                                                 <tenant-id>${tenantId}</tenant-id>
248                                                                 <volume-group-id>${volumeGroupId}</volume-group-id>
249                                                         </vnfOutputs>
250                                                 </req:updateInfraRequest>
251                                         </soapenv:Body>
252                                 </soapenv:Envelope>
253                         """
254
255                         updateInfraRequest = utils.formatXml(updateInfraRequest)
256                         execution.setVariable('UPDVfModI_updateInfraRequest', updateInfraRequest)
257                         logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)
258
259                         logDebug('Exited ' + method, isDebugLogEnabled)
260                 } catch (BpmnError e) {
261                         throw e;
262                 } catch (Exception e) {
263                         logError('Caught exception in ' + method, e)
264                         createWorkflowException(execution, 1002, 'Error in prepUpdateInfraRequest(): ' + e.getMessage())
265                 }
266         }
267         
268         /**
269          * Builds a "CompletionHandler" request and stores it in the specified execution variable.
270          * 
271          * @param execution the execution
272          * @param resultVar the execution variable in which the result will be stored
273          */
274         public void completionHandlerPrep(Execution execution, String resultVar) {
275                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
276                         'execution=' + execution.getId() +
277                         ', resultVar=' + resultVar +
278                         ')'
279                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
280                 logDebug('Entered ' + method, isDebugLogEnabled)
281
282                 try {
283                         def requestInfo = getVariable(execution, 'UPDVfModI_requestInfo')
284                         
285                         String content = """
286                                 <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
287                                                 xmlns:reqtype="http://org.openecomp/mso/request/types/v1">
288                                         ${requestInfo}
289                                         <sdncadapterworkflow:mso-bpel-name>MSO_ACTIVATE_BPEL</sdncadapterworkflow:mso-bpel-name>
290                                 </sdncadapterworkflow:MsoCompletionRequest>
291                         """
292
293                         content = utils.formatXml(content)
294                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
295                         execution.setVariable(resultVar, content)
296
297                         logDebug('Exited ' + method, isDebugLogEnabled)
298                 } catch (BpmnError e) {
299                         throw e;
300                 } catch (Exception e) {
301                         logError('Caught exception in ' + method, e)
302                         createWorkflowException(execution, 2000, 'Internal Error')
303                 }
304         }
305
306         /**
307          * Builds a "FalloutHandler" request and stores it in the specified execution variable.
308          * 
309          * @param execution the execution
310          * @param resultVar the execution variable in which the result will be stored
311          */
312         public void falloutHandlerPrep(Execution execution, String resultVar) {
313                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
314                         'execution=' + execution.getId() +
315                         ', resultVar=' + resultVar +
316                         ')'
317                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
318                 logDebug('Entered ' + method, isDebugLogEnabled)
319                 
320                 try {
321                         def prefix = execution.getVariable('prefix')
322                         def request = getVariable(execution, prefix+'Request')
323                         def requestInformation = utils.getNodeXml(request, 'request-info', false)
324                         
325                         def WorkflowException workflowException = execution.getVariable("WorkflowException")
326                         def errorResponseCode = workflowException.getErrorCode()
327                         def errorResponseMsg = workflowException.getErrorMessage()
328                         def encErrorResponseMsg = ""
329                         if (errorResponseMsg != null) {
330                                 encErrorResponseMsg = errorResponseMsg.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
331                         }
332
333                         String content = """
334                                 <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
335                                                 xmlns:reqtype="http://org.openecomp/mso/request/types/v1"
336                                                 xmlns:msoservtypes="http://org.openecomp/mso/request/types/v1"
337                                                 xmlns:structuredtypes="http://org.openecomp/mso/structured/types/v1">
338                                         ${requestInformation}
339                                         <sdncadapterworkflow:WorkflowException>
340                                                 <sdncadapterworkflow:ErrorMessage>${encErrorResponseMsg}</sdncadapterworkflow:ErrorMessage>
341                                                 <sdncadapterworkflow:ErrorCode>${errorResponseCode}</sdncadapterworkflow:ErrorCode>
342                                         </sdncadapterworkflow:WorkflowException>        
343                                 </sdncadapterworkflow:FalloutHandlerRequest>
344                         """
345                         content = utils.formatXml(content)
346                         logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
347                         execution.setVariable(resultVar, content)
348
349                         logDebug('Exited ' + method, isDebugLogEnabled)
350                 } catch (BpmnError e) {
351                         throw e;
352                 } catch (Exception e) {
353                         logError('Caught exception in ' + method, e)
354                         createWorkflowException(execution, 2000, 'Internal Error')
355                 }
356         }
357         
358         /**
359          * Validates the request, request id and service instance id.  If a problem is found,
360          * a WorkflowException is generated and an MSOWorkflowException event is thrown. This
361          * method also sets up the log context for the workflow.
362          * @param execution the execution
363          * @return the validated request
364          */
365         public String validateInfraRequest(Execution execution) {
366                 def method = getClass().getSimpleName() + '.validateInfraRequest(' +
367                         'execution=' + execution.getId() +
368                         ')'
369                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
370                 logDebug('Entered ' + method, isDebugLogEnabled)
371
372                 String processKey = getProcessKey(execution);
373                 def prefix = execution.getVariable("prefix")
374
375                 if (prefix == null) {
376                         createWorkflowException(execution, 1002, processKey + " prefix is null")
377                 }
378
379                 try {
380                         def request = execution.getVariable(prefix + 'Request')
381
382                         if (request == null) {
383                                 request = execution.getVariable(processKey + 'Request')
384         
385                                 if (request == null) {
386                                         request = execution.getVariable('bpmnRequest')
387                                 }
388         
389                                 setVariable(execution, processKey + 'Request', null);
390                                 setVariable(execution, 'bpmnRequest', null);
391                                 setVariable(execution, prefix + 'Request', request);
392                         }
393
394                         if (request == null) {
395                                 createWorkflowException(execution, 1002, processKey + " request is null")
396                         }
397                         
398                         /*
399
400                         def requestId = execution.getVariable("mso-request-id")
401                         
402                         if (requestId == null) {
403                                 createWorkflowException(execution, 1002, processKey + " request has no mso-request-id")
404                         }
405                         
406                         setVariable(execution, prefix + 'requestId', requestId)
407
408                         def serviceInstanceId = execution.getVariable("mso-service-instance-id")
409
410                         if (serviceInstanceId == null) {
411                                 createWorkflowException(execution, 1002, processKey + " request message has no mso-service-instance-id")
412                         }
413
414                         utils.logContext(requestId, serviceInstanceId)
415                         */
416                         logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
417                         logDebug('Exited ' + method, isDebugLogEnabled)
418                         return request
419                 } catch (BpmnError e) {
420                         throw e;
421                 } catch (Exception e) {
422                         logError('Caught exception in ' + method, e)
423                         createWorkflowException(execution, 1002, "Invalid Message")
424                 }
425         }
426         
427 }