Update dublin .gitreview
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdMatchableTranslator.java
index 9d3c626..dd44af7 100644 (file)
@@ -34,9 +34,9 @@ import com.att.research.xacml.api.XACML3;
 import com.att.research.xacml.std.annotations.RequestParser;
 import com.google.gson.Gson;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -67,6 +67,7 @@ import org.slf4j.LoggerFactory;
 public class StdMatchableTranslator implements ToscaPolicyTranslator {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchableTranslator.class);
+    private static final String POLICY_ID = "policy-id";
 
     public StdMatchableTranslator() {
         super();
@@ -74,7 +75,7 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
 
     @Override
     public Request convertRequest(DecisionRequest request) {
-        LOGGER.debug("Converting Request {}", request);
+        LOGGER.info("Converting Request {}", request);
         try {
             return RequestParser.parseRequest(StdMatchablePolicyRequest.createInstance(request));
         } catch (IllegalArgumentException | IllegalAccessException | DataTypeException e) {
@@ -88,9 +89,13 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
 
     @Override
     public DecisionResponse convertResponse(Response xacmlResponse) {
-        LOGGER.debug("Converting Response {}", xacmlResponse);
+        LOGGER.info("Converting Response {}", xacmlResponse);
         DecisionResponse decisionResponse = new DecisionResponse();
         //
+        // Setup policies
+        //
+        decisionResponse.setPolicies(new HashMap<>());
+        //
         // Iterate through all the results
         //
         for (Result xacmlResult : xacmlResponse.getResults()) {
@@ -98,21 +103,11 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
             // Check the result
             //
             if (xacmlResult.getDecision() == Decision.PERMIT) {
-                //
-                // Setup policies
-                //
-                decisionResponse.setPolicies(new ArrayList<>());
                 //
                 // Go through obligations
                 //
                 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) {
                 //
@@ -127,9 +122,9 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
 
     protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
         for (Obligation obligation : obligations) {
-            LOGGER.debug("Obligation: {}", obligation);
+            LOGGER.info("Obligation: {}", obligation);
             for (AttributeAssignment assignment : obligation.getAttributeAssignments()) {
-                LOGGER.debug("Attribute Assignment: {}", assignment);
+                LOGGER.info("Attribute Assignment: {}", assignment);
                 //
                 // We care about the content attribute
                 //
@@ -139,8 +134,8 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
                     // The contents are in Json form
                     //
                     Object stringContents = assignment.getAttributeValue().getValue();
-                    if (LOGGER.isDebugEnabled()) {
-                        LOGGER.debug("Policy contents: {}{}", System.lineSeparator(), stringContents);
+                    if (LOGGER.isInfoEnabled()) {
+                        LOGGER.info("Policy contents: {}{}", System.lineSeparator(), stringContents);
                     }
                     //
                     // Let's parse it into a map using Gson
@@ -148,7 +143,16 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
                     Gson gson = new Gson();
                     @SuppressWarnings("unchecked")
                     Map<String, Object> result = gson.fromJson(stringContents.toString() ,Map.class);
-                    decisionResponse.getPolicies().add(result);
+                    //
+                    // Find the metadata section
+                    //
+                    @SuppressWarnings("unchecked")
+                    Map<String, Object> metadata = (Map<String, Object>) result.get("metadata");
+                    if (metadata != null) {
+                        decisionResponse.getPolicies().put(metadata.get(POLICY_ID).toString(), result);
+                    } else {
+                        LOGGER.error("Missing metadata section in policy contained in obligation.");
+                    }
                 }
             }
         }
@@ -160,7 +164,7 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
         //
         // Policy name should be at the root
         //
-        String policyName = toscaPolicy.getMetadata().get("policy-id");
+        String policyName = toscaPolicy.getMetadata().get(POLICY_ID);
         //
         // Set it as the policy ID
         //
@@ -200,8 +204,7 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
         try {
             jsonPolicy = coder.encode(toscaPolicy);
         } catch (CoderException e) {
-            LOGGER.error("Failed to encode policy to json", e);
-            throw new ToscaPolicyConversionException(e);
+            throw new ToscaPolicyConversionException("Failed to encode policy to json", e);
         }
         addObligation(rule, jsonPolicy);
         //
@@ -224,7 +227,7 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
      */
     protected PolicyType fillMetadataSection(PolicyType policy,
             Map<String, String> map) throws ToscaPolicyConversionException {
-        if (! map.containsKey("policy-id")) {
+        if (! map.containsKey(POLICY_ID)) {
             throw new ToscaPolicyConversionException(policy.getPolicyId() + " missing metadata policy-id");
         } else {
             //
@@ -237,7 +240,7 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
             //
             // Add in the Policy Version
             //
-            policy.setVersion(map.get("policy-version").toString());
+            policy.setVersion(map.get("policy-version"));
         }
         return policy;
     }
@@ -266,7 +269,7 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
             // Find policyScope and policyType
             //
             if (entrySet.getKey().equals("policyScope")) {
-                LOGGER.debug("Found policyScope: {}", entrySet.getValue());
+                LOGGER.info("Found policyScope: {}", entrySet.getValue());
                 if (entrySet.getValue() instanceof Collection) {
                     targetType.getAnyOf().add(generateMatches((Collection<Object>) entrySet.getValue(),
                             ToscaDictionary.ID_RESOURCE_POLICY_SCOPE_PROPERTY));
@@ -276,7 +279,7 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
                 }
             }
             if (entrySet.getKey().equals("policyType")) {
-                LOGGER.debug("Found policyType: {}", entrySet.getValue());
+                LOGGER.info("Found policyType: {}", entrySet.getValue());
                 if (entrySet.getValue() instanceof Collection) {
                     targetType.getAnyOf().add(generateMatches((Collection<Object>) entrySet.getValue(),
                             ToscaDictionary.ID_RESOURCE_POLICY_TYPE_PROPERTY));
@@ -317,15 +320,15 @@ public class StdMatchableTranslator implements ToscaPolicyTranslator {
         //
         // Convert the YAML Policy to JSON Object
         //
-        if (LOGGER.isDebugEnabled()) {
-            LOGGER.debug("JSON Optimization Policy {}{}", System.lineSeparator(), jsonPolicy);
+        if (LOGGER.isInfoEnabled()) {
+            LOGGER.info("JSON Optimization Policy {}{}", System.lineSeparator(), jsonPolicy);
         }
         //
         // Create an AttributeValue for it
         //
         AttributeValueType value = new AttributeValueType();
         value.setDataType(ToscaDictionary.ID_OBLIGATION_POLICY_MONITORING_DATATYPE.stringValue());
-        value.getContent().add(jsonPolicy.toString());
+        value.getContent().add(jsonPolicy);
         //
         // Create our AttributeAssignmentExpression where we will
         // store the contents of the policy in JSON format.