From ae9007f3554ba021d76d001ca555a13d49babc8d Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 27 May 2021 15:10:18 -0400 Subject: Replace validation code with annotations Instead of having code to validate various values, created POJOs to represent the decoded data so that bean validation annotations could be used instead. Didn't see any obvious ways to use annotations in the Optimization code, but did notice a bug (passed role instead of provisions). Extracted a common method which fixed the bug as a side-effect. Issue-ID: POLICY-2418 Change-Id: I9ef589086fc8f7f66810b66405fbf302d7570e5a Signed-off-by: Jim Hahn --- .../OptimizationPdpApplicationTranslator.java | 47 ++++++++-------------- 1 file changed, 17 insertions(+), 30 deletions(-) (limited to 'applications/optimization/src/main') diff --git a/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java b/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java index 068245ff..e1fe2dcd 100644 --- a/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java +++ b/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java @@ -147,22 +147,13 @@ public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator } - @SuppressWarnings("unchecked") private static PolicyType addSubscriberNameIntoTarget(PolicyType policy, Map subscriberProperties) throws ToscaPolicyConversionException { // - // Find the subscriber names - // - Object subscriberNames = subscriberProperties.get("subscriberName"); - if (subscriberNames == null) { - throw new ToscaPolicyConversionException("Missing subscriberName property"); - } - // // Iterate through all the subscriber names // var anyOf = new AnyOfType(); - for (Object subscriberName : subscriberNames instanceof Collection ? (List) subscriberNames : - Arrays.asList(subscriberNames)) { + for (Object subscriberName : getPropAsList(subscriberProperties, "subscriberName")) { var match = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator( XACML3.ID_FUNCTION_STRING_EQUAL, @@ -183,17 +174,9 @@ public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator return policy; } - @SuppressWarnings("unchecked") private static AdviceExpressionsType generateSubscriberAdvice(Map subscriberProperties) throws ToscaPolicyConversionException { // - // Get the subscriber role - // - Object role = subscriberProperties.get(FIELD_SUBSCRIBER_ROLE); - if (role == null || StringUtils.isBlank(role.toString())) { - throw new ToscaPolicyConversionException("Missing subscriberRole"); - } - // // Create our subscriber advice expression // var adviceExpression = new AdviceExpressionType(); @@ -205,18 +188,14 @@ public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator generateSubscriberAdviceAttributes( adviceExpression, ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_ROLE, - role instanceof Collection ? (List) role : Arrays.asList(role)); + getPropAsList(subscriberProperties, FIELD_SUBSCRIBER_ROLE)); // // Get the provision status // - Object provision = subscriberProperties.get(FIELD_PROV_STATUS); - if (provision == null || StringUtils.isBlank(provision.toString())) { - throw new ToscaPolicyConversionException("Missing provStatus"); - } - adviceExpression = generateSubscriberAdviceAttributes( + generateSubscriberAdviceAttributes( adviceExpression, ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_STATUS, - role instanceof Collection ? (List) provision : Arrays.asList(role)); + getPropAsList(subscriberProperties, FIELD_PROV_STATUS)); // // Add it to the overall expressions // @@ -228,7 +207,7 @@ public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator return adviceExpressions; } - private static AdviceExpressionType generateSubscriberAdviceAttributes(AdviceExpressionType adviceExpression, + private static void generateSubscriberAdviceAttributes(AdviceExpressionType adviceExpression, Identifier attributeId, Collection adviceAttribute) { for (Object attribute : adviceAttribute) { var value = new AttributeValueType(); @@ -242,9 +221,17 @@ public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator adviceExpression.getAttributeAssignmentExpression().add(assignment); } - // - // Return for convenience - // - return adviceExpression; + } + + @SuppressWarnings("unchecked") + private static List getPropAsList(Map properties, String fieldName) + throws ToscaPolicyConversionException { + + Object raw = properties.get(fieldName); + if (raw == null || StringUtils.isBlank(raw.toString())) { + throw new ToscaPolicyConversionException("Missing " + fieldName); + } + + return raw instanceof Collection ? (List) raw : Arrays.asList(raw); } } -- cgit 1.2.3-korg