FIX SONAR ISSUE NESTED TRY BLOCKS
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / Policy.java
index 130b2e5..4c837bb 100644 (file)
 
 package org.onap.policy.pap.xacml.rest.components;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.json.Json;
-import javax.json.JsonReader;
-
 import org.apache.commons.io.FilenameUtils;
-import org.json.JSONObject;
 import org.onap.policy.common.logging.eelf.MessageCodes;
 import org.onap.policy.common.logging.eelf.PolicyLogger;
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
@@ -45,6 +39,7 @@ import org.onap.policy.rest.XACMLRestProperties;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
 import org.onap.policy.xacml.util.XACMLPolicyWriter;
 
+import com.att.research.xacml.api.pap.PAPException;
 import com.att.research.xacml.std.IdentifierImpl;
 import com.att.research.xacml.util.XACMLProperties;
 import com.att.research.xacmlatt.pdp.policy.PolicyDef;
@@ -113,7 +108,6 @@ public abstract class Policy {
        public static final String URI_DATATYPE = "http://www.w3.org/2001/XMLSchema#anyURI";
        public static final String RULE_VARIABLE = "var:";
        public static final String EMPTY_STRING = "";
-       private static final String String = null;
 
        protected static String CONFIG_HOME = null;
        protected static String ACTION_HOME = null;
@@ -144,11 +138,11 @@ public abstract class Policy {
         * @return Either the PolicyAdapter.getData() or PolicyAdapter.getPolicyData()
         */
        public abstract Object getCorrectPolicyDataObject();
-       public abstract Map<String, String>  savePolicies() throws Exception;
+       public abstract Map<String, String>  savePolicies() throws PAPException;
 
        //This is the method for preparing the policy for saving.  We have broken it out
        //separately because the fully configured policy is used for multiple things
-       public abstract boolean prepareToSave() throws Exception;
+       public abstract boolean prepareToSave() throws PAPException;
 
 
        // create match for onap and config name
@@ -201,33 +195,6 @@ public abstract class Policy {
                return dynamicMatch;
        }
 
-       //validation for numeric
-       protected boolean isNumeric(String str){
-               for (char c : str.toCharArray()){
-                       if (!Character.isDigit(c)) return false;
-               }
-               return true;
-       }
-
-       // Validation for json.
-       protected static boolean isJSONValid(String data) {
-               JsonReader jsonReader = null;
-               try {
-                       JSONObject j = new JSONObject(data);
-                       InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
-                       jsonReader = Json.createReader(stream);
-                       LOGGER.info("Json Value is: " + jsonReader.read().toString() );
-               } catch (Exception e) {
-                       LOGGER.error("Exception Occured while reading json"+e);
-                       return false;
-               }finally{
-                       if(jsonReader != null){
-                               jsonReader.close();
-                       }
-               }
-               return true;
-       }
-
        //  the Policy Name as Unique One throws error
        @SuppressWarnings("static-access")
        protected Path getNextFilename(Path parent, String policyType, String polcyFileName, Integer version) {
@@ -237,9 +204,9 @@ public abstract class Policy {
                String policyDir = EMPTY_STRING;
                String absolutePath = parent.toString();
                if (absolutePath != null && !absolutePath.equals(EMPTY_STRING)) {
-                       policyDir = absolutePath.substring(absolutePath.lastIndexOf("\\") + 1, absolutePath.length());
+                       policyDir = absolutePath.substring(absolutePath.lastIndexOf('\\') + 1, absolutePath.length());
                        if (policyDir == null || policyDir.equals(EMPTY_STRING)) {
-                               policyDir = absolutePath.substring(absolutePath.lastIndexOf("/") + 1, absolutePath.length());
+                               policyDir = absolutePath.substring(absolutePath.lastIndexOf('/') + 1, absolutePath.length());
                        }
                }
 
@@ -247,10 +214,9 @@ public abstract class Policy {
                if (policyDir != null && !policyDir.equals(EMPTY_STRING)) {
                        fileName = policyType + "_" + String.format(polcyFileName) + "." + version + ".xml";
                } 
-               if (fileName != null) {
-                       newFile = Paths.get(parent.toString(), fileName);
-               }
-               if (Files.notExists(newFile)) {
+                       
+               newFile = Paths.get(parent.toString(), fileName);
+               if (newFile.toFile().exists()) {
                        return newFile;
                }
                return null;
@@ -264,26 +230,28 @@ public abstract class Policy {
                String policyDir = EMPTY_STRING;
                String absolutePath = parentPath.toString();
                if (absolutePath != null && !absolutePath.equals(EMPTY_STRING)) {
-                       policyDir = absolutePath.substring(absolutePath.lastIndexOf("\\") + 1, absolutePath.length());
-                       if (policyDir == null || policyDir.equals(EMPTY_STRING)) {
-                               policyDir = absolutePath.substring(absolutePath.lastIndexOf("/") + 1, absolutePath.length());
+                       policyDir = absolutePath.substring(absolutePath.lastIndexOf('\\') + 1, absolutePath.length());
+                       if (policyDir.equals(EMPTY_STRING)) {
+                               policyDir = absolutePath.substring(absolutePath.lastIndexOf('/') + 1, absolutePath.length());
                        }
                }
 
                String fileName = "default";
-               if (policyDir != null && !policyDir.equals(EMPTY_STRING)) {
-                       if(policyConfigType.equals("ClosedLoop_PM")){
+               if (!policyDir.equals(EMPTY_STRING)) {
+                       if("ClosedLoop_PM".equals(policyConfigType)){
                                fileName = policyType + "_" + "PM" + "_" +java.lang.String.format(policyFileName) + "." +version +".xml";
-                       }else if(policyConfigType.equals("ClosedLoop_Fault")){
+                       }else if("ClosedLoop_Fault".equals(policyConfigType)){
                                fileName = policyType + "_" + "Fault" + "_" +java.lang.String.format(policyFileName) +  "." + version + ".xml";
-                       }else if(policyConfigType.equals("Micro Service")){
+                       }else if("Micro Service".equals(policyConfigType)){
                                fileName = policyType + "_" + "MS" + "_" + java.lang.String.format(policyFileName) + "." + version + ".xml";
+                       }else if("Optimization".equals(policyConfigType)) {
+                               fileName = policyType + "_" + "OOF" + "_" + java.lang.String.format(policyFileName) + "." + version + ".xml";
                        }
                } 
-               if (fileName != null) {
-                       newFile = Paths.get(parentPath.toString(), fileName);
-               }
-               if (Files.notExists(newFile)) {
+
+               newFile = Paths.get(parentPath.toString(), fileName);
+
+               if (newFile.toFile().exists()) {
                        return newFile;
                }
                return null;
@@ -303,9 +271,8 @@ public abstract class Policy {
                        //
                        //Does not need to be XACMLPolicyWriterWithPapNotify since it is already in the PAP
                        //and this transaction is intercepted up stream.
-                       InputStream inputStream = null;
-                       try {
-                               inputStream = XACMLPolicyWriter.getXmlAsInputStream((PolicyType) policyData);
+
+                       try(InputStream inputStream = XACMLPolicyWriter.getXmlAsInputStream((PolicyType) policyData)) {
                                PolicyDef policyDef = DOMPolicyDef.load(inputStream);
                                if (policyDef == null) {
                                        success.put("validation", "PolicyDef Validation Failed");
@@ -315,13 +282,6 @@ public abstract class Policy {
                        } catch (Exception e) {
                                LOGGER.error("PolicyDef Validation failed"+e);
                                success.put("error", "Validation Failed");
-                       }finally{
-                               try {
-                                       if(inputStream != null)
-                                               inputStream.close();
-                               } catch (IOException e) {
-                                       LOGGER.error("Exception Occured while closing the input stream"+e);
-                               }
                        }
                } else {
                        PolicyLogger.error("Unknown data type sent back.");
@@ -350,13 +310,13 @@ public abstract class Policy {
                return actionHome;
        }
 
-       private static void loadWebapps() throws Exception{
+       private static void loadWebapps() throws PAPException{
                if(actionHome == null || configHome == null){
                        Path webappsPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WEBAPPS));
                        //Sanity Check
                        if (webappsPath == null) {
                                PolicyLogger.error("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS);
-                               throw new Exception("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS);
+                               throw new PAPException("Invalid Webapps Path Location property : " + XACMLRestProperties.PROP_PAP_WEBAPPS);
                        }
                        Path webappsPathConfig;
                        Path webappsPathAction;
@@ -367,14 +327,14 @@ public abstract class Policy {
                                webappsPathConfig = Paths.get(webappsPath.toString()+"/Config");
                                webappsPathAction = Paths.get(webappsPath.toString()+"/Action");
                        }
-                       if(Files.notExists(webappsPathConfig)){
+                       if(!webappsPathConfig.toFile().exists()){
                                try {
                                        Files.createDirectories(webappsPathConfig);
                                } catch (IOException e) {
                                        PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "Policy", "Failed to create config directory");
                                }
                        }
-                       if(Files.notExists(webappsPathAction)){
+                       if(!webappsPathAction.toFile().exists()){
                                try {
                                        Files.createDirectories(webappsPathAction);
                                } catch (IOException e) {