Enhancement to use the common CryptoUtils
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / elk / client / ElasticSearchPolicyUpdate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.elk.client;
22
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;
57
58
59
60 /**
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.
65  *
66  *
67  *
68  * properties should be configured in policyelk.properties
69  *
70  */
71 public class ElasticSearchPolicyUpdate {
72
73     private static final Logger LOGGER = FlexLogger.getLogger(ElasticSearchPolicyUpdate.class);
74     protected static final JestClientFactory jestFactory = new JestClientFactory();
75
76     public static void main(String[] args) {
77
78         String elkURL = null;
79         String databseUrl = null;
80         String userName = null;
81         String txt = null;
82         String databaseDriver = null;
83
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());
89         } else {
90             if (file.toString().endsWith(".properties")) {
91                 try {
92                     InputStream in = new FileInputStream(file.toFile());
93                     config.load(in);
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");
102                     }
103                 } catch (Exception e) {
104                     LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString(), e);
105                 }
106             }
107         }
108
109         Builder bulk = null;
110
111         HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(elkURL).multiThreaded(true).build();
112         jestFactory.setHttpClientConfig(httpClientConfig);
113         JestHttpClient client = (JestHttpClient) jestFactory.getObject();
114
115         Connection conn = null;
116         Statement stmt = null;
117         ResultSet result = null;
118
119         List<Index> listIndex = new ArrayList<>();
120
121         try {
122             Class.forName(databaseDriver);
123             conn = DriverManager.getConnection(databseUrl, userName, txt);
124             stmt = conn.createStatement();
125
126             String policyEntityQuery = "Select * from PolicyEntity";
127             result = stmt.executeQuery(policyEntityQuery);
128
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+"\",");
135                 }
136                 String description = result.getString("description");
137                 if(description != null){
138                     policyDataString.append("\"policyDescription\":\""+description+"\",");
139                 }
140                 Object policyData = result.getString("policydata");
141
142                 if(scope != null){
143                     policyDataString.append("\"scope\":\""+scope+"\",");
144                 }
145                 String actionbodyid = result.getString("actionbodyid");
146                 String configurationdataid = result.getString("configurationdataid");
147
148
149                 String policyWithScopeName = scope + "." + policyName;
150                 String _type = null;
151
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\",");
160                     }else{
161                         _type = "config";
162                         policyDataString.append("\"configPolicyType\":\"Base\",");
163                     }
164                 }else if(policyWithScopeName.contains(".Action_")){
165                     _type = "action";
166                     policyDataString.append("\"policyType\":\"Action\",");
167                 }else if(policyWithScopeName.contains(".Decision_")){
168                     _type = "decision";
169                     policyDataString.append("\"policyType\":\"Decision\",");
170                 }
171
172                 if(!"decision".equals(_type)){
173                     if(configurationdataid != null){
174                         updateConfigData(conn, configurationdataid, policyDataString);
175                     }
176                     if(actionbodyid != null){
177                         updateActionData(conn, actionbodyid, policyDataString);
178                     }
179                 }
180
181                 String _id = policyWithScopeName;
182
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", "");
188
189                 try{
190                     Gson gson = new Gson();
191                     gson.fromJson(dataString, Object.class);
192                 }catch(Exception e){
193                     LOGGER.error(e);
194                     continue;
195                 }
196
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());
205                 }
206             }
207
208             result.close();
209             bulk = new Bulk.Builder();
210             for(int i =0; i < listIndex.size(); i++){
211                 bulk.addAction(listIndex.get(i));
212             }
213             BulkResult searchResult = client.execute(bulk.build());
214             if(searchResult.isSucceeded()){
215                 LOGGER.debug("Success");
216             }else{
217                 LOGGER.error("Failure");
218             }
219         } catch (Exception e) {
220             LOGGER.error("Exception Occured while performing database Operation for Elastic Search Policy Upgrade"+e);
221         }finally{
222                 if(result != null){
223                     try {
224                         result.close();
225                     } catch (Exception e) {
226                         LOGGER.error("Exception Occured while closing the resultset"+e);
227                     }
228                 }
229                 if(stmt != null){
230                     try {
231                         stmt.close();
232                     } catch (Exception e) {
233                         LOGGER.error("Exception Occured while closing the statement"+e);
234                     }
235                 }
236             if(conn != null){
237                 try {
238                     conn.close();
239                 } catch (Exception e) {
240                     LOGGER.error("Exception Occured while closing the connection"+e);
241                 }
242             }
243         }
244     }
245
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();
271                                         //
272                                         // Under the match we have attribute value and
273                                         // attributeDesignator. So,finally down to the actual attribute.
274                                         //
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+"\",");
282                                         }
283                                         if ("RiskType".equals(attributeId)){
284                                             policyDataString.append("\"riskType\":\""+value+"\",");
285                                         }
286                                         if ("RiskLevel".equals(attributeId)){
287                                             policyDataString.append("\"riskLevel\":\""+value+"\",");
288                                         }
289                                         if ("guard".equals(attributeId)){
290                                             policyDataString.append("\"guard\":\""+value+"\",");
291                                         }
292                                         if ("ConfigName".equals(attributeId)){
293                                             policyDataString.append("\"configName\":\""+value+"\",");
294                                         }
295                                     }
296                                 }
297                             }
298                         }
299                     }
300                 }
301             }
302         }
303         return policyDataString.toString();
304     }
305
306     private static void updateConfigData(Connection conn, String configurationdataid, StringBuilder policyDataString) throws Exception {
307
308             PreparedStatement pstmt = null;
309             ResultSet configResult = null;
310             try {
311                 String configEntityQuery = "Select * from ConfigurationDataEntity where configurationDataId = ?";
312                 pstmt = null;
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+"\",");
323                     }
324                 }
325             } catch(Exception e) {
326                 LOGGER.error("Exception Occured while updating configData"+e);
327                 throw(e);
328             } finally {
329                 if(configResult != null){
330                     try {
331                         configResult.close();
332                     } catch (Exception e) {
333                         LOGGER.error("Exception Occured while closing the ResultSet"+e);
334                     }
335                 }
336                 if(pstmt != null){
337                     try {
338                         pstmt.close();
339                     } catch (Exception e) {
340                         LOGGER.error("Exception Occured while closing the PreparedStatement"+e);
341                     }
342                 }
343            }
344     }
345
346     private static void updateActionData(Connection conn, String actionbodyid, StringBuilder policyDataString) throws Exception {
347
348             PreparedStatement pstmt = null;
349             ResultSet actionResult = null;
350             try {
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+"\",");
360                 }
361             } catch(Exception e) {
362                 LOGGER.error("Exception Occured while updating actionData"+e);
363                 throw(e);
364             } finally {
365                 if(actionResult != null){
366                     try {
367                         actionResult.close();
368                     } catch (Exception e) {
369                         LOGGER.error("Exception Occured while closing the ResultSet"+e);
370                     }
371                 }
372                 if(pstmt != null){
373                     try {
374                         pstmt.close();
375                     } catch (Exception e) {
376                         LOGGER.error("Exception Occured while closing the PreparedStatement"+e);
377                     }
378                 }
379            }
380     }
381 }