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;
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 {
44 * Initialize the flow's variables.
46 * @param execution The flow's execution instance.
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)
65 * Check for missing elements in the received request.
67 * @param execution The flow's execution instance.
69 public void preProcessRequest(Execution execution) {
70 def method = getClass().getSimpleName() + '.preProcessRequest(' +
71 'execution=' + execution.getId() +
73 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
74 logDebug('Entered ' + method, isDebugLogEnabled)
76 initProcessVariables(execution)
78 def prefix = "UPDVfModI_"
80 execution.setVariable("isVidRequest", "false")
82 def incomingRequest = execution.getVariable('bpmnRequest')
84 utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)
86 // check if request is xml or json
88 def jsonSlurper = new JsonSlurper()
89 Map reqMap = jsonSlurper.parseText(incomingRequest)
90 utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
92 def serviceInstanceId = execution.getVariable('serviceInstanceId')
93 def vnfId = execution.getVariable('vnfId')
95 def vidUtils = new VidUtils(this)
97 String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'UPDATE_VF_MODULE', serviceInstanceId)
99 utils.log("DEBUG", " Request in XML format: " + requestInXmlFormat, isDebugLogEnabled)
101 execution.setVariable(prefix + 'Request', requestInXmlFormat)
102 execution.setVariable(prefix+'vnfId', vnfId)
103 execution.setVariable("isVidRequest", "true")
106 catch(groovy.json.JsonException je) {
107 utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
108 workflowException(execution, "Invalid request format", 400)
112 String restFaultMessage = e.getMessage()
113 utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
114 workflowException(execution, restFaultMessage, 400)
120 String request = validateInfraRequest(execution)
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'))
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'))
134 def vnfParams = utils.getNodeXml(request, 'vnf-params')
135 execution.setVariable('UPDVfModI_vnfParams', vnfParams)
137 logDebug('Exited ' + method, isDebugLogEnabled)
138 } catch (BpmnError e) {
140 } catch (Exception e) {
141 logError('Caught exception in ' + method, e)
142 createWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
147 * Prepare and send the sychronous response for this flow.
149 * @param execution The flow's execution instance.
151 public void sendSynchResponse(Execution execution) {
152 def method = getClass().getSimpleName() + '.sendSynchResponse(' +
153 'execution=' + execution.getId() +
155 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
156 logDebug('Entered ' + method, isDebugLogEnabled)
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()) {
167 def startTime = getNodeTextForce(requestInfo, 'start-time')
168 if (startTime.isEmpty()) {
169 startTime = System.currentTimeMillis()
172 // RESTResponse (for API Handler (APIH) Reply Task)
173 def vfModuleId = execution.getVariable("vfModuleId")
174 String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()
176 sendWorkflowResponse(execution, 200, synchResponse)
178 logDebug('Exited ' + method, isDebugLogEnabled)
179 } catch (BpmnError e) {
181 } catch (Exception e) {
182 logError('Caught exception in ' + method, e)
183 createWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
188 * Prepare the Request for invoking the DoUpdateVfModule subflow.
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
195 * @param execution The flow's execution instance.
197 public void prepDoUpdateVfModule(Execution execution) {
198 def method = getClass().getSimpleName() + '.prepDoUpdateVfModule(' +
199 'execution=' + execution.getId() +
201 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
202 logDebug('Entered ' + method, isDebugLogEnabled)
206 logDebug('Exited ' + method, isDebugLogEnabled)
207 } catch (BpmnError e) {
209 } catch (Exception e) {
210 logError('Caught exception in ' + method, e)
211 createWorkflowException(execution, 1002, 'Error in prepDoUpdateVfModule(): ' + e.getMessage())
216 * Prepare the Request for updating the DB for this Infra Request.
218 * @param execution The flow's execution instance.
220 public void prepUpdateInfraRequest(Execution execution) {
221 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
222 'execution=' + execution.getId() +
224 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
225 logDebug('Entered ' + method, isDebugLogEnabled)
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')
234 String updateInfraRequest = """
235 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
236 xmlns:req="http://org.openecomp.mso/requestsdb">
239 <req:updateInfraRequest>
240 <requestId>${requestId}</requestId>
241 <lastModifiedBy>BPEL</lastModifiedBy>
242 <requestStatus>COMPLETE</requestStatus>
243 <progress>100</progress>
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>
250 </req:updateInfraRequest>
255 updateInfraRequest = utils.formatXml(updateInfraRequest)
256 execution.setVariable('UPDVfModI_updateInfraRequest', updateInfraRequest)
257 logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)
259 logDebug('Exited ' + method, isDebugLogEnabled)
260 } catch (BpmnError e) {
262 } catch (Exception e) {
263 logError('Caught exception in ' + method, e)
264 createWorkflowException(execution, 1002, 'Error in prepUpdateInfraRequest(): ' + e.getMessage())
269 * Builds a "CompletionHandler" request and stores it in the specified execution variable.
271 * @param execution the execution
272 * @param resultVar the execution variable in which the result will be stored
274 public void completionHandlerPrep(Execution execution, String resultVar) {
275 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
276 'execution=' + execution.getId() +
277 ', resultVar=' + resultVar +
279 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
280 logDebug('Entered ' + method, isDebugLogEnabled)
283 def requestInfo = getVariable(execution, 'UPDVfModI_requestInfo')
286 <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
287 xmlns:reqtype="http://org.openecomp/mso/request/types/v1">
289 <sdncadapterworkflow:mso-bpel-name>MSO_ACTIVATE_BPEL</sdncadapterworkflow:mso-bpel-name>
290 </sdncadapterworkflow:MsoCompletionRequest>
293 content = utils.formatXml(content)
294 logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
295 execution.setVariable(resultVar, content)
297 logDebug('Exited ' + method, isDebugLogEnabled)
298 } catch (BpmnError e) {
300 } catch (Exception e) {
301 logError('Caught exception in ' + method, e)
302 createWorkflowException(execution, 2000, 'Internal Error')
307 * Builds a "FalloutHandler" request and stores it in the specified execution variable.
309 * @param execution the execution
310 * @param resultVar the execution variable in which the result will be stored
312 public void falloutHandlerPrep(Execution execution, String resultVar) {
313 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
314 'execution=' + execution.getId() +
315 ', resultVar=' + resultVar +
317 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
318 logDebug('Entered ' + method, isDebugLogEnabled)
321 def prefix = execution.getVariable('prefix')
322 def request = getVariable(execution, prefix+'Request')
323 def requestInformation = utils.getNodeXml(request, 'request-info', false)
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("&", "&").replace("<", "<").replace(">", ">")
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>
345 content = utils.formatXml(content)
346 logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
347 execution.setVariable(resultVar, content)
349 logDebug('Exited ' + method, isDebugLogEnabled)
350 } catch (BpmnError e) {
352 } catch (Exception e) {
353 logError('Caught exception in ' + method, e)
354 createWorkflowException(execution, 2000, 'Internal Error')
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
365 public String validateInfraRequest(Execution execution) {
366 def method = getClass().getSimpleName() + '.validateInfraRequest(' +
367 'execution=' + execution.getId() +
369 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
370 logDebug('Entered ' + method, isDebugLogEnabled)
372 String processKey = getProcessKey(execution);
373 def prefix = execution.getVariable("prefix")
375 if (prefix == null) {
376 createWorkflowException(execution, 1002, processKey + " prefix is null")
380 def request = execution.getVariable(prefix + 'Request')
382 if (request == null) {
383 request = execution.getVariable(processKey + 'Request')
385 if (request == null) {
386 request = execution.getVariable('bpmnRequest')
389 setVariable(execution, processKey + 'Request', null);
390 setVariable(execution, 'bpmnRequest', null);
391 setVariable(execution, prefix + 'Request', request);
394 if (request == null) {
395 createWorkflowException(execution, 1002, processKey + " request is null")
400 def requestId = execution.getVariable("mso-request-id")
402 if (requestId == null) {
403 createWorkflowException(execution, 1002, processKey + " request has no mso-request-id")
406 setVariable(execution, prefix + 'requestId', requestId)
408 def serviceInstanceId = execution.getVariable("mso-service-instance-id")
410 if (serviceInstanceId == null) {
411 createWorkflowException(execution, 1002, processKey + " request message has no mso-service-instance-id")
414 utils.logContext(requestId, serviceInstanceId)
416 logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
417 logDebug('Exited ' + method, isDebugLogEnabled)
419 } catch (BpmnError e) {
421 } catch (Exception e) {
422 logError('Caught exception in ' + method, e)
423 createWorkflowException(execution, 1002, "Invalid Message")