2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.infrastructure.scripts
23 import groovy.json.JsonSlurper
24 import groovy.util.Node
25 import groovy.util.XmlParser;
26 import groovy.xml.QName
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
41 public class UpdateVfModuleInfra extends AbstractServiceTaskProcessor {
43 ExceptionUtil exceptionUtil = new ExceptionUtil()
46 * Initialize the flow's variables.
48 * @param execution The flow's execution instance.
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)
67 * Check for missing elements in the received request.
69 * @param execution The flow's execution instance.
71 public void preProcessRequest(Execution execution) {
72 def method = getClass().getSimpleName() + '.preProcessRequest(' +
73 'execution=' + execution.getId() +
75 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
76 logDebug('Entered ' + method, isDebugLogEnabled)
78 initProcessVariables(execution)
80 def prefix = "UPDVfModI_"
82 execution.setVariable("isVidRequest", "false")
84 def incomingRequest = execution.getVariable('bpmnRequest')
86 utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)
88 // check if request is xml or json
90 def jsonSlurper = new JsonSlurper()
91 Map reqMap = jsonSlurper.parseText(incomingRequest)
92 utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
94 def serviceInstanceId = execution.getVariable('serviceInstanceId')
95 def vnfId = execution.getVariable('vnfId')
97 def vidUtils = new VidUtils(this)
99 String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'UPDATE_VF_MODULE', serviceInstanceId)
101 utils.log("DEBUG", " Request in XML format: " + requestInXmlFormat, isDebugLogEnabled)
103 execution.setVariable(prefix + 'Request', requestInXmlFormat)
104 execution.setVariable(prefix+'vnfId', vnfId)
105 execution.setVariable("isVidRequest", "true")
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")
114 String restFaultMessage = e.getMessage()
115 utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
116 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, restFaultMessage)
122 String request = validateInfraRequest(execution)
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'))
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'))
136 def vnfParams = utils.getNodeXml(request, 'vnf-params')
137 execution.setVariable('UPDVfModI_vnfParams', vnfParams)
139 logDebug('Exited ' + method, isDebugLogEnabled)
140 } catch (BpmnError e) {
142 } catch (Exception e) {
143 logError('Caught exception in ' + method, e)
144 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
149 * Prepare and send the sychronous response for this flow.
151 * @param execution The flow's execution instance.
153 public void sendSynchResponse(Execution execution) {
154 def method = getClass().getSimpleName() + '.sendSynchResponse(' +
155 'execution=' + execution.getId() +
157 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
158 logDebug('Entered ' + method, isDebugLogEnabled)
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()) {
169 def startTime = getNodeTextForce(requestInfo, 'start-time')
170 if (startTime.isEmpty()) {
171 startTime = System.currentTimeMillis()
174 // RESTResponse (for API Handler (APIH) Reply Task)
175 def vfModuleId = execution.getVariable("vfModuleId")
176 String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()
178 sendWorkflowResponse(execution, 200, synchResponse)
180 logDebug('Exited ' + method, isDebugLogEnabled)
181 } catch (BpmnError e) {
183 } catch (Exception e) {
184 logError('Caught exception in ' + method, e)
185 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
190 * Prepare the Request for invoking the DoUpdateVfModule subflow.
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
197 * @param execution The flow's execution instance.
199 public void prepDoUpdateVfModule(Execution execution) {
200 def method = getClass().getSimpleName() + '.prepDoUpdateVfModule(' +
201 'execution=' + execution.getId() +
203 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
204 logDebug('Entered ' + method, isDebugLogEnabled)
208 logDebug('Exited ' + method, isDebugLogEnabled)
209 } catch (BpmnError e) {
211 } catch (Exception e) {
212 logError('Caught exception in ' + method, e)
213 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoUpdateVfModule(): ' + e.getMessage())
218 * Prepare the Request for updating the DB for this Infra Request.
220 * @param execution The flow's execution instance.
222 public void prepUpdateInfraRequest(Execution execution) {
223 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
224 'execution=' + execution.getId() +
226 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
227 logDebug('Entered ' + method, isDebugLogEnabled)
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')
236 String updateInfraRequest = """
237 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
238 xmlns:req="http://org.openecomp.mso/requestsdb">
241 <req:updateInfraRequest>
242 <requestId>${requestId}</requestId>
243 <lastModifiedBy>BPEL</lastModifiedBy>
244 <requestStatus>COMPLETE</requestStatus>
245 <progress>100</progress>
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>
252 </req:updateInfraRequest>
257 updateInfraRequest = utils.formatXml(updateInfraRequest)
258 execution.setVariable('UPDVfModI_updateInfraRequest', updateInfraRequest)
259 logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)
261 logDebug('Exited ' + method, isDebugLogEnabled)
262 } catch (BpmnError e) {
264 } catch (Exception e) {
265 logError('Caught exception in ' + method, e)
266 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateInfraRequest(): ' + e.getMessage())
271 * Builds a "CompletionHandler" request and stores it in the specified execution variable.
273 * @param execution the execution
274 * @param resultVar the execution variable in which the result will be stored
276 public void completionHandlerPrep(Execution execution, String resultVar) {
277 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
278 'execution=' + execution.getId() +
279 ', resultVar=' + resultVar +
281 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
282 logDebug('Entered ' + method, isDebugLogEnabled)
285 def requestInfo = getVariable(execution, 'UPDVfModI_requestInfo')
288 <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
289 xmlns:reqtype="http://org.openecomp/mso/request/types/v1">
291 <sdncadapterworkflow:mso-bpel-name>MSO_ACTIVATE_BPEL</sdncadapterworkflow:mso-bpel-name>
292 </sdncadapterworkflow:MsoCompletionRequest>
295 content = utils.formatXml(content)
296 logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
297 execution.setVariable(resultVar, content)
299 logDebug('Exited ' + method, isDebugLogEnabled)
300 } catch (BpmnError e) {
302 } catch (Exception e) {
303 logError('Caught exception in ' + method, e)
304 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')
309 * Builds a "FalloutHandler" request and stores it in the specified execution variable.
311 * @param execution the execution
312 * @param resultVar the execution variable in which the result will be stored
314 public void falloutHandlerPrep(Execution execution, String resultVar) {
315 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
316 'execution=' + execution.getId() +
317 ', resultVar=' + resultVar +
319 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
320 logDebug('Entered ' + method, isDebugLogEnabled)
323 def prefix = execution.getVariable('prefix')
324 def request = getVariable(execution, prefix+'Request')
325 def requestInformation = utils.getNodeXml(request, 'request-info', false)
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("&", "&").replace("<", "<").replace(">", ">")
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>
347 content = utils.formatXml(content)
348 logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
349 execution.setVariable(resultVar, content)
351 logDebug('Exited ' + method, isDebugLogEnabled)
352 } catch (BpmnError e) {
354 } catch (Exception e) {
355 logError('Caught exception in ' + method, e)
356 exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
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
367 public String validateInfraRequest(Execution execution) {
368 def method = getClass().getSimpleName() + '.validateInfraRequest(' +
369 'execution=' + execution.getId() +
371 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
372 logDebug('Entered ' + method, isDebugLogEnabled)
374 String processKey = getProcessKey(execution);
375 def prefix = execution.getVariable("prefix")
377 if (prefix == null) {
378 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " prefix is null")
382 def request = execution.getVariable(prefix + 'Request')
384 if (request == null) {
385 request = execution.getVariable(processKey + 'Request')
387 if (request == null) {
388 request = execution.getVariable('bpmnRequest')
391 setVariable(execution, processKey + 'Request', null);
392 setVariable(execution, 'bpmnRequest', null);
393 setVariable(execution, prefix + 'Request', request);
396 if (request == null) {
397 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request is null")
402 def requestId = execution.getVariable("mso-request-id")
404 if (requestId == null) {
405 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request has no mso-request-id")
408 setVariable(execution, prefix + 'requestId', requestId)
410 def serviceInstanceId = execution.getVariable("mso-service-instance-id")
412 if (serviceInstanceId == null) {
413 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request message has no mso-service-instance-id")
416 utils.logContext(requestId, serviceInstanceId)
418 logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
419 logDebug('Exited ' + method, isDebugLogEnabled)
421 } catch (BpmnError e) {
423 } catch (Exception e) {
424 logError('Caught exception in ' + method, e)
425 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")