summaryrefslogtreecommitdiffstats
path: root/csarvalidation/src/main/java/org
diff options
context:
space:
mode:
authorAvinashS <avinash.s@huawei.com>2017-09-21 19:21:01 +0530
committerAvinashS <avinash.s@huawei.com>2017-09-21 19:21:01 +0530
commit2d896d9051f4b05518a5da0c4b86b18ab1e0677d (patch)
tree92a676c0384f89da885442b773b9a85e564c08c8 /csarvalidation/src/main/java/org
parent554973c890c5b794ba41422c5b73552ab8faf5f8 (diff)
Added validation for MainServiceTemplate
Added code for validation of MainServiceTemplate requires further cleanup and smart schema validation. Change-Id: I65ecd9ec0d1b5ee32d50583b2670a1ce532d74e7 IssueId: VNFSDK-96 Signed-off-by: AvinashS <avinash.s@huawei.com>
Diffstat (limited to 'csarvalidation/src/main/java/org')
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java122
1 files changed, 68 insertions, 54 deletions
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 891f83e..8263e6b 100644
--- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java
+++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java
@@ -15,22 +15,18 @@
*/
package org.onap.validation.csar;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import java.io.*;
-
import java.nio.file.Files;
import java.nio.file.Paths;
import static java.nio.charset.StandardCharsets.*;
-
import java.util.*;
import java.util.stream.Collectors;
-
-
+import org.apache.commons.lang3.StringUtils;
import org.yaml.snakeyaml.Yaml;
-
public class CsarValidator {
private static final Logger LOG = LoggerFactory.getLogger(CsarValidator.class);
@@ -62,19 +58,23 @@ public class CsarValidator {
}
- public static boolean validateCsar() {
+ public static boolean validateCsar() {
+
+ boolean vsm = validateCsarMeta();
- validateCsarMeta();
+ boolean vtm = validateToscaMeta();
- validateToscaMeta();
+ boolean vms = validateMainService();
- validateMainService();
+ if ((vsm || vms ) && vtm) {
+ return true;
+ }
- //In future return the status handler object instead.
- return true;
+ //In future return the status handler object instead.
+ return false;
}
-
- public static boolean validateCsarIntegrity(String csarWithPath) {
+
+ public static boolean validateCsarIntegrity(String csarWithPath) {
try {
RandomAccessFile raf = new RandomAccessFile(csarWithPath, "r");
@@ -98,6 +98,9 @@ public class CsarValidator {
public static boolean validateCsarMeta() {
String cfile = csarFiles.get(CommonConstants.CSAR_META);
+ if (StringUtils.isEmpty(cfile)) {
+ return false;
+ }
if (!cfile.isEmpty()) {
File file = new File(cfile);
@@ -134,32 +137,42 @@ public class CsarValidator {
}
}
- return false;
- }
+ return false;
+ }
- public static boolean validateToscaMeta() {
+
+ public static boolean validateToscaMeta() {
String cfile = csarFiles.get(CommonConstants.TOSCA_META);
+ if(StringUtils.isEmpty(cfile)) {
+ return false;
+ }
try {
- if (!cfile.isEmpty() && cfile.contains( System.getProperty("file.separator")+ CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") + CommonConstants.TOSCA_META)) {
-
+ if (!cfile.isEmpty() && cfile.contains(System.getProperty("file.separator") +
+ CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") +
+ CommonConstants.TOSCA_META)) {
String value = checkEntryFor(cfile, "Entry-Definitions:");
- if (value == null) {
+ String[] splitPath = value.split("/");
+ String subValue = splitPath[splitPath.length - 1];
+
+ if (value.isEmpty() || subValue.isEmpty()) {
return false;
//Check if Entry-Defintions pointed file exists in CSAR
- } else if (csarFiles.get(value) != null) {
+
+ } else if (!(null == csarFiles.get(value)) ||
+ !(null == csarFiles.get(subValue))) {
return true;
}
}
} catch (IOException e) {
LOG.error("Could not read file %s ! " + e.getMessage(), cfile);
}
-
return false;
}
- private static boolean validateMainService() {
+ public static boolean validateMainService() {
+
String key = "metadata";
// Infuture load from the respective file template/schema
@@ -168,23 +181,22 @@ public class CsarValidator {
boolean mfResult = checkEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key);
List<String> tListMetadata = Arrays.asList("vendor", "csarVersion",
- "csarProvider","id", "version", "csarType", "name", "vnfdVersion",
+ "csarProvider", "id", "version", "csarType", "name", "vnfdVersion",
"vnfmType");
boolean tResult = checkEntryFor(CommonConstants.MAINSERV_TEMPLATE, tListMetadata, key);
if (tResult && mfResult) {
return true;
- }
- else {
+ } else {
return false;
}
}
- private static String checkEntryFor(String fileWithPath, String attribute) throws IOException {
+ private static String checkEntryFor(String fileWithPath, String attribute) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(fileWithPath), UTF_8);
- for(String strLine : lines) {
+ for (String strLine : lines) {
if (!attribute.isEmpty() && strLine.contains(attribute)) {
return strLine.substring(attribute.length(), strLine.length()).trim();
}
@@ -195,37 +207,39 @@ public class CsarValidator {
private static boolean checkEntryFor(String cFile, List<String> attributes, String key) {
String tFileWithPath = csarFiles.get(cFile);
- Yaml yaml = new Yaml();
- Map<String, ?> values = null;
- try {
- values = (Map<String, ?>) yaml.load(new FileInputStream(new File(tFileWithPath)));
- } catch (FileNotFoundException e) {
- return false;
- }
+ if(StringUtils.isEmpty(tFileWithPath)) {
+ return false;
+ }
- Map<String, String> subValues = (Map<String, String>) values.get(key);
+ Yaml yaml = new Yaml();
+ Map<String, ?> values = null;
+ try {
+ values = (Map<String, ?>) yaml.load(new FileInputStream(new File(tFileWithPath)));
+ } catch (FileNotFoundException e) {
+ return false;
+ }
- //1. Check for empty values in map and if number of mandatory attributes presence
- Map<String, String> mResult = subValues.entrySet()
- .stream()
- .filter(e -> e.getValue() != null)
- .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
- if (mResult.size() != attributes.size())
- {
- return false;
- }
+ Map<String,String> subValues = (Map<String,String>) values.get(key);
- //2. Validate the exact mandatory attributes with expected attributes list
- List<String> lResult = subValues.values().stream()
- .filter(attributes::contains)
- .collect(Collectors.toList());
+ //1. Check for empty values in map and if number of mandatory attributes presence
+ List<String> lResultNonNull = subValues.values().stream()
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
+ if (subValues.size() != attributes.size() &&
+ lResultNonNull.size() != attributes.size()) {
+ return false;
+ }
- // System.out.println(result);
- if (lResult.size() != attributes.size()) {
- return false;
- }
- return true;
+ //2. Validate the exact mandatory attributes with expected attributes list
+ List<? super String> lResult = subValues.keySet().stream()
+ .filter(attributes::contains)
+ .collect(Collectors.toList());
+ // System.out.println(result);
+ if (lResult.size() != attributes.size()) {
+ return false;
+ }
+ return true;
}
public static HashMap<String, HashMap<String, String>> getCsar() {