From 6750f9e73f44e7fc6854d570142e6d30f5d3cd24 Mon Sep 17 00:00:00 2001 From: SravanKumarGunda Date: Thu, 30 Jul 2020 21:30:09 +0530 Subject: Code Improvements-Vnfsdk-validation sonar issue fixes Signed-off-by: SravanKumarGunda Issue-ID: VNFSDK-608 Change-Id: Idba9f4876e41b699e2a2ac836eb2090f3694d0f1 --- .../java/org/onap/cvc/csar/VTPValidateCSAR.java | 54 ++++++++++-------- .../org/onap/cvc/csar/cc/VTPValidateCSARBase.java | 2 +- .../cvc/csar/cc/sol001/VTPValidateCSARR02454.java | 5 +- .../cvc/csar/cc/sol001/VTPValidateCSARR35851.java | 2 +- .../cvc/csar/cc/sol004/VTPValidateCSARR130206.java | 2 +- .../cvc/csar/cc/sol004/VTPValidateCSARR972082.java | 1 - .../org/onap/validation/csar/CsarValidator.java | 65 ++++++++++++---------- .../java/org/onap/validation/csar/FileUtil.java | 4 +- .../onap/validation/csar/ValidationException.java | 5 +- .../validation/csar/ValidatorSchemaLoader.java | 11 +--- .../validation/yaml/model/YamlDocumentFactory.java | 4 +- .../onap/validation/yaml/schema/YamlSchema.java | 1 - .../csar/security/ShaHashCodeGeneratorTest.java | 10 ++-- .../csarvalidationtest/CsarValidatorTest.java | 7 +++ 14 files changed, 91 insertions(+), 82 deletions(-) (limited to 'csarvalidation') diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java b/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java index 835bc11..95ad56f 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java @@ -221,30 +221,7 @@ public class VTPValidateCSAR extends OnapCommand { CSARValidation.Result result = new CSARValidation.Result(); result.setVnfreqName(vnfreq); - try { - String command = "csar-validate-" + vnfreq; - OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get(command, this.getInfo().getProduct()); - cmd.getParametersMap().get("csar").setValue(path); - setPnfValueIfAvailable(isPnf, cmd); - - result.setDescription(cmd.getDescription()); - cmd.execute(); - - for (CSARError error: (List) cmd.getResult().getOutput()) { - if (!ignoreCodes.contains(error.getCode()) && !ignoreCodes.contains(vnfreq + "-"+ error.getCode())) { - result.getErrors().add(error); - overallPass = false; - } - } - - result.setPassed(result.getErrors().isEmpty()); - validation.getResults().add(result); - } catch (Exception e) { - result.setPassed(false); - overallPass = false; - result.getErrors().add(new CSARArchive.CSARErrorUnknown(e.getMessage())); - validation.getResults().add(result); - } + overallPass = validateVnfOrPnf(path, validation, ignoreCodes, vnfreq, result, isPnf, overallPass); } validation.setDate(new Date().toString()); @@ -257,6 +234,35 @@ public class VTPValidateCSAR extends OnapCommand { } } + private boolean validateVnfOrPnf(String path, CSARValidation validation, + List < String > ignoreCodes, String vnfreq, CSARValidation.Result result, boolean isPnf, boolean overallPass) { + try { + String command = "csar-validate-" + vnfreq; + OnapCommand cmd = OnapCommandRegistrar.getRegistrar().get(command, this.getInfo().getProduct()); + cmd.getParametersMap().get("csar").setValue(path); + setPnfValueIfAvailable(isPnf, cmd); + + result.setDescription(cmd.getDescription()); + cmd.execute(); + + for (CSARError error: (List < CSARError > ) cmd.getResult().getOutput()) { + if (!ignoreCodes.contains(error.getCode()) && !ignoreCodes.contains(vnfreq + "-" + error.getCode())) { + result.getErrors().add(error); + overallPass = false; + } + } + + result.setPassed(result.getErrors().isEmpty()); + validation.getResults().add(result); + } catch (Exception e) { + result.setPassed(false); + overallPass = false; + result.getErrors().add(new CSARArchive.CSARErrorUnknown(e.getMessage())); + validation.getResults().add(result); + } + return overallPass; + } + static CSARValidation createCsarValidationFor(CSARArchive csar) { //Fill up the basic details CSARValidation validation = new CSARValidation(); diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/VTPValidateCSARBase.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/VTPValidateCSARBase.java index a5a13e3..e8c558b 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/VTPValidateCSARBase.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/VTPValidateCSARBase.java @@ -34,7 +34,7 @@ public abstract class VTPValidateCSARBase extends OnapCommand { protected List errors = new ArrayList<>(); - protected abstract void validateCSAR(CSARArchive csar) throws Exception; + protected abstract void validateCSAR(CSARArchive csar) throws Exception; //NOSONAR protected abstract String getVnfReqsNo(); diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR02454.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR02454.java index 7d0489e..4270d87 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR02454.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR02454.java @@ -60,10 +60,11 @@ public class VTPValidateCSARR02454 extends VTPValidateCSARBase { } } - if (!vlExist) - this.errors.add(new CSARErrorEntryMissingSwImage( + if (!vlExist) { + this.errors.add(new CSARErrorEntryMissingSwImage( csar.getDefinitionYamlFile().getName(), "Software Image")); + } } } } diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR35851.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR35851.java index 52582a6..78be3e6 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR35851.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol001/VTPValidateCSARR35851.java @@ -59,7 +59,7 @@ public class VTPValidateCSARR35851 extends VTPValidateCSARBase { yaml = (Map) yaml.get("topology_template"); Map nodeTmpls = (Map) yaml.get("node_templates"); - boolean vlExist[] = new boolean[3]; + boolean[] vlExist = new boolean[3]; for (Object nodeO: nodeTmpls.values()) { Map node = (Map) nodeO; diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR130206.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR130206.java index 41625a9..3a0f76b 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR130206.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR130206.java @@ -293,7 +293,7 @@ public class VTPValidateCSARR130206 extends VTPValidateCSARBase { class ManifestFileSignatureValidator { - private final Logger LOG = LoggerFactory.getLogger(ManifestFileSignatureValidator.class); + private final Logger log = LoggerFactory.getLogger(ManifestFileSignatureValidator.class); private final ManifestFileSplitter manifestFileSplitter = new ManifestFileSplitter(); private final CmsSignatureValidator cmsSignatureValidator = new CmsSignatureValidator(); diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR972082.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR972082.java index 4b35328..27e3ce9 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR972082.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR972082.java @@ -30,7 +30,6 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; import lombok.AccessLevel; import lombok.AllArgsConstructor; diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java index b853ca2..86952b1 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java @@ -54,9 +54,9 @@ public class CsarValidator { // Map of packageId and CSAR files private static Map> csar = new HashMap<>(); - private static String MAINSERV_TEMPLATE = CommonConstants.MAINSERV_TEMPLATE; + private static String mainServiceTemplate = CommonConstants.MAINSERV_TEMPLATE; - private static String MAINSERV_MANIFEST; + private static String mainServiceManifest; /** * @param packageId @@ -134,23 +134,32 @@ public class CsarValidator { try { RandomAccessFile raf = new RandomAccessFile(csarWithPath, "r"); - try { - long n = raf.readInt(); + return checkCsarIntegrity(raf); + } catch(IOException e1) { + LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); + return false; + } + } - // Check for the CSAR's integrity - if(n != 0x504B0304) { - LOG.error("CSAR %s contents are not a valid! "); - return false; - } - } catch(FileNotFoundException e1) { - LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); + /** + * @param raf + * @throws IOException + */ + private static boolean checkCsarIntegrity(RandomAccessFile raf) throws IOException { + + try { + long n = raf.readInt(); + + // Check for the CSAR's integrity + if (n != 0x504B0304) { + LOG.error("CSAR %s contents are not a valid! "); return false; - } finally { - raf.close(); } - } catch(IOException e1) { + } catch (FileNotFoundException e1) { LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); return false; + } finally { + raf.close(); } return true; } @@ -192,7 +201,6 @@ public class CsarValidator { } } } - reader.close(); return CommonConstants.SUCCESS_STR; } } catch(IOException e2) { @@ -220,14 +228,14 @@ public class CsarValidator { } try { - MAINSERV_MANIFEST = checkAndGetMRF(cfile, "Entry-Manifest"); - if(MAINSERV_MANIFEST == null) { - MAINSERV_MANIFEST = CommonConstants.MAINSERV_MANIFEST; + mainServiceManifest = checkAndGetMRF(cfile, "Entry-Manifest"); + if(mainServiceManifest == null) { + mainServiceManifest = CommonConstants.MAINSERV_MANIFEST; } - MAINSERV_TEMPLATE = checkAndGetMRF(cfile, "Entry-Definitions"); - if(MAINSERV_TEMPLATE == null) { - MAINSERV_TEMPLATE = CommonConstants.MAINSERV_TEMPLATE; + mainServiceTemplate = checkAndGetMRF(cfile, "Entry-Definitions"); + if(mainServiceTemplate == null) { + mainServiceTemplate = CommonConstants.MAINSERV_TEMPLATE; } return CommonConstants.SUCCESS_STR; @@ -276,9 +284,9 @@ public class CsarValidator { Arrays.asList("vnf_product_name", "vnf_provider_id", "vnf_package_version", "vnf_release_data_time"); @SuppressWarnings("unused") - boolean mfResult = checkEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key); + boolean mfResult = checkEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key); //NOSONAR - String mainServManifest = MAINSERV_MANIFEST; + String mainServManifest = mainServiceManifest; if(!Paths.get(mainServManifest).isAbsolute()) { mainServManifest = csarFiles.get(FilenameUtils.getName(mainServManifest)); } @@ -287,8 +295,8 @@ public class CsarValidator { // Do nothing for Rel-1 } - String mainservTemplate = MAINSERV_TEMPLATE; - if(!Paths.get(MAINSERV_TEMPLATE).isAbsolute()) { + String mainservTemplate = mainServiceTemplate; + if(!Paths.get(mainServiceTemplate).isAbsolute()) { mainservTemplate = csarFiles.get(FilenameUtils.getName(mainservTemplate)); } @@ -306,10 +314,10 @@ public class CsarValidator { * @return: boolean **/ public static String r02454() { - String mainservTemplate = MAINSERV_TEMPLATE; + String mainservTemplate = mainServiceTemplate; mainservTemplate = csarFiles.get(FilenameUtils.getName(mainservTemplate)); - //TODO: Fixme R3 only check the existence of swImage filed inside VNFD. + //TODO: Fixme R3 only check the existence of swImage filed inside VNFD. //NOSONAR File file = new File(mainservTemplate); try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String tempString = null; @@ -345,8 +353,6 @@ public class CsarValidator { @SuppressWarnings("unchecked") private static boolean checkEntryFor(String cFile, List attributes, String key) { - @SuppressWarnings("unused") - String tFileWithPath; if(!Paths.get(cFile).isAbsolute()) { cFile = csarFiles.get(FilenameUtils.getName(cFile)); @@ -403,7 +409,6 @@ public class CsarValidator { if(StringUtils.isEmpty(cfile)) { return false; } else { - File file = new File(cfile); Yaml yaml = new Yaml(); diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java b/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java index a488ed3..eedd628 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java @@ -48,9 +48,7 @@ public final class FileUtil { int tryCount = 0; while (tryCount < TRY_COUNT) { tryCount++; - if (!folder.exists() && !folder.mkdirs()) { - continue; - } else { + if (folder.exists() || folder.mkdirs()) { return true; } } diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java b/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java index 7c124d7..ac4c1eb 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java @@ -36,7 +36,7 @@ public class ValidationException extends RuntimeException { return wrappedInfo(exception, null); } - public ValidationException(ErrorCodes errCode, String message) { + public ValidationException(ErrorCodes errCode, String message) { //NOSONAR super(message); } @@ -58,7 +58,8 @@ public class ValidationException extends RuntimeException { super(message, cause); this.errorCode = errorCode; } - + + @Override public String toString(){ return ("Exception Number = "+errorMessage) ; } diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java b/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java index b56e5f2..c488c11 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/ValidatorSchemaLoader.java @@ -22,7 +22,6 @@ import java.io.*; import java.net.URISyntaxException; import java.util.*; import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.scanner.ScannerException; public class ValidatorSchemaLoader { @@ -51,15 +50,7 @@ public class ValidatorSchemaLoader { public ValidatorSchemaLoader() { - try { loadResources(); - } catch(FileNotFoundException e1) { - LOG.error("Schema file not found or schema repository corrupted", e1); - - } catch(URISyntaxException e) { - // TODO Auto-generated catch block - LOG.error("Illegal character in query at index", e); - } } private Map readYaml(String fileName) { @@ -68,7 +59,7 @@ public class ValidatorSchemaLoader { } @SuppressWarnings("unchecked") - private boolean loadResources() throws FileNotFoundException, URISyntaxException { + private boolean loadResources() { for (String metaFile: new String []{"TOSCA.meta", "CSAR.meta", "MRF.mf" }) { switch(metaFile) { case "TOSCA.meta": diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java index 16d94a2..b56422c 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java @@ -35,9 +35,9 @@ public class YamlDocumentFactory { } } - private Map transformMap(Map yaml) { + private Map transformMap(Map yaml) { Map parsedYaml = new HashMap<>(); - for (Map.Entry entry: yaml.entrySet()) { + for (Map.Entry entry: yaml.entrySet()) { parsedYaml.put(entry.getKey().toString(), entry.getValue()); } return parsedYaml; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java index 19e6af8..69bb6cd 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java @@ -22,7 +22,6 @@ import org.onap.validation.yaml.schema.node.YamlSchemaNode; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; public class YamlSchema { diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/security/ShaHashCodeGeneratorTest.java b/csarvalidation/src/test/java/org/onap/cvc/csar/security/ShaHashCodeGeneratorTest.java index 1ea5dd5..b5c4efa 100644 --- a/csarvalidation/src/test/java/org/onap/cvc/csar/security/ShaHashCodeGeneratorTest.java +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/security/ShaHashCodeGeneratorTest.java @@ -39,8 +39,9 @@ public class ShaHashCodeGeneratorTest { final String hashCode = shaHashCodeGenerator.generateSha256("test".getBytes()); - assertThat(hashCode).isEqualTo(shaHashCodeGenerator.generateSha256("test".getBytes())); - assertThat(hashCode).isNotEqualTo(shaHashCodeGenerator.generateSha256("Test".getBytes())); + assertThat(hashCode) + .isEqualTo(shaHashCodeGenerator.generateSha256("test".getBytes())) + .isNotEqualTo(shaHashCodeGenerator.generateSha256("Test".getBytes())); } @Test @@ -48,7 +49,8 @@ public class ShaHashCodeGeneratorTest { final String hashCode = shaHashCodeGenerator.generateSha512("test".getBytes()); - assertThat(hashCode).isEqualTo(shaHashCodeGenerator.generateSha512("test".getBytes())); - assertThat(hashCode).isNotEqualTo(shaHashCodeGenerator.generateSha512("Test".getBytes())); + assertThat(hashCode) + .isEqualTo(shaHashCodeGenerator.generateSha512("test".getBytes())) + .isNotEqualTo(shaHashCodeGenerator.generateSha512("Test".getBytes())); } } \ No newline at end of file diff --git a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java index c2856d9..395b866 100644 --- a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java @@ -312,4 +312,11 @@ public class CsarValidatorTest { dst.mkdir(); assertTrue(FileUtil.deleteFile(dstPath)); } + + @Test + public void testCreateDirectory() { + String dirPath = "src//test//resources//TestDirectory"; + boolean res = FileUtil.createDirectory(dirPath); + assertTrue(res); + } } -- cgit 1.2.3-korg