diff options
author | andre.schmid <andre.schmid@est.tech> | 2020-05-19 16:31:28 +0100 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2020-05-24 09:02:20 +0000 |
commit | c8f88d9b496e8f10179bfc26a09a27386d156d47 (patch) | |
tree | a5e7c5bac5921c95da9fe15cdb4f878ef9bb49b8 /asdctool/src/main/java/org | |
parent | 988c286efe09c01db6f2e41f0ce4b2f49f92d1a6 (diff) |
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 <andre.schmid@est.tech>
Diffstat (limited to 'asdctool/src/main/java/org')
-rw-r--r-- | asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java | 27 |
1 files 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<Path> 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<String, Object> load = yaml.loadAs(inputStream, Map.class); - Map<String, Object> nodeType = (Map<String, Object>) load.get(collectionTitle); + final FileInputStream inputStream = new FileInputStream(path); + final Map<String, Object> load = new Yaml().loadAs(inputStream, Map.class); + final Map<String, Object> nodeType = (Map<String, Object>) 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() { |