diff options
author | andre.schmid <andre.schmid@est.tech> | 2020-02-05 12:13:57 +0000 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-06-15 08:06:51 +0000 |
commit | 0b3c65c18977212bc74b1f01cefe5ebecc2586f3 (patch) | |
tree | dd81f6c5fafb4932868f505db0eddaa9af688b1a /openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java | |
parent | e3e2db1d777b89e8292b88f677bcc7d81e6bf2c0 (diff) |
Onboard TOSCA data_types defined in package
Onboard TOSCA data_types defined in a CSAR package
that does not exists in the SDC default data_types
library. Those data_types were being lost.
All data_types declared in the package will be
included in the "GlobalSubstitutionServiceTemplate".
This includes the data_types declared in the main
topology template and in its imported definition
files.
Defines a maven profile to compile the catalog-be
in the main SDC pom.
Change-Id: I39445b0f42e924dc1015945b7e605e804c1d505b
Issue-ID: SDC-2763
Signed-off-by: André Schmid <andre.schmid@est.tech>
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
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 | 99 |
1 files changed, 70 insertions, 29 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 index f6955bbdad..98d8d23751 100644 --- 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 @@ -16,43 +16,44 @@ package org.openecomp.core.impl; +import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.stream.Collectors; import org.apache.commons.collections4.MapUtils; +import org.onap.sdc.tosca.datatypes.model.DataType; import org.onap.sdc.tosca.datatypes.model.Import; import org.onap.sdc.tosca.datatypes.model.NodeType; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator; -import java.util.*; - -import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX; - 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 DEFINITION_VERSION = "tosca_simple_yaml_1_0_0"; public static final String HEAT_INDEX = "openecomp_heat_index"; public static final String HEAT_INDEX_IMPORT_FILE = "openecomp-heat/_index.yml"; public static final String ONAP_INDEX_IMPORT_FILE = "onap/_index.yml"; - private static final Map<String, ServiceTemplate> globalServiceTemplates = - GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR); + + // transient needed to avoid being parsed as a YAML String. Used parser is reading fields instead of getters, + // although it ignores static or transient fields. + private final transient Map<String, ServiceTemplate> globalServiceTemplates; + private final transient Map<String, DataType> globalDataTypeMap; 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)); + globalServiceTemplates = + GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR); + globalDataTypeMap = loadGlobalDataTypes(); } public void init() { @@ -60,6 +61,24 @@ public class GlobalSubstitutionServiceTemplate extends ServiceTemplate { writeMetadataSection(); writeImportsSection(); setNode_types(new HashMap<>()); + setData_types(new HashMap<>()); + } + + public void appendNodes(final Map<String, NodeType> nodes) { + final Optional<Map<String, NodeType>> nodeTypesToAdd = findNonGlobalTypesNodes(nodes); + nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes)); + } + + public void appendDataTypes(final Map<String, DataType> dataTypeMap) { + if (MapUtils.isEmpty(dataTypeMap)) { + return; + } + dataTypeMap.entrySet().stream() + .filter(dataTypeEntry -> !isGlobalDataType(dataTypeEntry.getKey())) + .forEach(dataTypeEntry -> { + final Optional<DataType> dataType = parseDataTypeToYamlObject(dataTypeEntry); + dataType.ifPresent(dataType1 -> getData_types().put(dataTypeEntry.getKey(), dataType1)); + }); } private void writeImportsSection() { @@ -83,24 +102,20 @@ public class GlobalSubstitutionServiceTemplate extends ServiceTemplate { } private void writeDefinitionSection() { - setTosca_definitions_version(DEFININTION_VERSION); + setTosca_definitions_version(DEFINITION_VERSION); } - private 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())){ + private Optional<Map<String, NodeType>> findNonGlobalTypesNodes(final Map<String, NodeType> nodes){ + final Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes(); + if (MapUtils.isEmpty(globalNodeTypes)) { return Optional.of(nodes); } - Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes(); + final Map<String, NodeType> nodeTypesToAdd = new HashMap<>(); + for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){ if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){ - Optional<NodeType> nodeType = - ToscaConverterUtil - .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class); - + Optional<NodeType> nodeType = parseNodeTypeToYamlObject(nodeTypeEntry); nodeType .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue)); } @@ -109,6 +124,32 @@ public class GlobalSubstitutionServiceTemplate extends ServiceTemplate { return Optional.of(nodeTypesToAdd); } + private boolean isGlobalDataType(final String dataType) { + if (MapUtils.isEmpty(globalDataTypeMap)) { + return false; + } + + return globalDataTypeMap.containsKey(dataType); + } + + private Optional<NodeType> parseNodeTypeToYamlObject(final Entry<String, NodeType> nodeTypeEntry) { + return ToscaConverterUtil + .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class); + } + + private Optional<DataType> parseDataTypeToYamlObject(final Entry<String, DataType> dataTypeEntry) { + return ToscaConverterUtil + .createObjectFromClass(dataTypeEntry.getKey(), dataTypeEntry.getValue(), DataType.class); + } + + private Map<String, DataType> loadGlobalDataTypes() { + return globalServiceTemplates.values().stream() + .map(ServiceTemplate::getData_types) + .filter(MapUtils::isNotEmpty) + .flatMap(stringDataTypeMap -> stringDataTypeMap.entrySet().stream()) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (dataType, dataType2) -> dataType)); + } + private Map<String, NodeType> getAllGlobalNodeTypes(){ Map<String, NodeType> globalNodeTypes = new HashMap<>(); |