Fixed the Policy API issues and Bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / Policy.java
index 0f8ccfb..536f8e6 100644 (file)
@@ -35,15 +35,12 @@ import java.util.Map;
 import javax.json.Json;
 import javax.json.JsonReader;
 
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
-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 org.apache.commons.io.FilenameUtils;
 import org.json.JSONObject;
 import org.openecomp.policy.common.logging.eelf.MessageCodes;
 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
+import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
+import org.openecomp.policy.common.logging.flexlogger.Logger;
 import org.openecomp.policy.rest.XACMLRestProperties;
 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
 import org.openecomp.policy.xacml.util.XACMLPolicyWriter;
@@ -53,7 +50,15 @@ import com.att.research.xacml.util.XACMLProperties;
 import com.att.research.xacmlatt.pdp.policy.PolicyDef;
 import com.att.research.xacmlatt.pdp.policy.dom.DOMPolicyDef;
 
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+
 public abstract class Policy {
+       
+       private static final Logger LOGGER      = FlexLogger.getLogger(Policy.class);
+       
 
        /**
         * Common Fields
@@ -113,7 +118,7 @@ public abstract class Policy {
        public static String ACTION_HOME = null;
        public static String CONFIG_URL = null;
 
-       protected Map<String, String> performer = new HashMap<String, String>();
+       protected Map<String, String> performer = new HashMap<>();
 
        private static String actionHome = null;
        private static String configHome = null;
@@ -158,7 +163,7 @@ public abstract class Policy {
                try {
                        uri = new URI(key);
                } catch (URISyntaxException e) {
-                       e.printStackTrace();
+                       LOGGER.error("Exception Occured"+e);
                }
                attributeDesignator.setCategory(CATEGORY_ACCESS_SUBJECT);
                attributeDesignator.setDataType(STRING_DATATYPE);
@@ -184,7 +189,7 @@ public abstract class Policy {
                try {
                        dynamicURI = new URI(key);
                } catch (URISyntaxException e) {
-                       e.printStackTrace();// log msg
+                       LOGGER.error("Exception Occured"+e);// log msg
                }
                dynamicAttributeDesignator.setCategory(CATEGORY_RESOURCE);
                dynamicAttributeDesignator.setDataType(dataType);
@@ -205,13 +210,17 @@ public abstract class Policy {
 
        // Validation for json.
        protected static boolean isJSONValid(String data) {
+               JsonReader jsonReader = null;
                try {
                        new JSONObject(data);
                        InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
-                       JsonReader jsonReader = Json.createReader(stream);
+                       jsonReader = Json.createReader(stream);
                        System.out.println("Json Value is: " + jsonReader.read().toString() );
                } catch (Exception e) {
+                       LOGGER.error("Exception Occured while reading json"+e);
                        return false;
+               }finally{
+                       jsonReader.close();
                }
                return true;
        }
@@ -282,7 +291,7 @@ public abstract class Policy {
 
        //create policy once all the validations are completed
        protected Map<String, String> createPolicy(final Path policyPath, final Object policyData) {
-               Map<String, String> success = new HashMap<String, String>(); 
+               Map<String, String> success = new HashMap<>(); 
                //
                // Is the root a PolicySet or Policy?
                //
@@ -303,6 +312,12 @@ public abstract class Policy {
                                }
                        } catch (Exception e) {
                                success.put("error", "Validation Failed");
+                       }finally{
+                               try {
+                                       inputStream.close();
+                               } catch (IOException e) {
+                                       LOGGER.error("Exception Occured while closing the input stream"+e);
+                               }
                        }
                } else {
                        PolicyLogger.error("Unknown data type sent back.");