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 | |
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')
11 files changed, 309 insertions, 159 deletions
diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java index ea507f54e2..1ec6a28e7d 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java @@ -149,6 +149,7 @@ public enum Messages { /*definition errors*/ MISSING_DEFINITION_FILE("Definition file %s referenced in TOSCA.meta does not exist"), MISSING_IMPORT_FILE("Package must contain the referenced import file '%s'"), + MISSING_MAIN_DEFINITION_FILE("Package must contain the given main definition file '%s'"), INVALID_IMPORT_STATEMENT("Definition file '%s' contains an invalid import statement: '%s'"), INVALID_YAML_EXTENSION("Expecting yaml or yml extension for file: %s"), diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java index 8a97957be9..3a0b2ce401 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java @@ -1,5 +1,4 @@ - -/*- +/* * ============LICENSE_START======================================================= * Modification Copyright (C) 2019 Nordix Foundation. * ================================================================================ @@ -26,7 +25,7 @@ import java.util.Map; public interface ServiceTemplateReaderService { - Map<String, Object> readServiceTemplate(byte[] serivceTemplateContent); + Map<String, Object> readServiceTemplate(byte[] serviceTemplateContent); Object getMetadata(); @@ -47,4 +46,6 @@ public interface ServiceTemplateReaderService { List<Object> getImports(); Map<String, Object> getPolicies(); + + Map<String, Object> getDataTypes(); } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java index 6619a4c580..43ae3c9f38 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaConverter.java @@ -33,7 +33,6 @@ import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.HEAT_IND import static org.openecomp.core.impl.GlobalSubstitutionServiceTemplate.ONAP_INDEX_IMPORT_FILE; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; - import java.io.File; import java.util.ArrayList; import java.util.Collection; @@ -43,7 +42,9 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.regex.Pattern; +import javax.validation.constraints.NotNull; import org.apache.commons.collections.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; @@ -60,11 +61,14 @@ import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.tosca.services.DataModelUtil; import org.openecomp.sdc.tosca.services.ToscaUtil; import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.error.YAMLException; public abstract class AbstractToscaConverter implements ToscaConverter { + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaConverter.class); - public abstract void convertTopologyTemplate(ServiceTemplate serviceTemplate, + public abstract void convertTopologyTemplate(@NotNull ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService); protected void handleMetadataFile(Map<String, byte[]> csarFiles) { @@ -74,8 +78,8 @@ public abstract class AbstractToscaConverter implements ToscaConverter { } } - protected void handleDefintionTemplate(String key, Map<String, byte[]> csarFiles, - GlobalSubstitutionServiceTemplate gsst) { + protected void handleDefinitionTemplate(String key, Map<String, byte[]> csarFiles, + GlobalSubstitutionServiceTemplate gsst) { try { ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(key)); Object nodeTypes = readerService.getNodeTypes(); @@ -83,6 +87,7 @@ public abstract class AbstractToscaConverter implements ToscaConverter { Map<String, NodeType> nodeTypeMap = (Map<String, NodeType>) nodeTypes; gsst.appendNodes(nodeTypeMap); } + gsst.appendDataTypes((Map) readerService.getDataTypes()); } catch (YAMLException ye) { throw new CoreException(new ErrorCode.ErrorCodeBuilder() .withMessage("Invalid YAML content in file " + key) @@ -158,6 +163,7 @@ public abstract class AbstractToscaConverter implements ToscaConverter { convertToscaVersion(serviceTemplate, readerService); convertImports(serviceTemplate); convertNodeTypes(serviceTemplate, readerService); + convertDataTypes(serviceTemplate, readerService); convertTopologyTemplate(serviceTemplate, readerService); } catch (YAMLException ye) { throw new CoreException(new ErrorCode.ErrorCodeBuilder() @@ -228,6 +234,22 @@ public abstract class AbstractToscaConverter implements ToscaConverter { } } + protected void convertDataTypes(final ServiceTemplate serviceTemplate, + final ServiceTemplateReaderService readerService) { + try { + final Map<String, Object> dataTypes = readerService.getDataTypes(); + for (final Map.Entry<String, Object> entry : dataTypes.entrySet()) { + final Optional<DataType> dataType = + ToscaConverterUtil.createObjectFromClass(entry.getKey(), entry.getValue(), DataType.class); + + dataType.ifPresent( + nodeTypeValue -> DataModelUtil.addDataType(serviceTemplate, entry.getKey(), nodeTypeValue)); + } + } catch (final Exception ex) { + LOGGER.error("Unable to process data types: ", ex); + } + } + protected CsarFileTypes getFileType(String fileName) { if (isMainServiceTemplate(fileName)) { return CsarFileTypes.mainServiceTemplate; diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaSolConverter.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaSolConverter.java index 6349963841..f94d2bf5dd 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaSolConverter.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/AbstractToscaSolConverter.java @@ -32,6 +32,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.core.converter.ServiceTemplateReaderService; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; @@ -87,9 +88,11 @@ public abstract class AbstractToscaSolConverter extends AbstractToscaConverter { if (toscaDefinitionImportHandler.hasError()) { throw new InvalidToscaDefinitionImportException(toscaDefinitionImportHandler.getErrors()); } - handledDefinitionFilesList.addAll(toscaDefinitionImportHandler.getHandledDefinitionFilesList()); + final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); + handledDefinitionFilesList.addAll(handledImportDefinitionFileMap.keySet()); for (final String file : handledDefinitionFilesList) { - handleDefintionTemplate(file, csarFiles, gsst); + handleDefinitionTemplate(file, csarFiles, gsst); } } 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<>(); diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java index 8d4b9850ea..fcc6f0ab31 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java @@ -55,7 +55,7 @@ public class ToscaConverterImpl extends AbstractToscaConverter { break; case definitionsFile: - handleDefintionTemplate(fileEntry.getKey(), csarFiles, gsst); + handleDefinitionTemplate(fileEntry.getKey(), csarFiles, gsst); break; default: diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java index 8422c89f2e..661cadc738 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java @@ -23,10 +23,13 @@ import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBU import java.net.URI; import java.util.ArrayList; -import java.util.LinkedHashSet; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; +import java.util.Map.Entry; +import java.util.Optional; +import lombok.Getter; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.openecomp.core.converter.ServiceTemplateReaderService; @@ -34,14 +37,23 @@ import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Handles TOSCA definition imports, checking for import definition errors. */ public class ToscaDefinitionImportHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDefinitionImportHandler.class); + private final Map<String, byte[]> fileMap; - private final Set<String> handledDefinitionFilesList = new LinkedHashSet<>(); + + /** + * Stores all processed files during the import handling + */ + @Getter + private final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap = new HashMap<>(); private final List<ErrorMessage> validationErrorList = new ArrayList<>(); private String currentFile; @@ -50,9 +62,39 @@ public class ToscaDefinitionImportHandler { * @param fileStructureMap The package structure with file path and respective file byte * @param mainDefinitionFilePath The main descriptor yaml file to start the reading */ - public ToscaDefinitionImportHandler(final Map<String, byte[]> fileStructureMap, final String mainDefinitionFilePath) { + public ToscaDefinitionImportHandler(final Map<String, byte[]> fileStructureMap, + final String mainDefinitionFilePath) { this.fileMap = fileStructureMap; - handleImports(mainDefinitionFilePath); + readImportsFromMainDefinition(mainDefinitionFilePath); + } + + private void readImportsFromMainDefinition(final String mainDefinitionFilePath) { + if(!checkMainDefinitionExists(mainDefinitionFilePath)) { + return; + } + final ServiceTemplateReaderService readerService = parseToServiceTemplate(mainDefinitionFilePath).orElse(null); + if (readerService == null) { + return; + } + final List<String> importFileList = extractFileImports(readerService.getImports()); + if (CollectionUtils.isNotEmpty(importFileList)) { + for (final String importFilePath : importFileList) { + final String resolvedPath = resolveImportPath(FilenameUtils.getPath(mainDefinitionFilePath), importFilePath); + handleImports(resolvedPath); + } + } + } + + private Optional<ServiceTemplateReaderService> parseToServiceTemplate(final String definitionFile) { + try { + return Optional.of(new ServiceTemplateReaderServiceImpl(fileMap.get(definitionFile))); + } catch (final Exception ex) { + LOGGER.debug("Could not parse '{}' to a ServiceTemplateReader", definitionFile, ex); + reportError(ErrorLevel.ERROR, + String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage(), ex.getMessage())); + } + + return Optional.empty(); } /** @@ -67,23 +109,18 @@ public class ToscaDefinitionImportHandler { if (!checkImportExists(fileName)) { return; } - final ServiceTemplateReaderService readerService; - try { - readerService = new ServiceTemplateReaderServiceImpl(fileMap.get(fileName)); - } catch (final Exception ex) { - reportError(ErrorLevel.ERROR, - String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage(), ex.getMessage())); + final ServiceTemplateReaderService readerService = parseToServiceTemplate(fileName).orElse(null); + if (readerService == null) { return; } - handledDefinitionFilesList.add(fileName); + + handledImportDefinitionFileMap.put(fileName, readerService); final List<Object> imports = readerService.getImports(); final List<String> extractImportFiles = extractFileImports(imports); - for (final String importedFile : extractImportFiles) { - final String resolvedPath = resolveImportPath(FilenameUtils.getPath(fileName), importedFile); - if (!handledDefinitionFilesList.contains(resolvedPath)) { - handleImports(resolvedPath); - } - } + extractImportFiles.stream() + .map(importedFile -> resolveImportPath(FilenameUtils.getPath(fileName), importedFile)) + .filter(resolvedPath -> !handledImportDefinitionFileMap.containsKey(resolvedPath)) + .forEach(this::handleImports); } /** @@ -129,7 +166,7 @@ public class ToscaDefinitionImportHandler { importedFileList.add((String) importObject); } else if (importObject instanceof Map) { final Map<String, Object> importObjectMap = (Map) importObject; - for (final Map.Entry entry : importObjectMap.entrySet()) { + for (final Entry<String, Object> entry : importObjectMap.entrySet()) { if (NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))) { importedFileList.addAll(readImportStatement(entry.getValue())); } @@ -175,17 +212,26 @@ public class ToscaDefinitionImportHandler { return resolvedImportPath; } + private boolean checkImportExists(final String filePath) { + return checkFileExists(filePath, Messages.MISSING_IMPORT_FILE.formatMessage(filePath)); + } + + private boolean checkMainDefinitionExists(final String filePath) { + return checkFileExists(filePath, Messages.MISSING_MAIN_DEFINITION_FILE.formatMessage(filePath)); + } + /** * Checks if the given file path exists inside the file structure. * Reports an error if the file was not found. * * @param filePath file path to check inside the file structure + * @param errorMsg the error message to report * @return * {@code true} if the file exists, {@code false} otherwise */ - private boolean checkImportExists(final String filePath) { - if (!fileMap.keySet().contains(filePath)) { - reportError(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(filePath)); + private boolean checkFileExists(final String filePath, final String errorMsg) { + if (!fileMap.containsKey(filePath)) { + reportError(ErrorLevel.ERROR, errorMsg); return false; } @@ -193,15 +239,6 @@ public class ToscaDefinitionImportHandler { } /** - * Gets all processed files during the import handling. - * @return - * A list containing the processed files paths - */ - public Set<String> getHandledDefinitionFilesList() { - return handledDefinitionFilesList; - } - - /** * Adds an error to the validation error list. * * @param errorLevel the error level diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java index 9b1c0126c7..a74ec8eb7c 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java @@ -20,21 +20,29 @@ package org.openecomp.core.impl.services; -import org.onap.sdc.tosca.services.YamlUtil; -import org.openecomp.core.converter.ServiceTemplateReaderService; +import static org.openecomp.core.converter.datatypes.Constants.POLICIES; +import static org.openecomp.core.converter.datatypes.Constants.inputs; +import static org.openecomp.core.converter.datatypes.Constants.metadata; +import static org.openecomp.core.converter.datatypes.Constants.nodeTemplates; +import static org.openecomp.core.converter.datatypes.Constants.nodeTypes; +import static org.openecomp.core.converter.datatypes.Constants.outputs; +import static org.openecomp.core.converter.datatypes.Constants.substitutionMappings; +import static org.openecomp.core.converter.datatypes.Constants.topologyTemplate; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DATA_TYPES; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IMPORTS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; - -import static org.openecomp.core.converter.datatypes.Constants.*; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.converter.ServiceTemplateReaderService; public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderService { - private Map<String, Object> readServiceTemplate = new HashMap<>(); + private final Map<String, Object> readServiceTemplate; public ServiceTemplateReaderServiceImpl(byte[] serviceTemplateContent) { this.readServiceTemplate = readServiceTemplate(serviceTemplateContent); @@ -42,22 +50,21 @@ public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderSe @Override public Map<String, Object> readServiceTemplate(byte[] serviceTemplateContent) { - return new YamlUtil().yamlToObject(new String(serviceTemplateContent), Map.class); - } @Override public List<Object> getImports() { - return Objects.isNull(this.readServiceTemplate.get("imports")) ? new ArrayList<>() - : (List<Object>) this.readServiceTemplate.get("imports"); + final String importsElementName = IMPORTS.getElementName(); + return Objects.isNull(this.readServiceTemplate.get(importsElementName)) ? new ArrayList<>() + : (List<Object>) this.readServiceTemplate.get(importsElementName); } @Override public Map<String, Object> getPolicies() { Map<String, Object> policiesAsMap = new HashMap<>(); if (!Objects.isNull(this.getTopologyTemplate()) && !Objects.isNull( - ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES))) { + ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES))) { policiesAsMap = (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES); } return policiesAsMap; @@ -70,13 +77,13 @@ public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderSe @Override public Object getToscaVersion() { - return this.readServiceTemplate.get(ToscaTagNamesEnum.TOSCA_VERSION.getElementName()); + return this.readServiceTemplate.get(TOSCA_VERSION.getElementName()); } @Override public Map<String, Object> getNodeTypes() { return Objects.isNull(this.readServiceTemplate.get(nodeTypes)) ? new HashMap<>() - : (Map<String, Object>) this.readServiceTemplate.get(nodeTypes); + : (Map<String, Object>) this.readServiceTemplate.get(nodeTypes); } @Override @@ -87,25 +94,32 @@ public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderSe @Override public Map<String, Object> getNodeTemplates() { return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>() - : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(nodeTemplates); + : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(nodeTemplates); } @Override public Map<String, Object> getInputs() { return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>() - : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(inputs); + : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(inputs); } @Override public Map<String, Object> getOutputs() { return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>() - : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(outputs); + : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(outputs); } @Override public Map<String, Object> getSubstitutionMappings() { return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>() - : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()) - .get(substitutionMappings); + : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()) + .get(substitutionMappings); + } + + @Override + public Map<String, Object> getDataTypes() { + final String dataTypesElementName = DATA_TYPES.getElementName(); + return Objects.isNull(this.readServiceTemplate.get(dataTypesElementName)) ? new HashMap<>() + : (Map<String, Object>) this.readServiceTemplate.get(dataTypesElementName); } } diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConvertorDefinitionsTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConvertorDefinitionsTest.java index b978b7db99..8167cce3aa 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConvertorDefinitionsTest.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaConvertorDefinitionsTest.java @@ -64,7 +64,7 @@ public class ToscaConvertorDefinitionsTest { assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(HEAT_INDEX)); assertTrue(((Map) serviceTemplate.getImports().get(0)).containsKey(ONAP_INDEX)); - assertEquals(DEFININTION_VERSION, serviceTemplate.getTosca_definitions_version()); + assertEquals(DEFINITION_VERSION, serviceTemplate.getTosca_definitions_version()); assertNotNull(serviceTemplate.getNode_types()); diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaDefinitionImportHandlerTest.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaDefinitionImportHandlerTest.java index fe0d5a8620..6f0901cfdb 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaDefinitionImportHandlerTest.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/test/java/org/openecomp/core/impl/ToscaDefinitionImportHandlerTest.java @@ -21,6 +21,8 @@ package org.openecomp.core.impl; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasSize; import static org.openecomp.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail; @@ -32,9 +34,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; +import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; +import org.openecomp.core.converter.ServiceTemplateReaderService; import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.datatypes.error.ErrorMessage; @@ -54,7 +57,10 @@ public class ToscaDefinitionImportHandlerTest { */ @Test public void testGivenDescriptorFiles_whenMainDescriptorImportsAreHandled_allDescriptorsAreProcessedWithoutError() { - final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml", "Definitions/descriptorBasicImport.yaml", + final String mainDefinitionFilePath = "Definitions/Main.yaml"; + descriptorFileMap + .put(mainDefinitionFilePath, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFilePath))); + final List<String> filesToHandleList = Arrays.asList("Definitions/descriptorBasicImport.yaml", "Definitions/descriptorWithRelativePaths.yaml", "Artifacts/descriptorWithAbsolutePaths.yaml", "Artifacts/descriptorCyclicReference.yaml"); @@ -64,12 +70,14 @@ public class ToscaDefinitionImportHandlerTest { final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler( descriptorFileMap, - "Definitions/Main.yaml"); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + mainDefinitionFilePath); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size())); + assertThat("The handled files should be the same", + actualHandledFiles.keySet(), hasSize(filesToHandleList.size())); assertThat("The handled files should be the same" - , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0])) + , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0])) ); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); @@ -83,19 +91,20 @@ public class ToscaDefinitionImportHandlerTest { public void testGivenEmptyPackage_whenMainDescriptorIsHandled_aMissingFileErrorIsReported() { final List<String> filesToHandleList = Collections.emptyList(); - final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler( - descriptorFileMap, - "Definitions/Main.yaml"); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + final ToscaDefinitionImportHandler toscaDefinitionImportHandler = + new ToscaDefinitionImportHandler(descriptorFileMap, "Definitions/Main.yaml"); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size())); + assertThat("The handled files should be the same", + actualHandledFiles.keySet(), hasSize(filesToHandleList.size())); assertThat("The handled files should be the same" - , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0])) + , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0])) ); final List<ErrorMessage> expectedErrorList = new ArrayList<>(); expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, - Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/Main.yaml"))); + Messages.MISSING_MAIN_DEFINITION_FILE.formatMessage("Definitions/Main.yaml"))); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size())); @@ -109,25 +118,30 @@ public class ToscaDefinitionImportHandlerTest { */ @Test public void testGivenOneMissingDescriptorFile_whenMainDescriptorImportsAreHandled_aMissingFileErrorIsReported() { - final List<String> filesToHandleList = Arrays.asList("Definitions/Main.yaml", - "Definitions/descriptorBasicImport.yaml", "Definitions/descriptorWithRelativePaths.yaml", - "Artifacts/descriptorWithAbsolutePaths.yaml"); + descriptorFileMap + .put("Definitions/Main.yaml", + getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve("Definitions/Main.yaml"))); + + final List<String> filesToHandleList = Arrays.asList("Definitions/descriptorBasicImport.yaml", + "Definitions/descriptorWithRelativePaths.yaml", "Artifacts/descriptorWithAbsolutePaths.yaml"); filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file))) ); final List<ErrorMessage> expectedErrorList = new ArrayList<>(); expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, - String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), "Artifacts/descriptorCyclicReference.yaml"))); + Messages.MISSING_IMPORT_FILE.formatMessage("Artifacts/descriptorCyclicReference.yaml"))); final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler( descriptorFileMap, "Definitions/Main.yaml"); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size())); + assertThat("The handled files should be the same", + actualHandledFiles.keySet(), hasSize(filesToHandleList.size())); assertThat("The handled files should be the same" - , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0])) + , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0])) ); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); @@ -143,25 +157,30 @@ public class ToscaDefinitionImportHandlerTest { @Test public void testGivenDescriptorWithInvalidImportStatement_whenMainDescriptorImportsAreHandled_aInvalidImportStatementErrorIsReported() { final String mainDefinitionFile = "Definitions/MainWithInvalidImportedFile.yaml"; + descriptorFileMap + .put(mainDefinitionFile, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile))); - final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, - "Definitions/descriptorInvalidImportStatement.yaml"); + final List<String> filesToHandleList = + Collections.singletonList("Definitions/descriptorInvalidImportStatement.yaml"); filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file))) ); final List<ErrorMessage> expectedErrorList = new ArrayList<>(); expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, - Messages.INVALID_IMPORT_STATEMENT.formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null"))); + Messages.INVALID_IMPORT_STATEMENT + .formatMessage("Definitions/descriptorInvalidImportStatement.yaml", "null")) + ); final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler( descriptorFileMap, mainDefinitionFile); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size())); + assertThat("The handled files should be the same", + actualHandledFiles.keySet(), hasSize(filesToHandleList.size())); assertThat("The handled files should be the same" - , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0])) + , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0])) ); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); @@ -179,20 +198,22 @@ public class ToscaDefinitionImportHandlerTest { final String mainDefinitionFilePath = "Definitions/Main1.yaml"; final String invalidMainDefinitionFilePath = "../Definitions/InvalidMainDefinitionFile.yaml"; - final List<String> filesToHandleList = Arrays.asList(mainDefinitionFilePath); + final List<String> filesToHandleList = Collections.singletonList(mainDefinitionFilePath); filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file))) ); final List<ErrorMessage> expectedErrorList = new ArrayList<>(); - expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(invalidMainDefinitionFilePath))); + expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, + Messages.MISSING_MAIN_DEFINITION_FILE.formatMessage(invalidMainDefinitionFilePath))); final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler( descriptorFileMap, invalidMainDefinitionFilePath); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("No files should be handled", actualHandledFiles, hasSize(0)); + assertThat("No files should be handled", actualHandledFiles.keySet(), hasSize(0)); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); @@ -208,36 +229,27 @@ public class ToscaDefinitionImportHandlerTest { @Test public void testGivenInvalidYamlDescriptorFile_whenDescriptorIsHandled_aInvalidYamlFormatErrorIsReported() { final String mainDefinitionFile = "Definitions/descriptorInvalid.yaml"; - - final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile); - filesToHandleList.forEach(file -> - descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file))) - ); - + descriptorFileMap.put(mainDefinitionFile, + getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile))); final List<ErrorMessage> expectedErrorList = new ArrayList<>(); - expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage() - , "while scanning a simple key\n" - + " in 'string', line 5, column 3:\n" - + " template_author= onap\n" - + " ^\n" - + "could not find expected ':'\n" - + " in 'string', line 6, column 1:\n" - + " description: vCPE_vgw\n" - + " ^\n"))); + expectedErrorList + .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_YAML_FORMAT.formatMessage(""))); - final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler( - descriptorFileMap, - mainDefinitionFile); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + final ToscaDefinitionImportHandler toscaDefinitionImportHandler = + new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("No files should be handled", actualHandledFiles, hasSize(0)); + assertThat("No files should be handled", actualHandledFiles.keySet(), hasSize(0)); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); assertThat("The errors should be the same", validationErrorList, hasSize(expectedErrorList.size())); - assertThat("The errors should be the same" - , validationErrorList, containsInAnyOrder(expectedErrorList.toArray(new ErrorMessage[0])) - ); + final List<String> actualErrorListAsStringList = validationErrorList.stream() + .map(ErrorMessage::getMessage) + .collect(Collectors.toList()); + expectedErrorList.forEach(expectedString -> + assertThat(actualErrorListAsStringList, hasItem(containsString(expectedString.getMessage())))); } /** @@ -246,19 +258,24 @@ public class ToscaDefinitionImportHandlerTest { @Test public void testGivenDescriptorFiles_whenMainDescriptorWithDifferentImportStatementsIsHandled_noErrorsAreReported() { final String mainDefinitionFile = "Definitions/descriptorFileWithValidImportStatements.yaml"; + descriptorFileMap.put(mainDefinitionFile, + getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile))); - final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, "Artifacts/descriptorCyclicReference.yaml"); - filesToHandleList.forEach(file -> + final List<String> importDescriptorToHandleList = + Collections.singletonList("Artifacts/descriptorCyclicReference.yaml"); + importDescriptorToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file))) ); final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(descriptorFileMap, mainDefinitionFile); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size())); + assertThat("The handled files should be the same", + actualHandledFiles.keySet(), hasSize(importDescriptorToHandleList.size())); assertThat("The handled files should be the same" - , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0])) + , actualHandledFiles.keySet(), containsInAnyOrder(importDescriptorToHandleList.toArray(new String[0])) ); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); @@ -271,8 +288,9 @@ public class ToscaDefinitionImportHandlerTest { @Test public void testGivenDescriptorFileWithNonexistentRelativeImport_whenIncorrectMainDescriptorIsHandled_aMissingFileErrorIsReported() { final String mainDefinitionFile = "Definitions/MainWithNonexistentReferences.yaml"; - - final List<String> filesToHandleList = Arrays.asList(mainDefinitionFile, + descriptorFileMap + .put(mainDefinitionFile, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(mainDefinitionFile))); + final List<String> filesToHandleList = Arrays.asList( "Definitions/descriptorNonexistentImport.yaml", "Artifacts/descriptorCyclicReference.yaml"); filesToHandleList.forEach(file -> descriptorFileMap.put(file, getResourceBytesOrFail(RESOURCES_FILE_PATH.resolve(file))) @@ -280,26 +298,24 @@ public class ToscaDefinitionImportHandlerTest { final List<ErrorMessage> expectedErrorList = new ArrayList<>(); expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, - String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), - "Definitions/descriptorCyclicReference.yaml")) - ); + Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/descriptorCyclicReference.yaml"))); expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, - String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), - "Definitions/descriptorCyclicReference.yaml")) + Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/descriptorCyclicReference.yaml")) ); expectedErrorList.add(new ErrorMessage(ErrorLevel.ERROR, - String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), - "Definitions/descriptorCyclicReference.yaml")) + Messages.MISSING_IMPORT_FILE.formatMessage("Definitions/descriptorCyclicReference.yaml")) ); final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler( descriptorFileMap, mainDefinitionFile); - final Set<String> actualHandledFiles = toscaDefinitionImportHandler.getHandledDefinitionFilesList(); + final Map<String, ServiceTemplateReaderService> actualHandledFiles = + toscaDefinitionImportHandler.getHandledImportDefinitionFileMap(); - assertThat("The handled files should be the same", actualHandledFiles, hasSize(filesToHandleList.size())); + assertThat("The handled files should be the same", + actualHandledFiles.keySet(), hasSize(filesToHandleList.size())); assertThat("The handled files should be the same" - , actualHandledFiles, containsInAnyOrder(filesToHandleList.toArray(new String[0])) + , actualHandledFiles.keySet(), containsInAnyOrder(filesToHandleList.toArray(new String[0])) ); final List<ErrorMessage> validationErrorList = toscaDefinitionImportHandler.getErrors(); diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java index af55aefc1a..70cae71f17 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/DataModelUtil.java @@ -39,6 +39,7 @@ import org.onap.sdc.tosca.datatypes.model.AttributeDefinition; import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment; import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition; import org.onap.sdc.tosca.datatypes.model.Constraint; +import org.onap.sdc.tosca.datatypes.model.DataType; import org.onap.sdc.tosca.datatypes.model.EntrySchema; import org.onap.sdc.tosca.datatypes.model.GroupDefinition; import org.onap.sdc.tosca.datatypes.model.Import; @@ -317,6 +318,20 @@ public class DataModelUtil { serviceTemplate.getNode_types().put(nodeTypeId, nodeType); } + public static void addDataType(final ServiceTemplate serviceTemplate, final String key, + final DataType nodeTypeValue) { + if (serviceTemplate == null) { + throw new CoreException(new InvalidAddActionNullEntityErrorBuilder("Data Type", SERVICE_TEMPLATE).build()); + } + + if (serviceTemplate.getData_types() == null) { + serviceTemplate.setData_types(new HashMap<>()); + } + + serviceTemplate.getData_types().put(key, nodeTypeValue); + } + + /** * Add relationship template. * |