From c8f88d9b496e8f10179bfc26a09a27386d156d47 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Tue, 19 May 2020 16:31:28 +0100 Subject: Fix potential NullPointer in SchemaFileImport While reading the YAML to import, if the content was missing the entry node_types it was causing a NullPointerException. Also the exception was being suppressed, making very difficult to understand what happened. Change-Id: I44badbee1c739ddf6db08b4e518adb33c430fa84 Issue-ID: SDC-3070 Signed-off-by: andre.schmid --- .../sdc/asdctool/main/SdcSchemaFileImport.java | 27 ++++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java index 989e362706..e2a0834356 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java @@ -32,6 +32,8 @@ import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.common.api.ConfigurationSource; import org.openecomp.sdc.common.impl.ExternalConfiguration; import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -56,6 +58,8 @@ import java.util.zip.ZipOutputStream; public class SdcSchemaFileImport { + private static final Logger LOGGER = LoggerFactory.getLogger(SdcSchemaFileImport.class); + private static final String SEPARATOR = FileSystems.getDefault().getSeparator(); private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1"; @@ -243,7 +247,6 @@ public class SdcSchemaFileImport { String[] importFileList = new String[]{"data.yml", "artifacts.yml", "capabilities.yml", "interfaces.yml", "relationships.yml"}; - String collectionTitle = "node_types"; //Create node.yaml - collect all types from normative-types and heat-types directories String[] nodeTypesMainFolders = new String[]{"normative-types", "heat-types"}; @@ -253,27 +256,31 @@ public class SdcSchemaFileImport { nodeTypesMainFolders = ArrayUtils.addAll(nodeTypesMainFolders, onapNodeTypesMainFolders); } + final String nodeTypesToscaEntry = "node_types"; for (String nodeTypesMainFolder : nodeTypesMainFolders) { try (Stream paths = Files.walk(Paths.get(importToscaPath + SEPARATOR + nodeTypesMainFolder))) { paths.filter(path -> path.getFileName().toString().toLowerCase().endsWith(YAML_EXTENSION)) .forEach(yamlFile -> { try { - String path = yamlFile.toAbsolutePath().toString(); + final String path = yamlFile.toAbsolutePath().toString(); System.out.println("Processing node type file " + path + "..."); - FileInputStream inputStream = new FileInputStream(path); - Yaml yaml = new Yaml(); - Map load = yaml.loadAs(inputStream, Map.class); - Map nodeType = (Map) load.get(collectionTitle); + final FileInputStream inputStream = new FileInputStream(path); + final Map load = new Yaml().loadAs(inputStream, Map.class); + final Map nodeType = (Map) load.get(nodeTypesToscaEntry); + if (nodeType == null) { + LOGGER.error("Expecting '{}' entry in TOSCA yaml file '{}'", nodeTypesToscaEntry, path); + System.exit(1); + } nodeTypeList.putAll(nodeType); - - } catch (Exception e) { - System.err.println("Error in opening file " + yamlFile.toAbsolutePath().toString()); + } catch (final Exception e) { + LOGGER.error("An error has occurred while processing YAML '{}'", + yamlFile.toAbsolutePath(), e); System.exit(1); } }); } } - createAndSaveSchemaFileYaml("nodes", importFileList, collectionTitle, nodeTypeList); + createAndSaveSchemaFileYaml("nodes", importFileList, nodeTypesToscaEntry, nodeTypeList); } private static void usageAndExit() { -- cgit 1.2.3-korg