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