From 0b3c65c18977212bc74b1f01cefe5ebecc2586f3 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 5 Feb 2020 12:13:57 +0000 Subject: Onboard TOSCA data_types defined in package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Vasyl Razinkov --- .../core/impl/ToscaDefinitionImportHandler.java | 99 +++++++++++++++------- 1 file changed, 68 insertions(+), 31 deletions(-) (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java') 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 fileMap; - private final Set handledDefinitionFilesList = new LinkedHashSet<>(); + + /** + * Stores all processed files during the import handling + */ + @Getter + private final Map handledImportDefinitionFileMap = new HashMap<>(); private final List 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 fileStructureMap, final String mainDefinitionFilePath) { + public ToscaDefinitionImportHandler(final Map 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 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 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 imports = readerService.getImports(); final List 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 importObjectMap = (Map) importObject; - for (final Map.Entry entry : importObjectMap.entrySet()) { + for (final Entry entry : importObjectMap.entrySet()) { if (NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))) { importedFileList.addAll(readImportStatement(entry.getValue())); } @@ -175,32 +212,32 @@ 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; } return true; } - /** - * Gets all processed files during the import handling. - * @return - * A list containing the processed files paths - */ - public Set getHandledDefinitionFilesList() { - return handledDefinitionFilesList; - } - /** * Adds an error to the validation error list. * -- cgit 1.2.3-korg