Set all cross references of policy/xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdCombinedPolicyResultsTranslator.java
index aba5e25..64a7db9 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,9 +32,9 @@ import com.att.research.xacml.std.annotations.RequestParser;
 import com.google.common.base.Strings;
 import java.util.Collection;
 import java.util.Map;
+import lombok.NoArgsConstructor;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
@@ -50,16 +50,13 @@ import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@NoArgsConstructor
 public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(StdCombinedPolicyResultsTranslator.class);
 
-    public StdCombinedPolicyResultsTranslator() {
-        super();
-    }
-
     @Override
-    public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+    public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
         //
         // Sanity checks
         //
@@ -72,11 +69,11 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // Get the policy Id
         //
-        String policyId = toscaPolicy.getMetadata().get(POLICY_ID);
+        String policyId = String.valueOf(toscaPolicy.getMetadata().get(POLICY_ID));
         //
         // Set it as the policy ID
         //
-        PolicyType newPolicyType = new PolicyType();
+        var newPolicyType = new PolicyType();
         newPolicyType.setPolicyId(policyId);
         //
         // Optional description
@@ -93,14 +90,14 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // Generate the TargetType
         //
-        TargetType target = this.generateTargetType(policyId, toscaPolicy.getType(), toscaPolicy.getVersion());
+        var target = this.generateTargetType(policyId, toscaPolicy.getType(), toscaPolicy.getVersion());
         newPolicyType.setTarget(target);
         //
         // Now create the Permit Rule
         // No target since the policy has a target
         // With obligations.
         //
-        RuleType rule = new RuleType();
+        var rule = new RuleType();
         rule.setDescription("Default is to PERMIT if the policy matches.");
         rule.setRuleId(policyId + ":rule");
         rule.setEffect(EffectType.PERMIT);
@@ -108,7 +105,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // Now represent the policy as Json
         //
-        StandardCoder coder = new StandardCoder();
+        var coder = new StandardCoder();
         String jsonPolicy;
         try {
             jsonPolicy = coder.encode(toscaPolicy);
@@ -127,17 +124,13 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
     }
 
     @Override
-    public Request convertRequest(DecisionRequest request) {
+    public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
         LOGGER.info("Converting Request {}", request);
         try {
             return RequestParser.parseRequest(StdCombinedPolicyRequest.createInstance(request));
         } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
-            LOGGER.error("Failed to convert DecisionRequest", e);
+            throw new ToscaPolicyConversionException("Failed to parse request", e);
         }
-        //
-        // TODO throw exception
-        //
-        return null;
     }
 
     /**
@@ -162,7 +155,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
      * scanAdvice - not implemented in this class.
      *
      * @param advice Collection of advice objects
-     * @param DecisionResponse DecisionResponse object
+     * @param decisionResponse DecisionResponse object
      */
     @Override
     protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
@@ -183,7 +176,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // Create our OnapObligation which will scan for attributes
         //
-        OnapObligation onapObligation = new OnapObligation(obligation);
+        var onapObligation = new OnapObligation(obligation);
         //
         // Get the attributes we care about
         //
@@ -193,7 +186,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         // Sanity check that we got the attributes we care about. NOTE: This translator
         // ensures that these are set when convertPolicy is called.
         //
-        if (! Strings.isNullOrEmpty(policyId) && policyContent != null) {
+        if (! Strings.isNullOrEmpty(policyId) && !policyContent.isEmpty()) {
             decisionResponse.getPolicies().put(policyId, policyContent);
         } else {
             LOGGER.error("Missing obligation policyId {} or policyContent {}", policyId,
@@ -215,7 +208,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // This is for the Policy Id
         //
-        MatchType matchPolicyId = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
+        var matchPolicyId = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
                 XACML3.ID_FUNCTION_STRING_EQUAL,
                 policyId,
                 XACML3.ID_DATATYPE_STRING,
@@ -224,7 +217,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // This is for the Policy Type
         //
-        MatchType matchPolicyType = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
+        var matchPolicyType = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
                 XACML3.ID_FUNCTION_STRING_EQUAL,
                 policyType,
                 XACML3.ID_DATATYPE_STRING,
@@ -233,7 +226,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // This is for the Policy Type version
         //
-        MatchType matchPolicyTypeVersion = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
+        var matchPolicyTypeVersion = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
                 XACML3.ID_FUNCTION_STRING_EQUAL,
                 policyTypeVersion,
                 XACML3.ID_DATATYPE_STRING,
@@ -242,7 +235,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         //
         // This is our outer AnyOf - which is an OR
         //
-        AnyOfType anyOf = new AnyOfType();
+        var anyOf = new AnyOfType();
         //
         // Create AllOf (AND) of just Policy Id
         //
@@ -259,7 +252,7 @@ public class StdCombinedPolicyResultsTranslator extends StdBaseTranslator {
         // Now we can create the TargetType, add the top-level anyOf (OR),
         // and return the value.
         //
-        TargetType target = new TargetType();
+        var target = new TargetType();
         target.getAnyOf().add(anyOf);
         return target;
     }