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;
25 import io.searchbox.client.JestClientFactory;
26 import io.searchbox.client.config.HttpClientConfig;
27 import io.searchbox.client.http.JestHttpClient;
28 import io.searchbox.core.Bulk;
29 import io.searchbox.core.Bulk.Builder;
30 import io.searchbox.core.BulkResult;
31 import io.searchbox.core.Index;
33 import java.io.ByteArrayInputStream;
34 import java.io.FileInputStream;
35 import java.io.InputStream;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.sql.Connection;
40 import java.sql.DriverManager;
41 import java.sql.PreparedStatement;
42 import java.sql.ResultSet;
43 import java.sql.Statement;
44 import java.util.ArrayList;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Properties;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
57 import org.onap.policy.common.logging.flexlogger.FlexLogger;
58 import org.onap.policy.common.logging.flexlogger.Logger;
59 import org.onap.policy.utils.PeCryptoUtils;
60 import org.onap.policy.xacml.util.XACMLPolicyScanner;
63 * This code will deals with parsing the XACML content on reading from
64 * database(PolicyEntity, ConfigurationDataEntity and ActionBodyEntity tables)
65 * and convert the data into json to do bulk operation on putting to elastic search database.
66 * Which is used to support Elastic Search in Policy Application GUI to search policies.
70 * properties should be configured in policyelk.properties
73 public class ElasticSearchPolicyUpdate {
75 private static final Logger LOGGER = FlexLogger.getLogger(ElasticSearchPolicyUpdate.class);
76 protected static final JestClientFactory jestFactory = new JestClientFactory();
78 public static void main(String[] args) {
81 String databseUrl = null;
82 String userName = null;
84 String databaseDriver = null;
86 String propertyFile = System.getProperty("PROPERTY_FILE");
87 Properties config = new Properties();
88 Path file = Paths.get(propertyFile);
89 if (!file.toFile().exists()) {
90 LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString());
92 if (file.toString().endsWith(".properties")) {
94 InputStream in = new FileInputStream(file.toFile());
96 elkURL = config.getProperty("policy.elk.url");
97 databseUrl = config.getProperty("policy.database.url");
98 userName = config.getProperty("policy.database.username");
99 txt = PeCryptoUtils.decrypt(config.getProperty("policy.database.password"));
100 databaseDriver = config.getProperty("policy.database.driver");
101 if (elkURL == null || databseUrl == null || userName == null || txt == null
102 || databaseDriver == null) {
103 LOGGER.error("please check the elk configuration");
105 } catch (Exception e) {
106 LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString(), e);
113 HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(elkURL).multiThreaded(true).build();
114 jestFactory.setHttpClientConfig(httpClientConfig);
115 JestHttpClient client = (JestHttpClient) jestFactory.getObject();
117 Connection conn = null;
118 Statement stmt = null;
119 ResultSet result = null;
121 List<Index> listIndex = new ArrayList<>();
124 Class.forName(databaseDriver);
125 conn = DriverManager.getConnection(databseUrl, userName, txt);
126 stmt = conn.createStatement();
128 String policyEntityQuery = "Select * from PolicyEntity";
129 result = stmt.executeQuery(policyEntityQuery);
131 while (result.next()) {
132 StringBuilder policyDataString = new StringBuilder("{");
133 String scope = result.getString("scope");
134 String policyName = result.getString("policyName");
135 if (policyName != null) {
136 policyDataString.append("\"policyName\":\"" + scope + "." + policyName + "\",");
138 String description = result.getString("description");
139 if (description != null) {
140 policyDataString.append("\"policyDescription\":\"" + description + "\",");
142 Object policyData = result.getString("policydata");
145 policyDataString.append("\"scope\":\"" + scope + "\",");
147 String actionbodyid = result.getString("actionbodyid");
148 String configurationdataid = result.getString("configurationdataid");
150 String policyWithScopeName = scope + "." + policyName;
153 if (policyWithScopeName.contains(".Config_")) {
154 policyDataString.append("\"policyType\":\"Config\",");
155 if (policyWithScopeName.contains(".Config_Fault_")) {
156 _type = "closedloop";
157 policyDataString.append("\"configPolicyType\":\"ClosedLoop_Fault\",");
158 } else if (policyWithScopeName.contains(".Config_PM_")) {
159 _type = "closedloop";
160 policyDataString.append("\"configPolicyType\":\"ClosedLoop_PM\",");
163 policyDataString.append("\"configPolicyType\":\"Base\",");
165 } else if (policyWithScopeName.contains(".Action_")) {
167 policyDataString.append("\"policyType\":\"Action\",");
168 } else if (policyWithScopeName.contains(".Decision_")) {
170 policyDataString.append("\"policyType\":\"Decision\",");
173 if (!"decision".equals(_type)) {
174 if (configurationdataid != null) {
175 updateConfigData(conn, configurationdataid, policyDataString);
177 if (actionbodyid != null) {
178 updateActionData(conn, actionbodyid, policyDataString);
182 String _id = policyWithScopeName;
184 String dataString = constructPolicyData(policyData, policyDataString);
185 dataString = dataString.substring(0, dataString.length() - 1);
186 dataString = dataString.trim().replace(System.getProperty("line.separator"), "") + "}";
187 dataString = dataString.replace("null", "\"\"");
188 dataString = dataString.replaceAll("\n", "");
191 Gson gson = new Gson();
192 gson.fromJson(dataString, Object.class);
193 } catch (Exception e) {
198 if ("config".equals(_type)) {
199 listIndex.add(new Index.Builder(dataString).index("policy").type("config").id(_id).build());
200 } else if ("closedloop".equals(_type)) {
201 listIndex.add(new Index.Builder(dataString).index("policy").type("closedloop").id(_id).build());
202 } else if ("action".equals(_type)) {
203 listIndex.add(new Index.Builder(dataString).index("policy").type("action").id(_id).build());
204 } else if ("decision".equals(_type)) {
205 listIndex.add(new Index.Builder(dataString).index("policy").type("decision").id(_id).build());
210 bulk = new Bulk.Builder();
211 for (int i = 0; i < listIndex.size(); i++) {
212 bulk.addAction(listIndex.get(i));
214 BulkResult searchResult = client.execute(bulk.build());
215 if (searchResult.isSucceeded()) {
216 LOGGER.debug("Success");
218 LOGGER.error("Failure");
220 } catch (Exception e) {
221 LOGGER.error("Exception Occured while performing database Operation for Elastic Search Policy Upgrade" + e);
223 if (result != null) {
226 } catch (Exception e) {
227 LOGGER.error("Exception Occured while closing the resultset" + e);
233 } catch (Exception e) {
234 LOGGER.error("Exception Occured while closing the statement" + e);
240 } catch (Exception e) {
241 LOGGER.error("Exception Occured while closing the connection" + e);
247 public static String constructPolicyData(Object policyContent, StringBuilder policyDataString) {
248 InputStream stream = new ByteArrayInputStream(policyContent.toString().getBytes(StandardCharsets.UTF_8));
249 Object policyData = XACMLPolicyScanner.readPolicy(stream);
250 if (policyData instanceof PolicyType) {
251 PolicyType policy = (PolicyType) policyData;
252 TargetType target = policy.getTarget();
253 if (target != null) {
254 // Under target we have AnyOFType
255 List<AnyOfType> anyOfList = target.getAnyOf();
256 if (anyOfList != null) {
257 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
258 while (iterAnyOf.hasNext()) {
259 AnyOfType anyOf = iterAnyOf.next();
260 // Under AnyOFType we have AllOFType
261 List<AllOfType> allOfList = anyOf.getAllOf();
262 if (allOfList != null) {
263 Iterator<AllOfType> iterAllOf = allOfList.iterator();
264 while (iterAllOf.hasNext()) {
265 AllOfType allOf = iterAllOf.next();
266 // Under AllOFType we have Match
267 List<MatchType> matchList = allOf.getMatch();
268 if (matchList != null) {
269 Iterator<MatchType> iterMatch = matchList.iterator();
270 while (iterMatch.hasNext()) {
271 MatchType match = iterMatch.next();
273 // Under the match we have attribute value and
274 // attributeDesignator. So,finally down to the actual attribute.
276 AttributeValueType attributeValue = match.getAttributeValue();
277 String value = (String) attributeValue.getContent().get(0);
278 AttributeDesignatorType designator = match.getAttributeDesignator();
279 String attributeId = designator.getAttributeId();
280 // First match in the target is OnapName, so set that value.
281 if ("ONAPName".equals(attributeId)) {
282 policyDataString.append("\"onapName\":\"" + value + "\",");
284 if ("RiskType".equals(attributeId)) {
285 policyDataString.append("\"riskType\":\"" + value + "\",");
287 if ("RiskLevel".equals(attributeId)) {
288 policyDataString.append("\"riskLevel\":\"" + value + "\",");
290 if ("guard".equals(attributeId)) {
291 policyDataString.append("\"guard\":\"" + value + "\",");
293 if ("ConfigName".equals(attributeId)) {
294 policyDataString.append("\"configName\":\"" + value + "\",");
304 return policyDataString.toString();
307 private static void updateConfigData(Connection conn, String configurationdataid, StringBuilder policyDataString)
310 PreparedStatement pstmt = null;
311 ResultSet configResult = null;
313 String configEntityQuery = "Select * from ConfigurationDataEntity where configurationDataId = ?";
315 pstmt = conn.prepareStatement(configEntityQuery);
316 pstmt.setString(1, configurationdataid);
317 configResult = pstmt.executeQuery();
318 while (configResult.next()) {
319 String configBody = configResult.getString("configbody");
320 String configType = configResult.getString("configtype");
321 if (configBody != null) {
322 configBody = configBody.replace("null", "\"\"");
323 configBody = configBody.replace("\"", "\\\"");
325 .append("\"jsonBodyData\":\"" + configBody + "\",\"configType\":\"" + configType + "\",");
328 } catch (Exception e) {
329 LOGGER.error("Exception Occured while updating configData" + e);
332 if (configResult != null) {
334 configResult.close();
335 } catch (Exception e) {
336 LOGGER.error("Exception Occured while closing the ResultSet" + e);
342 } catch (Exception e) {
343 LOGGER.error("Exception Occured while closing the PreparedStatement" + e);
349 private static void updateActionData(Connection conn, String actionbodyid, StringBuilder policyDataString)
352 PreparedStatement pstmt = null;
353 ResultSet actionResult = null;
355 String actionEntityQuery = "Select * from ActionBodyEntity where actionBodyId = ?";
356 pstmt = conn.prepareStatement(actionEntityQuery);
357 pstmt.setString(1, actionbodyid);
358 actionResult = pstmt.executeQuery();
359 while (actionResult.next()) {
360 String actionBody = actionResult.getString("actionbody");
361 actionBody = actionBody.replace("null", "\"\"");
362 actionBody = actionBody.replace("\"", "\\\"");
363 policyDataString.append("\"jsonBodyData\":\"" + actionBody + "\",");
365 } catch (Exception e) {
366 LOGGER.error("Exception Occured while updating actionData" + e);
369 if (actionResult != null) {
371 actionResult.close();
372 } catch (Exception e) {
373 LOGGER.error("Exception Occured while closing the ResultSet" + e);
379 } catch (Exception e) {
380 LOGGER.error("Exception Occured while closing the PreparedStatement" + e);