X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Frest%2Futil%2FPolicyValidation.java;h=2e685d423027146510961342de682e22ae7dbc91;hb=a65d684a684105c39a64181bbd44f1debb80fb06;hp=446073d405049287a565af891680082bfa46e358;hpb=b7594ee217d0fd4c724fd64dd48d2e154fdd6e90;p=policy%2Fengine.git diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java index 446073d40..2e685d423 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java @@ -44,6 +44,7 @@ import org.onap.policy.rest.adapter.ClosedLoopPMBody; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.MicroServiceModels; +import org.onap.policy.rest.jpa.OptimizationModels; import org.onap.policy.rest.jpa.SafePolicyWarning; import org.onap.policy.utils.PolicyUtils; import org.onap.policy.xacml.api.XACMLErrorConstants; @@ -65,12 +66,19 @@ public class PolicyValidation { public static final String DECISION_POLICY = "Decision"; public static final String CLOSEDLOOP_POLICY = "ClosedLoop_Fault"; public static final String CLOSEDLOOP_PM = "ClosedLoop_PM"; - public static final String ENFORCER_CONFIG_POLICY= "Enforcer Config"; - public static final String MICROSERVICES="Micro Service"; - public static final String FIREWALL="Firewall Config"; + public static final String ENFORCER_CONFIG_POLICY = "Enforcer Config"; + public static final String MICROSERVICES = "Micro Service"; + public static final String FIREWALL = "Firewall Config"; + public static final String OPTIMIZATION="Optimization"; + public static final String BRMSPARAM = "BRMS_Param"; + public static final String BRMSRAW = "BRMS_Raw"; public static final String HTML_ITALICS_LNBREAK = "
"; public static final String SUCCESS = "success"; public static final String EMPTY_COMPONENT_ATTR = "Component Attributes: One or more Fields in Component Attributes is Empty."; + public static final String ISREQUIRED = " is required"; + public static final String SPACESINVALIDCHARS = " : value has spaces or invalid characters
"; + public static final String RULEALGORITHMS = "Rule Algorithms:"; + public static final String VALUE = "value"; private static Map mapAttribute = new HashMap<>(); private static Map jsonRequestMap = new HashMap<>(); @@ -122,8 +130,8 @@ public class PolicyValidation { String key = null; if(((LinkedHashMap) attribute).get("key") != null){ key = ((LinkedHashMap) attribute).get("key").toString(); - if(!PolicyUtils.policySpecialCharValidator(key).contains("success")){ - responseString.append("Attributes or Component Attributes:" + value + " : value has spaces or invalid characters
"); + if(!PolicyUtils.policySpecialCharValidator(key).contains(SUCCESS)){ + responseString.append("Attributes or Component Attributes:" + value + SPACESINVALIDCHARS); valid = false; } }else{ @@ -131,7 +139,7 @@ public class PolicyValidation { if("Base".equals(policyData.getConfigPolicyType())){ responseString.append("Attributes: has one missing Attribute key
"); } - if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){ + if(BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())){ responseString.append("Rule Attributes: has one missing Attribute key
"); } }else{ @@ -139,18 +147,18 @@ public class PolicyValidation { } valid = false; } - if(((LinkedHashMap) attribute).get("value") != null){ - value = ((LinkedHashMap) attribute).get("value").toString(); - if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){ + if(((LinkedHashMap) attribute).get(VALUE) != null){ + value = ((LinkedHashMap) attribute).get(VALUE).toString(); + if(!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)){ if(CONFIG_POLICY.equals(policyData.getPolicyType())){ if("Base".equals(policyData.getConfigPolicyType())){ - responseString.append("Attributes:" + value + " : value has spaces or invalid characters
"); + responseString.append("Attributes:" + value + SPACESINVALIDCHARS); } - if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){ - responseString.append("Rule Attributes:" + value + " : value has spaces or invalid characters
"); + if(BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())){ + responseString.append("Rule Attributes:" + value + SPACESINVALIDCHARS); } }else{ - responseString.append("Component Attributes:" + value + " : value has spaces or invalid characters
"); + responseString.append("Component Attributes:" + value + SPACESINVALIDCHARS); } valid = false; } @@ -159,7 +167,7 @@ public class PolicyValidation { if("Base".equals(policyData.getConfigPolicyType())){ responseString.append("Attributes: has one missing Attribute value
"); } - if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){ + if(BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())){ responseString.append("Rule Attributes: has one missing Attribute value
"); } }else{ @@ -180,10 +188,10 @@ public class PolicyValidation { responseString.append("Settings Attributes: has one missing Attribute key
"); valid = false; } - if(((LinkedHashMap) attribute).get("value") != null){ - value = ((LinkedHashMap) attribute).get("value").toString(); - if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){ - responseString.append("Settings Attributes:" + value + " : value has spaces or invalid characters
"); + if(((LinkedHashMap) attribute).get(VALUE) != null){ + value = ((LinkedHashMap) attribute).get(VALUE).toString(); + if(!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)){ + responseString.append("Settings Attributes:" + value + SPACESINVALIDCHARS); valid = false; } }else{ @@ -199,21 +207,21 @@ public class PolicyValidation { if(attribute instanceof LinkedHashMap){ String label = ((LinkedHashMap) attribute).get("id").toString(); if(((LinkedHashMap) attribute).get("dynamicRuleAlgorithmField1") == null){ - responseString.append("Rule Algorithms:" + label + " : Field 1 value is not selected
"); + responseString.append(RULEALGORITHMS + label + " : Field 1 value is not selected

"); valid = false; } if(((LinkedHashMap) attribute).get("dynamicRuleAlgorithmCombo") == null){ - responseString.append("Rule Algorithms:" + label + " : Field 2 value is not selected
"); + responseString.append(RULEALGORITHMS + label + " : Field 2 value is not selected

"); valid = false; } if(((LinkedHashMap) attribute).get("dynamicRuleAlgorithmField2") != null){ String value = ((LinkedHashMap) attribute).get("dynamicRuleAlgorithmField2").toString(); - if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){ - responseString.append("Rule Algorithms:" + label + " : Field 3 value has special characters
"); + if(!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)){ + responseString.append(RULEALGORITHMS + label + " : Field 3 value has special characters

"); valid = false; } }else{ - responseString.append("Rule Algorithms:" + label + " : Field 3 value is empty
"); + responseString.append(RULEALGORITHMS + label + " : Field 3 value is empty

"); valid = false; } } @@ -223,7 +231,7 @@ public class PolicyValidation { if(CONFIG_POLICY.equalsIgnoreCase(policyData.getPolicyType())){ if ("Base".equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_POLICY.equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_PM.equals(policyData.getConfigPolicyType()) || ENFORCER_CONFIG_POLICY.equals(policyData.getConfigPolicyType()) - || MICROSERVICES.equals(policyData.getConfigPolicyType())) { + || MICROSERVICES.equals(policyData.getConfigPolicyType()) || OPTIMIZATION.equals(policyData.getConfigPolicyType())) { if(!Strings.isNullOrEmpty(policyData.getOnapName())) { String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName()); @@ -269,7 +277,8 @@ public class PolicyValidation { responseString.append("Guard: Guard Value Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } - + + // Validate Config Base Policy Data if("Base".equalsIgnoreCase(policyData.getConfigPolicyType())){ if(!Strings.isNullOrEmpty(policyData.getConfigName())) { String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); @@ -320,7 +329,8 @@ public class PolicyValidation { valid = false; } } - + + // Validate Config Firewall Policy Data if(FIREWALL.equalsIgnoreCase(policyData.getConfigPolicyType())){ if(policyData.getConfigName() != null && !policyData.getConfigName().isEmpty()){ String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); @@ -337,11 +347,15 @@ public class PolicyValidation { valid = false; } } - if("BRMS_Param".equalsIgnoreCase(policyData.getConfigPolicyType()) && Strings.isNullOrEmpty(policyData.getRuleName())){ + + // Validate BRMS_Param Policy Data + if(BRMSPARAM.equalsIgnoreCase(policyData.getConfigPolicyType()) && Strings.isNullOrEmpty(policyData.getRuleName())){ responseString.append("BRMS Template:BRMS Template is required" + HTML_ITALICS_LNBREAK); valid = false; } - if("BRMS_Raw".equalsIgnoreCase(policyData.getConfigPolicyType())){ + + // Validate BRMS_Raw Policy Data + if(BRMSRAW.equalsIgnoreCase(policyData.getConfigPolicyType())){ if(policyData.getConfigBodyData() != null && !policyData.getConfigBodyData().isEmpty()){ String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData()); @@ -355,6 +369,8 @@ public class PolicyValidation { valid = false; } } + + // Validate ClosedLoop_PM Policy Data if(CLOSEDLOOP_PM.equalsIgnoreCase(policyData.getConfigPolicyType())){ try{ if(Strings.isNullOrEmpty(policyData.getServiceTypePolicyName().get("serviceTypePolicyName").toString())){ @@ -407,6 +423,8 @@ public class PolicyValidation { valid = false; } } + + // Validate ClosedLoop_Fault Policy Data if(CLOSEDLOOP_POLICY.equalsIgnoreCase(policyData.getConfigPolicyType())){ if(policyData.getJsonBody() != null){ @@ -511,11 +529,13 @@ public class PolicyValidation { valid = false; } } - + + // Validate MicroServices Policy Data if (MICROSERVICES.equals(policyData.getConfigPolicyType())){ + if(!Strings.isNullOrEmpty(policyData.getServiceType())){ - modelRequiredFieldsList = new ArrayList<>(); + modelRequiredFieldsList.clear(); pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON()); String service; @@ -529,7 +549,7 @@ public class PolicyValidation { } if(!Strings.isNullOrEmpty(version)) { - MicroServiceModels returnModel = getAttributeObject(service, version); + MicroServiceModels returnModel = getMSModelData(service, version); if(returnModel != null) { @@ -570,7 +590,7 @@ public class PolicyValidation { } } else { // Validate for configName, location, uuid, and policyScope if no annotations exist for this model - if(Strings.isNullOrEmpty(policyData.getMsLocation())){ + if(Strings.isNullOrEmpty(policyData.getLocation())){ responseString.append("Micro Service Model: location is required for this model" + HTML_ITALICS_LNBREAK); valid = false; } @@ -591,62 +611,213 @@ public class PolicyValidation { } } - // get list of required fields from the sub_Attributes of the Model - if(!Strings.isNullOrEmpty(subAttributes)) { - JsonObject subAttributesJson = stringToJsonObject(subAttributes); - findRequiredFields(subAttributesJson); - } + // If request comes from the API we need to validate required fields in the Micro Service Model + // GUI request are already validated from the SDK-APP + if("API".equals(policyData.getApiflag())){ + // get list of required fields from the sub_Attributes of the Model + if(!Strings.isNullOrEmpty(subAttributes)) { + JsonObject subAttributesJson = stringToJsonObject(subAttributes); + findRequiredFields(subAttributesJson); + } + + // get list of required fields from the attributes of the Model + if (!Strings.isNullOrEmpty(modelAttributes)) { + Map modelAttributesMap = null; + if (",".equals(modelAttributes.substring(modelAttributes.length()-1))) { + String attributeString = modelAttributes.substring(0, modelAttributes.length()-1); + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributeString); + } else { + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(modelAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } + + // get list of required fields from the ref_Attributes of the Model + if (!Strings.isNullOrEmpty(refAttributes)) { + Map refAttributesMap = null; + if (",".equals(refAttributes.substring(refAttributes.length()-1))) { + String attributesString = refAttributes.substring(0, refAttributes.length()-1); + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributesString); + } else { + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(refAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } + + if (modelRequiredFieldsList!=null || !modelRequiredFieldsList.isEmpty()) { + // create jsonRequestMap with all json keys and values from request + JsonNode rootNode = (JsonNode) policyData.getPolicyJSON(); + jsonRequestMap.clear(); + pullModelJsonKeyPairs(rootNode); + + // validate if the requiredFields are in the request + for(String requiredField : modelRequiredFieldsList) { + if (jsonRequestMap.containsKey(requiredField)) { + String value = jsonRequestMap.get(requiredField); + if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || + "\"\"".equals(value) || + "".equals(jsonRequestMap.get(requiredField))){ + responseString.append("Micro Service Model: " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { + responseString.append("Micro Service Model: " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); + valid = false; + } + } + } + } + } else { + responseString.append("Micro Service Model: Invalid Model. The model name, " + service + + " of version, " + version + " was not found in the dictionary" + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { + responseString.append("Micro Service Version: Micro Service Version is required" + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { + responseString.append("Micro Service: Micro Service Model is required" + HTML_ITALICS_LNBREAK); + valid = false; + } + + if(Strings.isNullOrEmpty(policyData.getPriority())){ + responseString.append("Priority: Priority is required" + HTML_ITALICS_LNBREAK); + valid = false; + } + } + + // Validate Optimization Policy Data + if (OPTIMIZATION.equals(policyData.getConfigPolicyType())){ + + if(!Strings.isNullOrEmpty(policyData.getServiceType())){ + + modelRequiredFieldsList.clear(); + pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON()); + + String service; + String version; + if (policyData.getServiceType().contains("-v")){ + service = policyData.getServiceType().split("-v")[0]; + version = policyData.getServiceType().split("-v")[1]; + }else { + service = policyData.getServiceType(); + version = policyData.getVersion(); + } + + if(!Strings.isNullOrEmpty(version)) { + OptimizationModels returnModel = getOptimizationModelData(service, version); + + if(returnModel != null) { - // get list of required fields from the attributes of the Model - if (!Strings.isNullOrEmpty(modelAttributes)) { - Map modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); - String json = new ObjectMapper().writeValueAsString(modelAttributesMap); - findRequiredFields(stringToJsonObject(json)); - } + String annotation = returnModel.getAnnotation(); + String refAttributes = returnModel.getRefattributes(); + String subAttributes = returnModel.getSubattributes(); + String modelAttributes = returnModel.getAttributes(); - // get list of required fields from the ref_Attributes of the Model - if (!Strings.isNullOrEmpty(refAttributes)) { - Map refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(refAttributes); - String json = new ObjectMapper().writeValueAsString(refAttributesMap); - findRequiredFields(stringToJsonObject(json)); + if (!Strings.isNullOrEmpty(annotation)){ + Map rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annotation); + for (Entry rMap : rangeMap.entrySet()){ + if (rMap.getValue().contains("range::")){ + String value = mapAttribute.get(rMap.getKey().trim()); + String[] tempString = rMap.getValue().split("::")[1].split("-"); + int startNum = Integer.parseInt(tempString[0]); + int endNum = Integer.parseInt(tempString[1]); + String returnString = "InvalidreturnModel Range:" + rMap.getKey() + " must be between " + + startNum + " - " + endNum + ","; + + if(value != null) { + if (PolicyUtils.isInteger(value.replace("\"", ""))){ + int result = Integer.parseInt(value.replace("\"", "")); + if (result < startNum || result > endNum){ + responseString.append(returnString); + valid = false; + } + }else { + responseString.append(returnString); + valid = false; + } + } else { + responseString.append(""+rMap.getKey()+":" + rMap.getKey() + + " is required for the Optimization model " + service + HTML_ITALICS_LNBREAK); + valid = false; + } + + } + } } - // Validate Required Fields in the Micro Service Model - if (modelRequiredFieldsList!=null || !modelRequiredFieldsList.isEmpty()) { - // create jsonRequestMap with all json keys and values from request - JsonNode rootNode = (JsonNode) policyData.getPolicyJSON(); - pullModelJsonKeyPairs(rootNode); + // If request comes from the API we need to validate required fields in the Micro Service Model + // GUI request are already validated from the SDK-APP + if("API".equals(policyData.getApiflag())){ + // get list of required fields from the sub_Attributes of the Model + if(!Strings.isNullOrEmpty(subAttributes)) { + JsonObject subAttributesJson = stringToJsonObject(subAttributes); + findRequiredFields(subAttributesJson); + } + + // get list of required fields from the attributes of the Model + if (!Strings.isNullOrEmpty(modelAttributes)) { + Map modelAttributesMap = null; + if (",".equals(modelAttributes.substring(modelAttributes.length()-1))) { + String attributeString = modelAttributes.substring(0, modelAttributes.length()-1); + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributeString); + } else { + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(modelAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } + + // get list of required fields from the ref_Attributes of the Model + if (!Strings.isNullOrEmpty(refAttributes)) { + Map refAttributesMap = null; + if (",".equals(refAttributes.substring(refAttributes.length()-1))) { + String attributesString = refAttributes.substring(0, refAttributes.length()-1); + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributesString); + } else { + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(refAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } - // validate if the requiredFields are in the request - for(String requiredField : modelRequiredFieldsList) { - if (jsonRequestMap.containsKey(requiredField)) { - String value = jsonRequestMap.get(requiredField); - if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || - "\"\"".equals(value) || - "".equals(jsonRequestMap.get(requiredField))){ - responseString.append("Micro Service Model: " + requiredField + " is required" + HTML_ITALICS_LNBREAK); + if (modelRequiredFieldsList!=null || !modelRequiredFieldsList.isEmpty()) { + // create jsonRequestMap with all json keys and values from request + JsonNode rootNode = (JsonNode) policyData.getPolicyJSON(); + jsonRequestMap.clear(); + pullModelJsonKeyPairs(rootNode); + + // validate if the requiredFields are in the request + for(String requiredField : modelRequiredFieldsList) { + if (jsonRequestMap.containsKey(requiredField)) { + String value = jsonRequestMap.get(requiredField); + if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || + "\"\"".equals(value) || + "".equals(jsonRequestMap.get(requiredField))){ + responseString.append("Optimization Service Model: " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { + responseString.append("Optimization Service Model: " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); valid = false; } - } else { - responseString.append("Micro Service Model: " + requiredField + " is required" + HTML_ITALICS_LNBREAK); - valid = false; } } - } - - - + } } else { - responseString.append("Micro Service Model: Invalid Model. The model name, " + service + + responseString.append("Optimization Service Model: Invalid Model. The model name, " + service + " of version, " + version + " was not found in the dictionary" + HTML_ITALICS_LNBREAK); valid = false; } } else { - responseString.append("Micro Service Version: Micro Service Version is required" + HTML_ITALICS_LNBREAK); + responseString.append("Optimization Service Version: Optimization Service Version is required" + HTML_ITALICS_LNBREAK); valid = false; } } else { - responseString.append("Micro Service: Micro Service Model is required" + HTML_ITALICS_LNBREAK); + responseString.append("Optimization Service: Optimization Service Model is required" + HTML_ITALICS_LNBREAK); valid = false; } @@ -851,7 +1022,7 @@ public class PolicyValidation { return res; } - private MicroServiceModels getAttributeObject(String name, String version) { + private MicroServiceModels getMSModelData(String name, String version) { MicroServiceModels workingModel = null; try{ List microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name+":"+version); @@ -867,6 +1038,23 @@ public class PolicyValidation { return workingModel; } + + private OptimizationModels getOptimizationModelData(String name, String version) { + OptimizationModels workingModel = null; + try{ + List optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, "modelName:version", name+":"+version); + if(optimizationModelsData != null){ + workingModel = (OptimizationModels) optimizationModelsData.get(0); + } + }catch(Exception e){ + String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Template. The template name, " + + name + " was not found in the dictionary: "; + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message + e); + return null; + } + + return workingModel; + } private void pullJsonKeyPairs(JsonNode rootNode) { Iterator> fieldsIterator = rootNode.fields();