X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcomponents%2FPolicy.java;h=4c837bb18835f7a4ef088f278adf18e4dc943a81;hb=eff265962c081edb751d5d2ed99dc443cb97f3fb;hp=169e6e14ea43f90ae3789b92df3b513e3fd1fa46;hpb=1bfd18f999cda39a7b27bef8972864aafe59aecb;p=policy%2Fengine.git diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java index 169e6e14e..4c837bb18 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java @@ -20,23 +20,17 @@ 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; @@ -114,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; @@ -202,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 { - 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) { @@ -238,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()); } } @@ -248,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; @@ -265,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; @@ -304,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"); @@ -316,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.");