From 9ff68b1ed845e94944f99c32eef209eb63c6469d Mon Sep 17 00:00:00 2001 From: HOCKLA Date: Mon, 14 Oct 2019 14:25:29 -0500 Subject: Modified Decision API to accept query parameters and abbreviate Monitor decision results Issue-ID: POLICY-2126 Change-Id: I18d50a8746ede792ec9507ab98125383c5914ba1 Signed-off-by: HOCKLA --- .../monitoring/MonitoringPdpApplication.java | 63 +++++++++++++++++++++- .../monitoring/MonitoringPdpApplicationTest.java | 8 +-- 2 files changed, 66 insertions(+), 5 deletions(-) (limited to 'applications/monitoring') diff --git a/applications/monitoring/src/main/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplication.java b/applications/monitoring/src/main/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplication.java index 1591b919..3c9a4fba 100644 --- a/applications/monitoring/src/main/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplication.java +++ b/applications/monitoring/src/main/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplication.java @@ -22,14 +22,22 @@ package org.onap.policy.xacml.pdp.application.monitoring; +import com.att.research.xacml.api.Request; +import com.att.research.xacml.api.Response; import java.util.ArrayList; import java.util.Arrays; import java.util.List; - +import java.util.Map; +import java.util.Map.Entry; +import org.apache.commons.lang3.tuple.Pair; +import org.onap.policy.models.decisions.concepts.DecisionRequest; +import org.onap.policy.models.decisions.concepts.DecisionResponse; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator; import org.onap.policy.pdp.xacml.application.common.std.StdCombinedPolicyResultsTranslator; import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This is the engine class that manages the instance of the XACML PDP engine. @@ -41,6 +49,7 @@ import org.onap.policy.pdp.xacml.application.common.std.StdXacmlApplicationServi * */ public class MonitoringPdpApplication extends StdXacmlApplicationServiceProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplication.class); private static final String ONAP_MONITORING_BASE_POLICY_TYPE = "onap.Monitoring"; private static final String ONAP_MONITORING_CDAP = "onap.policies.monitoring.cdap.tca.hi.lo.app"; @@ -95,9 +104,61 @@ public class MonitoringPdpApplication extends StdXacmlApplicationServiceProvider || policyTypeId.getName().startsWith(ONAP_MONITORING_DERIVED_POLICY_TYPE)); } + @Override + public Pair makeDecision(DecisionRequest request, + Map requestQueryParams) { + // + // Convert to a XacmlRequest + // + Request xacmlRequest = this.getTranslator().convertRequest(request); + // + // Now get a decision + // + Response xacmlResponse = this.xacmlDecision(xacmlRequest); + // + // Convert to a DecisionResponse + // + DecisionResponse decisionResponse = this.getTranslator().convertResponse(xacmlResponse); + // + // Abbreviate results if needed + // + if (checkAbbreviateResults(requestQueryParams) && decisionResponse.getPolicies() != null + && !decisionResponse.getPolicies().isEmpty()) { + LOGGER.info("Abbreviating decision results {}", decisionResponse); + for (Entry entry : decisionResponse.getPolicies().entrySet()) { + if (entry.getValue() instanceof Map) { + @SuppressWarnings("unchecked") + Map policy = (Map) entry.getValue(); + policy.remove("properties"); + policy.remove("name"); + policy.remove("version"); + } + } + } + return Pair.of(decisionResponse, xacmlResponse); + } + @Override protected ToscaPolicyTranslator getTranslator(String type) { return translator; } + /** + * Checks the query parameters to determine whether the decision results should be abbreviated. + * + * @param queryParams - http request query parameters + */ + private boolean checkAbbreviateResults(Map queryParams) { + if (queryParams != null && !queryParams.isEmpty()) { + // Check if query params contains "abbrev" flag + if (queryParams.containsKey("abbrev")) { + return Arrays.asList(queryParams.get("abbrev")).contains("true"); + } else { + LOGGER.info("Unsupported query param for Monitoring application: {}", queryParams); + return false; + } + } + LOGGER.info("Query parameters empty"); + return false; + } } diff --git a/applications/monitoring/src/test/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplicationTest.java b/applications/monitoring/src/test/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplicationTest.java index a92ba6f8..1cba4967 100644 --- a/applications/monitoring/src/test/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplicationTest.java +++ b/applications/monitoring/src/test/java/org/onap/policy/xacml/pdp/application/monitoring/MonitoringPdpApplicationTest.java @@ -156,7 +156,7 @@ public class MonitoringPdpApplicationTest { // // Ask for a decision // - Pair decision = service.makeDecision(requestSinglePolicy); + Pair decision = service.makeDecision(requestSinglePolicy, null); LOGGER.info("Decision {}", decision); assertThat(decision.getKey()).isNotNull(); @@ -178,7 +178,7 @@ public class MonitoringPdpApplicationTest { // // Ask for a decision // - Pair decision = service.makeDecision(requestSinglePolicy); + Pair decision = service.makeDecision(requestSinglePolicy, null); LOGGER.info("Decision {}", decision); assertThat(decision.getKey()).isNotNull(); @@ -190,7 +190,7 @@ public class MonitoringPdpApplicationTest { // // Ask for a decision based on policy-type // - decision = service.makeDecision(requestPolicyType); + decision = service.makeDecision(requestPolicyType, null); LOGGER.info("Decision {}", decision); assertThat(decision.getKey()).isNotNull(); @@ -209,7 +209,7 @@ public class MonitoringPdpApplicationTest { // // Ask for a decision // - decision = service.makeDecision(requestSinglePolicy); + decision = service.makeDecision(requestSinglePolicy, null); LOGGER.info("Decision {}", decision.getKey()); assertThat(decision.getKey()).isNotNull(); -- cgit 1.2.3-korg