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.runtime.Execution
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(Execution 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, userParam.value)
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 def cloudConfiguration = reqMap.requestDetails?.cloudConfiguration
175 def lcpCloudRegionId = cloudConfiguration.lcpCloudRegionId
176 execution.setVariable(prefix + 'lcpCloudRegionId', lcpCloudRegionId)
177 def tenantId = cloudConfiguration.tenantId
178 execution.setVariable(prefix + 'tenantId', tenantId)
180 def globalSubscriberId = reqMap.requestDetails?.subscriberInfo?.globalSubscriberId ?: ''
181 execution.setVariable(prefix + 'globalSubscriberId', globalSubscriberId)
183 execution.setVariable(prefix + 'sdncVersion', '1702')
185 execution.setVariable("CreateVfModuleInfraSuccessIndicator", false)
186 execution.setVariable("RollbackCompleted", false)
188 execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
191 def source = reqMap.requestDetails?.requestInfo?.source
192 execution.setVariable("CVFMI_source", source)
194 //For Completion Handler & Fallout Handler
196 """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
197 <request-id>${requestId}</request-id>
198 <action>CREATE</action>
199 <source>${source}</source>
202 execution.setVariable("CVFMI_requestInfo", requestInfo)
206 execution.setVariable("CVFMI_originalWorkflowException", null)
209 def newVfModuleId = UUID.randomUUID().toString()
210 execution.setVariable("newVfModuleId", newVfModuleId)
211 execution.setVariable(prefix + 'vfModuleId', newVfModuleId)
213 logDebug('RequestInfo: ' + execution.getVariable("CVFMI_requestInfo"), isDebugLogEnabled)
215 logDebug('rollbackEnabled: ' + execution.getVariable("CVFMI_rollbackEnabled"), isDebugLogEnabled)
217 logDebug('Exited ' + method, isDebugLogEnabled)
218 } catch (BpmnError bpmnError) {
221 catch(groovy.json.JsonException je) {
222 utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
223 exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Internal Error - During PreProcessRequest")
226 String restFaultMessage = e.getMessage()
227 //execution.setVariable("CVFMODVOL2_RESTFault", restFaultMessage)
228 //execution.setVariable("CVFMODVOL2_isDataOk", false)
229 utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
230 exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Internal Error - During PreProcessRequest")
236 * Validates a workflow response.
237 * @param execution the execution
238 * @param responseVar the execution variable in which the response is stored
239 * @param responseCodeVar the execution variable in which the response code is stored
240 * @param errorResponseVar the execution variable in which the error response is stored
242 public void validateWorkflowResponse(Execution execution, String responseVar,
243 String responseCodeVar, String errorResponseVar) {
244 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
245 sdncAdapterUtils.validateSDNCResponse(execution, responseVar, responseCodeVar, errorResponseVar)
250 * Sends the empty, synchronous response back to the API Handler.
251 * @param execution the execution
253 public void sendResponse(Execution execution) {
254 def method = getClass().getSimpleName() + '.sendResponse(' +
255 'execution=' + execution.getId() +
257 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
258 logDebug('Entered ' + method, isDebugLogEnabled)
261 def requestInfo = execution.getVariable('CVFMI_requestInfo')
262 def requestId = execution.getVariable('CVFMI_requestId')
263 def source = execution.getVariable('CVFMI_source')
265 // RESTResponse (for API Handler (APIH) Reply Task)
266 def newVfModuleId = execution.getVariable("newVfModuleId")
267 String synchResponse = """{"requestReferences":{"instanceId":"${newVfModuleId}","requestId":"${requestId}"}}""".trim()
269 sendWorkflowResponse(execution, 200, synchResponse)
271 execution.setVariable("CVFMI_sentSyncResponse", true)
272 utils.logAudit("CreateVfModule Infra Response: " + synchResponse)
273 logDebug('Exited ' + method, isDebugLogEnabled)
274 } catch (BpmnError e) {
276 } catch (Exception e) {
277 logError('Caught exception in ' + method, e)
278 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
284 * @param execution the execution
286 public void postProcessResponse(Execution execution){
287 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
289 utils.log("DEBUG", " ======== STARTED PostProcessResponse Process ======== ", isDebugEnabled)
291 def requestInfo = execution.getVariable("CVFMI_requestInfo")
292 def action = utils.getNodeText1(requestInfo, "action")
294 utils.log("DEBUG", "requestInfo is: " + requestInfo, isDebugEnabled)
295 utils.log("DEBUG", "action is: " + action, isDebugEnabled)
298 """ <aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
299 xmlns:ns="http://org.openecomp/mso/request/types/v1"
300 xmlns:ns8="http://org.openecomp/mso/workflow/schema/v1">
301 <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
304 <ns8:status-message>Vf Module has been created successfully.</ns8:status-message>
305 <ns8:mso-bpel-name>BPMN</ns8:mso-bpel-name>
306 </aetgt:MsoCompletionRequest>"""
308 payload = utils.formatXml(payload)
309 execution.setVariable("CVFMI_SuccessFlag", true)
310 execution.setVariable("CVFMI_msoCompletionRequest", payload)
311 utils.logAudit("CreateVfModuleInfra completion request: " + payload)
312 utils.log("DEBUG", "Outgoing MsoCompletionRequest: \n" + payload, isDebugEnabled)
315 utils.log("ERROR", "Exception Occured Processing PostProcessResponse. Exception is:\n" + e, isDebugEnabled)
316 execution.setVariable("CVFMI_ErrorResponse", "Error Occured during PostProcessResponse Method:\n" + e.getMessage())
318 utils.log("DEBUG", "======== COMPLETED PostProcessResponse Process ======== ", isDebugEnabled)
326 * Validates the request, request id and service instance id. If a problem is found,
327 * a WorkflowException is generated and an MSOWorkflowException event is thrown. This
328 * method also sets up the log context for the workflow.
329 * @param execution the execution
330 * @return the validated request
332 public String validateInfraRequest(Execution execution) {
333 def method = getClass().getSimpleName() + '.validateInfraRequest(' +
334 'execution=' + execution.getId() +
336 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
337 logDebug('Entered ' + method, isDebugLogEnabled)
339 String processKey = getProcessKey(execution);
340 def prefix = execution.getVariable("prefix")
342 if (prefix == null) {
343 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " prefix is null")
347 def request = execution.getVariable(prefix + 'Request')
349 if (request == null) {
350 request = execution.getVariable(processKey + 'Request')
352 if (request == null) {
353 request = execution.getVariable('bpmnRequest')
356 setVariable(execution, processKey + 'Request', null);
357 setVariable(execution, 'bpmnRequest', null);
358 setVariable(execution, prefix + 'Request', request);
361 if (request == null) {
362 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request is null")
367 def requestId = execution.getVariable("mso-request-id")
369 if (requestId == null) {
370 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request has no mso-request-id")
373 setVariable(execution, prefix + 'requestId', requestId)
375 def serviceInstanceId = execution.getVariable("mso-service-instance-id")
377 if (serviceInstanceId == null) {
378 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request message has no mso-service-instance-id")
381 utils.logContext(requestId, serviceInstanceId)
383 utils.logAudit("CreateVfModule incoming request: " + request)
384 logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
385 logDebug('Exited ' + method, isDebugLogEnabled)
387 } catch (BpmnError e) {
389 } catch (Exception e) {
390 logError('Caught exception in ' + method, e)
391 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")
395 public void prepareUpdateInfraRequest(Execution execution){
396 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
398 utils.log("DEBUG", " ======== STARTED prepareUpdateInfraRequest Process ======== ", isDebugEnabled)
402 String requestInfo = execution.getVariable("CVFMI_requestInfo")
403 def aicCloudRegion = execution.getVariable("CVFMI_lcpCloudRegionId")
404 def tenantId = execution.getVariable("CVFMI_tenantId")
405 def requestId = utils.getNodeText1(requestInfo, "request-id")
406 def vnfId = execution.getVariable("CVFMI_vnfId")
407 def vfModuleId = execution.getVariable("CVFMI_vfModuleId")
408 // vfModuleName may be generated by DoCreateVfModule subprocess if it is not specified on the input
409 def vfModuleName = execution.getVariable("CVFMI_vfModuleName")
411 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
412 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
413 utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
416 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
417 xmlns:ns="http://org.openecomp.mso/requestsdb">
420 <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">
421 <requestId>${requestId}</requestId>
422 <lastModifiedBy>BPMN</lastModifiedBy>
423 <statusMessage>VF Module successfully created</statusMessage>
424 <responseBody></responseBody>
425 <requestStatus>COMPLETE</requestStatus>
426 <progress>100</progress>
427 <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>
428 <vfModuleId>${vfModuleId}</vfModuleId>
429 <vfModuleName>${vfModuleName}</vfModuleName>
430 </ns:updateInfraRequest>
432 </soapenv:Envelope>"""
434 payload = utils.formatXml(payload)
435 execution.setVariable("CVFMI_updateInfraRequest", payload)
436 utils.log("DEBUG", "Outgoing UpdateInfraRequest: \n" + payload, isDebugEnabled)
437 utils.logAudit("CreateVfModuleInfra Outgoing UpdateInfra Request: " + payload)
440 utils.log("ERROR", "Exception Occured Processing prepareUpdateInfraRequest. Exception is:\n" + e, isDebugEnabled)
441 execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareUpdateInfraRequest Method:\n" + e.getMessage())
443 utils.log("DEBUG", "======== COMPLETED prepareUpdateInfraRequest Process ======== ", isDebugEnabled)
447 * Builds a "FalloutHandler" request and stores it in the specified execution variable.
449 * @param execution the execution
450 * @param resultVar the execution variable in which the result will be stored
452 public void falloutHandlerPrep(Execution execution, String resultVar) {
453 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
454 'execution=' + execution.getId() +
455 ', resultVar=' + resultVar +
457 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
458 logDebug('Entered ' + method, isDebugLogEnabled)
462 def WorkflowException workflowException = execution.getVariable("WorkflowException")
463 def requestInformation = execution.getVariable("CVFMI_requestInfo")
464 def errorResponseCode = workflowException.getErrorCode()
465 def errorResponseMsg = workflowException.getErrorMessage()
466 def encErrorResponseMsg = ""
467 if (errorResponseMsg != null) {
468 encErrorResponseMsg = errorResponseMsg.replace("&", "&").replace("<", "<").replace(">", ">")
472 <aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
473 xmlns:reqtype="http://org.openecomp/mso/request/types/v1"
474 xmlns:msoservtypes="http://org.openecomp/mso/request/types/v1"
475 xmlns:structuredtypes="http://org.openecomp/mso/structured/types/v1">
476 ${requestInformation}
477 <aetgt:WorkflowException>
478 <aetgt:ErrorMessage>${encErrorResponseMsg}</aetgt:ErrorMessage>
479 <aetgt:ErrorCode>${errorResponseCode}</aetgt:ErrorCode>
480 </aetgt:WorkflowException>
481 </aetgt:FalloutHandlerRequest>
484 logDebug("CONTENT before translation: " + content, isDebugLogEnabled)
485 content = utils.formatXml(content)
486 logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
487 utils.logAudit("CreateVfModuleInfra FallOutHander Request: " + content)
488 execution.setVariable(resultVar, content)
490 logDebug('Exited ' + method, isDebugLogEnabled)
491 } catch (BpmnError e) {
493 } catch (Exception e) {
494 logError('Caught exception in ' + method, e)
495 exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
499 public void logAndSaveOriginalException(Execution execution) {
500 def method = getClass().getSimpleName() + '.validateRollbackResponse(' +
501 'execution=' + execution.getId() +
503 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
504 logDebug('Entered ' + method, isDebugLogEnabled)
506 logWorkflowException(execution, 'CreateVfModuleInfra caught an event')
507 saveWorkflowException(execution, 'CVFMI_originalWorkflowException')
510 public void validateRollbackResponse(Execution execution) {
511 def method = getClass().getSimpleName() + '.validateRollbackResponse(' +
512 'execution=' + execution.getId() +
514 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
515 logDebug('Entered ' + method, isDebugLogEnabled)
517 def originalException = execution.getVariable("CVFMI_originalWorkflowException")
518 execution.setVariable("WorkflowException", originalException)
520 execution.setVariable("RollbackCompleted", true)
524 public void sendErrorResponse(Execution execution){
525 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
527 utils.log("DEBUG", " *** STARTED CreateVfModulenfra sendErrorResponse Process *** ", isDebugEnabled)
529 def sentSyncResponse = execution.getVariable("CVFMI_sentSyncResponse")
530 if(sentSyncResponse == false){
531 WorkflowException wfex = execution.getVariable("WorkflowException")
532 String response = exceptionUtil.buildErrorResponseXml(wfex)
534 utils.logAudit(response)
535 sendWorkflowResponse(execution, 500, response)
537 utils.log("DEBUG", "Not Sending Error Response. Sync Response Already Sent", isDebugEnabled)
540 } catch (Exception ex) {
541 utils.log("DEBUG", "Error Occured in CreateVfModuleInfra sendErrorResponse Process " + ex.getMessage(), isDebugEnabled)
542 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in CreateVfModuleInfra sendErrorResponse Process")
545 utils.log("DEBUG", "*** COMPLETED CreateVfModuleInfra sendErrorResponse Process ***", isDebugEnabled)