From a26e2683d10fdd4484d67bb8b7d6a2e0a41ab753 Mon Sep 17 00:00:00 2001 From: ojasdubey Date: Thu, 29 Mar 2018 11:41:55 +0530 Subject: Component Dependency create VF bug fix 1. Fixed create VF issues for scenarios where dependencies were added from the UI 2. Added supporting tests and fixed existing Change-Id: Ib8bb0074183567c263855f8723028646c9629038 Issue-ID: SDC-1184 Signed-off-by: ojasdubey --- .../tosca/AbstractSubstituteToscaEnricher.java | 99 +++++++++++++--------- 1 file changed, 61 insertions(+), 38 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/main/java') diff --git a/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/main/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricher.java b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/main/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricher.java index fbd0d225a5..e414b31b76 100644 --- a/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/main/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricher.java +++ b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/main/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricher.java @@ -1,12 +1,32 @@ package org.openecomp.sdc.enrichment.impl.tosca; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.HIGH_AVAIL_MODE; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MANDATORY; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MAX_INSTANCES; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MIN_INSTANCES; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_CODE; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_FUNCTION; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_NAMING_CODE; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VM_TYPE_TAG; +import static org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType.NATIVE_NODE; +import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.VFC_ABSTRACT_SUBSTITUTE; +import static org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType.NATIVE_DEPENDS_ON; +import static org.openecomp.sdc.tosca.services.ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME; +import static org.openecomp.sdc.translator.services.heattotosca.Constants.ABSTRACT_NODE_TEMPLATE_ID_PREFIX; + +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate; +import org.openecomp.sdc.tosca.datatypes.model.NodeType; import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment; +import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition; import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.services.DataModelUtil; import org.openecomp.sdc.tosca.services.ToscaAnalyzerService; +import org.openecomp.sdc.tosca.services.ToscaConstants; import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl; import org.openecomp.sdc.versioning.dao.types.Version; @@ -14,22 +34,9 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.HIGH_AVAIL_MODE; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MANDATORY; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MAX_INSTANCES; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MIN_INSTANCES; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_CODE; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_FUNCTION; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_NAMING_CODE; -import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VM_TYPE_TAG; -import static org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType.NATIVE_NODE; -import static org.openecomp.sdc.tosca.datatypes.ToscaNodeType.VFC_ABSTRACT_SUBSTITUTE; -import static org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType.NATIVE_DEPENDS_ON; -import static org.openecomp.sdc.tosca.services.ToscaConstants.SERVICE_TEMPLATE_FILTER_PROPERTY_NAME; -import static org.openecomp.sdc.translator.services.heattotosca.Constants.ABSTRACT_NODE_TEMPLATE_ID_PREFIX; - public class AbstractSubstituteToscaEnricher { private ToscaAnalyzerService toscaAnalyzerService ; private ComponentQuestionnaireData componentQuestionnaireData; @@ -123,7 +130,7 @@ public class AbstractSubstituteToscaEnricher { } enrichRequirements(sourceToTargetDependencies, componentDisplayName, nodeTemplate, - componentDisplayNameToNodeTempalteIds); + componentDisplayNameToNodeTempalteIds, serviceTemplate, toscaModel); } }); return errors; @@ -168,35 +175,51 @@ public class AbstractSubstituteToscaEnricher { private void enrichRequirements(Map> sourceToTargetDependencies, String componentDisplayName, NodeTemplate nodeTemplate, - Map> componentDisplayNameToNodeTempalteIds) { - List> requirements = - nodeTemplate.getRequirements(); - - if(requirements == null) { - requirements = new ArrayList>(); + Map> componentDisplayNameToNodeTempalteIds, + ServiceTemplate serviceTemplate, ToscaServiceModel toscaServiceModel) { + final List targets = sourceToTargetDependencies.get(componentDisplayName); + if (CollectionUtils.isEmpty(targets)) { + return; } - final List targets = sourceToTargetDependencies.get(componentDisplayName); - if(targets != null) { - for (String target : targets) { - List targetNodeTemplateIds = componentDisplayNameToNodeTempalteIds.get(target); - if(targetNodeTemplateIds != null) { - for (String targetNodeTemplateId : targetNodeTemplateIds) { - Map requirement = new HashMap(); - RequirementAssignment requirementAssignment = new RequirementAssignment(); - requirementAssignment.setCapability(NATIVE_NODE); - requirementAssignment.setRelationship(NATIVE_DEPENDS_ON); - requirementAssignment.setNode(targetNodeTemplateId); - requirement.put("dependency", requirementAssignment); - requirements.add(requirement); - } + for (String target : targets) { + List targetNodeTemplateIds = componentDisplayNameToNodeTempalteIds.get(target); + if (CollectionUtils.isEmpty(targetNodeTemplateIds)) { + continue; + } + for (String targetNodeTemplateId : targetNodeTemplateIds) { + Optional dependencyRequirementKey = + getDependencyRequirementKey(serviceTemplate, componentDisplayName, nodeTemplate, toscaServiceModel); + if (dependencyRequirementKey.isPresent()) { + RequirementAssignment requirementAssignment = new RequirementAssignment(); + requirementAssignment.setCapability(NATIVE_NODE); + requirementAssignment.setRelationship(NATIVE_DEPENDS_ON); + requirementAssignment.setNode(targetNodeTemplateId); + DataModelUtil.addRequirementAssignment(nodeTemplate, dependencyRequirementKey.get(), requirementAssignment); } } } + } - if (!requirements.isEmpty()) - nodeTemplate.setRequirements(requirements); + private Optional getDependencyRequirementKey(ServiceTemplate serviceTemplate, + String componentDisplayName, + NodeTemplate nodeTemplate, + ToscaServiceModel toscaServiceModel) { + String nodeType = nodeTemplate.getType(); + NodeType flatNodeType = (NodeType) toscaAnalyzerService + .getFlatEntity(ToscaElementTypes.NODE_TYPE, nodeType, serviceTemplate, toscaServiceModel); + List> flatNodeTypeRequirements = flatNodeType.getRequirements(); + if (Objects.isNull(flatNodeTypeRequirements)) { + return Optional.empty() ; + } + for (Map requirementDefinitionMap : flatNodeTypeRequirements) { + String requirementKey = requirementDefinitionMap.keySet().iterator().next(); + String expectedKey = ToscaConstants.DEPENDS_ON_REQUIREMENT_ID + "_" + componentDisplayName; + if (requirementKey.equals(expectedKey)) { + return Optional.of(requirementKey); + } + } + return Optional.empty(); } private String getComponentDisplayName(String nodeTemplateId, NodeTemplate nodeTemplate) { -- cgit 1.2.3-korg