diff options
author | 2019-11-03 17:42:09 -0500 | |
---|---|---|
committer | 2019-11-04 08:45:32 -0500 | |
commit | 1bedb591cc68c10c7db916fda8a3f02d67c2314d (patch) | |
tree | 42984cf2c3668f95cf84b721e80d0ed91d79a7e7 /applications/common/src/main/java/org | |
parent | 3c1ae81a9c05ee9bfbb76bb23a2d2d75013d37ae (diff) |
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 <pdragosh@research.att.com>
Diffstat (limited to 'applications/common/src/main/java/org')
-rw-r--r-- | applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java | 61 |
1 files changed, 33 insertions, 28 deletions
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,14 +227,8 @@ public class StdMatchableTranslator extends StdBaseTranslator { protected boolean isMatchable(String propertyName, Collection<ToscaPolicyType> policyTypes) { for (ToscaPolicyType policyType : policyTypes) { for (Entry<String, ToscaProperty> propertiesEntry : policyType.getProperties().entrySet()) { - if (! propertiesEntry.getKey().equals(propertyName) - || propertiesEntry.getValue().getMetadata() == null) { - continue; - } - for (Entry<String, String> entrySet : propertiesEntry.getValue().getMetadata().entrySet()) { - if ("matchable".equals(entrySet.getKey()) && "true".equals(entrySet.getValue())) { - return true; - } + if (checkIsMatchableProperty(propertyName, propertiesEntry)) { + return true; } } } @@ -253,6 +236,28 @@ public class StdMatchableTranslator extends StdBaseTranslator { } /** + * 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<String, ToscaProperty> propertiesEntry) { + if (! propertiesEntry.getKey().equals(propertyName) + || propertiesEntry.getValue().getMetadata() == null) { + return false; + } + for (Entry<String, String> 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. * |