diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java')
-rw-r--r-- | openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java | 94 |
1 files changed, 35 insertions, 59 deletions
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 661cadc738..a48fb36374 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 @@ -16,7 +16,6 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ - package org.openecomp.core.impl; import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBUTES; @@ -46,9 +45,7 @@ import org.slf4j.LoggerFactory; public class ToscaDefinitionImportHandler { private static final Logger LOGGER = LoggerFactory.getLogger(ToscaDefinitionImportHandler.class); - private final Map<String, byte[]> fileMap; - /** * Stores all processed files during the import handling */ @@ -59,17 +56,17 @@ public class ToscaDefinitionImportHandler { /** * Reads the provided package structure starting from a main definition yaml file. - * @param fileStructureMap The package structure with file path and respective file byte - * @param mainDefinitionFilePath The main descriptor yaml file to start the reading + * + * @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; readImportsFromMainDefinition(mainDefinitionFilePath); } private void readImportsFromMainDefinition(final String mainDefinitionFilePath) { - if(!checkMainDefinitionExists(mainDefinitionFilePath)) { + if (!checkMainDefinitionExists(mainDefinitionFilePath)) { return; } final ServiceTemplateReaderService readerService = parseToServiceTemplate(mainDefinitionFilePath).orElse(null); @@ -90,19 +87,16 @@ public class ToscaDefinitionImportHandler { 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())); + reportError(ErrorLevel.ERROR, String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage(), ex.getMessage())); } - return Optional.empty(); } /** - * Reads and validates the descriptor imports recursively. - * Starts from the provided descriptor and goes until the end of the import tree. + * Reads and validates the descriptor imports recursively. Starts from the provided descriptor and goes until the end of the import tree. * Processes each file just once. * - * @param fileName the descriptor file path + * @param fileName the descriptor file path */ private void handleImports(final String fileName) { currentFile = fileName; @@ -113,14 +107,11 @@ public class ToscaDefinitionImportHandler { if (readerService == null) { return; } - handledImportDefinitionFileMap.put(fileName, readerService); final List<Object> imports = readerService.getImports(); final List<String> extractImportFiles = extractFileImports(imports); - extractImportFiles.stream() - .map(importedFile -> resolveImportPath(FilenameUtils.getPath(fileName), importedFile)) - .filter(resolvedPath -> !handledImportDefinitionFileMap.containsKey(resolvedPath)) - .forEach(this::handleImports); + extractImportFiles.stream().map(importedFile -> resolveImportPath(FilenameUtils.getPath(fileName), importedFile)) + .filter(resolvedPath -> !handledImportDefinitionFileMap.containsKey(resolvedPath)).forEach(this::handleImports); } /** @@ -133,22 +124,19 @@ public class ToscaDefinitionImportHandler { * - yetAnotherDescriptor: * yetAnotherDescriptor: ../Definitions/yetAnotherDescriptor.yaml * </pre> - * @param imports the import statements - * @return - * The list of import file paths found + * + * @param imports the import statements + * @return The list of import file paths found */ private List<String> extractFileImports(final List<Object> imports) { final List<String> importedFileList = new ArrayList<>(); imports.forEach(importObject -> importedFileList.addAll(readImportStatement(importObject))); - return importedFileList; } /** - * Reads an import statement which can be a value, a [key:value] or a [key:[key:value]]. - * Ignores entries which contains the same keys as - * {@link org.openecomp.sdc.tosca.csar.CSARConstants#NON_FILE_IMPORT_ATTRIBUTES}. - * Reports invalid import statements. + * Reads an import statement which can be a value, a [key:value] or a [key:[key:value]]. Ignores entries which contains the same keys as {@link + * org.openecomp.sdc.tosca.csar.CSARConstants#NON_FILE_IMPORT_ATTRIBUTES}. Reports invalid import statements. * <pre> * example of yaml imports statements: * - /Artifacts/anImportedDescriptor.yaml @@ -156,9 +144,9 @@ public class ToscaDefinitionImportHandler { * - yetAnotherDescriptor: * yetAnotherDescriptor: ../Definitions/yetAnotherDescriptor.yaml * </pre> - * @param importObject the object representing the yaml import statement - * @return - * The list of import file paths found + * + * @param importObject the object representing the yaml import statement + * @return The list of import file paths found */ private List<String> readImportStatement(final Object importObject) { final List<String> importedFileList = new ArrayList<>(); @@ -172,43 +160,34 @@ public class ToscaDefinitionImportHandler { } } } else { - reportError(ErrorLevel.ERROR, - String.format(Messages.INVALID_IMPORT_STATEMENT.getErrorMessage(), currentFile, importObject)); + reportError(ErrorLevel.ERROR, String.format(Messages.INVALID_IMPORT_STATEMENT.getErrorMessage(), currentFile, importObject)); } - return importedFileList; } /** * Given a directory path, resolves the import path. - * @param directoryPath A directory path to resolve the import path - * @param importPath An import statement path - * @return - * The resolved path of the import, using as base the directory path + * + * @param directoryPath A directory path to resolve the import path + * @param importPath An import statement path + * @return The resolved path of the import, using as base the directory path */ private String resolveImportPath(final String directoryPath, final String importPath) { final String fixedParentDir; if (StringUtils.isEmpty(directoryPath)) { fixedParentDir = "/"; } else { - fixedParentDir = String.format("%s%s%s", - directoryPath.startsWith("/") ? "" : "/" - , directoryPath - , directoryPath.endsWith("/") ? "" : "/"); + fixedParentDir = String.format("%s%s%s", directoryPath.startsWith("/") ? "" : "/", directoryPath, directoryPath.endsWith("/") ? "" : "/"); } - final URI parentDirUri = URI.create(fixedParentDir); - String resolvedImportPath = parentDirUri.resolve(importPath).toString(); if (resolvedImportPath.contains("../")) { - reportError(ErrorLevel.ERROR, - Messages.INVALID_IMPORT_STATEMENT.formatMessage(currentFile, importPath)); + reportError(ErrorLevel.ERROR, Messages.INVALID_IMPORT_STATEMENT.formatMessage(currentFile, importPath)); return null; } if (resolvedImportPath.startsWith("/")) { resolvedImportPath = resolvedImportPath.substring(1); } - return resolvedImportPath; } @@ -221,28 +200,25 @@ public class ToscaDefinitionImportHandler { } /** - * Checks if the given file path exists inside the file structure. - * Reports an error if the file was not found. + * 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 + * @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 checkFileExists(final String filePath, final String errorMsg) { if (!fileMap.containsKey(filePath)) { reportError(ErrorLevel.ERROR, errorMsg); return false; } - return true; } /** * Adds an error to the validation error list. * - * @param errorLevel the error level - * @param errorMessage the error message + * @param errorLevel the error level + * @param errorMessage the error message */ private void reportError(final ErrorLevel errorLevel, final String errorMessage) { validationErrorList.add(new ErrorMessage(errorLevel, errorMessage)); @@ -250,8 +226,8 @@ public class ToscaDefinitionImportHandler { /** * Gets the list of errors. - * @return - * The import validation errors detected + * + * @return The import validation errors detected */ public List<ErrorMessage> getErrors() { return validationErrorList; @@ -259,8 +235,8 @@ public class ToscaDefinitionImportHandler { /** * Checks if the handler detected a import error. - * @return - * {@code true} if the handler detected any error, {@code false} otherwise. + * + * @return {@code true} if the handler detected any error, {@code false} otherwise. */ public boolean hasError() { return !validationErrorList.isEmpty(); |