2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.elk.client;
23 import com.google.gson.Gson;
24 import io.searchbox.client.JestClientFactory;
25 import io.searchbox.client.config.HttpClientConfig;
26 import io.searchbox.client.http.JestHttpClient;
27 import io.searchbox.core.Bulk;
28 import io.searchbox.core.Bulk.Builder;
29 import io.searchbox.core.BulkResult;
30 import io.searchbox.core.Index;
31 import java.io.ByteArrayInputStream;
32 import java.io.FileInputStream;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.sql.Connection;
38 import java.sql.DriverManager;
39 import java.sql.PreparedStatement;
40 import java.sql.ResultSet;
41 import java.sql.Statement;
42 import java.util.ArrayList;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Properties;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
53 import org.onap.policy.common.logging.flexlogger.FlexLogger;
54 import org.onap.policy.common.logging.flexlogger.Logger;
55 import org.onap.policy.utils.PeCryptoUtils;
56 import org.onap.policy.xacml.util.XACMLPolicyScanner;
61 * This code will deals with parsing the XACML content on reading from
62 * database(PolicyEntity, ConfigurationDataEntity and ActionBodyEntity tables)
63 * and convert the data into json to do bulk operation on putting to elastic search database.
64 * Which is used to support Elastic Search in Policy Application GUI to search policies.
68 * properties should be configured in policyelk.properties
71 public class ElasticSearchPolicyUpdate {
73 private static final Logger LOGGER = FlexLogger.getLogger(ElasticSearchPolicyUpdate.class);
74 protected static final JestClientFactory jestFactory = new JestClientFactory();
76 public static void main(String[] args) {
79 String databseUrl = null;
80 String userName = null;
82 String databaseDriver = null;
84 String propertyFile = System.getProperty("PROPERTY_FILE");
85 Properties config = new Properties();
86 Path file = Paths.get(propertyFile);
87 if (!file.toFile().exists()) {
88 LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString());
90 if (file.toString().endsWith(".properties")) {
92 InputStream in = new FileInputStream(file.toFile());
94 elkURL = config.getProperty("policy.elk.url");
95 databseUrl = config.getProperty("policy.database.url");
96 userName = config.getProperty("policy.database.username");
97 txt = PeCryptoUtils.decrypt(config.getProperty("policy.database.password"));
98 databaseDriver = config.getProperty("policy.database.driver");
99 if (elkURL == null || databseUrl == null || userName == null || txt == null
100 || databaseDriver == null) {
101 LOGGER.error("please check the elk configuration");
103 } catch (Exception e) {
104 LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString(), e);
111 HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(elkURL).multiThreaded(true).build();
112 jestFactory.setHttpClientConfig(httpClientConfig);
113 JestHttpClient client = (JestHttpClient) jestFactory.getObject();
115 Connection conn = null;
116 Statement stmt = null;
117 ResultSet result = null;
119 List<Index> listIndex = new ArrayList<>();
122 Class.forName(databaseDriver);
123 conn = DriverManager.getConnection(databseUrl, userName, txt);
124 stmt = conn.createStatement();
126 String policyEntityQuery = "Select * from PolicyEntity";
127 result = stmt.executeQuery(policyEntityQuery);
129 while(result.next()){
130 StringBuilder policyDataString = new StringBuilder("{");
131 String scope = result.getString("scope");
132 String policyName = result.getString("policyName");
133 if(policyName != null){
134 policyDataString.append("\"policyName\":\""+scope+"."+policyName+"\",");
136 String description = result.getString("description");
137 if(description != null){
138 policyDataString.append("\"policyDescription\":\""+description+"\",");
140 Object policyData = result.getString("policydata");
143 policyDataString.append("\"scope\":\""+scope+"\",");
145 String actionbodyid = result.getString("actionbodyid");
146 String configurationdataid = result.getString("configurationdataid");
149 String policyWithScopeName = scope + "." + policyName;
152 if(policyWithScopeName.contains(".Config_")){
153 policyDataString.append("\"policyType\":\"Config\",");
154 if(policyWithScopeName.contains(".Config_Fault_")){
155 _type = "closedloop";
156 policyDataString.append("\"configPolicyType\":\"ClosedLoop_Fault\",");
157 }else if(policyWithScopeName.contains(".Config_PM_")){
158 _type = "closedloop";
159 policyDataString.append("\"configPolicyType\":\"ClosedLoop_PM\",");
162 policyDataString.append("\"configPolicyType\":\"Base\",");
164 }else if(policyWithScopeName.contains(".Action_")){
166 policyDataString.append("\"policyType\":\"Action\",");
167 }else if(policyWithScopeName.contains(".Decision_")){
169 policyDataString.append("\"policyType\":\"Decision\",");
172 if(!"decision".equals(_type)){
173 if(configurationdataid != null){
174 updateConfigData(conn, configurationdataid, policyDataString);
176 if(actionbodyid != null){
177 updateActionData(conn, actionbodyid, policyDataString);
181 String _id = policyWithScopeName;
183 String dataString = constructPolicyData(policyData, policyDataString);
184 dataString = dataString.substring(0, dataString.length()-1);
185 dataString = dataString.trim().replace(System.getProperty("line.separator"), "") + "}";
186 dataString = dataString.replace("null", "\"\"");
187 dataString = dataString.replaceAll("\n", "");
190 Gson gson = new Gson();
191 gson.fromJson(dataString, Object.class);
197 if("config".equals(_type)){
198 listIndex.add(new Index.Builder(dataString).index("policy").type("config").id(_id).build());
199 }else if("closedloop".equals(_type)){
200 listIndex.add(new Index.Builder(dataString).index("policy").type("closedloop").id(_id).build());
201 }else if("action".equals(_type)){
202 listIndex.add(new Index.Builder(dataString).index("policy").type("action").id(_id).build());
203 }else if("decision".equals(_type)){
204 listIndex.add(new Index.Builder(dataString).index("policy").type("decision").id(_id).build());
209 bulk = new Bulk.Builder();
210 for(int i =0; i < listIndex.size(); i++){
211 bulk.addAction(listIndex.get(i));
213 BulkResult searchResult = client.execute(bulk.build());
214 if(searchResult.isSucceeded()){
215 LOGGER.debug("Success");
217 LOGGER.error("Failure");
219 } catch (Exception e) {
220 LOGGER.error("Exception Occured while performing database Operation for Elastic Search Policy Upgrade"+e);
225 } catch (Exception e) {
226 LOGGER.error("Exception Occured while closing the resultset"+e);
232 } catch (Exception e) {
233 LOGGER.error("Exception Occured while closing the statement"+e);
239 } catch (Exception e) {
240 LOGGER.error("Exception Occured while closing the connection"+e);
246 public static String constructPolicyData(Object policyContent, StringBuilder policyDataString){
247 InputStream stream = new ByteArrayInputStream(policyContent.toString().getBytes(StandardCharsets.UTF_8));
248 Object policyData = XACMLPolicyScanner.readPolicy(stream);
249 if(policyData instanceof PolicyType){
250 PolicyType policy = (PolicyType) policyData;
251 TargetType target = policy.getTarget();
252 if (target != null) {
253 // Under target we have AnyOFType
254 List<AnyOfType> anyOfList = target.getAnyOf();
255 if (anyOfList != null) {
256 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
257 while (iterAnyOf.hasNext()) {
258 AnyOfType anyOf = iterAnyOf.next();
259 // Under AnyOFType we have AllOFType
260 List<AllOfType> allOfList = anyOf.getAllOf();
261 if (allOfList != null) {
262 Iterator<AllOfType> iterAllOf = allOfList.iterator();
263 while (iterAllOf.hasNext()) {
264 AllOfType allOf = iterAllOf.next();
265 // Under AllOFType we have Match
266 List<MatchType> matchList = allOf.getMatch();
267 if (matchList != null) {
268 Iterator<MatchType> iterMatch = matchList.iterator();
269 while (iterMatch.hasNext()) {
270 MatchType match = iterMatch.next();
272 // Under the match we have attribute value and
273 // attributeDesignator. So,finally down to the actual attribute.
275 AttributeValueType attributeValue = match.getAttributeValue();
276 String value = (String) attributeValue.getContent().get(0);
277 AttributeDesignatorType designator = match.getAttributeDesignator();
278 String attributeId = designator.getAttributeId();
279 // First match in the target is OnapName, so set that value.
280 if ("ONAPName".equals(attributeId)) {
281 policyDataString.append("\"onapName\":\""+value+"\",");
283 if ("RiskType".equals(attributeId)){
284 policyDataString.append("\"riskType\":\""+value+"\",");
286 if ("RiskLevel".equals(attributeId)){
287 policyDataString.append("\"riskLevel\":\""+value+"\",");
289 if ("guard".equals(attributeId)){
290 policyDataString.append("\"guard\":\""+value+"\",");
292 if ("ConfigName".equals(attributeId)){
293 policyDataString.append("\"configName\":\""+value+"\",");
303 return policyDataString.toString();
306 private static void updateConfigData(Connection conn, String configurationdataid, StringBuilder policyDataString) throws Exception {
308 PreparedStatement pstmt = null;
309 ResultSet configResult = null;
311 String configEntityQuery = "Select * from ConfigurationDataEntity where configurationDataId = ?";
313 pstmt = conn.prepareStatement(configEntityQuery);
314 pstmt.setString(1, configurationdataid);
315 configResult = pstmt.executeQuery();
316 while(configResult.next()){
317 String configBody = configResult.getString("configbody");
318 String configType = configResult.getString("configtype");
319 if(configBody!=null){
320 configBody = configBody.replace("null", "\"\"");
321 configBody= configBody.replace("\"", "\\\"");
322 policyDataString.append("\"jsonBodyData\":\""+configBody+"\",\"configType\":\""+configType+"\",");
325 } catch(Exception e) {
326 LOGGER.error("Exception Occured while updating configData"+e);
329 if(configResult != null){
331 configResult.close();
332 } catch (Exception e) {
333 LOGGER.error("Exception Occured while closing the ResultSet"+e);
339 } catch (Exception e) {
340 LOGGER.error("Exception Occured while closing the PreparedStatement"+e);
346 private static void updateActionData(Connection conn, String actionbodyid, StringBuilder policyDataString) throws Exception {
348 PreparedStatement pstmt = null;
349 ResultSet actionResult = null;
351 String actionEntityQuery = "Select * from ActionBodyEntity where actionBodyId = ?";
352 pstmt = conn.prepareStatement(actionEntityQuery);
353 pstmt.setString(1, actionbodyid);
354 actionResult = pstmt.executeQuery();
355 while(actionResult.next()){
356 String actionBody = actionResult.getString("actionbody");
357 actionBody = actionBody.replace("null", "\"\"");
358 actionBody = actionBody.replace("\"", "\\\"");
359 policyDataString.append("\"jsonBodyData\":\""+actionBody+"\",");
361 } catch(Exception e) {
362 LOGGER.error("Exception Occured while updating actionData"+e);
365 if(actionResult != null){
367 actionResult.close();
368 } catch (Exception e) {
369 LOGGER.error("Exception Occured while closing the ResultSet"+e);
375 } catch (Exception e) {
376 LOGGER.error("Exception Occured while closing the PreparedStatement"+e);