Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / elk / client / ElasticSearchPolicyUpdate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
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 package org.openecomp.policy.pap.xacml.rest.elk.client;
21
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;
35
36 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
37 import org.openecomp.policy.common.logging.flexlogger.Logger;
38
39 import com.google.gson.Gson;
40
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;
55
56
57
58 /**
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.
63  * 
64  * 
65  * 
66  * properties should be configured in policyelk.properties
67  *
68  */
69 public class ElasticSearchPolicyUpdate {
70         
71         private static final Logger LOGGER = FlexLogger.getLogger(ElasticSearchPolicyUpdate.class);
72         protected final static JestClientFactory jestFactory = new JestClientFactory();
73         
74         public static void main(String[] args) {
75                 
76                 String elkURL = null;
77                 String databseUrl = null;
78                 String userName = null;
79                 String password = null;
80                 String databaseDriver = null; 
81                 
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());
87                 }else{
88                         if(file.toString().endsWith(".properties")){
89                                 try {
90                                         InputStream in = new FileInputStream(file.toFile());
91                                         config.load(in);
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 + ":");
100                                         }
101                                 } catch (Exception e) {
102                                         LOGGER.error("Config File doesn't Exist in the specified Path " + file.toString());
103                                 } 
104                         }
105                 }
106
107                 Builder bulk = null;
108                 
109                 HttpClientConfig httpClientConfig = new HttpClientConfig.Builder(elkURL).multiThreaded(true).build();
110                 jestFactory.setHttpClientConfig(httpClientConfig);
111             JestHttpClient client = (JestHttpClient) jestFactory.getObject();
112             
113                 Connection conn = null;
114                 Statement stmt = null;
115                 
116                 List<Index> listIndex = new ArrayList<Index>();
117                 
118                 try {
119                         Class.forName(databaseDriver);
120                         conn = DriverManager.getConnection(databseUrl, userName, password);
121                         stmt = conn.createStatement();
122                         
123                         String policyEntityQuery = "Select * from PolicyEntity";
124                         ResultSet result = stmt.executeQuery(policyEntityQuery);
125                         
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+"\",");
132                                 }
133                                 String description = result.getString("description");
134                                 if(description != null){
135                                         policyDataString.append("\"policyDescription\":\""+description+"\",");
136                                 }
137                                 Object policyData = result.getString("policydata");
138                                 
139                                 if(scope != null){
140                                         policyDataString.append("\"scope\":\""+scope+"\",");
141                                 }
142                                 String actionbodyid = result.getString("actionbodyid");
143                                 String configurationdataid = result.getString("configurationdataid");
144                                 
145                                 
146                                 String policyWithScopeName = scope + "." + policyName;
147                                 String _type = null;
148                                 
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\",");
157                                         }else{
158                                                 _type = "config";
159                                                 policyDataString.append("\"configPolicyType\":\"Base\",");
160                                         }
161                                 }else if(policyWithScopeName.contains(".Action_")){
162                                         _type = "action";
163                                         policyDataString.append("\"policyType\":\"Action\",");
164                                 }else if(policyWithScopeName.contains(".Decision_")){
165                                         _type = "decision";
166                                         policyDataString.append("\"policyType\":\"Decision\",");
167                                 }
168                                 
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+"\",");
183                                                                 }
184                                                         }
185                                                 }
186                                                 configResult.close();
187                                         }
188                                         
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+",");
196                                                 }
197                                                 actionResult.close();
198                                         }       
199                                 }
200                                 
201                                 String _id = policyWithScopeName;
202                                 
203                                 policyDataString.append(constructPolicyData(policyData, policyDataString));
204                                 
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", "");
210                                 
211                                 try{
212                                         Gson gson = new Gson();
213                                         gson.fromJson(dataString, Object.class);
214                                 }catch(Exception e){
215                                         continue;
216                                 }
217                                 
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());
226                                 }
227                         }
228                         
229                         result.close();
230                         bulk = new Bulk.Builder();
231                         for(int i =0; i < listIndex.size(); i++){
232                                 bulk.addAction(listIndex.get(i));
233                         }
234                         BulkResult searchResult = client.execute(bulk.build());
235                         if(searchResult.isSucceeded()){
236                                 LOGGER.debug("Success");
237                         }else{
238                                 LOGGER.error("Failure");
239                         }
240                 } catch (Exception e) {
241                         LOGGER.error("Exception Occured while performing database Operation for Elastic Search Policy Upgrade"+e);
242                 }finally{
243                         if(conn != null){
244                                 try {
245                                         conn.close();
246                                 } catch (Exception e) {
247                                         LOGGER.error("Exception Occured while closing the connection"+e);
248                                 }
249                         }
250                 }
251         }
252         
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();
276                                                                                 //
277                                                                                 // Under the match we have attribute value and
278                                                                                 // attributeDesignator. So,finally down to the actual attribute.
279                                                                                 //
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 EcompName, so set that value.
285                                                                                 if ("ECOMPName".equals(attributeId)) {
286                                                                                         policyDataString.append("\"ecompName\":\""+value+"\",");
287                                                                                 }
288                                                                                 if ("RiskType".equals(attributeId)){
289                                                                                         policyDataString.append("\"riskType\":\""+value+"\",");
290                                                                                 }
291                                                                                 if ("RiskLevel".equals(attributeId)){
292                                                                                         policyDataString.append("\"riskLevel\":\""+value+"\",");
293                                                                                 }
294                                                                                 if ("guard".equals(attributeId)){
295                                                                                         policyDataString.append("\"guard\":\""+value+"\",");
296                                                                                 }
297                                                                                 if ("ConfigName".equals(attributeId)){
298                                                                                         policyDataString.append("\"configName\":\""+value+"\",");
299                                                                                 }
300                                                                         }
301                                                                 }
302                                                         }
303                                                 }
304                                         }
305                                 }
306                         }
307                 }
308                 return policyDataString.toString();
309         }
310         
311 }