diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java')
-rw-r--r-- | openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java new file mode 100644 index 0000000000..778445c513 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java @@ -0,0 +1,87 @@ +package org.openecomp.core.impl; + +import org.apache.commons.collections4.MapUtils; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.tosca.datatypes.model.Import; +import org.openecomp.sdc.tosca.datatypes.model.NodeType; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +public class GlobalSubstitutionServiceTemplate extends ServiceTemplate { + private static final Logger logger = LoggerFactory.getLogger(ServiceTemplate.class); + + public static final String GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME = + "GlobalSubstitutionTypesServiceTemplate.yaml"; + public static final String TEMPLATE_NAME_PROPERTY = "template_name"; + public static final String DEFININTION_VERSION = "tosca_simple_yaml_1_0_0"; + public static final String HEAT_INDEX = "openecomp_heat_index"; + private static final Map<String, ServiceTemplate> globalServiceTemplates = + GlobalTypesGenerator.getGlobalTypesServiceTemplate(); + + public GlobalSubstitutionServiceTemplate() { + super(); + init(); + } + + + public void appendNodes(Map<String, NodeType> nodes) { + Optional<Map<String, NodeType>> nodeTypesToAdd = + removeExistingGlobalTypes(nodes); + + nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes)); + } + + public void init() { + writeDefinitionSection(); + writeMetadataSection(); + writeImportsSection(); + setNode_types(new HashMap<>()); + } + + private void writeImportsSection() { + List<Map<String, Import>> imports = new ArrayList<>(); + Map<String, Import> stringImportMap = new HashMap<>(); + imports.add(stringImportMap); + setImports(imports); + Import imprtObj = new Import(); + imprtObj.setFile("openecomp-heat/_index.yml"); + stringImportMap.put("openecomp_heat_index", imprtObj); + } + + + private void writeMetadataSection() { + Map<String, String> metadata = new HashMap<>(); + metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes"); + setMetadata(metadata); + } + + private void writeDefinitionSection() { + setTosca_definitions_version(DEFININTION_VERSION); + } + + public Optional<Map<String, NodeType>> removeExistingGlobalTypes(Map<String, NodeType> nodes){ + Map<String, NodeType> nodeTypesToAdd = new HashMap<>(); + ServiceTemplate serviceTemplate = globalServiceTemplates.get("openecomp/nodes.yml"); + + if(Objects.isNull(serviceTemplate) || MapUtils.isEmpty(serviceTemplate.getNode_types())){ + return Optional.of(nodes); + } + + Map<String, NodeType> globalNodeTypes = serviceTemplate.getNode_types(); + for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){ + if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){ + nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeEntry.getValue()); + } + } + + return Optional.of(nodeTypesToAdd); + } +} |