Fix sonars in xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdMatchablePolicyRequest.java
index c32eeca..c038f38 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 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.
@@ -47,6 +47,7 @@ import lombok.Setter;
 import lombok.ToString;
 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
+import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -58,6 +59,8 @@ public class StdMatchablePolicyRequest {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchablePolicyRequest.class);
 
+    public static final String POLICY_TYPE_KEY = "policy-type";
+
     @XACMLSubject(includeInResults = true)
     private String onapName;
 
@@ -70,42 +73,37 @@ public class StdMatchablePolicyRequest {
     @XACMLAction()
     private String action;
 
+    protected static DataTypeFactory dataTypeFactory        = null;
+
     public StdMatchablePolicyRequest() {
         super();
     }
 
-    protected static DataTypeFactory dataTypeFactory        = null;
-
     protected static synchronized DataTypeFactory getDataTypeFactory() {
         try {
             if (dataTypeFactory != null) {
                 return dataTypeFactory;
             }
             dataTypeFactory = DataTypeFactory.newInstance();
-            if (dataTypeFactory == null) {
-                LOGGER.error("Could not create data type factory");
-            }
         } catch (FactoryException e) {
-            LOGGER.error("Can't get Data type Factory: {}", e);
+            LOGGER.error("Can't get Data type Factory", e);
         }
         return dataTypeFactory;
     }
 
     /**
-     * Parses the DecisionRequest into a MonitoringRequest.
+     * Parses the DecisionRequest into a XAML request.
      *
      * @param decisionRequest Input DecisionRequest
      * @return Request XACML Request object
-     * @throws DataTypeException DataType exception
-     * @throws IllegalAccessException  Illegal access exception
+     * @throws XacmlApplicationException Exception occurred parsing or creating request
      */
     @SuppressWarnings({"rawtypes", "unchecked"})
-    public static Request createInstance(DecisionRequest decisionRequest) throws IllegalAccessException,
-        DataTypeException {
+    public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
         //
         // Create our request object
         //
-        StdMatchablePolicyRequest request = new StdMatchablePolicyRequest();
+        var request = new StdMatchablePolicyRequest();
         //
         // Add the subject attributes
         //
@@ -120,44 +118,62 @@ public class StdMatchablePolicyRequest {
         // Parse the request - we use the annotations to create a
         // basic XACML request.
         //
-        Request xacmlRequest = RequestParser.parseRequest(request);
+        Request xacmlRequest;
+        try {
+            xacmlRequest = RequestParser.parseRequest(request);
+        } catch (IllegalAccessException | DataTypeException e) {
+            throw new XacmlApplicationException("Could not parse request ", e);
+        }
         //
         // Create an object we can add to
         //
-        StdMutableRequest mutableRequest = new StdMutableRequest(xacmlRequest);
-        StdMutableRequestAttributes resourceAttributes = new StdMutableRequestAttributes();
+        var mutableRequest = new StdMutableRequest(xacmlRequest);
+        var resourceAttributes = new StdMutableRequestAttributes();
         resourceAttributes.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
         //
         // Add the resource attributes
         //
         Map<String, Object> resources = decisionRequest.getResource();
         for (Entry<String, Object> entrySet : resources.entrySet()) {
+            //
+            // Check for special policy-type
+            //
+            String attributeId;
+            if (POLICY_TYPE_KEY.equals(entrySet.getKey())) {
+                attributeId = ToscaDictionary.ID_RESOURCE_POLICY_TYPE.stringValue();
+            } else {
+                attributeId = ToscaDictionary.ID_RESOURCE_MATCHABLE + entrySet.getKey();
+            }
             //
             // Making an assumption that these fields are matchable.
             // Its possible we may have to load the policy type model
             // and use that to validate the fields that are matchable.
             //
-            if (entrySet.getValue() instanceof Collection) {
-                addResources(resourceAttributes, (Collection) entrySet.getValue(), entrySet.getKey());
-            } else {
-                addResources(resourceAttributes, Arrays.asList(entrySet.getValue().toString()), entrySet.getKey());
+            try {
+                if (entrySet.getValue() instanceof Collection) {
+                    addResources(resourceAttributes, (Collection) entrySet.getValue(), attributeId);
+                } else {
+                    addResources(resourceAttributes, Arrays.asList(entrySet.getValue().toString()), attributeId);
+                }
+            } catch (DataTypeException e) {
+                throw new XacmlApplicationException("Failed to add resource ", e);
             }
         }
         mutableRequest.add(resourceAttributes);
         return mutableRequest;
     }
 
-    private static StdMutableRequestAttributes addResources(StdMutableRequestAttributes attributes,
+    protected static StdMutableRequestAttributes addResources(StdMutableRequestAttributes attributes,
             Collection<Object> values, String id) throws DataTypeException {
 
-        DataTypeFactory factory = getDataTypeFactory();
+        var factory = getDataTypeFactory();
         if (factory == null) {
             return null;
         }
         for (Object value : values) {
-            StdMutableAttribute mutableAttribute    = new StdMutableAttribute();
+            var mutableAttribute    = new StdMutableAttribute();
             mutableAttribute.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
-            mutableAttribute.setAttributeId(new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + id));
+            mutableAttribute.setAttributeId(new IdentifierImpl(id));
             mutableAttribute.setIncludeInResults(true);
 
             DataType<?> dataTypeExtended    = factory.getDataType(XACML3.ID_DATATYPE_STRING);
@@ -170,5 +186,4 @@ public class StdMatchablePolicyRequest {
         }
         return attributes;
     }
-
 }