aboutsummaryrefslogtreecommitdiffstats
path: root/applications/optimization/src
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-05-27 15:10:18 -0400
committerJim Hahn <jrh3@att.com>2021-05-27 17:09:00 -0400
commitae9007f3554ba021d76d001ca555a13d49babc8d (patch)
treeddf359c36bfd60f49e55e25f91788c04ec99726d /applications/optimization/src
parentd1fa4ea52b243f60047a0bad5e63e947572b036b (diff)
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 <jrh3@att.com>
Diffstat (limited to 'applications/optimization/src')
-rw-r--r--applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTranslator.java47
1 files changed, 17 insertions, 30 deletions
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<String, Object> 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<Object>) 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<String, Object> 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<Object>) 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<Object>) 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<Object> 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<Object> getPropAsList(Map<String, Object> 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<Object>) raw : Arrays.asList(raw);
}
}