/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.mso.bpmn.infrastructure.scripts;
import static org.apache.commons.lang3.StringUtils.*;
import groovy.xml.XmlUtil
import groovy.json.*
import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
import org.openecomp.mso.bpmn.core.domain.ServiceInstance
import org.openecomp.mso.bpmn.core.domain.ModelInfo
import org.openecomp.mso.bpmn.core.json.JsonUtils
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.core.RollbackData
import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.rest.APIResponse;
import org.openecomp.mso.rest.RESTClient
import org.openecomp.mso.rest.RESTConfig
import java.util.UUID;
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.json.JSONObject;
import org.json.JSONArray;
import org.apache.commons.lang3.*
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.util.UriUtils;
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import org.xml.sax.InputSource
/**
* This groovy class supports the DoCreateServiceInstance.bpmn process.
*
* Inputs:
* @param - msoRequestId
* @param - globalSubscriberId
* @param - subscriptionServiceType
* @param - serviceInstanceId
* @param - serviceInstanceName - O
* @param - serviceModelInfo
* @param - productFamilyId
* @param - disableRollback
* @param - failExists - TODO
* @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
* @param - sdncVersion ("1610")
* @param - serviceDecomposition - Decomposition for R1710
* (if macro provides serviceDecompsition then serviceModelInfo, serviceInstanceId & serviceInstanceName will be ignored)
*
* Outputs:
* @param - rollbackData (localRB->null)
* @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
* @param - WorkflowException
* @param - serviceInstanceName - (GET from AAI if null in input)
*
*/
public class DoUpdateE2EServiceInstance extends AbstractServiceTaskProcessor {
String Prefix="DUPDSI_"
private static final String DebugFlag = "isDebugEnabled"
ExceptionUtil exceptionUtil = new ExceptionUtil()
JsonUtils jsonUtil = new JsonUtils()
public void preProcessRequest (DelegateExecution execution) {
//only for dug
execution.setVariable("isDebugLogEnabled","true")
execution.setVariable("unit_test", "true")
execution.setVariable("skipVFC", "true")
def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
utils.log("INFO","Entered " + method, isDebugEnabled)
String msg = ""
utils.log("INFO"," ***** Enter DoUpdateE2EServiceInstance preProcessRequest *****", isDebugEnabled)
utils.log("INFO"," unit test : " + execution.getVariable("unit_test"), isDebugEnabled)
try {
execution.setVariable("prefix", Prefix)
//Inputs
//requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
utils.log("INFO"," ***** globalSubscriberId *****" + globalSubscriberId, isDebugEnabled)
//requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
String serviceType = execution.getVariable("serviceType")
utils.log("INFO"," ***** serviceType *****" + serviceType, isDebugEnabled)
//requestDetails.requestParameters. for SDNC assignTopology
String productFamilyId = execution.getVariable("productFamilyId") //AAI productFamilyId
if (isBlank(globalSubscriberId)) {
msg = "Input globalSubscriberId is null"
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
}
if (isBlank(serviceType)) {
msg = "Input serviceType is null"
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
}
//Generated in parent for AAI
String serviceInstanceId = execution.getVariable("serviceInstanceId")
if (isBlank(serviceInstanceId)){
msg = "Input serviceInstanceId is null"
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
}
if (productFamilyId == null) {
execution.setVariable("productFamilyId", "")
}
String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
if (isBlank(sdncCallbackUrl)) {
msg = "URN_mso_workflow_sdncadapter_callback is null"
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
}
execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
utils.log("INFO","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
//requestDetails.modelInfo.for AAI PUT servieInstanceData
//requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
String serviceInstanceName = execution.getVariable("serviceInstanceName")
//String serviceInstanceId = execution.getVariable("serviceInstanceId")
String uuiRequest = execution.getVariable("uuiRequest")
utils.log("INFO","uuiRequest: " + uuiRequest, isDebugEnabled)
String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceDefId")
utils.log("INFO","modelInvariantUuid: " + modelInvariantUuid, isDebugEnabled)
String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.templateId")
utils.log("INFO","modelUuid: " + modelUuid, isDebugEnabled)
String serviceModelName = jsonUtil.getJsonValue(uuiRequest, "service.parameters.templateName")
utils.log("INFO","serviceModelName: " + serviceModelName, isDebugEnabled)
execution.setVariable("serviceModelName", serviceModelName)
//aai serviceType and Role can be setted as fixed value now.
String aaiServiceType = serviceType
String aaiServiceRole = serviceType+"Role"
execution.setVariable("modelInvariantUuid", modelInvariantUuid)
execution.setVariable("model-invariant-id-target", modelInvariantUuid)
execution.setVariable("modelUuid", modelUuid)
execution.setVariable("model-version-id-target", modelUuid_target)
//AAI PUT
String oStatus = execution.getVariable("initialStatus") ?: ""
utils.log("INFO","oStatus: " + oStatus, isDebugEnabled)
if ("TRANSPORT".equalsIgnoreCase(serviceType))
{
oStatus = "Update"
}
String statusLine = isBlank(oStatus) ? "" : "${oStatus}"
utils.log("INFO","statusLine: " + statusLine, isDebugEnabled)
AaiUtil aaiUriUtil = new AaiUtil(this)
utils.log("INFO","start create aai uri: " + aaiUriUtil, isDebugEnabled)
String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
utils.log("INFO","aai_uri: " + aai_uri, isDebugEnabled)
String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
utils.log("INFO","namespace: " + namespace, isDebugEnabled)
/*
String serviceInstanceData =
"""
${serviceInstanceId}
${serviceInstanceName}
${aaiServiceType}
${aaiServiceRole}
${statusLine}
${modelInvariantUuid}
${modelUuid}
""".trim()
*/
//begin only for test
String serviceInstanceData =
"""
${serviceInstanceId}
${serviceInstanceName}
${aaiServiceType}
${aaiServiceRole}
${statusLine}
""".trim()
//end only for test
execution.setVariable("serviceInstanceData", serviceInstanceData)
utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)
utils.logAudit(serviceInstanceData)
utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
utils.log("INFO", " 'payload' to create Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
execution.setVariable("serviceSDNCCreate", "false")
execution.setVariable("operationStatus", "Waiting deploy resource...")
} catch (BpmnError e) {
throw e;
} catch (Exception ex){
msg = "Exception in preProcessRequest " + ex.getMessage()
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
utils.log("INFO", "Exited " + method, isDebugEnabled)
}
public void postProcessAAIGET(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
String msg = ""
try {
String serviceInstanceId = execution.getVariable("serviceInstanceId")
boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
String serviceType = ""
if(foundInAAI){
utils.log("INFO","Found Service-instance in AAI", isDebugEnabled)
String siData = execution.getVariable("GENGS_service")
utils.log("INFO", "SI Data", isDebugEnabled)
if (isBlank(siData))
{
msg = "Could not retrive ServiceInstance data from AAI, Id:" + serviceInstanceId
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
}
else
{
utils.log("INFO", "SI Data" + siData, isDebugEnabled)
InputSource source = new InputSource(new StringReader(siData));
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
Document serviceXml = docBuilder.parse(source)
serviceXml.getDocumentElement().normalize()
// Get Template uuid and version
if (utils.nodeExists(siData, "model-invariant-id") && utils.nodeExists(siData, "model-version-id") ) {
utils.log("INFO", "SI Data model-invariant-id and model-version-id exist:", isDebugEnabled)
def modelInvariantId = serviceXml.getElementsByTagName("model-invariant-id").item(0).getTextContent()
def modelVersionId = serviceXml.getElementsByTagName("model-version-id").item(0).getTextContent()
// Set Original Template info
execution.setVariable("model-invariant-id-original", modelInvariantId)
execution.setVariable("model-version-id-original", modelVersionId)
}
//Confirm there are no related service instances (vnf/network or volume)
if (utils.nodeExists(siData, "relationship-list")) {
utils.log("INFO", "SI Data relationship-list exists:", isDebugEnabled)
//test(siData)
NodeList nodeList = serviceXml.getElementsByTagName("relationship")
JSONArray jArray = new JSONArray()
for (int x = 0; x < nodeList.getLength(); x++) {
Node node = nodeList.item(x)
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node
def e = eElement.getElementsByTagName("related-to").item(0).getTextContent() //for ns
if(e.equals("service-instance")){
def relatedObject = eElement.getElementsByTagName("related-link").item(0).getTextContent()
utils.log("INFO", "ServiceInstance Related NS :" + relatedObject, isDebugEnabled)
NodeList dataList = node.getChildNodes()
if(null != dataList) {
JSONObject jObj = new JSONObject()
for (int i = 0; i < dataList.getLength(); i++) {
Node dNode = dataList.item(i)
if(dNode.getNodeName() == "relationship-data") {
Element rDataEle = (Element)dNode
def eKey = rDataEle.getElementsByTagName("relationship-key").item(0).getTextContent()
def eValue = rDataEle.getElementsByTagName("relationship-value").item(0).getTextContent()
if(eKey.equals("service-instance.service-instance-id")){
jObj.put("resourceInstanceId", eValue)
}
}
else if(dNode.getNodeName() == "related-to-property"){
Element rDataEle = (Element)dNode
def eKey = rDataEle.getElementsByTagName("property-key").item(0).getTextContent()
def eValue = rDataEle.getElementsByTagName("property-value").item(0).getTextContent()
if(eKey.equals("service-instance.service-instance-name")){
jObj.put("resourceType", eValue)
}
}
}
utils.log("INFO", "Relationship related to Resource:" + jObj.toString(), isDebugEnabled)
jArray.put(jObj)
}
//for overlay/underlay
}else if (e.equals("configuration")){
def relatedObject = eElement.getElementsByTagName("related-link").item(0).getTextContent()
utils.log("INFO", "ServiceInstance Related Configuration :" + relatedObject, isDebugEnabled)
NodeList dataList = node.getChildNodes()
if(null != dataList) {
JSONObject jObj = new JSONObject()
for (int i = 0; i < dataList.getLength(); i++) {
Node dNode = dataList.item(i)
if(dNode.getNodeName() == "relationship-data") {
Element rDataEle = (Element)dNode
def eKey = rDataEle.getElementsByTagName("relationship-key").item(0).getTextContent()
def eValue = rDataEle.getElementsByTagName("relationship-value").item(0).getTextContent()
if(eKey.equals("configuration.configuration-id")){
jObj.put("resourceInstanceId", eValue)
}
}
else if(dNode.getNodeName() == "related-to-property"){
Element rDataEle = (Element)dNode
def eKey = rDataEle.getElementsByTagName("property-key").item(0).getTextContent()
def eValue = rDataEle.getElementsByTagName("property-value").item(0).getTextContent()
if(eKey.equals("configuration.configuration-type")){
jObj.put("resourceType", eValue)
}
}
}
utils.log("INFO", "Relationship related to Resource:" + jObj.toString(), isDebugEnabled)
jArray.put(jObj)
}
}
}
}
execution.setVariable("serviceRelationShip", jArray.toString())
}
}
}else{
boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
if(!succInAAI){
utils.log("INFO","Error getting Service-instance from AAI", + serviceInstanceId, isDebugEnabled)
WorkflowException workflowException = execution.getVariable("WorkflowException")
utils.logAudit("workflowException: " + workflowException)
if(workflowException != null){
exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
}
else
{
msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
}
}
utils.log("INFO","Service-instance NOT found in AAI. Silent Success", isDebugEnabled)
}
}catch (BpmnError e) {
throw e;
} catch (Exception ex) {
msg = "Exception in DoDeleteE2EServiceInstance.postProcessAAIGET. " + ex.getMessage()
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
}
public void preInitResourcesOperStatus(DelegateExecution execution){
def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
utils.log("INFO", " ======== STARTED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
try{
String serviceId = execution.getVariable("serviceInstanceId")
String operationId = execution.getVariable("operationId")
String operationType = execution.getVariable("operationType")
String resourceTemplateUUIDs = ""
String result = "processing"
String progress = "0"
String reason = ""
String operationContent = "Prepare service updating"
utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId + " operationType:" + operationType, isDebugEnabled)
serviceId = UriUtils.encode(serviceId,"UTF-8")
execution.setVariable("serviceInstanceId", serviceId)
execution.setVariable("operationId", operationId)
execution.setVariable("operationType", operationType)
String serviceRelationShip = execution.getVariable("serviceRelationShip")
def jsonSlurper = new JsonSlurper()
def jsonOutput = new JsonOutput()
List relationShipList = jsonSlurper.parseText(serviceRelationShip)
if (relationShipList != null) {
relationShipList.each {
resourceTemplateUUIDs = resourceTemplateUUIDs + it.resourceInstanceId + ":"
}
}
execution.setVariable("URN_mso_openecomp_adapters_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
String payload =
"""
${serviceId}
${operationId}
${operationType}
${resourceTemplateUUIDs}
"""
payload = utils.formatXml(payload)
execution.setVariable("CVFMI_initResOperStatusRequest", payload)
utils.log("INFO", "Outgoing initResourceOperationStatus: \n" + payload, isDebugEnabled)
utils.logAudit("CreateVfModuleInfra Outgoing initResourceOperationStatus Request: " + payload)
}catch(Exception e){
utils.log("ERROR", "Exception Occured Processing preInitResourcesOperStatus. Exception is:\n" + e, isDebugEnabled)
execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preInitResourcesOperStatus Method:\n" + e.getMessage())
}
utils.log("INFO", "======== COMPLETED preInitResourcesOperStatus Process ======== ", isDebugEnabled)
}
/**
* Init the service Operation Status
*/
public void preUpdateServiceOperationStatus(DelegateExecution execution){
def method = getClass().getSimpleName() + '.preUpdateServiceOperationStatus(' +'execution=' + execution.getId() +')'
def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
utils.log("INFO","Entered " + method, isDebugEnabled)
try{
String serviceId = execution.getVariable("serviceInstanceId")
String operationId = execution.getVariable("operationId")
String serviceName = execution.getVariable("serviceInstanceName")
String userId = ""
String result = "processing"
String progress = execution.getVariable("progress")
utils.log("INFO", "progress: " + progress , isDebugEnabled)
if ("100".equalsIgnoreCase(progress))
{
result = "finished"
}
String reason = ""
String operationContent = "Prepare service : " + execution.getVariable("operationStatus")
utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)
serviceId = UriUtils.encode(serviceId,"UTF-8")
execution.setVariable("serviceInstanceId", serviceId)
execution.setVariable("operationId", operationId)
execution.setVariable("operationType", operationType)
def dbAdapterEndpoint = "http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter"
execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
execution.setVariable("URN_mso_openecomp_adapters_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
String payload =
"""
${serviceId}
${operationId}
${serviceName}
${operationType}
${userId}
${result}
${operationContent}
${reason}
"""
payload = utils.formatXml(payload)
execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
utils.log("INFO", "Outgoing preUpdateServiceOperationStatus: \n" + payload, isDebugEnabled)
}catch(Exception e){
utils.log("ERROR", "Exception Occured Processing preUpdateServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preUpdateServiceOperationStatus Method:\n" + e.getMessage())
}
utils.log("INFO", "======== COMPLETED preUpdateServiceOperationStatus Process ======== ", isDebugEnabled)
utils.log("INFO", "Exited " + method, isDebugEnabled)
}
public void postResourcesOperStatus(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
}
public void preCompareModelVersions(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
}
public void postCompareModelVersions(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
}
public void preProcessForAddResource(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
}
public void postProcessForAddResource(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
}
public void preProcessForDeleteResource(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
}
public void postProcessForDeleteResource(DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
}
public void postConfigRequest(execution){
//now do noting
}
}