2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.onap.policy.pap.xacml.rest.elk.client;
22 import java.io.FileInputStream;
23 import java.io.InputStream;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.sql.Connection;
28 import java.sql.DriverManager;
29 import java.sql.ResultSet;
30 import java.sql.Statement;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Properties;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
39 import com.google.gson.Gson;
41 import io.searchbox.client.JestClientFactory;
42 import io.searchbox.client.config.HttpClientConfig;
43 import io.searchbox.client.http.JestHttpClient;
44 import io.searchbox.core.Bulk;
45 import io.searchbox.core.Bulk.Builder;
46 import io.searchbox.core.BulkResult;
47 import io.searchbox.core.Index;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
59 * This code will deals with parsing the XACML content on reading from
60 * database(PolicyEntity, ConfigurationDataEntity and ActionBodyEntity tables)
61 * and convert the data into json to do bulk operation on putting to elastic search database.
62 * Which is used to support Elastic Search in Policy Application GUI to search policies.
66 * properties should be configured in policyelk.properties
69 public class ElasticSearchPolicyUpdate {
71 private static final Logger LOGGER = FlexLogger.getLogger(ElasticSearchPolicyUpdate.class);
72 protected final static JestClientFactory jestFactory = new JestClientFactory();
74 public static void main(String[] args) {
77 String databseUrl = null;
78 String userName = null;
79 String password = null;
80 String databaseDriver = null;
82 String propertyFile = System.getProperty("PROPERTY_FILE");
83 Properties config = new Properties();
84 Path file = Paths.get(propertyFile);
85 if(Files.notExists(file)){
86 LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString());
88 if(file.toString().endsWith(".properties")){
90 InputStream in = new FileInputStream(file.toFile());
92 elkURL = config.getProperty("policy.elk.url");
93 databseUrl = config.getProperty("policy.database.url");
94 userName = config.getProperty("policy.database.username");
95 password = config.getProperty("policy.database.password");
96 databaseDriver = config.getProperty("policy.database.driver");
97 if(elkURL == null || databseUrl == null || userName == null || password == null || databaseDriver == null){
98 LOGGER.error("One of the Property is null in policyelk.properties = elkurl:databaseurl:username:password:databasedriver "
99 + elkURL + ":"+ databseUrl + ":"+ userName + ":"+ password + ":"+ databaseDriver + ":");
101 } catch (Exception e) {
102 LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString());
109 HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(elkURL).multiThreaded(true).build();
110 jestFactory.setHttpClientConfig(httpClientConfig);
111 JestHttpClient client = (JestHttpClient) jestFactory.getObject();
113 Connection conn = null;
114 Statement stmt = null;
116 List<Index> listIndex = new ArrayList<Index>();
119 Class.forName(databaseDriver);
120 conn = DriverManager.getConnection(databseUrl, userName, password);
121 stmt = conn.createStatement();
123 String policyEntityQuery = "Select * from PolicyEntity";
124 ResultSet result = stmt.executeQuery(policyEntityQuery);
126 while(result.next()){
127 StringBuilder policyDataString = new StringBuilder("{");
128 String scope = result.getString("scope");
129 String policyName = result.getString("policyName");
130 if(policyName != null){
131 policyDataString.append("\"policyName\":\""+scope+"."+policyName+"\",");
133 String description = result.getString("description");
134 if(description != null){
135 policyDataString.append("\"policyDescription\":\""+description+"\",");
137 Object policyData = result.getString("policydata");
140 policyDataString.append("\"scope\":\""+scope+"\",");
142 String actionbodyid = result.getString("actionbodyid");
143 String configurationdataid = result.getString("configurationdataid");
146 String policyWithScopeName = scope + "." + policyName;
149 if(policyWithScopeName.contains(".Config_")){
150 policyDataString.append("\"policyType\":\"Config\",");
151 if(policyWithScopeName.contains(".Config_Fault_")){
152 _type = "closedloop";
153 policyDataString.append("\"configPolicyType\":\"ClosedLoop_Fault\",");
154 }else if(policyWithScopeName.contains(".Config_PM_")){
155 _type = "closedloop";
156 policyDataString.append("\"configPolicyType\":\"ClosedLoop_PM\",");
159 policyDataString.append("\"configPolicyType\":\"Base\",");
161 }else if(policyWithScopeName.contains(".Action_")){
163 policyDataString.append("\"policyType\":\"Action\",");
164 }else if(policyWithScopeName.contains(".Decision_")){
166 policyDataString.append("\"policyType\":\"Decision\",");
169 if(!"decision".equals(_type)){
170 if(configurationdataid != null){
171 String configEntityQuery = "Select * from ConfigurationDataEntity where configurationDataId = "+configurationdataid+"";
172 Statement configstmt = conn.createStatement();
173 ResultSet configResult = configstmt.executeQuery(configEntityQuery);
174 while(configResult.next()){
175 String configBody = configResult.getString("configbody");
176 String configType = configResult.getString("configtype");
177 if("JSON".equalsIgnoreCase(configType)){
178 policyDataString.append("\"jsonBodyData\":"+configBody+",\"configType\":\""+configType+"\",");
179 }else if("OTHER".equalsIgnoreCase(configType)){
180 if(configBody!=null){
181 configBody= configBody.replaceAll("\"", "");
182 policyDataString.append("\"jsonBodyData\":\""+configBody+"\",\"configType\":\""+configType+"\",");
186 configResult.close();
189 if(actionbodyid != null){
190 String actionEntityQuery = "Select * from ActionBodyEntity where actionBodyId = "+actionbodyid+"";
191 Statement actionstmt = conn.createStatement();
192 ResultSet actionResult = actionstmt.executeQuery(actionEntityQuery);
193 while(actionResult.next()){
194 String actionBody = actionResult.getString("actionbody");
195 policyDataString.append("\"jsonBodyData\":"+actionBody+",");
197 actionResult.close();
201 String _id = policyWithScopeName;
203 policyDataString.append(constructPolicyData(policyData, policyDataString));
205 String dataString = policyDataString.toString();
206 dataString = dataString.substring(0, dataString.length()-1);
207 dataString = dataString.trim().replace(System.getProperty("line.separator"), "") + "}";
208 dataString = dataString.replace("null", "\"\"");
209 dataString = dataString.replaceAll(" ", "").replaceAll("\n", "");
212 Gson gson = new Gson();
213 gson.fromJson(dataString, Object.class);
218 if("config".equals(_type)){
219 listIndex.add(new Index.Builder(dataString).index("policy").type("config").id(_id).build());
220 }else if("closedloop".equals(_type)){
221 listIndex.add(new Index.Builder(dataString).index("policy").type("closedloop").id(_id).build());
222 }else if("action".equals(_type)){
223 listIndex.add(new Index.Builder(dataString).index("policy").type("action").id(_id).build());
224 }else if("decision".equals(_type)){
225 listIndex.add(new Index.Builder(dataString).index("policy").type("decision").id(_id).build());
230 bulk = new Bulk.Builder();
231 for(int i =0; i < listIndex.size(); i++){
232 bulk.addAction(listIndex.get(i));
234 BulkResult searchResult = client.execute(bulk.build());
235 if(searchResult.isSucceeded()){
236 LOGGER.debug("Success");
238 LOGGER.error("Failure");
240 } catch (Exception e) {
241 LOGGER.error("Exception Occured while performing database Operation for Elastic Search Policy Upgrade"+e);
246 } catch (Exception e) {
247 LOGGER.error("Exception Occured while closing the connection"+e);
253 private static String constructPolicyData(Object policyData, StringBuilder policyDataString){
254 if(policyData instanceof PolicyType){
255 PolicyType policy = (PolicyType) policyData;
256 TargetType target = policy.getTarget();
257 if (target != null) {
258 // Under target we have AnyOFType
259 List<AnyOfType> anyOfList = target.getAnyOf();
260 if (anyOfList != null) {
261 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
262 while (iterAnyOf.hasNext()) {
263 AnyOfType anyOf = iterAnyOf.next();
264 // Under AnyOFType we have AllOFType
265 List<AllOfType> allOfList = anyOf.getAllOf();
266 if (allOfList != null) {
267 Iterator<AllOfType> iterAllOf = allOfList.iterator();
268 while (iterAllOf.hasNext()) {
269 AllOfType allOf = iterAllOf.next();
270 // Under AllOFType we have Match
271 List<MatchType> matchList = allOf.getMatch();
272 if (matchList != null) {
273 Iterator<MatchType> iterMatch = matchList.iterator();
274 while (iterMatch.hasNext()) {
275 MatchType match = iterMatch.next();
277 // Under the match we have attribute value and
278 // attributeDesignator. So,finally down to the actual attribute.
280 AttributeValueType attributeValue = match.getAttributeValue();
281 String value = (String) attributeValue.getContent().get(0);
282 AttributeDesignatorType designator = match.getAttributeDesignator();
283 String attributeId = designator.getAttributeId();
284 // First match in the target is OnapName, so set that value.
285 if ("ONAPName".equals(attributeId)) {
286 policyDataString.append("\"onapName\":\""+value+"\",");
288 if ("RiskType".equals(attributeId)){
289 policyDataString.append("\"riskType\":\""+value+"\",");
291 if ("RiskLevel".equals(attributeId)){
292 policyDataString.append("\"riskLevel\":\""+value+"\",");
294 if ("guard".equals(attributeId)){
295 policyDataString.append("\"guard\":\""+value+"\",");
297 if ("ConfigName".equals(attributeId)){
298 policyDataString.append("\"configName\":\""+value+"\",");
308 return policyDataString.toString();