/*- * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved. * ================================================================================ * Modifications Copyright (c) 2019 Samsung * ================================================================================ * 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.onap.so.bpmn.infrastructure.scripts import org.onap.so.bpmn.common.scripts.CatalogDbUtilsFactory import static org.apache.commons.lang3.StringUtils.isBlank import org.apache.commons.lang3.StringUtils import org.apache.http.HttpResponse import org.camunda.bpm.engine.delegate.BpmnError import org.camunda.bpm.engine.delegate.DelegateExecution import org.json.JSONObject import org.onap.so.bpmn.common.recipe.BpmnRestClient import org.onap.so.bpmn.common.recipe.ResourceInput import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor import org.onap.so.bpmn.common.scripts.CatalogDbUtils import org.onap.so.bpmn.common.scripts.ExceptionUtil import org.onap.so.bpmn.common.scripts.MsoUtils import org.onap.so.bpmn.core.domain.AllottedResource import org.onap.so.bpmn.core.domain.NetworkResource import org.onap.so.bpmn.core.domain.Resource import org.onap.so.bpmn.core.domain.ServiceDecomposition import org.onap.so.bpmn.core.domain.VnfResource import org.onap.so.bpmn.core.json.JsonUtils import org.onap.so.bpmn.core.UrnPropertiesReader import org.onap.so.bpmn.infrastructure.properties.BPMNProperties import org.slf4j.Logger import org.slf4j.LoggerFactory /** * input for script : * msoRequestId * isDebugLogEnabled * globalSubscriberId * serviceType * serviceInstanceId * URN_mso_workflow_sdncadapter_callback * serviceInputParams * deleteResourceList * resourceInstanceIDs * * output from script: * */ public class DoDeleteResourcesV1 extends AbstractServiceTaskProcessor { private static final Logger logger = LoggerFactory.getLogger( DoDeleteResourcesV1.class); String Prefix="DDR_" ExceptionUtil exceptionUtil = new ExceptionUtil() JsonUtils jsonUtil = new JsonUtils() CatalogDbUtils catalogDbUtils = new CatalogDbUtilsFactory().create() public void preProcessRequest (DelegateExecution execution) { logger.debug(" ***** preProcessRequest *****") String msg = "" try { String requestId = execution.getVariable("msoRequestId") execution.setVariable("prefix",Prefix) //Inputs //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId if (globalSubscriberId == null) { execution.setVariable("globalSubscriberId", "") } //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology String serviceType = execution.getVariable("serviceType") if (serviceType == null) { execution.setVariable("serviceType", "") } //Generated in parent for AAI PUT String serviceInstanceId = execution.getVariable("serviceInstanceId") if (isBlank(serviceInstanceId)){ msg = "Input serviceInstanceId is null" logger.error(msg) exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg) } String sdncCallbackUrl = UrnPropertiesReader.getVariable('URN_mso_workflow_sdncadapter_callback', execution) if (isBlank(sdncCallbackUrl)) { msg = "URN_mso_workflow_sdncadapter_callback is null" logger.error(msg) exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg) } execution.setVariable("sdncCallbackUrl", sdncCallbackUrl) logger.debug("SDNC Callback URL: " + sdncCallbackUrl) StringBuilder sbParams = new StringBuilder() Map paramsMap = execution.getVariable("serviceInputParams") if (paramsMap != null) { sbParams.append("") for (Map.Entry entry : paramsMap.entrySet()) { String paramsXml String paramName = entry.getKey() String paramValue = entry.getValue() paramsXml = """ ${MsoUtils.xmlEscape(paramName)} ${MsoUtils.xmlEscape(paramValue)} """ sbParams.append(paramsXml) } sbParams.append("") } String siParamsXml = sbParams.toString() if (siParamsXml == null) siParamsXml = "" execution.setVariable("siParamsXml", siParamsXml) } catch (BpmnError e) { throw e; } catch (Exception ex){ msg = "Exception in preProcessRequest " + ex.getMessage() logger.error(msg) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg) } logger.debug(" ***** Exit preProcessRequest *****",) } public void sequenceResource(DelegateExecution execution){ logger.debug(" ======== STARTED sequenceResource Process ======== ") List sequencedResourceList = new ArrayList() List wanResources = new ArrayList() // get delete resource list and order list List delResourceList = execution.getVariable("deleteResourceList") ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") String serviceModelName = serviceDecomposition.getModelInfo().getModelName(); def resourceSequence = BPMNProperties.getResourceSequenceProp(serviceModelName) if(resourceSequence != null) { for (resourceType in resourceSequence.reverse()) { for (resource in delResourceList) { if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) { sequencedResourceList.add(resource) if (resource instanceof NetworkResource) { wanResources.add(resource) } } } } }else { //define sequenced resource list, we deploy vf first and then network and then ar //this is defaule sequence List vnfResourceList = new ArrayList() List arResourceList = new ArrayList() for (Resource rc : delResourceList) { if (rc instanceof VnfResource) { vnfResourceList.add(rc) } else if (rc instanceof NetworkResource) { wanResources.add(rc) } else if (rc instanceof AllottedResource) { arResourceList.add(rc) } } sequencedResourceList.addAll(arResourceList) sequencedResourceList.addAll(wanResources) sequencedResourceList.addAll(vnfResourceList) } String isContainsWanResource = wanResources.isEmpty() ? "false" : "true" //if no networkResource, get SDNC config from properties file if( "false".equals(isContainsWanResource)) { String serviceNeedSDNC = "mso.workflow.custom." + serviceModelName + ".sdnc.need"; isContainsWanResource = BPMNProperties.getProperty(serviceNeedSDNC, isContainsWanResource) } execution.setVariable("isContainsWanResource", isContainsWanResource) execution.setVariable("currentResourceIndex", 0) execution.setVariable("sequencedResourceList", sequencedResourceList) logger.debug("resourceSequence: " + resourceSequence) logger.debug(" ======== END sequenceResource Process ======== ") } /** * prepare delete parameters */ public void preResourceDelete(DelegateExecution execution){ logger.debug(" ======== STARTED preResourceDelete Process ======== ") List sequencedResourceList = execution.getVariable("sequencedResourceList") int currentIndex = execution.getVariable("currentResourceIndex") if(sequencedResourceList != null && sequencedResourceList.size() > currentIndex){ Resource curResource = sequencedResourceList.get(currentIndex); String resourceInstanceUUID = curResource.getResourceId() String resourceTemplateUUID = curResource.getModelInfo().getModelUuid() execution.setVariable("resourceInstanceId", resourceInstanceUUID) execution.setVariable("currentResource", curResource) logger.debug("Delete Resource Info resourceTemplate Id :" + resourceTemplateUUID + " resourceInstanceId: " + resourceInstanceUUID + " resourceModelName: " + curResource.getModelInfo().getModelName()) } else { execution.setVariable("resourceInstanceId", "") } logger.debug(" ======== END preResourceDelete Process ======== ") } /** * Execute delete workflow for resource */ public void executeResourceDelete(DelegateExecution execution) { logger.debug("======== Start executeResourceDelete Process ======== ") try { String requestId = execution.getVariable("msoRequestId") String serviceInstanceId = execution.getVariable("serviceInstanceId") String serviceType = execution.getVariable("serviceType") String resourceInstanceId = execution.getVariable("resourceInstanceId") Resource currentResource = execution.getVariable("currentResource") String action = "deleteInstance" JSONObject resourceRecipe = catalogDbUtils.getResourceRecipe(execution, currentResource.getModelInfo().getModelUuid(), action) String recipeUri = resourceRecipe.getString("orchestrationUri") int recipeTimeout = resourceRecipe.getInt("recipeTimeout") String recipeParamXsd = resourceRecipe.get("paramXSD") ResourceInput resourceInput = new ResourceInput(); resourceInput.setServiceInstanceId(serviceInstanceId) resourceInput.setResourceInstanceName(currentResource.getResourceInstanceName()) resourceInput.setResourceInstancenUuid(currentResource.getResourceId()) resourceInput.setOperationId(execution.getVariable("operationId")) resourceInput.setOperationType(execution.getVariable("operationType")) String globalSubscriberId = execution.getVariable("globalSubscriberId") resourceInput.setGlobalSubscriberId(globalSubscriberId) resourceInput.setResourceModelInfo(currentResource.getModelInfo()); ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo()); resourceInput.setServiceType(serviceType) String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + recipeUri HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeout, action, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd) logger.debug(" ======== END executeResourceDelete Process ======== ") }catch(BpmnError b){ logger.error("Rethrowing MSOWorkflowException") throw b }catch(Exception e){ logger.error("Error occured within DoDeleteResourcesV1 executeResourceDelete method: " + e) exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured during DoDeleteResourcesV1 executeResourceDelete Catalog") } } public void parseNextResource(DelegateExecution execution){ logger.debug("======== Start parseNextResource Process ======== ") def currentIndex = execution.getVariable("currentResourceIndex") def nextIndex = currentIndex + 1 execution.setVariable("currentResourceIndex", nextIndex) List sequencedResourceList = execution.getVariable("sequencedResourceList") if(nextIndex >= sequencedResourceList.size()){ execution.setVariable("allResourceFinished", "true") }else{ execution.setVariable("allResourceFinished", "false") } logger.debug("======== COMPLETED parseNextResource Process ======== ") } public void prepareFinishedProgressForResource(DelegateExecution execution) { String serviceInstanceId = execution.getVariable("serviceInstanceId") String serviceType = execution.getVariable("serviceType") String resourceInstanceId = execution.getVariable("resourceInstanceId") Resource currentResource = execution.getVariable("currentResource") String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid() String resourceModelName = currentResource.getModelInfo().getModelName() String operationType = execution.getVariable("operationType") String progress = "100" String status = "finished" String statusDescription = "The resource instance does not exist for " + resourceModelName String operationId = execution.getVariable("operationId") String body = """ ${MsoUtils.xmlEscape(operationType)} ${MsoUtils.xmlEscape(operationId)} ${MsoUtils.xmlEscape(progress)} ${MsoUtils.xmlEscape(resourceCustomizationUuid)} ${MsoUtils.xmlEscape(serviceInstanceId)} ${MsoUtils.xmlEscape(status)} ${MsoUtils.xmlEscape(statusDescription)} """; def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution) execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint) execution.setVariable("CVFMI_updateResOperStatusRequest", body) } public void prepareSDNCServiceDeactivateRequest (DelegateExecution execution) { prepareSDNCServiceRequest (execution, "deactivate") } public void prepareSDNCServiceDeleteRequest (DelegateExecution execution) { prepareSDNCServiceRequest (execution, "delete") } public void prepareSDNCServiceRequest (DelegateExecution execution, String svcAction) { logger.debug(" ***** Started prepareSDNCServiceRequest for " + svcAction + "*****") try { // get variables String sdnc_svcAction = svcAction String sdncCallback = execution.getVariable("URN_mso_workflow_sdncadapter_callback") String hdrRequestId = execution.getVariable("msoRequestId") String serviceInstanceId = execution.getVariable("serviceInstanceId") String source = execution.getVariable("source") String sdnc_service_id = serviceInstanceId ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") String serviceType = execution.getVariable("serviceType") String globalCustomerId = execution.getVariable("globalSubscriberId") String serviceModelInvariantUuid = serviceDecomposition.getModelInfo().getModelInvariantUuid() String serviceModelUuid = serviceDecomposition.getModelInfo().getModelUuid() String serviceModelVersion = serviceDecomposition.getModelInfo().getModelVersion() String serviceModelName = serviceDecomposition.getModelInfo().getModelName() // 1. prepare assign topology via SDNC Adapter SUBFLOW call String sndcTopologyDeleteRequest = """ ${MsoUtils.xmlEscape(hdrRequestId)} ${MsoUtils.xmlEscape(serviceInstanceId)} ${MsoUtils.xmlEscape(sdnc_svcAction)} service-topology-operation sdncCallback generic-resource ${MsoUtils.xmlEscape(hdrRequestId)} DeleteServiceInstance ${MsoUtils.xmlEscape(source)} ${MsoUtils.xmlEscape(serviceInstanceId)} ${MsoUtils.xmlEscape(serviceType)} ${MsoUtils.xmlEscape(serviceModelInvariantUuid)} ${MsoUtils.xmlEscape(serviceModelUuid)} ${MsoUtils.xmlEscape(serviceModelVersion)} ${MsoUtils.xmlEscape(serviceModelName)} ${MsoUtils.xmlEscape(serviceInstanceId)} ${MsoUtils.xmlEscape(globalCustomerId)} """.trim() String sndcTopologyDeleteRequesAsString = utils.formatXml(sndcTopologyDeleteRequest) logger.debug(sndcTopologyDeleteRequesAsString) execution.setVariable("sdncAdapterWorkflowRequest", sndcTopologyDeleteRequesAsString) logger.debug("sdncAdapterWorkflowRequest - " + "\n" + sndcTopologyDeleteRequesAsString) } catch (Exception ex) { String exceptionMessage = " Bpmn error encountered in DoDeleteResourcesV1 flow. prepareSDNCServiceRequest() - " + ex.getMessage() logger.debug(exceptionMessage) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage) } logger.debug("***** Exit prepareSDNCServiceRequest for " + svcAction + "*****") } }