diff options
author | 2018-10-23 13:33:57 -0500 | |
---|---|---|
committer | 2018-10-23 14:19:14 -0500 | |
commit | b906dff993b018302fdbdf7e2cfecdbb5cada7f7 (patch) | |
tree | 016a692848f033b650bd33a7eada41f5f14943b2 /ONAP-REST/src/main/java/org/onap | |
parent | 4947a957b436d4689cdee6faefedd58e69cd7cb0 (diff) |
Made change to validation to reduce complexity
I had this change before Jorge merged the my previous commit for fixing
optimization policy validation and was planning to amend but Jorge was
quicker on the draw :)
Thanks Pam for pointing this out.. a simple way to reduce complexity
that I will use going forward.
Change-Id: I3ab8ae36591c7c69629f36d84261f95c559d0f42
Issue-ID: POLICY-1205
Signed-off-by: Michael Mokry <michael.mokry@att.com>
Diffstat (limited to 'ONAP-REST/src/main/java/org/onap')
-rw-r--r-- | ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java | 199 |
1 files changed, 100 insertions, 99 deletions
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 924864443..de20cd3f8 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 @@ -1212,116 +1212,117 @@ public class PolicyValidation { private boolean validateOptimization(PolicyRestAdapter policyData, StringBuilder responseString) { boolean valid = true; - 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) { - - String annotation = returnModel.getAnnotation(); - String refAttributes = returnModel.getRefattributes(); - String subAttributes = returnModel.getSubattributes(); - String modelAttributes = returnModel.getAttributes(); - - if (!Strings.isNullOrEmpty(annotation)) { - Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=") - .split(annotation); - for (Entry<String, String> 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("<b>" + rMap.getKey() + "</b>:<i>" + rMap.getKey() - + " is required for the Optimization model " + service - + HTML_ITALICS_LNBREAK); - valid = false; - } + + // Checks for required policy data in request + if (Strings.isNullOrEmpty(policyData.getServiceType())) { + responseString.append( + "<b>Optimization Service</b>:<i> Optimization policy data is missing or invalid Json." + + HTML_ITALICS_LNBREAK); + return false; + } + + modelRequiredFieldsList.clear(); + pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON()); + + // parse the service and version from the policy data + 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(); + } + + // Checks for required version in policy data + if (Strings.isNullOrEmpty(version)) { + responseString.append( + "<b>Optimization Service Version</b>:<i> Optimization Service Version is required" + + HTML_ITALICS_LNBREAK); + return false; + } + + OptimizationModels returnModel = getOptimizationModelData(service, version); + + // Checks if valid model exists in the database + if (returnModel == null) { + responseString.append("<b>Optimization Service Model</b>:<i> Invalid Model. The model name, " + + service + " of version, " + version + + " was not found in the dictionary" + HTML_ITALICS_LNBREAK); + return false; + } + String annotation = returnModel.getAnnotation(); + String refAttributes = returnModel.getRefattributes(); + String subAttributes = returnModel.getSubattributes(); + String modelAttributes = returnModel.getAttributes(); + + if (!Strings.isNullOrEmpty(annotation)) { + Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=") + .split(annotation); + for (Entry<String, String> 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("<b>" + rMap.getKey() + "</b>:<i>" + rMap.getKey() + + " is required for the Optimization model " + service + + HTML_ITALICS_LNBREAK); + valid = false; } - // If request comes from the API we need to validate required fields in the Micro Service Modelvalid - // GUI request are already validated from the SDK-APP - if ("API".equals(policyData.getApiflag())) { - // first , get the complete set of required fields - populateReqFieldSet(new String[] {refAttributes, modelAttributes}, subAttributes); - - Iterator<String> reqTrueKeysIter = allOptReqTrueKeys.iterator(); - while (reqTrueKeysIter.hasNext()) { - modelRequiredFieldsList.add(reqTrueKeysIter.next()); - } + } + } + } - if (modelRequiredFieldsList != null && !modelRequiredFieldsList.isEmpty()) { - - // create jsonRequestMap with all json keys and values from request - JsonNode rootNode = (JsonNode) policyData.getPolicyJSON(); - jsonRequestMap.clear(); - pullModelJsonKeyPairs(rootNode); + // If request comes from the API we need to validate required fields in the Micro Service Modelvalid + // GUI request are already validated from the SDK-APP + if ("API".equals(policyData.getApiflag())) { + // first , get the complete set of required fields + populateReqFieldSet(new String[] {refAttributes, modelAttributes}, subAttributes); - // validate if the requiredFields are in the request - for (String requiredField : modelRequiredFieldsList) { - if (jsonRequestMap.containsKey(requiredField)) { - String value = jsonRequestMap.get(requiredField); - if (StringUtils.isBlank(value) || "\"\"".equals(value)) { - responseString.append("<b>Optimization Service Model</b>:<i> " - + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); - valid = false; - } - } else { - responseString.append("<b>Optimization Service Model</b>:<i> " - + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); - valid = false; - } - } + modelRequiredFieldsList.addAll(allOptReqTrueKeys); + + 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 (StringUtils.isBlank(value) || "\"\"".equals(value)) { + responseString.append("<b>Optimization Service Model</b>:<i> " + + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); + valid = false; } + } else { + responseString.append("<b>Optimization Service Model</b>:<i> " + + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); + valid = false; } - } else { - responseString.append("<b>Optimization Service Model</b>:<i> Invalid Model. The model name, " - + service + " of version, " + version - + " was not found in the dictionary" + HTML_ITALICS_LNBREAK); - valid = false; } - } else { - responseString.append( - "<b>Optimization Service Version</b>:<i> Optimization Service Version is required" - + HTML_ITALICS_LNBREAK); - valid = false; } - } else { - responseString.append("<b>Optimization Service</b>:<i> Optimization policy data is missing or invalid Json." - + HTML_ITALICS_LNBREAK); - valid = false; } if (Strings.isNullOrEmpty(policyData.getPriority())) { |