[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / CreateVnfInfra.groovy
  * ============LICENSE_END=========================================================
  */
 
-package com.att.bpm.scripts
+package org.openecomp.mso.bpmn.infrastructure.scripts
 
 import java.util.UUID;
 
 import org.camunda.bpm.engine.delegate.BpmnError
 import org.camunda.bpm.engine.runtime.Execution;
+
 import static org.apache.commons.lang3.StringUtils.*;
 
+import org.openecomp.mso.bpmn.common.scripts.AaiUtil;
+import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
+import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
+import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils;
+import org.openecomp.mso.bpmn.common.scripts.VidUtils;
 import org.openecomp.mso.bpmn.core.WorkflowException
 import org.openecomp.mso.bpmn.core.json.JsonUtils;
 
@@ -47,6 +53,7 @@ class CreateVnfInfra extends AbstractServiceTaskProcessor {
         * request.
         *
         * @param - execution
+        *
         */
        public void preProcessRequest(Execution execution) {
                def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
@@ -63,7 +70,7 @@ class CreateVnfInfra extends AbstractServiceTaskProcessor {
 
                        if(createVnfRequest != null){
 
-                               String requestId = execution.getVariable("att-mso-request-id")
+                               String requestId = execution.getVariable("mso-request-id")
                                execution.setVariable("CREVI_requestId", requestId)
                                utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
 
@@ -90,6 +97,9 @@ class CreateVnfInfra extends AbstractServiceTaskProcessor {
                                String suppressRollback = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.requestInfo.suppressRollback")
                                execution.setVariable("CREVI_suppressRollback", suppressRollback)
                                utils.log("DEBUG", "Incoming Suppress Rollback is: " + suppressRollback, isDebugEnabled)
+                               
+                               def vnfModelInfo = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo")
+                               execution.setVariable("CREVI_vnfModelInfo", vnfModelInfo)
 
                                String modelInvariantId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo.modelInvariantId")
                                execution.setVariable("CREVI_modelInvariantId", modelInvariantId)
@@ -98,10 +108,21 @@ class CreateVnfInfra extends AbstractServiceTaskProcessor {
                                String modelVersion = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.modelInfo.modelVersion")
                                execution.setVariable("CREVI_modelVersion", modelVersion)
                                utils.log("DEBUG", "Incoming Model Version is: " + modelVersion, isDebugEnabled)
+                               
+                               def cloudConfiguration = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.cloudConfiguration")
+                               execution.setVariable("CREVI_cloudConfiguration", cloudConfiguration)
+                               
+                               String cloudSiteId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.cloudConfiguration.lcpCloudRegionId")
+                               execution.setVariable("CREVI_cloudSiteId", cloudSiteId)
+                               utils.log("DEBUG", "Incoming Cloud Site Id is: " + cloudSiteId, isDebugEnabled)
+                               
+                               String tenantId = jsonUtil.getJsonValue(createVnfRequest, "requestDetails.cloudConfiguration.tenantId")
+                               execution.setVariable("CREVI_tenantId", tenantId)
+                               utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId, isDebugEnabled)
 
                                //For Completion Handler & Fallout Handler
                                String requestInfo =
-                               """<request-info xmlns="http://ecomp.att.com/mso/infra/vnf-request/v1">
+                               """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
                                        <request-id>${requestId}</request-id>
                                        <action>CREATE</action>
                                        <source>${source}</source>
@@ -127,6 +148,21 @@ class CreateVnfInfra extends AbstractServiceTaskProcessor {
                                // Setting for Sub Flow Calls
                                execution.setVariable("CREVI_type", "generic-vnf")
                                execution.setVariable("GENGS_type", "service-instance")
+                               
+                               String sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
+                               if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
+                                       def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'
+                                       logError(msg)
+                                       exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
+                               }
+                               execution.setVariable("CREVI_sdncCallbackUrl", sdncCallbackUrl)
+                               
+                               def vnfInputParameters = jsonUtil.getJsonValue(createVnfRequest, "requestParameters.userParams")
+                               execution.setVariable("CREVI_vnfInputParameters", vnfInputParameters)
+                               
+                               
+                               utils.logAudit("SDNC Callback URL: " + sdncCallbackUrl)
+                               logDebug("SDNC Callback URL is: " + sdncCallbackUrl, isDebugEnabled)
 
                        }else{
                                exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Bpmn Request is Null.")
@@ -239,6 +275,146 @@ class CreateVnfInfra extends AbstractServiceTaskProcessor {
                }
                utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCreateGenericVnf Process ***", isDebugEnabled)
        }
+       
+       public void preProcessSDNCAssignRequest(Execution execution){
+               def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
+               execution.setVariable("prefix", Prefix)
+               logDebug(" ======== STARTED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
+               def vnfId = execution.getVariable("CREVI_vnfId")                
+               def serviceInstanceId = execution.getVariable("CREVI_serviceInstanceId")
+               logDebug("NEW VNF ID: " + vnfId, isDebugLogEnabled)
+               utils.logAudit("NEW VNF ID: " + vnfId)
+
+               try{
+                       //Build SDNC Request
+                       
+                       String assignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "assign")
+
+                       assignSDNCRequest = utils.formatXml(assignSDNCRequest)
+                       execution.setVariable("CREVI_assignSDNCRequest", assignSDNCRequest)
+                       logDebug("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest, isDebugLogEnabled)
+                       utils.logAudit("Outgoing AssignSDNCRequest is: \n" + assignSDNCRequest)
+
+               }catch(Exception e){
+                       utils.log("ERROR", "Exception Occured Processing preProcessSDNCAssignRequest. Exception is:\n" + e, isDebugLogEnabled)
+                       exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareProvision Method:\n" + e.getMessage())
+               }
+               logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
+       }
+       
+       public void preProcessSDNCActivateRequest(Execution execution) {
+               def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
+                       'execution=' + execution.getId() +
+                       ')'
+               def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
+               logDebug('Entered ' + method, isDebugLogEnabled)
+               execution.setVariable("prefix", Prefix)
+               logDebug(" ======== STARTED preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
+               try{
+                       String vnfId = execution.getVariable("CREVI_vnfId")                     
+                       String serviceInstanceId = execution.getVariable("CREVI_serviceInstanceId")
+
+                       String activateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "activate")
+
+                       execution.setVariable("CREVI_activateSDNCRequest", activateSDNCRequest)
+                       logDebug("Outgoing CommitSDNCRequest is: \n" + activateSDNCRequest, isDebugLogEnabled)
+                       utils.logAudit("Outgoing CommitSDNCRequest is: \n"  + activateSDNCRequest)
+
+               }catch(Exception e){
+                       log.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e, isDebugLogEnabled)
+                       exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
+               }
+               logDebug("======== COMPLETED  preProcessSDNCActivateRequest Process ======== ", isDebugLogEnabled)
+       }
+       
+       public String buildSDNCRequest(Execution execution, String svcInstId, String action){
+               
+                               String uuid = execution.getVariable('testReqId') // for junits
+                               if(uuid==null){
+                                       uuid = execution.getVariable("mso-request-id") + "-" +          System.currentTimeMillis()
+                               }
+                               def callbackURL = execution.getVariable("CREVI_sdncCallbackUrl")
+                               def requestId = execution.getVariable("CREVI_requestId")
+                               def serviceId = execution.getVariable("CREVI_serviceId")
+                               def vnfType = execution.getVariable("CREVI_vnfType")
+                               def vnfName = execution.getVariable("CREVI_vnfName")
+                               def tenantId = execution.getVariable("CREVI_tenantId")
+                               def source = execution.getVariable("CREVI_source")                              
+                               def vnfId = execution.getVariable("CREVI_vnfId")
+                               def cloudSiteId = execution.getVariable("CREVI_cloudSiteId")
+               
+                               String sdncVNFParamsXml = ""
+               
+                               if(execution.getVariable("CREVI_vnfParamsExistFlag") == true){
+                                       sdncVNFParamsXml = buildSDNCParamsXml(execution)
+                               }else{
+                                       sdncVNFParamsXml = ""
+                               }
+               
+                               String sdncRequest =
+                               """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
+                                                                                                       xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
+                                                                                                       xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
+          <sdncadapter:RequestHeader>
+                               <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
+                               <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
+                               <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
+                               <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
+                               <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
+               </sdncadapter:RequestHeader>
+       <sdncadapterworkflow:SDNCRequestData>
+               <request-information>
+                       <request-id>${requestId}</request-id>
+                       <request-action>VNFActivateRequest</request-action>
+                       <source>${source}</source>
+                       <notification-url/>
+               </request-information>
+               <service-information>
+                       <service-id>${serviceId}</service-id>
+                       <service-type>${serviceId}</service-type>
+                       <service-instance-id>${svcInstId}</service-instance-id>
+                       <subscriber-name>notsurewecare</subscriber-name>
+               </service-information>
+               <vnf-request-information>                       
+                       <vnf-id>${vnfId}</vnf-id>
+                       <vnf-name>${vnfName}</vnf-name>
+                       <vnf-type>${vnfType}</vnf-type>
+                       <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
+                       <tenant>${tenantId}</tenant>
+               ${sdncVNFParamsXml}
+               </vnf-request-information>
+       </sdncadapterworkflow:SDNCRequestData>
+       </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
+               
+                       utils.logAudit("sdncRequest:  " + sdncRequest)
+                       return sdncRequest              
+       }
+               
+       public void validateSDNCResponse(Execution execution, String response, String method){
+               def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
+               execution.setVariable("prefix",Prefix)
+               logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
+
+               WorkflowException workflowException = execution.getVariable("WorkflowException")
+               boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
+
+               utils.logAudit("workflowException: " + workflowException)
+
+               SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
+               sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
+
+               utils.logAudit("SDNCResponse: " + response)
+
+               String sdncResponse = response
+               if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
+                       logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse, isDebugLogEnabled)
+                       
+               }else{
+                       logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
+                       throw new BpmnError("MSOWorkflowException")
+               }
+               logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
+       }
 
        public void prepareCompletionHandlerRequest(Execution execution){
                def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
@@ -252,8 +428,8 @@ class CreateVnfInfra extends AbstractServiceTaskProcessor {
                        requestInfo = utils.removeXmlPreamble(requestInfo)
 
                        String request =
-                               """<aetgt:MsoCompletionRequest xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1"
-                                                                       xmlns:ns="http://ecomp.att.com/mso/request/types/v1">
+                               """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
+                                                                       xmlns:ns="http://org.openecomp/mso/request/types/v1">
                                                        ${requestInfo}
                                                        <status-message>Vnf has been created successfully.</status-message>
                                                        <vnfId>${vnfId}</vnfId>