aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/main/java/org
diff options
context:
space:
mode:
authoreramkve <ram.krishna.verma@ericsson.com>2018-03-16 15:44:07 +0000
committereramkve <ram.krishna.verma@ericsson.com>2018-03-20 15:28:04 +0000
commiteef3c224cb6cad4b82a96938ade1e42ca881eea2 (patch)
tree37fc08a9b229e5634c0ffb983227f9f563ac7766 /ONAP-PAP-REST/src/main/java/org
parent44fd25bcbb86f8600ec88eeeac6bb8312672c470 (diff)
Fix all bugs reported by Sonar in policy/engine
Code fixes for 27 bugs reported by Sonar in policy/engine Change-Id: I96da96b38a2e2954ec549e5cdf6ed3ad1d76722c Issue-ID: POLICY-662 Signed-off-by: eramkve <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'ONAP-PAP-REST/src/main/java/org')
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java27
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java15
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java14
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java26
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java15
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java10
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/Policy.java1
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java30
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/PushPolicyController.java4
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElasticSearchPolicyUpdate.java122
10 files changed, 153 insertions, 111 deletions
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java
index 8e9c4b659..7c05a31c6 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ClosedLoopPolicy.java
@@ -70,27 +70,16 @@ public class ClosedLoopPolicy extends Policy {
//save configuration of the policy based on the policyname
private void saveConfigurations(String policyName, String jsonBody) {
- try {
+
+ if(policyName.endsWith(".xml")){
+ policyName = policyName.replace(".xml", "");
+ }
+ try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator+ policyName +".json")){
String body = jsonBody;
- try {
- try{
- //Remove the trapMaxAge in Verification Signature
- body = body.replace(",\"trapMaxAge\":null", "");
- }catch(Exception e){
- LOGGER.debug("No Trap Max Age in JSON body", e);
- }
- this.policyAdapter.setJsonBody(body);
- } catch (Exception e) {
- LOGGER.error("Exception Occured"+e);
- }
-
- if(policyName.endsWith(".xml")){
- policyName = policyName.replace(".xml", "");
- }
- PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator+ policyName +".json");
+ //Remove the trapMaxAge in Verification Signature
+ body = body.replace(",\"trapMaxAge\":null", "");
+ this.policyAdapter.setJsonBody(body);
out.println(body);
- out.close();
-
} catch (Exception e) {
LOGGER.error("Exception Occured while writing Configuration Data"+e);
}
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java
index adcffe939..48eb784a7 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/ConfigPolicy.java
@@ -80,17 +80,24 @@ public class ConfigPolicy extends Policy {
// Saving the Configurations file at server location for config policy.
protected void saveConfigurations(String policyName) {
+ BufferedWriter bw = null;
try {
String fileName = getConfigFile(policyName);
- FileWriter fw = new FileWriter(CONFIG_HOME + File.separator + fileName);
- BufferedWriter bw = new BufferedWriter(fw);
+ bw = new BufferedWriter(new FileWriter(CONFIG_HOME + File.separator + fileName));
bw.write(configBodyData);
- bw.close();
if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Configuration is succesfully saved");
+ LOGGER.debug("Configuration is succesfully saved");
}
} catch (IOException e) {
LOGGER.error("Exception Occured while writing Configuration Data"+e);
+ } finally {
+ if(bw != null){
+ try {
+ bw.close();
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured while closing the BufferedWriter"+e);
+ }
+ }
}
}
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java
index a6240e0ee..0bac0e066 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateBrmsRawPolicy.java
@@ -72,15 +72,13 @@ public class CreateBrmsRawPolicy extends Policy {
// Saving the Configurations file at server location for CreateBrmsRawPolicy policy.
protected void saveConfigurations(String policyName, String jsonBody) {
- try {
- if (policyName.endsWith(".xml")) {
- policyName = policyName.substring(0,
- policyName.lastIndexOf(".xml"));
- }
- PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".txt");
+
+ if (policyName.endsWith(".xml")) {
+ policyName = policyName.substring(0,
+ policyName.lastIndexOf(".xml"));
+ }
+ try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName + ".txt")){
out.println(jsonBody);
- out.close();
-
} catch (Exception e) {
PolicyLogger.error(MessageCodes.ERROR_PROCESS_FLOW, e, "CreateBrmsRawPolicy", "Exception saving configurations file");
}
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java
index 68cc9eff7..456924ac9 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/CreateClosedLoopPerformanceMetrics.java
@@ -67,23 +67,15 @@ public class CreateClosedLoopPerformanceMetrics extends Policy {
}
//save configuration of the policy based on the policyname
- private void saveConfigurations(String policyName, String jsonBody) {
- try {
- String body = null;
- try {
- body = jsonBody;
- } catch (Exception e) {
- LOGGER.error("Exception Occured"+e);
- }
- if(policyName.endsWith(".xml")){
- policyName = policyName.substring(0, policyName.lastIndexOf(".xml"));
- }
- PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + "."+ policyName +".json");
- out.println(body);
- policyAdapter.setJsonBody(body);
- policyAdapter.setConfigBodyData(body);
- out.close();
-
+ private void saveConfigurations(String policyName, final String jsonBody) {
+
+ if(policyName.endsWith(".xml")){
+ policyName = policyName.substring(0, policyName.lastIndexOf(".xml"));
+ }
+ try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + "."+ policyName +".json")){
+ out.println(jsonBody);
+ policyAdapter.setJsonBody(jsonBody);
+ policyAdapter.setConfigBodyData(jsonBody);
} catch (Exception e) {
LOGGER.error("Exception Occured"+e);
}
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java
index 6e46ab1c0..e747edb9b 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/FirewallConfigPolicy.java
@@ -1265,16 +1265,11 @@ public class FirewallConfigPolicy extends Policy {
}
private JsonObject stringToJson(String jsonString) {
- JsonObject json = null;
- if (jsonString != null) {
- //Read jsonBody to JsonObject
- StringReader in = null;
- in = new StringReader(jsonString);
-
- JsonReader jsonReader = Json.createReader(in);
- json = jsonReader.readObject();
- jsonReader.close();
- }
+ //Read jsonBody to JsonObject
+ StringReader in = new StringReader(jsonString);
+ JsonReader jsonReader = Json.createReader(in);
+ JsonObject json = jsonReader.readObject();
+ jsonReader.close();
return json;
}
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java
index b0e48a7f4..e02121125 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/MicroServiceConfigPolicy.java
@@ -88,13 +88,11 @@ public class MicroServiceConfigPolicy extends Policy {
//save configuration of the policy based on the policyname
private void saveConfigurations(String policyName, String jsonBody) {
- try {
- if(policyName.endsWith(".xml")){
- policyName = policyName.replace(".xml", "");
- }
- PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName +".json");
+ if(policyName.endsWith(".xml")){
+ policyName = policyName.replace(".xml", "");
+ }
+ try (PrintWriter out = new PrintWriter(CONFIG_HOME + File.separator + policyName +".json")){
out.println(jsonBody);
- out.close();
} catch (Exception e) {
LOGGER.error("Exception Occured While writing Configuration data"+e);
}
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 20c76f3be..2196209d8 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
@@ -108,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;
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java
index 71eb1156f..9ac858277 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/PolicyDBDao.java
@@ -479,11 +479,14 @@ public class PolicyDBDao {
HttpURLConnection connection = null;
UUID requestID = UUID.randomUUID();
URL url;
+ String papUrl;
try {
- String papUrl = getPapUrlUserPass()[0];
- if(papUrl == null){
- papUrl = "undefined";
- }
+ String[] papUrlUserPass = getPapUrlUserPass();
+ if(papUrlUserPass == null ){
+ papUrl = "undefined";
+ } else {
+ papUrl = papUrlUserPass[0];
+ }
logger.debug("We are going to try to notify "+o);
//is this our own url?
String ourUrl = o;
@@ -1315,8 +1318,7 @@ public class PolicyDBDao {
private String[] getNameScopeAndVersionFromPdpPolicy(String fileName){
String[] splitByDots = fileName.split("\\.");
if(splitByDots.length < 3){
- //should we throw something
- return null;
+ return null;
}
String policyName = splitByDots[splitByDots.length-3];
String version = splitByDots[splitByDots.length-2];
@@ -1365,10 +1367,7 @@ public class PolicyDBDao {
*/
private static boolean isNullOrEmpty(String... strings){
for(String s : strings){
- if(!(s instanceof String)){
- return true;
- }
- if("".equals(s)){
+ if(s == null || "".equals(s)){
return true;
}
}
@@ -1532,7 +1531,7 @@ public class PolicyDBDao {
}
}
}
- if(transactionTimer instanceof Thread){
+ if(transactionTimer != null){
transactionTimer.interrupt();
}
}
@@ -1563,7 +1562,7 @@ public class PolicyDBDao {
}
}
- if(transactionTimer instanceof Thread){
+ if(transactionTimer != null){
transactionTimer.interrupt();
}
}
@@ -2042,7 +2041,7 @@ public class PolicyDBDao {
}
em.close();
}
- if(transactionTimer instanceof Thread){
+ if(transactionTimer != null){
transactionTimer.interrupt();
}
}
@@ -2544,7 +2543,10 @@ public class PolicyDBDao {
//we need to convert the form of the policy id that is used groups into the form that is used
//for the database. (com.Config_mypol.1.xml) to (Config_mypol.xml)
- String[] policyNameScopeAndVersion = getNameScopeAndVersionFromPdpPolicy(policyID);
+ String[] policyNameScopeAndVersion = getNameScopeAndVersionFromPdpPolicy(policyID);
+ if(policyNameScopeAndVersion == null) {
+ throw new IllegalArgumentException("Invalid input - policyID must contain name, scope and version");
+ }
Query policyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.policyName=:policyName AND p.scope=:scope AND p.deleted=:deleted");
policyQuery.setParameter("policyName", policyNameScopeAndVersion[0]);
policyQuery.setParameter(scope, policyNameScopeAndVersion[1]);
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/PushPolicyController.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/PushPolicyController.java
index 107983562..397904f30 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/PushPolicyController.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/PushPolicyController.java
@@ -161,10 +161,8 @@ public class PushPolicyController {
return;
}
File temp = new File(policyName);
- try {
- BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter(temp))){
bw.write(policyEntity.getPolicyData());
- bw.close();
URI selectedURI = temp.toURI();
// Create the policy Object
selectedPolicy = new StdPDPPolicy(policyName, true, policyID, selectedURI);
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElasticSearchPolicyUpdate.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElasticSearchPolicyUpdate.java
index 5de09215c..dcabefcc8 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElasticSearchPolicyUpdate.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/elk/client/ElasticSearchPolicyUpdate.java
@@ -115,6 +115,7 @@ public class ElasticSearchPolicyUpdate {
Connection conn = null;
Statement stmt = null;
+ ResultSet result = null;
List<Index> listIndex = new ArrayList<>();
@@ -124,7 +125,7 @@ public class ElasticSearchPolicyUpdate {
stmt = conn.createStatement();
String policyEntityQuery = "Select * from PolicyEntity";
- ResultSet result = stmt.executeQuery(policyEntityQuery);
+ result = stmt.executeQuery(policyEntityQuery);
while(result.next()){
StringBuilder policyDataString = new StringBuilder("{");
@@ -171,36 +172,10 @@ public class ElasticSearchPolicyUpdate {
if(!"decision".equals(_type)){
if(configurationdataid != null){
- String configEntityQuery = "Select * from ConfigurationDataEntity where configurationDataId = ?";
- PreparedStatement pstmt = null;
- pstmt = conn.prepareStatement(configEntityQuery);
- pstmt.setString(1, configurationdataid);
- ResultSet configResult = pstmt.executeQuery();
- while(configResult.next()){
- String configBody = configResult.getString("configbody");
- String configType = configResult.getString("configtype");
- if(configBody!=null){
- configBody = configBody.replace("null", "\"\"");
- configBody= configBody.replace("\"", "\\\"");
- policyDataString.append("\"jsonBodyData\":\""+configBody+"\",\"configType\":\""+configType+"\",");
- }
- }
- configResult.close();
+ updateConfigData(conn, configurationdataid, policyDataString);
}
-
if(actionbodyid != null){
- String actionEntityQuery = "Select * from ActionBodyEntity where actionBodyId = ?";
- PreparedStatement pstmt = null;
- pstmt = conn.prepareStatement(actionEntityQuery);
- pstmt.setString(1, actionbodyid);
- ResultSet actionResult = pstmt.executeQuery();
- while(actionResult.next()){
- String actionBody = actionResult.getString("actionbody");
- actionBody = actionBody.replace("null", "\"\"");
- actionBody = actionBody.replace("\"", "\\\"");
- policyDataString.append("\"jsonBodyData\":\""+actionBody+"\",");
- }
- actionResult.close();
+ updateActionData(conn, actionbodyid, policyDataString);
}
}
@@ -245,6 +220,20 @@ public class ElasticSearchPolicyUpdate {
} catch (Exception e) {
LOGGER.error("Exception Occured while performing database Operation for Elastic Search Policy Upgrade"+e);
}finally{
+ if(result != null){
+ try {
+ result.close();
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured while closing the resultset"+e);
+ }
+ }
+ if(stmt != null){
+ try {
+ stmt.close();
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured while closing the statement"+e);
+ }
+ }
if(conn != null){
try {
conn.close();
@@ -315,4 +304,79 @@ public class ElasticSearchPolicyUpdate {
return policyDataString.toString();
}
+ private static void updateConfigData(Connection conn, String configurationdataid, StringBuilder policyDataString) throws Exception {
+
+ PreparedStatement pstmt = null;
+ ResultSet configResult = null;
+ try {
+ String configEntityQuery = "Select * from ConfigurationDataEntity where configurationDataId = ?";
+ pstmt = null;
+ pstmt = conn.prepareStatement(configEntityQuery);
+ pstmt.setString(1, configurationdataid);
+ configResult = pstmt.executeQuery();
+ while(configResult.next()){
+ String configBody = configResult.getString("configbody");
+ String configType = configResult.getString("configtype");
+ if(configBody!=null){
+ configBody = configBody.replace("null", "\"\"");
+ configBody= configBody.replace("\"", "\\\"");
+ policyDataString.append("\"jsonBodyData\":\""+configBody+"\",\"configType\":\""+configType+"\",");
+ }
+ }
+ } catch(Exception e) {
+ LOGGER.error("Exception Occured while updating configData"+e);
+ throw(e);
+ } finally {
+ if(configResult != null){
+ try {
+ configResult.close();
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured while closing the ResultSet"+e);
+ }
+ }
+ if(pstmt != null){
+ try {
+ pstmt.close();
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured while closing the PreparedStatement"+e);
+ }
+ }
+ }
+ }
+
+ private static void updateActionData(Connection conn, String actionbodyid, StringBuilder policyDataString) throws Exception {
+
+ PreparedStatement pstmt = null;
+ ResultSet actionResult = null;
+ try {
+ String actionEntityQuery = "Select * from ActionBodyEntity where actionBodyId = ?";
+ pstmt = conn.prepareStatement(actionEntityQuery);
+ pstmt.setString(1, actionbodyid);
+ actionResult = pstmt.executeQuery();
+ while(actionResult.next()){
+ String actionBody = actionResult.getString("actionbody");
+ actionBody = actionBody.replace("null", "\"\"");
+ actionBody = actionBody.replace("\"", "\\\"");
+ policyDataString.append("\"jsonBodyData\":\""+actionBody+"\",");
+ }
+ } catch(Exception e) {
+ LOGGER.error("Exception Occured while updating actionData"+e);
+ throw(e);
+ } finally {
+ if(actionResult != null){
+ try {
+ actionResult.close();
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured while closing the ResultSet"+e);
+ }
+ }
+ if(pstmt != null){
+ try {
+ pstmt.close();
+ } catch (Exception e) {
+ LOGGER.error("Exception Occured while closing the PreparedStatement"+e);
+ }
+ }
+ }
+ }
} \ No newline at end of file