diff options
author | liamfallon <liam.fallon@est.tech> | 2019-11-19 12:17:44 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-11-19 17:23:07 +0000 |
commit | 0a16964d442fca9ee8e628e43e4f2aee5b8fb182 (patch) | |
tree | 099683f09cbd5f4e3bb230cc4947e74313ccc362 /ONAP-REST/src/main | |
parent | 779125e31adbcc59a9864843b523bd6ed2751cbb (diff) |
Unit/SONAR/Checkstyle in ONAP-REST
Refactor of the PolicyValidationRequestWrapper class to make it
testable and unit tests for the class.
Issue-ID: POLICY-2131
Change-Id: Ie6b0b20aafd6a823fa778bb8070d057f27b28603
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'ONAP-REST/src/main')
-rw-r--r-- | ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java | 625 |
1 files changed, 303 insertions, 322 deletions
diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java index 8fdc1e1d2..ad60a7512 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java @@ -42,6 +42,7 @@ import javax.json.JsonReader; import javax.servlet.http.HttpServletRequest; import org.onap.policy.api.AttributeType; +import org.onap.policy.api.PolicyClass; import org.onap.policy.api.PolicyParameters; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -64,8 +65,10 @@ public class PolicyValidationRequestWrapper { private static final String PRIORITY = "priority"; private static final String RISKLEVEL = "riskLevel"; private static final String RISKTYPE = "riskType"; + private static final String SECURITY_ZONE_ID = "securityZoneId"; private static final String SERVICE = "service"; private static final String SERVICETYPE_POLICY_NAME = "serviceTypePolicyName"; + private static final String UUID = "uuid"; private static final String VERSION = "version"; /** @@ -75,7 +78,6 @@ public class PolicyValidationRequestWrapper { * @return the policy rest adapter */ public PolicyRestAdapter populateRequestParameters(HttpServletRequest request) { - PolicyRestAdapter policyData = null; ClosedLoopFaultTrapDatas trapDatas = null; ClosedLoopFaultTrapDatas faultDatas = null; @@ -93,18 +95,13 @@ public class PolicyValidationRequestWrapper { policyData.setFaultDatas(faultDatas); } - JsonObject json; - json = stringToJsonObject(root.toString()); - - if (json != null) { - if (json.containsKey("policyJSON")) { - policyData.setPolicyJSON(root.get("policyJSON")); - } else { - String jsonBodyData = json.getJsonObject("policyData").get("jsonBodyData").toString(); - policyData.setJsonBody(jsonBodyData); - } + JsonObject json = stringToJsonObject(root.toString()); + if (json.containsKey("policyJSON")) { + policyData.setPolicyJSON(root.get("policyJSON")); + } else { + String jsonBodyData = json.getJsonObject("policyData").get("jsonBodyData").toString(); + policyData.setJsonBody(jsonBodyData); } - } catch (Exception e) { LOGGER.error("Exception Occured while populating request parameters: " + e); } @@ -119,7 +116,6 @@ public class PolicyValidationRequestWrapper { * @return the policy rest adapter */ public PolicyRestAdapter populateRequestParameters(PolicyParameters parameters) { - PolicyRestAdapter policyData = new PolicyRestAdapter(); /* @@ -148,336 +144,321 @@ public class PolicyValidationRequestWrapper { return null; } - if (parameters.getPolicyClass() != null && !"Config".equals(parameters.getPolicyClass().toString())) { + if (parameters.getPolicyClass() == null) { + parameters.setPolicyClass(PolicyClass.Config); + } + + policyData.setPolicyType(parameters.getPolicyClass().toString()); + + switch (parameters.getPolicyClass()) { + case Config: + return populateConfigParameters(parameters, policyData, json); + + case Decision: + return populateDecisionParameters(parameters, policyData, getMatchingAttributeValues(parameters)); + + case Action: + return populateActionParameters(parameters, policyData, getMatchingAttributeValues(parameters)); + + default: + return null; + } + } + + private PolicyRestAdapter populateConfigParameters(PolicyParameters parameters, PolicyRestAdapter policyData, + JsonObject json) { + policyData.setConfigPolicyType(parameters.getPolicyConfigType().toString()); + + // Config Specific + policyData.setConfigBodyData(parameters.getConfigBody()); // Base + policyData.setConfigType((parameters.getConfigBodyType() != null) + ? parameters.getConfigBodyType().toString().toUpperCase() + : null); // Base + + switch (parameters.getPolicyConfigType()) { + case Firewall: + return populateConfigFirewallParameters(policyData, json); + case MicroService: + return populateConfigMicroserviceParameters(parameters, policyData, json); + case Optimization: + return populateConfigOptimizationParameters(parameters, policyData, json); + case ClosedLoop_Fault: + return populateConfigClosedLoopFaultParameters(policyData, json); + case ClosedLoop_PM: + return populateConfigClosedLoopPmParameters(policyData, json); + case BRMS_PARAM: + return populateConfigBrmsParameters(parameters, policyData); + + // case BRMS_RAW:, case Base:, and case Extended: handled as default cases + default: + return policyData; + } + } + + private PolicyRestAdapter populateConfigFirewallParameters(PolicyRestAdapter policyData, JsonObject json) { + policyData.setConfigPolicyType("Firewall Config"); + + // get values and attributes from the JsonObject + if (json != null) { + policyData.setSecurityZone(getNewOrExistingKeyValue(json, SECURITY_ZONE_ID, policyData.getSecurityZone())); + policyData.setConfigName(getNewOrExistingKeyValue(json, CONFIG_NAME, policyData.getConfigName())); + } + return policyData; + } + + private PolicyRestAdapter populateConfigMicroserviceParameters(PolicyParameters parameters, + PolicyRestAdapter policyData, JsonObject json) { + policyData.setConfigPolicyType("Micro Service"); + + // Get values and attributes from the JsonObject + if (json != null) { + return getJsonObjectValuesAndAttributes(parameters, policyData, json); + } else { + String message = XACMLErrorConstants.ERROR_DATA_ISSUE + INVALIDJSON + parameters.getConfigBody(); + LOGGER.error(message); + return null; + } + } + + private PolicyRestAdapter populateConfigOptimizationParameters(PolicyParameters parameters, + PolicyRestAdapter policyData, JsonObject json) { + policyData.setConfigPolicyType("Optimization"); + + // get values and attributes from the JsonObject + if (json != null) { + return getJsonObjectValuesAndAttributes(parameters, policyData, json); + } else { + return policyData; + } + } + + private PolicyRestAdapter populateConfigClosedLoopFaultParameters(PolicyRestAdapter policyData, JsonObject json) { + policyData.setConfigPolicyType("ClosedLoop_Fault"); + + if (json != null) { + policyData.setJsonBody(json.toString()); + policyData.setOnapName(getNewOrExistingKeyValue(json, ONAPNAME, policyData.getOnapName())); + } + return policyData; + } + + private PolicyRestAdapter populateConfigClosedLoopPmParameters(PolicyRestAdapter policyData, JsonObject json) { + policyData.setConfigPolicyType("ClosedLoop_PM"); + + if (json != null) { + policyData.setJsonBody(json.toString()); + policyData.setOnapName(getNewOrExistingKeyValue(json, ONAPNAME, policyData.getOnapName())); + if (json.get(SERVICETYPE_POLICY_NAME) != null) { + String serviceType = json.get(SERVICETYPE_POLICY_NAME).toString().replace("\"", ""); + LinkedHashMap<String, String> serviceTypePolicyName = new LinkedHashMap<>(); + serviceTypePolicyName.put(SERVICETYPE_POLICY_NAME, serviceType); + policyData.setServiceTypePolicyName(serviceTypePolicyName); + } + } + return policyData; + } + + private PolicyRestAdapter populateConfigBrmsParameters(PolicyParameters parameters, PolicyRestAdapter policyData) { + Map<AttributeType, Map<String, String>> drlRuleAndUiParams = parameters.getAttributes(); + Map<String, String> rule = drlRuleAndUiParams.get(AttributeType.RULE); + policyData.setRuleName(rule.get("templateName")); + + return policyData; + } + + private PolicyRestAdapter populateDecisionParameters(PolicyParameters parameters, PolicyRestAdapter policyData, + Map<String, String> matching) { + policyData.setRuleProvider(parameters.getRuleProvider().toString()); + + switch (parameters.getRuleProvider()) { + case RAINY_DAY: + return populateDecisionRainyDayParameters(parameters, policyData, matching); + + case GUARD_BL_YAML: + case GUARD_MIN_MAX: + case GUARD_YAML: + return populateDecisionGuardParameters(policyData, matching); + + case AAF: + case CUSTOM: + case RAW: + default: + return policyData; + } + } + + private PolicyRestAdapter populateDecisionRainyDayParameters(PolicyParameters parameters, + PolicyRestAdapter policyData, Map<String, String> matching) { + // Set Matching attributes in RainyDayParams in adapter + RainyDayParams rainyday = new RainyDayParams(); + + if (matching != null) { + rainyday.setServiceType(matching.get("ServiceType")); + rainyday.setVnfType(matching.get("VNFType")); + rainyday.setBbid(matching.get("BB_ID")); + rainyday.setWorkstep(matching.get("WorkStep")); + } + + Map<String, String> treatments = parameters.getTreatments(); + ArrayList<Object> treatmentsTableChoices = new ArrayList<>(); + + for (String keyField : treatments.keySet()) { + LinkedHashMap<String, String> treatmentMap = new LinkedHashMap<>(); + String errorcode = keyField; + String treatment = treatments.get(errorcode); + treatmentMap.put("errorcode", errorcode); + treatmentMap.put("treatment", treatment); + treatmentsTableChoices.add(treatmentMap); + } + rainyday.setTreatmentTableChoices(treatmentsTableChoices); + policyData.setRainyday(rainyday); + + return policyData; + } + + private PolicyRestAdapter populateDecisionGuardParameters(PolicyRestAdapter policyData, + Map<String, String> matching) { + // Set Matching attributes in YAMLParams in adapter + YAMLParams yamlparams = new YAMLParams(); + + if (matching == null) { + policyData.setYamlparams(yamlparams); + return policyData; + } + + yamlparams.setActor(matching.get("actor")); + yamlparams.setRecipe(matching.get("recipe")); + yamlparams.setGuardActiveStart(matching.get("guardActiveStart")); + yamlparams.setGuardActiveEnd(matching.get("guardActiveEnd")); - policyData.setPolicyType(parameters.getPolicyClass().toString()); + yamlparams.setLimit(matching.get("limit")); + yamlparams.setTimeWindow(matching.get("timeWindow")); + yamlparams.setTimeUnits(matching.get("timeUnits")); - // Get Matching attribute values - Map<AttributeType, Map<String, String>> attributes = parameters.getAttributes(); - Map<String, String> matching = null; - if (attributes != null) { - matching = attributes.get(AttributeType.MATCHING); + yamlparams.setMin(matching.get("min")); + yamlparams.setMax(matching.get("max")); + + List<String> blackList = new ArrayList<>(); + + if (!Strings.isNullOrEmpty(matching.get("blackList"))) { + String[] blackListArray = matching.get("blackList").split(","); + for (String element : blackListArray) { + blackList.add(element); } + } - if ("Decision".equals(parameters.getPolicyClass().toString())) { + yamlparams.setBlackList(blackList); + + policyData.setYamlparams(yamlparams); + return policyData; + } - String ruleProvider = parameters.getRuleProvider().toString(); - policyData.setRuleProvider(ruleProvider); - - if ("Rainy_Day".equals(ruleProvider)) { - - // Set Matching attributes in RainyDayParams in adapter - RainyDayParams rainyday = new RainyDayParams(); - - if (matching != null) { - rainyday.setServiceType(matching.get("ServiceType")); - rainyday.setVnfType(matching.get("VNFType")); - rainyday.setBbid(matching.get("BB_ID")); - rainyday.setWorkstep(matching.get("WorkStep")); - } - - Map<String, String> treatments = parameters.getTreatments(); - ArrayList<Object> treatmentsTableChoices = new ArrayList<>(); - - for (String keyField : treatments.keySet()) { - LinkedHashMap<String, String> treatmentMap = new LinkedHashMap<>(); - String errorcode = keyField; - String treatment = treatments.get(errorcode); - treatmentMap.put("errorcode", errorcode); - treatmentMap.put("treatment", treatment); - treatmentsTableChoices.add(treatmentMap); - } - rainyday.setTreatmentTableChoices(treatmentsTableChoices); - policyData.setRainyday(rainyday); - - } else if ("GUARD_YAML".equals(ruleProvider) || "GUARD_BL_YAML".equals(ruleProvider) - || "GUARD_MIN_MAX".equals(ruleProvider)) { - - // Set Matching attributes in YAMLParams in adapter - YAMLParams yamlparams = new YAMLParams(); - - if (matching != null) { - yamlparams.setActor(matching.get("actor")); - yamlparams.setRecipe(matching.get("recipe")); - yamlparams.setGuardActiveStart(matching.get("guardActiveStart")); - yamlparams.setGuardActiveEnd(matching.get("guardActiveEnd")); - - if ("GUARD_YAML".equals(ruleProvider)) { - yamlparams.setLimit(matching.get("limit")); - yamlparams.setTimeWindow(matching.get("timeWindow")); - yamlparams.setTimeUnits(matching.get("timeUnits")); - } else if ("GUARD_MIN_MAX".equals(ruleProvider)) { - yamlparams.setMin(matching.get("min")); - yamlparams.setMax(matching.get("max")); - } else { - - List<String> blackList = new ArrayList<>(); - - if (!Strings.isNullOrEmpty(matching.get("blackList"))) { - String[] blackListArray = matching.get("blackList").split(","); - for (String element : blackListArray) { - blackList.add(element); - } - } - - yamlparams.setBlackList(blackList); - - } - } - policyData.setYamlparams(yamlparams); - } - - } else if ("Action".equals(parameters.getPolicyClass().toString())) { - - ArrayList<Object> ruleAlgorithmChoices = new ArrayList<>(); - - List<String> dynamicLabelRuleAlgorithms = parameters.getDynamicRuleAlgorithmLabels(); - List<String> dynamicFieldFunctionRuleAlgorithms = parameters.getDynamicRuleAlgorithmFunctions(); - List<String> dynamicFieldOneRuleAlgorithms = parameters.getDynamicRuleAlgorithmField1(); - List<String> dyrnamicFieldTwoRuleAlgorithms = parameters.getDynamicRuleAlgorithmField2(); - - if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) { - - for (int i = dynamicLabelRuleAlgorithms.size() - 1; i >= 0; i--) { - LinkedHashMap<String, String> ruleAlgorithm = new LinkedHashMap<>(); - - String id = dynamicLabelRuleAlgorithms.get(i); - String dynamicRuleAlgorithmField1 = dynamicFieldOneRuleAlgorithms.get(i); - String dynamicRuleAlgorithmCombo = dynamicFieldFunctionRuleAlgorithms.get(i); - String dynamicRuleAlgorithmField2 = dyrnamicFieldTwoRuleAlgorithms.get(i); - - ruleAlgorithm.put("id", id); - ruleAlgorithm.put("dynamicRuleAlgorithmField1", dynamicRuleAlgorithmField1); - ruleAlgorithm.put("dynamicRuleAlgorithmCombo", dynamicRuleAlgorithmCombo); - ruleAlgorithm.put("dynamicRuleAlgorithmField2", dynamicRuleAlgorithmField2); - - ruleAlgorithmChoices.add(ruleAlgorithm); - } - } - - policyData.setRuleAlgorithmschoices(ruleAlgorithmChoices); - - ArrayList<Object> attributeList = new ArrayList<>(); - if (matching != null) { - for (Map.Entry<String, String> entry : matching.entrySet()) { - LinkedHashMap<String, String> attributeMap = new LinkedHashMap<>(); - String key = entry.getKey(); - String value = entry.getValue(); - attributeMap.put("key", key); - attributeMap.put("value", value); - attributeList.add(attributeMap); - } - } - - policyData.setAttributes(attributeList); - policyData.setActionAttributeValue(parameters.getActionAttribute()); - policyData.setActionPerformer(parameters.getActionPerformer()); + private PolicyRestAdapter populateActionParameters(PolicyParameters parameters, PolicyRestAdapter policyData, + Map<String, String> matching) { + ArrayList<Object> ruleAlgorithmChoices = new ArrayList<>(); + List<String> dynamicLabelRuleAlgorithms = parameters.getDynamicRuleAlgorithmLabels(); + List<String> dynamicFieldFunctionRuleAlgorithms = parameters.getDynamicRuleAlgorithmFunctions(); + List<String> dynamicFieldOneRuleAlgorithms = parameters.getDynamicRuleAlgorithmField1(); + List<String> dynamicFieldTwoRuleAlgorithms = parameters.getDynamicRuleAlgorithmField2(); + + if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) { + + for (int i = dynamicLabelRuleAlgorithms.size() - 1; i >= 0; i--) { + LinkedHashMap<String, String> ruleAlgorithm = new LinkedHashMap<>(); + + String id = dynamicLabelRuleAlgorithms.get(i); + String dynamicRuleAlgorithmField1 = dynamicFieldOneRuleAlgorithms.get(i); + String dynamicRuleAlgorithmCombo = dynamicFieldFunctionRuleAlgorithms.get(i); + String dynamicRuleAlgorithmField2 = dynamicFieldTwoRuleAlgorithms.get(i); + + ruleAlgorithm.put("id", id); + ruleAlgorithm.put("dynamicRuleAlgorithmField1", dynamicRuleAlgorithmField1); + ruleAlgorithm.put("dynamicRuleAlgorithmCombo", dynamicRuleAlgorithmCombo); + ruleAlgorithm.put("dynamicRuleAlgorithmField2", dynamicRuleAlgorithmField2); + + ruleAlgorithmChoices.add(ruleAlgorithm); } - } else { + } + + policyData.setRuleAlgorithmschoices(ruleAlgorithmChoices); + + ArrayList<Object> attributeList = new ArrayList<>(); + if (matching != null) { + for (Map.Entry<String, String> entry : matching.entrySet()) { + LinkedHashMap<String, String> attributeMap = new LinkedHashMap<>(); + String key = entry.getKey(); + String value = entry.getValue(); + attributeMap.put("key", key); + attributeMap.put("value", value); + attributeList.add(attributeMap); + } + } - policyData.setPolicyType("Config"); - policyData.setConfigPolicyType(parameters.getPolicyConfigType().toString()); - - // Config Specific - policyData.setConfigBodyData(parameters.getConfigBody()); // Base - policyData.setConfigType((parameters.getConfigBodyType() != null) - ? parameters.getConfigBodyType().toString().toUpperCase() - : null); // Base - - if ("FW".equalsIgnoreCase(parameters.getPolicyConfigType().toString())) { - - policyData.setConfigPolicyType("Firewall Config"); - - // get values and attributes from the JsonObject - if (json != null) { - if (json.get("securityZoneId") != null) { - String securityZone = json.get("securityZoneId").toString().replace("\"", ""); - policyData.setSecurityZone(securityZone); - } - if (json.get(CONFIG_NAME) != null) { - String configName = json.get(CONFIG_NAME).toString().replace("\"", ""); - policyData.setConfigName(configName); - } - } - - } else if ("MS".equals(parameters.getPolicyConfigType().toString())) { - - policyData.setConfigPolicyType("Micro Service"); - - // get values and attributes from the JsonObject - if (json != null) { - if (json.containsKey(CONTENT)) { - String content = json.get(CONTENT).toString(); - ObjectMapper mapper = new ObjectMapper(); - JsonNode policyJson = null; - try { - policyJson = mapper.readTree(content); - } catch (IOException e) { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + INVALIDJSON - + parameters.getConfigBody(); - LOGGER.error(message, e); - return null; - } - policyData.setPolicyJSON(policyJson); - } - if (json.containsKey(SERVICE)) { - String serviceType = json.get(SERVICE).toString().replace("\"", ""); - policyData.setServiceType(serviceType); - } - if (json.containsKey("uuid")) { - String uuid = json.get("uuid").toString().replace("\"", ""); - policyData.setUuid(uuid); - } - if (json.containsKey(LOCATION)) { - String msLocation = json.get(LOCATION).toString().replace("\"", ""); - policyData.setLocation(msLocation); - } - if (json.containsKey(CONFIG_NAME)) { - String configName = json.get(CONFIG_NAME).toString().replace("\"", ""); - policyData.setConfigName(configName); - } - if (json.containsKey(PRIORITY)) { - String priority = json.get(PRIORITY).toString().replace("\"", ""); - policyData.setPriority(priority); - } - if (json.containsKey(VERSION)) { - String version = json.get(VERSION).toString().replace("\"", ""); - policyData.setVersion(version); - } - if (json.containsKey(POLICYSCOPE)) { - String policyScope = json.get(POLICYSCOPE).toString().replace("\"", ""); - policyData.setPolicyScope(policyScope); - } - if (json.containsKey(RISKTYPE)) { - String riskType = json.get(RISKTYPE).toString().replace("\"", ""); - policyData.setRiskType(riskType); - } - if (json.containsKey(RISKLEVEL)) { - String riskLevel = json.get(RISKLEVEL).toString().replace("\"", ""); - policyData.setRiskLevel(riskLevel); - } - if (json.containsKey(GUARD)) { - String guard = json.get(GUARD).toString().replace("\"", ""); - policyData.setGuard(guard); - } - } else { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + INVALIDJSON + parameters.getConfigBody(); - LOGGER.error(message); - return null; - } - - } else if ("Optimization".equals(parameters.getPolicyConfigType().toString())) { - - policyData.setConfigPolicyType("Optimization"); - - // get values and attributes from the JsonObject - if (json != null) { - if (json.containsKey(CONTENT)) { - String content = json.get(CONTENT).toString(); - ObjectMapper mapper = new ObjectMapper(); - JsonNode policyJson = null; - try { - policyJson = mapper.readTree(content); - } catch (IOException e) { - String message = XACMLErrorConstants.ERROR_DATA_ISSUE + INVALIDJSON - + parameters.getConfigBody(); - LOGGER.error(message, e); - return null; - } - policyData.setPolicyJSON(policyJson); - } - if (json.containsKey(SERVICE)) { - String serviceType = json.get(SERVICE).toString().replace("\"", ""); - policyData.setServiceType(serviceType); - } - if (json.containsKey("uuid")) { - String uuid = json.get("uuid").toString().replace("\"", ""); - policyData.setUuid(uuid); - } - if (json.containsKey(LOCATION)) { - String msLocation = json.get(LOCATION).toString().replace("\"", ""); - policyData.setLocation(msLocation); - } - if (json.containsKey(CONFIG_NAME)) { - String configName = json.get(CONFIG_NAME).toString().replace("\"", ""); - policyData.setConfigName(configName); - } - if (json.containsKey(PRIORITY)) { - String priority = json.get(PRIORITY).toString().replace("\"", ""); - policyData.setPriority(priority); - } - if (json.containsKey(VERSION)) { - String version = json.get(VERSION).toString().replace("\"", ""); - policyData.setVersion(version); - } - if (json.containsKey(POLICYSCOPE)) { - String policyScope = json.get(POLICYSCOPE).toString().replace("\"", ""); - policyData.setPolicyScope(policyScope); - } - if (json.containsKey(RISKTYPE)) { - String riskType = json.get(RISKTYPE).toString().replace("\"", ""); - policyData.setRiskType(riskType); - } - if (json.containsKey(RISKLEVEL)) { - String riskLevel = json.get(RISKLEVEL).toString().replace("\"", ""); - policyData.setRiskLevel(riskLevel); - } - if (json.containsKey(GUARD)) { - String guard = json.get(GUARD).toString().replace("\"", ""); - policyData.setGuard(guard); - } - } - - } else if ("Fault".equals(parameters.getPolicyConfigType().toString())) { - - policyData.setConfigPolicyType("ClosedLoop_Fault"); - - if (json != null) { - policyData.setJsonBody(json.toString()); - if (json.get(ONAPNAME) != null) { - String onapName = json.get(ONAPNAME).toString().replace("\"", ""); - policyData.setOnapName(onapName); - } - } - - } else if ("PM".equals(parameters.getPolicyConfigType().toString())) { - - policyData.setConfigPolicyType("ClosedLoop_PM"); - - if (json != null) { - policyData.setJsonBody(json.toString()); - if (json.get(ONAPNAME) != null) { - String onapName = json.get(ONAPNAME).toString().replace("\"", ""); - policyData.setOnapName(onapName); - } - if (json.get(SERVICETYPE_POLICY_NAME) != null) { - String serviceType = json.get(SERVICETYPE_POLICY_NAME).toString().replace("\"", ""); - LinkedHashMap<String, String> serviceTypePolicyName = new LinkedHashMap<>(); - serviceTypePolicyName.put(SERVICETYPE_POLICY_NAME, serviceType); - policyData.setServiceTypePolicyName(serviceTypePolicyName); - } - } - } else if ("BRMS_Param".equals(parameters.getPolicyConfigType().toString())) { - Map<AttributeType, Map<String, String>> drlRuleAndUiParams = parameters.getAttributes(); - Map<String, String> rule = drlRuleAndUiParams.get(AttributeType.RULE); - policyData.setRuleName(rule.get("templateName")); + policyData.setAttributes(attributeList); + policyData.setActionAttributeValue(parameters.getActionAttribute()); + policyData.setActionPerformer(parameters.getActionPerformer()); + return policyData; + } + + private PolicyRestAdapter getJsonObjectValuesAndAttributes(PolicyParameters parameters, + PolicyRestAdapter policyData, JsonObject json) { + if (json.containsKey(CONTENT)) { + String content = json.get(CONTENT).toString(); + JsonNode policyJson = null; + try { + policyJson = new ObjectMapper().readTree(content); + } catch (IOException e) { + String message = XACMLErrorConstants.ERROR_DATA_ISSUE + INVALIDJSON + parameters.getConfigBody(); + LOGGER.error(message, e); + return null; } + policyData.setPolicyJSON(policyJson); } + // @formatter:off + policyData.setServiceType(getNewOrExistingKeyValue(json, SERVICE, policyData.getServiceType())); + policyData.setUuid(getNewOrExistingKeyValue( json, UUID, policyData.getUuid())); + policyData.setLocation(getNewOrExistingKeyValue( json, LOCATION, policyData.getLocation())); + policyData.setConfigName(getNewOrExistingKeyValue( json, CONFIG_NAME, policyData.getConfigName())); + policyData.setPriority(getNewOrExistingKeyValue( json, PRIORITY, policyData.getPriority())); + policyData.setVersion(getNewOrExistingKeyValue( json, VERSION, policyData.getVersion())); + policyData.setPolicyScope(getNewOrExistingKeyValue(json, POLICYSCOPE, policyData.getPolicyScope())); + policyData.setRiskType(getNewOrExistingKeyValue( json, RISKTYPE, policyData.getRiskType())); + policyData.setRiskLevel(getNewOrExistingKeyValue( json, RISKLEVEL, policyData.getRiskLevel())); + policyData.setGuard(getNewOrExistingKeyValue( json, GUARD, policyData.getGuard())); + // @formatter:on + return policyData; + } + private String getNewOrExistingKeyValue(final JsonObject json, final String key, final String existingValue) { + if (json.containsKey(key)) { + return json.get(key).toString().replace("\"", ""); + } else { + return existingValue; + } + } + + private Map<String, String> getMatchingAttributeValues(PolicyParameters parameters) { + // Get Matching attribute values + Map<AttributeType, Map<String, String>> attributes = parameters.getAttributes(); + + if (attributes != null) { + return attributes.get(AttributeType.MATCHING); + } + return null; } private JsonObject stringToJsonObject(String value) { try (JsonReader jsonReader = Json.createReader(new StringReader(value))) { return jsonReader.readObject(); - } catch (JsonException | IllegalStateException e) { + } catch (JsonException | IllegalStateException jsonHandlingException) { LOGGER.info(XACMLErrorConstants.ERROR_DATA_ISSUE + "Improper JSON format... may or may not cause issues in validating the policy: " + value, - e); - return null; + jsonHandlingException); + throw jsonHandlingException; } } |