From 24446884306a87a1d22fb8d0b28efbf41201a87c Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Mon, 19 Jul 2021 14:37:41 -0400 Subject: Include returned attributes in Decision Adds new method to return attributes in a decision, which can be configurable. By default it is turned off which ensures that the current functionality is maintained for our client applications. Enhanced the JUnits for the applications to ensure that no extra information is passed, thus maintaining backward compatibility. Issue-ID: POLICY-2865 Change-Id: Ia533e5462c0cb475cb1f72f34e95f128d6c52678 Signed-off-by: Pamela Dragosh --- .../application/common/std/StdBaseTranslator.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'applications/common/src/main/java/org') diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java index 61d28f58..58bdafa2 100644 --- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java +++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java @@ -23,6 +23,8 @@ package org.onap.policy.pdp.xacml.application.common.std; import com.att.research.xacml.api.Advice; +import com.att.research.xacml.api.Attribute; +import com.att.research.xacml.api.AttributeCategory; import com.att.research.xacml.api.Decision; import com.att.research.xacml.api.Obligation; import com.att.research.xacml.api.Request; @@ -32,6 +34,8 @@ import com.att.research.xacml.api.XACML3; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import lombok.Getter; +import lombok.Setter; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType; @@ -59,6 +63,14 @@ public abstract class StdBaseTranslator implements ToscaPolicyTranslator { private static final Logger LOGGER = LoggerFactory.getLogger(StdBaseTranslator.class); private static final ObjectFactory factory = new ObjectFactory(); + @Getter + @Setter + protected boolean booleanReturnAttributes = false; + + @Getter + @Setter + protected boolean booleanReturnSingleValueAttributesAsCollection = false; + public static final String POLICY_ID = "policy-id"; public static final String POLICY_VERSION = "policy-version"; @@ -103,6 +115,12 @@ public abstract class StdBaseTranslator implements ToscaPolicyTranslator { decisionResponse.setStatus("error"); decisionResponse.setMessage(xacmlResult.getStatus().getStatusMessage()); } + // + // Add attributes + // + if (booleanReturnAttributes) { + scanAttributes(xacmlResult.getAttributes(), decisionResponse); + } } return decisionResponse; @@ -128,6 +146,38 @@ public abstract class StdBaseTranslator implements ToscaPolicyTranslator { */ protected abstract void scanAdvice(Collection advice, DecisionResponse decisionResponse); + /** + * scanAttributes - scans the attributes that are returned in the XACML Decision and puts them into the + * DecisionResponse object. + * + * @param attributeCategories Collection of AttributeCategory objects + * @param decisionResponse DecisionResponse object used to store any attributes + */ + protected void scanAttributes(Collection attributeCategories, + DecisionResponse decisionResponse) { + var returnedAttributes = new HashMap(); + for (AttributeCategory attributeCategory : attributeCategories) { + var mapCategory = new HashMap(); + for (Attribute attribute : attributeCategory.getAttributes()) { + // + // Most attributes have a single value, thus the collection is not necessary to + // return. However, we will allow this to be configurable. + // + if (! booleanReturnSingleValueAttributesAsCollection && attribute.getValues().size() == 1) { + var iterator = attribute.getValues().iterator(); + var value = iterator.next(); + mapCategory.put(attribute.getAttributeId().stringValue(), value.getValue().toString()); + } else { + mapCategory.put(attribute.getAttributeId().stringValue(), attribute.getValues()); + } + } + returnedAttributes.put(attributeCategory.getCategory().stringValue(), mapCategory); + } + if (! returnedAttributes.isEmpty()) { + decisionResponse.setAttributes(returnedAttributes); + } + } + /** * From the TOSCA metadata section, pull in values that are needed into the XACML policy. * -- cgit 1.2.3-korg