From fc5c07705edc4dcb7083b39116a43844bb6a1490 Mon Sep 17 00:00:00 2001 From: Ravindra Bakkamanthala Date: Mon, 5 Jun 2017 22:42:02 -0400 Subject: Fixed the Policy API issues and Bugfixes Change-Id: I6e8b54442421b6ebd0ff88426fc6e5f3a36b0690 Signed-off-by: Ravindra Bakkamanthala --- .../org/openecomp/policy/utils/PolicyUtils.java | 84 ++++++++++++++-------- 1 file changed, 55 insertions(+), 29 deletions(-) (limited to 'PolicyEngineUtils/src/main/java/org') diff --git a/PolicyEngineUtils/src/main/java/org/openecomp/policy/utils/PolicyUtils.java b/PolicyEngineUtils/src/main/java/org/openecomp/policy/utils/PolicyUtils.java index 75197932a..dfbefcd73 100644 --- a/PolicyEngineUtils/src/main/java/org/openecomp/policy/utils/PolicyUtils.java +++ b/PolicyEngineUtils/src/main/java/org/openecomp/policy/utils/PolicyUtils.java @@ -37,9 +37,7 @@ import org.drools.verifier.builder.VerifierBuilder; import org.drools.verifier.builder.VerifierBuilderFactory; import org.kie.api.io.ResourceType; -import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.CharMatcher; @@ -48,16 +46,21 @@ public class PolicyUtils { public static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; + private static final String PACKAGE_ERROR = "mismatched input '{' expecting one of the following tokens: '[package"; + private static final String SUCCESS = "success"; + + private PolicyUtils(){ + // Private Constructor + } public static String objectToJsonString(Object o) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(o); } - public static T jsonStringToObject(String jsonString, Class className) throws JsonParseException, JsonMappingException, IOException{ + public static T jsonStringToObject(String jsonString, Class className) throws IOException{ ObjectMapper mapper = new ObjectMapper(); - T t = mapper.readValue(jsonString, className); - return t; + return mapper.readValue(jsonString, className); } public static String decode(String encodedString) throws UnsupportedEncodingException { @@ -79,18 +82,34 @@ public class PolicyUtils { String password = tokenizer.nextToken(); return new String[]{username, password}; }else{ - return null; + return new String[]{}; } } public static String emptyPolicyValidator(String field){ - String error = "success"; - if (field.equals("") || field.contains(" ") || !field.matches("^[a-zA-Z0-9_]*$")) { + String error; + if ("".equals(field) || field.contains(" ") || !field.matches("^[a-zA-Z0-9_]*$")) { + error = "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _' following set of Combinations"; + return error; + } else { + if(CharMatcher.ASCII.matchesAllOf((CharSequence) field)){ + error = SUCCESS; + }else{ + error = "The Value Contains Non ASCII Characters"; + return error; + } + } + return error; + } + + public static String emptyPolicyValidatorWithSpaceAllowed(String field){ + String error; + if ("".equals(field) || !field.matches("^[a-zA-Z0-9_ ]*$")) { error = "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _' following set of Combinations"; return error; } else { if(CharMatcher.ASCII.matchesAllOf((CharSequence) field)){ - error = "success"; + error = SUCCESS; }else{ error = "The Value Contains Non ASCII Characters"; return error; @@ -100,19 +119,19 @@ public class PolicyUtils { } public static String descriptionValidator(String field) { - String error = "success"; + String error; if (field.contains("@CreatedBy:") || field.contains("@ModifiedBy:")) { error = "The value in the description shouldn't contain @CreatedBy: or @ModifiedBy:"; return error; } else { - error = "success"; + error = SUCCESS; } return error; } public static String validateEmailAddress(String emailAddressValue) { - String error = "success"; - List emailList = Arrays.asList(emailAddressValue.toString().split(",")); + String error = SUCCESS; + List emailList = Arrays.asList(emailAddressValue.split(",")); for(int i =0 ; i < emailList.size() ; i++){ Pattern pattern = Pattern.compile(EMAIL_PATTERN); Matcher matcher = pattern.matcher(emailList.get(i).trim()); @@ -120,7 +139,7 @@ public class PolicyUtils { error = "Please check the Following Email Address is not Valid .... " +emailList.get(i).toString(); return error; }else{ - error = "success"; + error = SUCCESS; } } return error; @@ -130,21 +149,28 @@ public class PolicyUtils { * Check for "[ERR" to see if there are any errors. */ public static String brmsRawValidate(String rule){ - VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder(); - Verifier verifier = vBuilder.newVerifier(); - verifier.addResourcesToVerify(new ReaderResource(new StringReader(rule)), ResourceType.DRL); - // Check if there are any Errors in Verification. - if(verifier.getErrors().size()!=0){ - String message = "Not a Valid DRL rule"; - for(VerifierError error: verifier.getErrors()){ - // Ignore annotations Error Messages - if(!error.getMessage().contains("'@'")){ - message = message + "\n" + error.getMessage(); - } - } - return message; - } - return ""; + VerifierBuilder vBuilder = VerifierBuilderFactory.newVerifierBuilder(); + Verifier verifier = vBuilder.newVerifier(); + verifier.addResourcesToVerify(new ReaderResource(new StringReader(rule)), ResourceType.DRL); + // Check if there are any Errors in Verification. + if(!verifier.getErrors().isEmpty()){ + boolean ignore = false; + StringBuilder message = new StringBuilder("Not a Valid DRL rule"); + for(VerifierError error: verifier.getErrors()){ + // Ignore annotations Error Messages + if(!error.getMessage().contains("'@'") && !error.getMessage().contains(PACKAGE_ERROR)){ + ignore= true; + message.append("\n" + error.getMessage()); + } + } + // Ignore new package names with { + // More checks for message to check if its a package error. + if(ignore && !message.toString().contains("Parser returned a null Package")){ + message.append("[ERR 107]"); + } + return message.toString(); + } + return ""; } /** -- cgit 1.2.3-korg