From 1bedb591cc68c10c7db916fda8a3f02d67c2314d Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Sun, 3 Nov 2019 17:42:09 -0500 Subject: More examples of optimization policies and cleanup Fixed a couple of sonar issues - log exception and do not nest more 3 if-else-try. Cleaned up the JUnit test to make debugging a bit easier. Added more examples for testing optimization tests. Moved the target back into the Policy and kept the Condition on the Rule. Works exactly the same, just a bit cleaner and one less rule to deal with. Issue-ID: POLICY-2066 Change-Id: Ife28dc2ce959dcf1fb8ca72061ebc5dca862a7f4 Signed-off-by: Pamela Dragosh --- .../common/std/StdMatchableTranslator.java | 61 ++++++++++++---------- 1 file changed, 33 insertions(+), 28 deletions(-) (limited to 'applications/common/src') diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java index 7b47ad14..66770e91 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java @@ -140,9 +140,10 @@ public class StdMatchableTranslator extends StdBaseTranslator { // newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_FIRST_APPLICABLE.stringValue()); // - // Generate the TargetType + // Generate the TargetType - the policy should not be evaluated + // unless all the matchable properties it cares about are matched. // - newPolicyType.setTarget(new TargetType()); + newPolicyType.setTarget(generateTargetType(toscaPolicy.getProperties(), toscaPolicyTypes)); // // Now represent the policy as Json // @@ -158,30 +159,18 @@ public class StdMatchableTranslator extends StdBaseTranslator { // addObligation(newPolicyType, jsonPolicy); // - // Now create the Permit Rule for all the - // matchable properties. + // Now create the Permit Rule. // RuleType rule = new RuleType(); rule.setDescription("Default is to PERMIT if the policy matches."); rule.setRuleId(policyName + ":rule"); rule.setEffect(EffectType.PERMIT); - rule.setTarget(generateTargetType(toscaPolicy.getProperties(), toscaPolicyTypes)); - rule.setCondition(generateConditionForPolicyType(toscaPolicy.getType())); - // - // Add the rule to the policy - // - newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule); + rule.setTarget(new TargetType()); // - // If a Decision is for a specific policy-type, make sure - // there is a rule for it. + // The rule contains the Condition which adds logic for + // optional policy-type filtering. // - rule = new RuleType(); - rule.setDescription("Match on policy-type " + toscaPolicy.getType()); - rule.setRuleId(policyName + ":rule:policy-type"); - rule.setEffect(EffectType.PERMIT); - TargetType target = new TargetType(); - target.getAnyOf().add(this.generateAnyOfForPolicyType(toscaPolicy.getType())); - rule.setTarget(target); + rule.setCondition(generateConditionForPolicyType(toscaPolicy.getType())); // // Add the rule to the policy // @@ -219,7 +208,7 @@ public class StdMatchableTranslator extends StdBaseTranslator { // Find matchable properties // if (isMatchable(entrySet.getKey(), policyTypes)) { - LOGGER.info("Found matchable property {}", entrySet.getValue()); + LOGGER.info("Found matchable property {}", entrySet.getKey()); generateMatchable(targetType, entrySet.getKey(), entrySet.getValue()); } } @@ -238,20 +227,36 @@ public class StdMatchableTranslator extends StdBaseTranslator { protected boolean isMatchable(String propertyName, Collection policyTypes) { for (ToscaPolicyType policyType : policyTypes) { for (Entry propertiesEntry : policyType.getProperties().entrySet()) { - if (! propertiesEntry.getKey().equals(propertyName) - || propertiesEntry.getValue().getMetadata() == null) { - continue; - } - for (Entry entrySet : propertiesEntry.getValue().getMetadata().entrySet()) { - if ("matchable".equals(entrySet.getKey()) && "true".equals(entrySet.getValue())) { - return true; - } + if (checkIsMatchableProperty(propertyName, propertiesEntry)) { + return true; } } } return false; } + /** + * checkIsMatchableProperty - checks the policy contents for matchable field. If the metadata doesn't exist, + * then definitely not. If the property doesn't exist, then definitely not. Otherwise need to have a metadata + * section with the matchable property set to true. + * + * @param propertyName String value of property + * @param propertiesEntry Section of the TOSCA Policy Type where properties and metadata sections are held + * @return true if matchable + */ + protected boolean checkIsMatchableProperty(String propertyName, Entry propertiesEntry) { + if (! propertiesEntry.getKey().equals(propertyName) + || propertiesEntry.getValue().getMetadata() == null) { + return false; + } + for (Entry entrySet : propertiesEntry.getValue().getMetadata().entrySet()) { + if ("matchable".equals(entrySet.getKey()) && "true".equals(entrySet.getValue())) { + return true; + } + } + return false; + } + /** * generateMatchable - Given the object, generates list of MatchType objects and add them * to the TargetType object. -- cgit 1.2.3-korg