Adding optimization application finish guard
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdCombinedPolicyResultsTranslator.java
index b39c2e6..1679837 100644 (file)
@@ -37,6 +37,7 @@ import com.google.gson.Gson;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -144,40 +145,55 @@ public class StdCombinedPolicyResultsTranslator implements ToscaPolicyTranslator
                 //
                 // Go through obligations
                 //
-                for (Obligation obligation : xacmlResult.getObligations()) {
-                    LOGGER.debug("Obligation: {}", obligation);
-                    for (AttributeAssignment assignment : obligation.getAttributeAssignments()) {
-                        LOGGER.debug("Attribute Assignment: {}", assignment);
-                        //
-                        // We care about the content attribute
-                        //
-                        if (ToscaDictionary.ID_OBLIGATION_POLICY_MONITORING_CONTENTS
-                                .equals(assignment.getAttributeId())) {
-                            //
-                            // The contents are in Json form
-                            //
-                            Object stringContents = assignment.getAttributeValue().getValue();
-                            if (LOGGER.isDebugEnabled()) {
-                                LOGGER.debug("DCAE contents: {}{}", System.lineSeparator(), stringContents);
-                            }
-                            //
-                            // Let's parse it into a map using Gson
-                            //
-                            Gson gson = new Gson();
-                            @SuppressWarnings("unchecked")
-                            Map<String, Object> result = gson.fromJson(stringContents.toString() ,Map.class);
-                            decisionResponse.getPolicies().add(result);
-                        }
-                    }
-                }
-            } else {
-                decisionResponse.setErrorMessage("A better error message");
+                scanObligations(xacmlResult.getObligations(), decisionResponse);
+            }
+            if (xacmlResult.getDecision() == Decision.NOTAPPLICABLE) {
+                //
+                // There is no policy
+                //
+                decisionResponse.setPolicies(new ArrayList<>());
+            }
+            if (xacmlResult.getDecision() == Decision.DENY
+                    || xacmlResult.getDecision() == Decision.INDETERMINATE) {
+                //
+                // TODO we have to return an ErrorResponse object instead
+                //
+                decisionResponse.setStatus("A better error message");
             }
         }
 
         return decisionResponse;
     }
 
+    protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
+        for (Obligation obligation : obligations) {
+            LOGGER.debug("Obligation: {}", obligation);
+            for (AttributeAssignment assignment : obligation.getAttributeAssignments()) {
+                LOGGER.debug("Attribute Assignment: {}", assignment);
+                //
+                // We care about the content attribute
+                //
+                if (ToscaDictionary.ID_OBLIGATION_POLICY_MONITORING_CONTENTS
+                        .equals(assignment.getAttributeId())) {
+                    //
+                    // The contents are in Json form
+                    //
+                    Object stringContents = assignment.getAttributeValue().getValue();
+                    if (LOGGER.isDebugEnabled()) {
+                        LOGGER.debug("DCAE contents: {}{}", System.lineSeparator(), stringContents);
+                    }
+                    //
+                    // Let's parse it into a map using Gson
+                    //
+                    Gson gson = new Gson();
+                    @SuppressWarnings("unchecked")
+                    Map<String, Object> result = gson.fromJson(stringContents.toString() ,Map.class);
+                    decisionResponse.getPolicies().add(result);
+                }
+            }
+        }
+    }
+
     @SuppressWarnings("unchecked")
     protected PolicyType convertPolicy(Entry<String, Object> entrySet) throws ToscaPolicyConversionException {
         //
@@ -211,9 +227,6 @@ public class StdCombinedPolicyResultsTranslator implements ToscaPolicyTranslator
         //
         // Generate the TargetType
         //
-        //
-        // There should be a metadata section
-        //
         if (! policyDefinition.containsKey("type")) {
             throw new ToscaPolicyConversionException(policyName + " missing type value");
         }