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.json.JsonOutput
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.apache.commons.lang3.*
29 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
30 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
31 import org.openecomp.mso.bpmn.common.scripts.NetworkUtils;
32 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils;
33 import org.openecomp.mso.bpmn.common.scripts.VidUtils;
34 import org.openecomp.mso.bpmn.core.RollbackData
35 import org.openecomp.mso.bpmn.core.WorkflowException
36 import org.openecomp.mso.bpmn.core.json.JsonUtils
38 public class CreateVfModuleInfra extends AbstractServiceTaskProcessor {
40 ExceptionUtil exceptionUtil = new ExceptionUtil()
41 JsonUtils jsonUtil = new JsonUtils()
44 * Validates the request message and sets up the workflow.
45 * @param execution the execution
47 public void preProcessRequest(DelegateExecution execution) {
48 def method = getClass().getSimpleName() + '.preProcessRequest(' +
49 'execution=' + execution.getId() +
51 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
52 logDebug('Entered ' + method, isDebugLogEnabled)
54 execution.setVariable("CVFMI_sentSyncResponse", false)
57 logDebug('Entered 1' + method, isDebugLogEnabled)
58 execution.setVariable('prefix', prefix)
59 logDebug('Entered 2' + method, isDebugLogEnabled)
60 execution.setVariable("isVidRequest", "false")
62 logDebug("Set variables", isDebugLogEnabled)
64 def rollbackData = execution.getVariable("RollbackData")
65 if (rollbackData == null) {
66 rollbackData = new RollbackData()
68 execution.setVariable("RollbackData", rollbackData)
70 logDebug("Set rollback data", isDebugLogEnabled)
71 def incomingRequest = execution.getVariable('bpmnRequest')
73 utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)
74 utils.logAudit("CreateVfModule Infra incoming Request: " + incomingRequest)
76 setBasicDBAuthHeader(execution, isDebugLogEnabled)
78 // check if request is xml or json
80 def jsonSlurper = new JsonSlurper()
81 def jsonOutput = new JsonOutput()
82 Map reqMap = jsonSlurper.parseText(incomingRequest)
83 utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
85 def serviceInstanceId = execution.getVariable('serviceInstanceId')
86 def vnfId = execution.getVariable('vnfId')
88 execution.setVariable(prefix + 'serviceInstanceId', serviceInstanceId)
89 execution.setVariable(prefix+'vnfId', vnfId)
90 execution.setVariable("isVidRequest", "true")
93 def asdcServiceModelVersion = ''
94 def serviceModelInfo = null
95 def vnfModelInfo = null
97 def relatedInstanceList = reqMap.requestDetails?.relatedInstanceList
99 if (relatedInstanceList != null) {
100 relatedInstanceList.each {
101 if (it.relatedInstance.modelInfo?.modelType == 'service') {
102 asdcServiceModelVersion = it.relatedInstance.modelInfo?.modelVersion
103 serviceModelInfo = jsonOutput.toJson(it.relatedInstance.modelInfo)
106 if (it.relatedInstance.modelInfo.modelType == 'vnf') {
107 vnfName = it.relatedInstance.instanceName ?: ''
108 vnfModelInfo = jsonOutput.toJson(it.relatedInstance.modelInfo)
113 execution.setVariable(prefix + 'vnfName', vnfName)
114 execution.setVariable(prefix + 'asdcServiceModelVersion', asdcServiceModelVersion)
115 execution.setVariable(prefix + 'serviceModelInfo', serviceModelInfo)
116 execution.setVariable(prefix + 'vnfModelInfo', vnfModelInfo)
119 def vnfType = execution.getVariable('vnfType')
120 execution.setVariable(prefix + 'vnfType', vnfType)
121 def vfModuleId = execution.getVariable('vfModuleId')
122 execution.setVariable(prefix + 'vfModuleId', vfModuleId)
123 def volumeGroupId = execution.getVariable('volumeGroupId')
124 execution.setVariable(prefix + 'volumeGroupId', volumeGroupId)
125 def userParams = reqMap.requestDetails?.requestParameters?.userParams
127 Map<String, String> userParamsMap = [:]
128 if (userParams != null) {
129 userParams.each { userParam ->
130 userParamsMap.put(userParam.name, jsonOutput.toJson(userParam.value).toString())
134 utils.log("DEBUG", 'Processed user params: ' + userParamsMap, isDebugLogEnabled)
136 execution.setVariable(prefix + 'vfModuleInputParams', userParamsMap)
138 def isBaseVfModule = "false"
139 if (execution.getVariable('isBaseVfModule') == true) {
140 isBaseVfModule = "true"
143 execution.setVariable(prefix + 'isBaseVfModule', isBaseVfModule)
145 def requestId = execution.getVariable("mso-request-id")
146 execution.setVariable(prefix + 'requestId', requestId)
148 def vfModuleModelInfo = jsonOutput.toJson(reqMap.requestDetails?.modelInfo)
149 execution.setVariable(prefix + 'vfModuleModelInfo', vfModuleModelInfo)
151 def suppressRollback = reqMap.requestDetails?.requestInfo?.suppressRollback
154 def backoutOnFailure = ""
155 if(suppressRollback != null){
156 if ( suppressRollback == true) {
157 backoutOnFailure = "false"
158 } else if ( suppressRollback == false) {
159 backoutOnFailure = "true"
163 execution.setVariable('disableRollback', suppressRollback)
165 def vfModuleName = reqMap.requestDetails?.requestInfo?.instanceName ?: null
166 execution.setVariable(prefix + 'vfModuleName', vfModuleName)
168 def serviceId = reqMap.requestDetails?.requestParameters?.serviceId ?: ''
169 execution.setVariable(prefix + 'serviceId', serviceId)
171 def usePreload = reqMap.requestDetails?.requestParameters?.usePreload
172 execution.setVariable(prefix + 'usePreload', usePreload)
174 // This is aLaCarte flow, so aLaCarte flag is always on
175 execution.setVariable(prefix + 'aLaCarte', true)
177 def cloudConfiguration = reqMap.requestDetails?.cloudConfiguration
178 def lcpCloudRegionId = cloudConfiguration.lcpCloudRegionId
179 execution.setVariable(prefix + 'lcpCloudRegionId', lcpCloudRegionId)
180 def tenantId = cloudConfiguration.tenantId
181 execution.setVariable(prefix + 'tenantId', tenantId)
183 def globalSubscriberId = reqMap.requestDetails?.subscriberInfo?.globalSubscriberId ?: ''
184 execution.setVariable(prefix + 'globalSubscriberId', globalSubscriberId)
186 execution.setVariable(prefix + 'sdncVersion', '1702')
188 execution.setVariable("CreateVfModuleInfraSuccessIndicator", false)
189 execution.setVariable("RollbackCompleted", false)
191 execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
194 def source = reqMap.requestDetails?.requestInfo?.source
195 execution.setVariable("CVFMI_source", source)
197 //For Completion Handler & Fallout Handler
199 """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
200 <request-id>${requestId}</request-id>
201 <action>CREATE</action>
202 <source>${source}</source>
205 execution.setVariable("CVFMI_requestInfo", requestInfo)
209 execution.setVariable("CVFMI_originalWorkflowException", null)
212 def newVfModuleId = UUID.randomUUID().toString()
213 execution.setVariable("newVfModuleId", newVfModuleId)
214 execution.setVariable(prefix + 'vfModuleId', newVfModuleId)
216 logDebug('RequestInfo: ' + execution.getVariable("CVFMI_requestInfo"), isDebugLogEnabled)
218 logDebug('rollbackEnabled: ' + execution.getVariable("CVFMI_rollbackEnabled"), isDebugLogEnabled)
220 logDebug('Exited ' + method, isDebugLogEnabled)
221 } catch (BpmnError bpmnError) {
224 catch(groovy.json.JsonException je) {
225 utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
226 exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Internal Error - During PreProcessRequest")
229 String restFaultMessage = e.getMessage()
230 //execution.setVariable("CVFMODVOL2_RESTFault", restFaultMessage)
231 //execution.setVariable("CVFMODVOL2_isDataOk", false)
232 utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
233 exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Internal Error - During PreProcessRequest")
239 * Validates a workflow response.
240 * @param execution the execution
241 * @param responseVar the execution variable in which the response is stored
242 * @param responseCodeVar the execution variable in which the response code is stored
243 * @param errorResponseVar the execution variable in which the error response is stored
245 public void validateWorkflowResponse(DelegateExecution execution, String responseVar,
246 String responseCodeVar, String errorResponseVar) {
247 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
248 sdncAdapterUtils.validateSDNCResponse(execution, responseVar, responseCodeVar, errorResponseVar)
253 * Sends the empty, synchronous response back to the API Handler.
254 * @param execution the execution
256 public void sendResponse(DelegateExecution execution) {
257 def method = getClass().getSimpleName() + '.sendResponse(' +
258 'execution=' + execution.getId() +
260 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
261 logDebug('Entered ' + method, isDebugLogEnabled)
264 def requestInfo = execution.getVariable('CVFMI_requestInfo')
265 def requestId = execution.getVariable('CVFMI_requestId')
266 def source = execution.getVariable('CVFMI_source')
268 // RESTResponse (for API Handler (APIH) Reply Task)
269 def newVfModuleId = execution.getVariable("newVfModuleId")
270 String synchResponse = """{"requestReferences":{"instanceId":"${newVfModuleId}","requestId":"${requestId}"}}""".trim()
272 sendWorkflowResponse(execution, 200, synchResponse)
274 execution.setVariable("CVFMI_sentSyncResponse", true)
275 utils.logAudit("CreateVfModule Infra Response: " + synchResponse)
276 logDebug('Exited ' + method, isDebugLogEnabled)
277 } catch (BpmnError e) {
279 } catch (Exception e) {
280 logError('Caught exception in ' + method, e)
281 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
287 * @param execution the execution
289 public void postProcessResponse(DelegateExecution execution){
290 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
292 utils.log("DEBUG", " ======== STARTED PostProcessResponse Process ======== ", isDebugEnabled)
294 def requestInfo = execution.getVariable("CVFMI_requestInfo")
295 def action = utils.getNodeText1(requestInfo, "action")
297 utils.log("DEBUG", "requestInfo is: " + requestInfo, isDebugEnabled)
298 utils.log("DEBUG", "action is: " + action, isDebugEnabled)
301 """ <aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
302 xmlns:ns="http://org.openecomp/mso/request/types/v1"
303 xmlns:ns8="http://org.openecomp/mso/workflow/schema/v1">
304 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
307 <ns8:status-message>Vf Module has been created successfully.</ns8:status-message>
308 <ns8:mso-bpel-name>BPMN</ns8:mso-bpel-name>
309 </aetgt:MsoCompletionRequest>"""
311 payload = utils.formatXml(payload)
312 execution.setVariable("CVFMI_SuccessFlag", true)
313 execution.setVariable("CVFMI_msoCompletionRequest", payload)
314 utils.logAudit("CreateVfModuleInfra completion request: " + payload)
315 utils.log("DEBUG", "Outgoing MsoCompletionRequest: \n" + payload, isDebugEnabled)
318 utils.log("ERROR", "Exception Occured Processing PostProcessResponse. Exception is:\n" + e, isDebugEnabled)
319 execution.setVariable("CVFMI_ErrorResponse", "Error Occured during PostProcessResponse Method:\n" + e.getMessage())
321 utils.log("DEBUG", "======== COMPLETED PostProcessResponse Process ======== ", isDebugEnabled)
329 * Validates the request, request id and service instance id. If a problem is found,
330 * a WorkflowException is generated and an MSOWorkflowException event is thrown. This
331 * method also sets up the log context for the workflow.
332 * @param execution the execution
333 * @return the validated request
335 public String validateInfraRequest(DelegateExecution execution) {
336 def method = getClass().getSimpleName() + '.validateInfraRequest(' +
337 'execution=' + execution.getId() +
339 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
340 logDebug('Entered ' + method, isDebugLogEnabled)
342 String processKey = getProcessKey(execution);
343 def prefix = execution.getVariable("prefix")
345 if (prefix == null) {
346 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " prefix is null")
350 def request = execution.getVariable(prefix + 'Request')
352 if (request == null) {
353 request = execution.getVariable(processKey + 'Request')
355 if (request == null) {
356 request = execution.getVariable('bpmnRequest')
359 setVariable(execution, processKey + 'Request', null);
360 setVariable(execution, 'bpmnRequest', null);
361 setVariable(execution, prefix + 'Request', request);
364 if (request == null) {
365 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request is null")
370 def requestId = execution.getVariable("mso-request-id")
372 if (requestId == null) {
373 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request has no mso-request-id")
376 setVariable(execution, prefix + 'requestId', requestId)
378 def serviceInstanceId = execution.getVariable("mso-service-instance-id")
380 if (serviceInstanceId == null) {
381 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request message has no mso-service-instance-id")
384 utils.logContext(requestId, serviceInstanceId)
386 utils.logAudit("CreateVfModule incoming request: " + request)
387 logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
388 logDebug('Exited ' + method, isDebugLogEnabled)
390 } catch (BpmnError e) {
392 } catch (Exception e) {
393 logError('Caught exception in ' + method, e)
394 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")
398 public void prepareUpdateInfraRequest(DelegateExecution execution){
399 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
401 utils.log("DEBUG", " ======== STARTED prepareUpdateInfraRequest Process ======== ", isDebugEnabled)
405 String requestInfo = execution.getVariable("CVFMI_requestInfo")
406 def aicCloudRegion = execution.getVariable("CVFMI_lcpCloudRegionId")
407 def tenantId = execution.getVariable("CVFMI_tenantId")
408 def requestId = utils.getNodeText1(requestInfo, "request-id")
409 def vnfId = execution.getVariable("CVFMI_vnfId")
410 def vfModuleId = execution.getVariable("CVFMI_vfModuleId")
411 // vfModuleName may be generated by DoCreateVfModule subprocess if it is not specified on the input
412 def vfModuleName = execution.getVariable("CVFMI_vfModuleName")
414 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
415 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
416 utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
419 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
420 xmlns:ns="http://org.openecomp.mso/requestsdb">
423 <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">
424 <requestId>${requestId}</requestId>
425 <lastModifiedBy>BPMN</lastModifiedBy>
426 <statusMessage>VF Module successfully created</statusMessage>
427 <responseBody></responseBody>
428 <requestStatus>COMPLETE</requestStatus>
429 <progress>100</progress>
430 <vnfOutputs><vnf-outputs xmlns="http://org.openecomp/mso/infra/vnf-request/v1" xmlns:aetgt="http://org.openecomp/mso/infra/vnf-request/v1" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"><vnf-id>${vnfId}</vnf-id><vf-module-id>${vfModuleId}</vf-module-id></vnf-outputs></vnfOutputs>
431 <vfModuleId>${vfModuleId}</vfModuleId>
432 <vfModuleName>${vfModuleName}</vfModuleName>
433 </ns:updateInfraRequest>
435 </soapenv:Envelope>"""
437 payload = utils.formatXml(payload)
438 execution.setVariable("CVFMI_updateInfraRequest", payload)
439 utils.log("DEBUG", "Outgoing UpdateInfraRequest: \n" + payload, isDebugEnabled)
440 utils.logAudit("CreateVfModuleInfra Outgoing UpdateInfra Request: " + payload)
443 utils.log("ERROR", "Exception Occured Processing prepareUpdateInfraRequest. Exception is:\n" + e, isDebugEnabled)
444 execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareUpdateInfraRequest Method:\n" + e.getMessage())
446 utils.log("DEBUG", "======== COMPLETED prepareUpdateInfraRequest Process ======== ", isDebugEnabled)
450 * Builds a "FalloutHandler" request and stores it in the specified execution variable.
452 * @param execution the execution
453 * @param resultVar the execution variable in which the result will be stored
455 public void falloutHandlerPrep(DelegateExecution execution, String resultVar) {
456 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
457 'execution=' + execution.getId() +
458 ', resultVar=' + resultVar +
460 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
461 logDebug('Entered ' + method, isDebugLogEnabled)
465 def WorkflowException workflowException = execution.getVariable("WorkflowException")
466 def requestInformation = execution.getVariable("CVFMI_requestInfo")
467 def errorResponseCode = workflowException.getErrorCode()
468 def errorResponseMsg = workflowException.getErrorMessage()
469 def encErrorResponseMsg = ""
470 if (errorResponseMsg != null) {
471 encErrorResponseMsg = errorResponseMsg.replace("&", "&").replace("<", "<").replace(">", ">")
475 <aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
476 xmlns:reqtype="http://org.openecomp/mso/request/types/v1"
477 xmlns:msoservtypes="http://org.openecomp/mso/request/types/v1"
478 xmlns:structuredtypes="http://org.openecomp/mso/structured/types/v1">
479 ${requestInformation}
480 <aetgt:WorkflowException>
481 <aetgt:ErrorMessage>${encErrorResponseMsg}</aetgt:ErrorMessage>
482 <aetgt:ErrorCode>${errorResponseCode}</aetgt:ErrorCode>
483 </aetgt:WorkflowException>
484 </aetgt:FalloutHandlerRequest>
487 logDebug("CONTENT before translation: " + content, isDebugLogEnabled)
488 content = utils.formatXml(content)
489 logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
490 utils.logAudit("CreateVfModuleInfra FallOutHander Request: " + content)
491 execution.setVariable(resultVar, content)
493 logDebug('Exited ' + method, isDebugLogEnabled)
494 } catch (BpmnError e) {
496 } catch (Exception e) {
497 logError('Caught exception in ' + method, e)
498 exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
502 public void logAndSaveOriginalException(DelegateExecution execution) {
503 def method = getClass().getSimpleName() + '.validateRollbackResponse(' +
504 'execution=' + execution.getId() +
506 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
507 logDebug('Entered ' + method, isDebugLogEnabled)
509 logWorkflowException(execution, 'CreateVfModuleInfra caught an event')
510 saveWorkflowException(execution, 'CVFMI_originalWorkflowException')
513 public void validateRollbackResponse(DelegateExecution execution) {
514 def method = getClass().getSimpleName() + '.validateRollbackResponse(' +
515 'execution=' + execution.getId() +
517 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
518 logDebug('Entered ' + method, isDebugLogEnabled)
520 def originalException = execution.getVariable("CVFMI_originalWorkflowException")
521 execution.setVariable("WorkflowException", originalException)
523 execution.setVariable("RollbackCompleted", true)
527 public void sendErrorResponse(DelegateExecution execution){
528 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
530 utils.log("DEBUG", " *** STARTED CreateVfModulenfra sendErrorResponse Process *** ", isDebugEnabled)
532 def sentSyncResponse = execution.getVariable("CVFMI_sentSyncResponse")
533 if(sentSyncResponse == false){
534 WorkflowException wfex = execution.getVariable("WorkflowException")
535 String response = exceptionUtil.buildErrorResponseXml(wfex)
537 utils.logAudit(response)
538 sendWorkflowResponse(execution, 500, response)
540 utils.log("DEBUG", "Not Sending Error Response. Sync Response Already Sent", isDebugEnabled)
543 } catch (Exception ex) {
544 utils.log("DEBUG", "Error Occured in CreateVfModuleInfra sendErrorResponse Process " + ex.getMessage(), isDebugEnabled)
545 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVfModuleInfra sendErrorResponse Process")
548 utils.log("DEBUG", "*** COMPLETED CreateVfModuleInfra sendErrorResponse Process ***", isDebugEnabled)