Added Junits for Policy PAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / elk / client / ElasticSearchPolicyUpdate.java
index dcd44f8..5de0921 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Engine
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
  */
 package org.onap.policy.pap.xacml.rest.elk.client;
 
+import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.InputStream;
-import java.nio.file.Files;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.Statement;
 import java.util.ArrayList;
@@ -35,6 +37,8 @@ import java.util.Properties;
 
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.utils.CryptoUtils;
+import org.onap.policy.xacml.util.XACMLPolicyScanner;
 
 import com.google.gson.Gson;
 
@@ -76,13 +80,13 @@ public class ElasticSearchPolicyUpdate {
                String elkURL = null;
                String databseUrl = null;
                String userName = null;
-               String password = null;
+               String txt = null;
                String databaseDriver = null; 
                
                String propertyFile = System.getProperty("PROPERTY_FILE");
                Properties config = new Properties();
                Path file = Paths.get(propertyFile);
-               if(Files.notExists(file)){
+               if(!file.toFile().exists()){
                        LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString());
                }else{
                        if(file.toString().endsWith(".properties")){
@@ -92,14 +96,13 @@ public class ElasticSearchPolicyUpdate {
                                        elkURL = config.getProperty("policy.elk.url");
                                        databseUrl = config.getProperty("policy.database.url");
                                        userName = config.getProperty("policy.database.username");
-                                       password = config.getProperty("policy.database.password");
+                                       txt = CryptoUtils.decryptTxtNoExStr(config.getProperty("policy.database.password"));
                                        databaseDriver = config.getProperty("policy.database.driver");
-                                       if(elkURL == null || databseUrl == null || userName == null || password == null || databaseDriver == null){
-                                               LOGGER.error("One of the Property is null in policyelk.properties = elkurl:databaseurl:username:password:databasedriver  " 
-                                                               + elkURL + ":"+ databseUrl + ":"+ userName + ":"+ password + ":"+ databaseDriver + ":");
+                                       if(elkURL == null || databseUrl == null || userName == null || txt == null || databaseDriver == null){
+                                               LOGGER.error("please check the elk configuration");
                                        }
                                } catch (Exception e) {
-                                       LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString());
+                                       LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString(),e);
                                } 
                        }
                }
@@ -113,11 +116,11 @@ public class ElasticSearchPolicyUpdate {
                Connection conn = null;
                Statement stmt = null;
                
-               List<Index> listIndex = new ArrayList<Index>();
+               List<Index> listIndex = new ArrayList<>();
                
                try {
                        Class.forName(databaseDriver);
-                       conn = DriverManager.getConnection(databseUrl, userName, password);
+                       conn = DriverManager.getConnection(databseUrl, userName, txt);
                        stmt = conn.createStatement();
                        
                        String policyEntityQuery = "Select * from PolicyEntity";
@@ -168,31 +171,34 @@ public class ElasticSearchPolicyUpdate {
                                
                                if(!"decision".equals(_type)){
                                        if(configurationdataid != null){
-                                               String configEntityQuery = "Select * from ConfigurationDataEntity where configurationDataId = "+configurationdataid+"";
-                                               Statement configstmt = conn.createStatement();
-                                               ResultSet configResult = configstmt.executeQuery(configEntityQuery);
+                                               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("JSON".equalsIgnoreCase(configType)){
-                                                               policyDataString.append("\"jsonBodyData\":"+configBody+",\"configType\":\""+configType+"\",");
-                                                       }else if("OTHER".equalsIgnoreCase(configType)){
-                                                               if(configBody!=null){
-                                                                       configBody= configBody.replaceAll("\"", "");
-                                                                       policyDataString.append("\"jsonBodyData\":\""+configBody+"\",\"configType\":\""+configType+"\",");
-                                                               }
+                                                       if(configBody!=null){
+                                                               configBody = configBody.replace("null", "\"\"");
+                                                               configBody= configBody.replace("\"", "\\\"");
+                                                               policyDataString.append("\"jsonBodyData\":\""+configBody+"\",\"configType\":\""+configType+"\",");
                                                        }
                                                }
                                                configResult.close();
                                        }
                                        
                                        if(actionbodyid != null){
-                                               String actionEntityQuery = "Select * from ActionBodyEntity where actionBodyId = "+actionbodyid+"";
-                                               Statement actionstmt = conn.createStatement();
-                                               ResultSet actionResult = actionstmt.executeQuery(actionEntityQuery);
+                                               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");
-                                                       policyDataString.append("\"jsonBodyData\":"+actionBody+",");
+                                                       actionBody = actionBody.replace("null", "\"\"");
+                                                       actionBody = actionBody.replace("\"", "\\\"");
+                                                       policyDataString.append("\"jsonBodyData\":\""+actionBody+"\",");
                                                }
                                                actionResult.close();
                                        }       
@@ -200,18 +206,17 @@ public class ElasticSearchPolicyUpdate {
                                
                                String _id = policyWithScopeName;
                                
-                               policyDataString.append(constructPolicyData(policyData, policyDataString));
-                               
-                               String dataString = policyDataString.toString();
+                               String dataString = constructPolicyData(policyData, policyDataString);
                                dataString = dataString.substring(0, dataString.length()-1);
                                dataString = dataString.trim().replace(System.getProperty("line.separator"), "") + "}";
                                dataString = dataString.replace("null", "\"\"");
-                               dataString = dataString.replaceAll(" ", "").replaceAll("\n", "");
+                               dataString = dataString.replaceAll("\n", "");
                                
                                try{
                                        Gson gson = new Gson();
                                        gson.fromJson(dataString, Object.class);
                                }catch(Exception e){
+                                       LOGGER.error(e);
                                        continue;
                                }
                                
@@ -250,7 +255,9 @@ public class ElasticSearchPolicyUpdate {
                }
        }
        
-       private static String constructPolicyData(Object policyData, StringBuilder policyDataString){
+       public static String constructPolicyData(Object policyContent, StringBuilder policyDataString){
+               InputStream stream = new ByteArrayInputStream(policyContent.toString().getBytes(StandardCharsets.UTF_8));
+               Object policyData = XACMLPolicyScanner.readPolicy(stream);
                if(policyData instanceof PolicyType){
                        PolicyType policy = (PolicyType) policyData;
                        TargetType target = policy.getTarget();