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%2Felk%2Fclient%2FElasticSearchPolicyUpdate.java;h=dcabefcc8fcf028fb2350c1ac5e0f9f339134096;hb=eef3c224cb6cad4b82a96938ade1e42ca881eea2;hp=dcd44f84ff7be6eede1988637c1ab562f0262fc8;hpb=073cc188efe9abb4c010cf674e34e2cf46ef1c52;p=policy%2Fengine.git 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 dcd44f84f..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 @@ -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. @@ -19,13 +19,15 @@ */ 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); } } } @@ -112,16 +115,17 @@ public class ElasticSearchPolicyUpdate { Connection conn = null; Statement stmt = null; + ResultSet result = null; - List listIndex = new ArrayList(); + List 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"; - ResultSet result = stmt.executeQuery(policyEntityQuery); + result = stmt.executeQuery(policyEntityQuery); while(result.next()){ StringBuilder policyDataString = new StringBuilder("{"); @@ -168,50 +172,26 @@ 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); - 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+"\","); - } - } - } - configResult.close(); + updateConfigData(conn, configurationdataid, policyDataString); } - if(actionbodyid != null){ - String actionEntityQuery = "Select * from ActionBodyEntity where actionBodyId = "+actionbodyid+""; - Statement actionstmt = conn.createStatement(); - ResultSet actionResult = actionstmt.executeQuery(actionEntityQuery); - while(actionResult.next()){ - String actionBody = actionResult.getString("actionbody"); - policyDataString.append("\"jsonBodyData\":"+actionBody+","); - } - actionResult.close(); + updateActionData(conn, actionbodyid, policyDataString); } } 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; } @@ -240,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(); @@ -250,7 +244,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(); @@ -308,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