summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java
diff options
context:
space:
mode:
authorandre.schmid <andre.schmid@est.tech>2020-02-05 12:13:57 +0000
committerOfir Sonsino <ofir.sonsino@intl.att.com>2020-06-15 08:06:51 +0000
commit0b3c65c18977212bc74b1f01cefe5ebecc2586f3 (patch)
treedd81f6c5fafb4932868f505db0eddaa9af688b1a /openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java
parente3e2db1d777b89e8292b88f677bcc7d81e6bf2c0 (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/ToscaDefinitionImportHandler.java')
-rw-r--r--openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaDefinitionImportHandler.java99
1 files changed, 68 insertions, 31 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 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