X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=POLICY-SDK-APP%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fpolicy%2Fcontroller%2FPolicyValidationController.java;h=0f8b3795f6f1c0f573dabba0fe3d04ec2c6265d1;hb=4ca818fdfb9b807562166800a086b413593d6894;hp=ff91e93814fea4a0b264a9b029363667e92c55b4;hpb=87c95be02a8a4d77e165dede90777e811b59dcae;p=policy%2Fengine.git diff --git a/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/PolicyValidationController.java b/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/PolicyValidationController.java index ff91e9381..0f8b3795f 100644 --- a/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/PolicyValidationController.java +++ b/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/PolicyValidationController.java @@ -22,7 +22,6 @@ package org.openecomp.policy.controller; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringReader; @@ -30,6 +29,7 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -41,7 +41,6 @@ import javax.json.Json; import javax.json.JsonReader; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -64,7 +63,6 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.xml.sax.InputSource; -import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -89,8 +87,6 @@ public class PolicyValidationController extends RestrictedBaseController { public static final String MICROSERVICES="Micro Service"; private Pattern pattern; private Matcher matcher; - - private static Map rangeMap = new HashMap<>(); private static Map mapAttribute = new HashMap<>(); private static final String EMAIL_PATTERN = @@ -104,7 +100,7 @@ public class PolicyValidationController extends RestrictedBaseController { public ModelAndView validatePolicy(HttpServletRequest request, HttpServletResponse response) throws Exception{ try{ boolean valid = true; - String responseString = ""; + StringBuilder responseString = new StringBuilder(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); @@ -112,17 +108,17 @@ public class PolicyValidationController extends RestrictedBaseController { if(policyData.getPolicyName() != null){ String policyNameValidate = emptyValidator(policyData.getPolicyName()); if(!policyNameValidate.contains("success")){ - responseString = responseString + "PolicyName:" + policyNameValidate + "
"; + responseString.append("PolicyName:" + policyNameValidate + "
"); valid = false; }; }else{ - responseString = responseString + "PolicyName: PolicyName Should not be empty" + "
"; + responseString.append( "PolicyName: PolicyName Should not be empty" + "
"); valid = false; } if(policyData.getPolicyDescription() != null){ String descriptionValidate = descriptionValidator(policyData.getPolicyDescription()); if(!descriptionValidate.contains("success")){ - responseString = responseString + "Description:" + descriptionValidate + "
"; + responseString.append("Description:" + descriptionValidate + "
"); valid = false; } } @@ -133,11 +129,11 @@ public class PolicyValidationController extends RestrictedBaseController { if(policyData.getEcompName() != null){ String ecompNameValidate = emptyValidator(policyData.getEcompName()); if(!ecompNameValidate.contains("success")){ - responseString = responseString + "EcompName:" + ecompNameValidate + "
"; + responseString.append("EcompName:" + ecompNameValidate + "
"); valid = false; } }else{ - responseString = responseString + "Ecomp Name: Ecomp Name Should not be empty" + "
"; + responseString.append("Ecomp Name: Ecomp Name Should not be empty" + "
"); valid = false; } } @@ -145,33 +141,33 @@ public class PolicyValidationController extends RestrictedBaseController { if(policyData.getRiskType() != null){ String riskTypeValidate = emptyValidator(policyData.getRiskType()); if(!riskTypeValidate.contains("success")){ - responseString = responseString + "RiskType:" + riskTypeValidate + "
"; + responseString.append("RiskType:" + riskTypeValidate + "
"); valid = false; } }else { - responseString = responseString + "Risk Type: Risk Type Should not be Empty" + "
"; + responseString.append("Risk Type: Risk Type Should not be Empty" + "
"); valid = false; } if(policyData.getRiskLevel() != null){ String validateRiskLevel = emptyValidator(policyData.getRiskLevel()); if(!validateRiskLevel.contains("success")){ - responseString = responseString + "RiskLevel:" + validateRiskLevel + "
"; + responseString.append("RiskLevel:" + validateRiskLevel + "
"); valid = false; } }else { - responseString = responseString + "Risk Level: Risk Level Should not be Empty" + "
"; + responseString.append("Risk Level: Risk Level Should not be Empty" + "
"); valid = false; } if(policyData.getGuard() != null){ String validateGuard = emptyValidator(policyData.getGuard()); if(!validateGuard.contains("success")){ - responseString = responseString + "Guard:" + validateGuard + "
"; + responseString.append("Guard:" + validateGuard + "
"); valid = false; } }else { - responseString = responseString + "Guard: Guard Value Should not be Empty" + "
"; + responseString.append("Guard: Guard Value Should not be Empty" + "
"); valid = false; } @@ -179,21 +175,21 @@ public class PolicyValidationController extends RestrictedBaseController { if(policyData.getConfigName() != null){ String configNameValidate = emptyValidator(policyData.getConfigName()); if(!configNameValidate.contains("success")){ - responseString = responseString + "ConfigName:" + configNameValidate + "
"; + responseString.append("ConfigName:" + configNameValidate + "
"); valid = false; } }else{ - responseString = responseString + "Config Name: Config Name Should not be Empty" + "
"; + responseString.append("Config Name: Config Name Should not be Empty" + "
"); valid = false; } if(policyData.getConfigType() != null){ String configTypeValidate = emptyValidator(policyData.getConfigType()); if(!configTypeValidate.contains("success")){ - responseString = responseString + "ConfigType:" + configTypeValidate + "
"; + responseString.append("ConfigType:" + configTypeValidate + "
"); valid = false; } }else{ - responseString = responseString + "Config Type: Config Type Should not be Empty" + "
"; + responseString.append("Config Type: Config Type Should not be Empty" + "
"); valid = false; } if(policyData.getConfigBodyData() != null){ @@ -202,28 +198,28 @@ public class PolicyValidationController extends RestrictedBaseController { if (policyType != null) { if (policyType.equals("JSON")) { if (!isJSONValid(configBodyData)) { - responseString = responseString + "Config Body: JSON Content is not valid" + "
"; + responseString.append("Config Body: JSON Content is not valid" + "
"); valid = false; } } else if (policyType.equals("XML")) { if (!isXMLValid(configBodyData)) { - responseString = responseString + "Config Body: XML Content data is not valid" + "
"; + responseString.append("Config Body: XML Content data is not valid" + "
"); valid = false; } } else if (policyType.equals("PROPERTIES")) { if (!isPropValid(configBodyData)||configBodyData.equals("")) { - responseString = responseString + "Config Body: Property data is not valid" + "
"; + responseString.append("Config Body: Property data is not valid" + "
"); valid = false; } } else if (policyType.equals("OTHER")) { if (configBodyData.equals("")) { - responseString = responseString + "Config Body: Config Body Should not be Empty" + "
"; + responseString.append("Config Body: Config Body Should not be Empty" + "
"); valid = false; } } } }else{ - responseString = responseString + "Config Body: Config Body Should not be Empty" + "
"; + responseString.append("Config Body: Config Body Should not be Empty" + "
"); valid = false; } } @@ -232,21 +228,21 @@ public class PolicyValidationController extends RestrictedBaseController { if(policyData.getConfigName() != null){ String configNameValidate = PolicyUtils.emptyPolicyValidator(policyData.getConfigName()); if(!configNameValidate.contains("success")){ - responseString = responseString + "ConfigName:" + configNameValidate + "
"; + responseString.append("ConfigName:" + configNameValidate + "
"); valid = false; } }else{ - responseString = responseString + "Config Name: Config Name is required" + "
"; + responseString.append("Config Name: Config Name is required" + "
"); valid = false; } if(policyData.getSecurityZone() == null){ - responseString = responseString + "Security Zone: Security Zone is required" + "
"; + responseString.append("Security Zone: Security Zone is required" + "
"); valid = false; } } if(policyData.getConfigPolicyType().equals("BRMS_Param")){ if(policyData.getRuleName() == null){ - responseString = responseString + "BRMS Template:BRMS Template is required
"; + responseString.append("BRMS Template:BRMS Template is required
"); valid = false; } } @@ -255,38 +251,38 @@ public class PolicyValidationController extends RestrictedBaseController { String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData()); // If there are any error other than Annotations then this is not Valid if(message.contains("[ERR")){ - responseString = responseString + "Raw Rule Validate:Raw Rule has error"+ message +"
"; + responseString.append("Raw Rule Validate:Raw Rule has error"+ message +"
"); valid = false; } }else{ - responseString = responseString + "Raw Rule:Raw Rule is required
"; + responseString.append("Raw Rule:Raw Rule is required
"); valid = false; } } if(policyData.getConfigPolicyType().equals("ClosedLoop_PM")){ try{ if(root.get("policyData").get("verticaMetrics").get("serviceTypePolicyName") == null && policyData.getServiceTypePolicyName().isEmpty()){ - responseString = responseString + "ServiceType PolicyName:ServiceType PolicyName is required
"; + responseString.append("ServiceType PolicyName:ServiceType PolicyName is required
"); valid = false; } }catch(Exception e){ - responseString = responseString + "ServiceType PolicyName:ServiceType PolicyName is required
"; + responseString.append("ServiceType PolicyName:ServiceType PolicyName is required
"); valid = false; } if(root.get("policyData").get("jsonBodyData") != null){ ClosedLoopPMBody pmBody = (ClosedLoopPMBody)mapper.readValue(root.get("policyData").get("jsonBodyData").toString(), ClosedLoopPMBody.class); if(pmBody.getEmailAddress() != null){ - String result = emailValidation(pmBody.getEmailAddress(), responseString); + String result = emailValidation(pmBody.getEmailAddress(), responseString.toString()); if(result != "success"){ - responseString = result + "
"; + responseString.append(result + "
"); valid = false; } } if(pmBody.getGeoLink() != null){ String result = PolicyUtils.emptyPolicyValidator(pmBody.getGeoLink()); if(!result.contains("success")){ - responseString = responseString + "GeoLink:" + result + "
"; + responseString.append("GeoLink:" + result + "
"); valid = false; }; } @@ -297,14 +293,14 @@ public class PolicyValidationController extends RestrictedBaseController { if(!key.contains("Message")){ String attributeValidate = PolicyUtils.emptyPolicyValidator(value); if(!attributeValidate.contains("success")){ - responseString = responseString + "Attributes:" + key + " : value has spaces
"; + responseString.append("Attributes:" + key + " : value has spaces
"); valid = false; }; } } } }else{ - responseString = responseString + "D2/Virtualized Services:Select atleast one D2/Virtualized Services
"; + responseString.append("D2/Virtualized Services:Select atleast one D2/Virtualized Services
"); valid = false; } } @@ -312,73 +308,69 @@ public class PolicyValidationController extends RestrictedBaseController { if(root.get("policyData").get("jsonBodyData") != null){ ClosedLoopFaultBody faultBody = (ClosedLoopFaultBody)mapper.readValue(root.get("policyData").get("jsonBodyData").toString(), ClosedLoopFaultBody.class); if(faultBody.getEmailAddress() != null){ - String result = emailValidation(faultBody.getEmailAddress(), responseString); + String result = emailValidation(faultBody.getEmailAddress(), responseString.toString()); if(result != "success"){ - responseString = result+ "
"; + responseString.append(result+ "
"); valid = false; } } if((faultBody.isGama() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS() || faultBody.isvUSP()) != true){ - responseString = responseString + "D2/Virtualized Services:Select atleast one D2/Virtualized Services
"; + responseString.append("D2/Virtualized Services:Select atleast one D2/Virtualized Services
"); valid = false; } if(faultBody.getActions() == null){ - responseString = responseString + "vPRO Actions:vPRO Actions is required
"; - valid = false; - } - if(faultBody.getAgingWindow() == 0){ - responseString = responseString + "Aging Window:Aging Window is required
"; + responseString.append("vPRO Actions:vPRO Actions is required
"); valid = false; } if(faultBody.getClosedLoopPolicyStatus() == null){ - responseString = responseString + "Policy Status:Policy Status is required
"; + responseString.append("Policy Status:Policy Status is required
"); valid = false; } if(faultBody.getConditions() == null){ - responseString = responseString + "Conditions:Select Atleast one Condition
"; + responseString.append("Conditions:Select Atleast one Condition
"); valid = false; } if(faultBody.getGeoLink() != null){ - String result = PolicyUtils.emptyPolicyValidator(faultBody.getGeoLink()); + String result = PolicyUtils.emptyPolicyValidatorWithSpaceAllowed(faultBody.getGeoLink()); if(!result.contains("success")){ - responseString = responseString + "GeoLink:" + result + "
"; + responseString.append("GeoLink:" + result + "
"); valid = false; }; } if(faultBody.getTimeInterval() == 0){ - responseString = responseString + "Time Interval:Time Interval is required
"; + responseString.append("Time Interval:Time Interval is required
"); valid = false; } if(faultBody.getRetrys() == 0){ - responseString = responseString + "Number of Retries:Number of Retries is required
"; + responseString.append("Number of Retries:Number of Retries is required
"); valid = false; } if(faultBody.getTimeOutvPRO() == 0){ - responseString = responseString + "APP-C Timeout:APP-C Timeout is required
"; + responseString.append("APP-C Timeout:APP-C Timeout is required
"); valid = false; } if(faultBody.getTimeOutRuby() == 0){ - responseString = responseString + "TimeOutRuby:TimeOutRuby is required
"; + responseString.append("TimeOutRuby:TimeOutRuby is required
"); valid = false; } if(faultBody.getVnfType() == null){ - responseString = responseString + "Vnf Type:Vnf Type is required
"; + responseString.append("Vnf Type:Vnf Type is required
"); valid = false; } }else{ - responseString = responseString + "D2/Virtualized Services:Select atleast one D2/Virtualized Services
"; - responseString = responseString + "vPRO Actions:vPRO Actions is required
"; - responseString = responseString + "Aging Window:Aging Window is required
"; - responseString = responseString + "Policy Status:Policy Status is required
"; - responseString = responseString + "Conditions:Select Atleast one Condition
"; - responseString = responseString + "PEP Name:PEP Name is required
"; - responseString = responseString + "PEP Action:PEP Action is required
"; - responseString = responseString + "Time Interval:Time Interval is required
"; - responseString = responseString + "Number of Retries:Number of Retries is required
"; - responseString = responseString + "APP-C Timeout:APP-C Timeout is required
"; - responseString = responseString + "TimeOutRuby:TimeOutRuby is required
"; - responseString = responseString + "Vnf Type:Vnf Type is required
"; + responseString.append("D2/Virtualized Services:Select atleast one D2/Virtualized Services
"); + responseString.append("vPRO Actions:vPRO Actions is required
"); + responseString.append("Aging Window:Aging Window is required
"); + responseString.append("Policy Status:Policy Status is required
"); + responseString.append("Conditions:Select Atleast one Condition
"); + responseString.append("PEP Name:PEP Name is required
"); + responseString.append("PEP Action:PEP Action is required
"); + responseString.append("Time Interval:Time Interval is required
"); + responseString.append("Number of Retries:Number of Retries is required
"); + responseString.append("APP-C Timeout:APP-C Timeout is required
"); + responseString.append("TimeOutRuby:TimeOutRuby is required
"); + responseString.append("Vnf Type:Vnf Type is required
"); valid = false; } } @@ -399,7 +391,7 @@ public class PolicyValidationController extends RestrictedBaseController { returnModel = getAttributeObject(service, version); String annoation = returnModel.getAnnotation(); if (!Strings.isNullOrEmpty(annoation)){ - rangeMap = new HashMap(); + Map rangeMap = new HashMap<>(); rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annoation); for (Entry rMap : rangeMap.entrySet()){ if (rMap.getValue().contains("range::")){ @@ -409,34 +401,26 @@ public class PolicyValidationController extends RestrictedBaseController { int endNum = Integer.parseInt(tempString[1]); String returnString = "Invalid Range:" + rMap.getKey() + " must be between " + startNum + " - " + endNum + ","; - if (isType(value.replace("\"", ""))){ + if (PolicyUtils.isInteger(value.replace("\"", ""))){ int result = Integer.parseInt(value.replace("\"", "")); - - if (result < startNum || result > endNum){ - responseString = responseString + returnString; + responseString.append(returnString); valid = false; } }else { - responseString = responseString + returnString; + responseString.append(returnString); valid = false; } } } } - //for continue testing for Dkat, just blocked this validation until fixing it. gw1218 on 3/30/17 - //if (!checkAttributeValues()){ - //responseString = responseString + "Micro Service: Attribute Values Missing" + "
"; - //valid = false; - //} - }else{ - responseString = responseString + "Micro Service: Micro Service is required" + "
"; + responseString.append("Micro Service: Micro Service is required" + "
"); valid = false; } if(policyData.getPriority() == null){ - responseString = responseString + "Priority: Priority is required" + "
"; + responseString.append("Priority: Priority is required" + "
"); valid = false; } } @@ -445,49 +429,96 @@ public class PolicyValidationController extends RestrictedBaseController { if(policyData.getEcompName() != null){ String ecompNameValidate = emptyValidator(policyData.getEcompName()); if(!ecompNameValidate.contains("success")){ - responseString = responseString + "EcompName:" + ecompNameValidate + "
"; + responseString.append("EcompName:" + ecompNameValidate + "
"); valid = false; } }else{ - responseString = responseString + "Ecomp Name: Ecomp Name Should not be empty" + "
"; + responseString.append("Ecomp Name: Ecomp Name Should not be empty" + "
"); valid = false; } - if(policyData.getRuleProvider().equals("GUARD_YAML")){ - if(policyData.getYamlparams()==null){ - responseString = responseString + " Guard Params are Required " + "
"; + + if("Rainy_Day".equals(policyData.getRuleProvider())){ + if(policyData.getRainyday()==null){ + responseString.append(" Rainy Day Parameters are Required
"); valid = false; }else{ - if(policyData.getYamlparams().getActor()==null){ - responseString = responseString + "Guard Params Actor is Required " + "
"; + if(policyData.getRainyday().getServiceType()==null){ + responseString.append("Rainy Day Service Type is Required
"); valid = false; } - if(policyData.getYamlparams().getRecipe()==null){ - responseString = responseString + "Guard Params Recipe is Required " + "
"; + if(policyData.getRainyday().getVnfType()==null){ + responseString.append("Rainy Day VNF Type is Required
"); + valid = false; + } + if(policyData.getRainyday().getBbid()==null){ + responseString.append("Rainy Day Building Block ID is Required
"); valid = false; } - if(policyData.getYamlparams().getLimit()==null){ - responseString = responseString + " Guard Params Limit is Required " + "
"; + if(policyData.getRainyday().getWorkstep()==null){ + responseString.append("Rainy Day Work Step is Required
"); + valid = false; + } + if(policyData.getRainyday().getServiceType()==null){ + responseString.append("Rainy Day Error Code is Required
"); + valid = false; + } + } + } + + if("GUARD_YAML".equals(policyData.getRuleProvider()) || "GUARD_BL_YAML".equals(policyData.getRuleProvider())){ + if(policyData.getYamlparams()==null){ + responseString.append(" Guard Params are Required " + "
"); + valid = false; + }else{ + if(policyData.getYamlparams().getActor()==null){ + responseString.append("Guard Params Actor is Required " + "
"); valid = false; - }else{ - try{ - Integer.parseInt(policyData.getYamlparams().getLimit()); - }catch(NumberFormatException e){ - responseString = responseString + " Guard Params Limit Should be Integer " + "
"; - valid = false; - } } - if(policyData.getYamlparams().getTimeWindow()==null){ - responseString = responseString + "Guard Params Time Window is Required" + "
"; + if(policyData.getYamlparams().getRecipe()==null){ + responseString.append("Guard Params Recipe is Required " + "
"); valid = false; } if(policyData.getYamlparams().getGuardActiveStart()==null){ - responseString = responseString + "Guard Params Guard Active Start/b>is Required " + "
"; + responseString.append("Guard Params Guard Active Start/b>is Required " + "
"); valid = false; } if(policyData.getYamlparams().getGuardActiveEnd()==null){ - responseString = responseString + "Guard Params Guard Active Endis Required " + "
"; + responseString.append("Guard Params Guard Active Endis Required " + "
"); valid = false; } + if("GUARD_YAML".equals(policyData.getRuleProvider())){ + if(policyData.getYamlparams().getLimit()==null){ + responseString.append(" Guard Params Limit is Required " + "
"); + valid = false; + }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getLimit())){ + responseString.append(" Guard Params Limit Should be Integer " + "
"); + valid = false; + } + if(policyData.getYamlparams().getTimeWindow()==null){ + responseString.append("Guard Params Time Window is Required" + "
"); + valid = false; + }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getTimeWindow())){ + responseString.append(" Guard Params Time Window Should be Integer " + "
"); + valid = false; + } + if(policyData.getYamlparams().getTimeUnits()==null){ + responseString.append("Guard Params Time Units is Required" + "
"); + valid = false; + } + }else if("GUARD_BL_YAML".equals(policyData.getRuleProvider())){ + if(policyData.getYamlparams().getBlackList()==null || policyData.getYamlparams().getBlackList().isEmpty()){ + responseString.append(" Guard Params BlackList is Required " + "
"); + valid = false; + }else{ + for(String blackList: policyData.getYamlparams().getBlackList()){ + if(blackList==null || !("success".equals(emptyValidator(blackList)))){ + responseString.append(" Guard Params BlackList Should be valid String" + "
"); + valid = false; + break; + } + } + } + } } } } @@ -496,49 +527,99 @@ public class PolicyValidationController extends RestrictedBaseController { if(policyData.getActionPerformer() != null){ String actionPerformer = emptyValidator(policyData.getActionPerformer()); if(!actionPerformer.contains("success")){ - responseString = responseString + "ActionPerformer:" + actionPerformer + "
"; + responseString.append("ActionPerformer:" + actionPerformer + "
"); valid = false; }; }else{ - responseString = responseString + "ActionPerformer: ActionPerformer Should not be empty" + "
"; + responseString.append("ActionPerformer: ActionPerformer Should not be empty" + "
"); + valid = false; + } + if(policyData.getAttributes() != null){ + for(Object attribute : policyData.getAttributes()){ + if(attribute instanceof LinkedHashMap){ + try{ + //This is for validation check if the value exists or not + String key = ((LinkedHashMap) attribute).get("key").toString(); + String value = ((LinkedHashMap) attribute).get("value").toString(); + if("".equals(key) || "".equals(value)){ + responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "
"); + valid = false; + break; + } + }catch(Exception e){ + LOGGER.error("This is a Policy Validation check" +e); + responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "
"); + valid = false; + break; + } + } + } + }else{ + responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "
"); valid = false; } if(policyData.getActionAttributeValue() != null){ String actionAttribute = emptyValidator(policyData.getActionAttributeValue()); if(!actionAttribute.contains("success")){ - responseString = responseString + "ActionAttribute:" + actionAttribute + "
"; + responseString.append("ActionAttribute:" + actionAttribute + "
"); valid = false; }; }else{ - responseString = responseString + "ActionAttribute: ActionAttribute Should not be empty" + "
"; + responseString.append("ActionAttribute: ActionAttribute Should not be empty" + "
"); valid = false; } } + + if(policyData.getPolicyType().equals(ACTION_POLICY) || policyData.getPolicyType().equals(DECISION_POLICY)){ + if(!policyData.getRuleAlgorithmschoices().isEmpty()){ + for(Object attribute : policyData.getRuleAlgorithmschoices()){ + if(attribute instanceof LinkedHashMap){ + try{ + String label = ((LinkedHashMap) attribute).get("id").toString(); + String key = ((LinkedHashMap) attribute).get("dynamicRuleAlgorithmField1").toString(); + String rule = ((LinkedHashMap) attribute).get("dynamicRuleAlgorithmCombo").toString(); + String value = ((LinkedHashMap) attribute).get("dynamicRuleAlgorithmField2").toString(); + if("".equals(label) || "".equals(key) || "".equals(rule) || "".equals(value)){ + responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "
"); + valid = false; + } + }catch(Exception e){ + LOGGER.error("This is a Policy Validation check" +e); + responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "
"); + valid = false; + break; + } + } + } + } + } if(policyData.getPolicyType().equals(CONFIG_POLICY)){ + String value = ""; if(valid){ List spData = commonClassDao.getDataById(SafePolicyWarning.class, "riskType", policyData.getRiskType()); if (!spData.isEmpty()){ SafePolicyWarning safePolicyWarningData = (SafePolicyWarning) spData.get(0); safePolicyWarningData.getMessage(); - responseString = responseString + "Messaage:" + safePolicyWarningData.getMessage(); + value = "Message:" + safePolicyWarningData.getMessage(); } - responseString = "success" + "@#"+ responseString; + responseString.append("success" + "@#"+ value); } }else{ if(valid){ - responseString = "success"; + responseString.append("success"); } } PrintWriter out = response.getWriter(); - JsonMessage msg = new JsonMessage(mapper.writeValueAsString(responseString)); + JsonMessage msg = new JsonMessage(mapper.writeValueAsString(responseString.toString())); JSONObject j = new JSONObject(msg); out.write(j.toString()); return null; } catch (Exception e){ + LOGGER.error("Exception Occured while Policy Validation" +e); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -548,8 +629,8 @@ public class PolicyValidationController extends RestrictedBaseController { } protected String emptyValidator(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 { @@ -564,7 +645,7 @@ public class PolicyValidationController extends RestrictedBaseController { } protected 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; @@ -590,16 +671,16 @@ public class PolicyValidationController extends RestrictedBaseController { return error; } - protected String emailValidation(String email, String responseString){ + protected String emailValidation(String email, String response){ if(email != null){ String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", "")); if(!validateEmail.contains("success")){ - responseString = responseString + "Email:" + validateEmail+ "
"; + response += "Email:" + validateEmail+ "
"; }else{ return "success"; } } - return responseString; + return response; } private MicroServiceModels getAttributeObject(String name, String version) { @@ -631,34 +712,20 @@ public class PolicyValidationController extends RestrictedBaseController { } } - private Boolean isType(String testStr) { - try { - Integer.parseInt(testStr); - return true; - } catch(Exception e) { - return false; - } - } - // Validation for json. protected static boolean isJSONValid(String data) { - InputStream stream = null; JsonReader jsonReader = null; try { new JSONObject(data); - stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); + InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)); jsonReader = Json.createReader(stream); + LOGGER.info("Json Value is: " + jsonReader.read().toString() ); } catch (Exception e) { - LOGGER.error("Exception Occured"+e); + LOGGER.error("Exception Occured While Validating"+e); return false; }finally{ - try { - if(stream != null && jsonReader != null){ - jsonReader.close(); - stream.close(); - } - } catch (IOException e) { - LOGGER.error("Exception Occured while closing the input stream"+e); + if(jsonReader != null){ + jsonReader.close(); } } return true; @@ -674,11 +741,8 @@ public class PolicyValidationController extends RestrictedBaseController { XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new XMLErrorHandler()); reader.parse(new InputSource(new StringReader(data))); - } catch (ParserConfigurationException e) { - return false; - } catch (SAXException e) { - return false; - } catch (IOException e) { + } catch (Exception e) { + LOGGER.error("Exception Occured While Validating"+e); return false; } return true; @@ -689,7 +753,7 @@ public class PolicyValidationController extends RestrictedBaseController { Scanner scanner = new Scanner(prop); while (scanner.hasNextLine()) { String line = scanner.nextLine(); - line.replaceAll("\\s+", ""); + line = line.replaceAll("\\s+", ""); if (line.startsWith("#")) { continue; } else { @@ -709,4 +773,4 @@ public class PolicyValidationController extends RestrictedBaseController { return true; } -} +} \ No newline at end of file