Merge "Add fix for SQL injection."
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / daoimpl / CommonClassDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
21 package org.onap.policy.pap.xacml.rest.daoimpl;
22
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.script.SimpleBindings;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.hibernate.Criteria;
31 import org.hibernate.Query;
32 import org.hibernate.Session;
33 import org.hibernate.SessionFactory;
34 import org.hibernate.Transaction;
35 import org.hibernate.criterion.Conjunction;
36 import org.hibernate.criterion.Criterion;
37 import org.hibernate.criterion.Disjunction;
38 import org.hibernate.criterion.Projections;
39 import org.hibernate.criterion.Restrictions;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.ClosedLoops;
42 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
43 import org.onap.policy.rest.jpa.PolicyRoles;
44 import org.onap.policy.xacml.api.XACMLErrorConstants;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Service;
47
48 @Service("CommonClassDao")
49 public class CommonClassDaoImpl implements CommonClassDao{
50
51         private static final Log LOGGER = LogFactory.getLog(CommonClassDaoImpl.class);
52         
53         
54         private static SessionFactory sessionFactory;
55         
56         @Autowired
57         private CommonClassDaoImpl(SessionFactory sessionFactory){
58                 CommonClassDaoImpl.sessionFactory = sessionFactory;
59         }
60         
61         public CommonClassDaoImpl(){
62                 //Default Constructor
63         }
64         
65         @SuppressWarnings({ "unchecked", "rawtypes" })
66         @Override
67         public List<Object> getData(Class className) {
68                 Session session = sessionFactory.openSession();
69                 List<Object> data = null;
70                 try{
71                         Criteria cr = session.createCriteria(className);
72                         data = cr.list();
73                 }catch(Exception e){
74                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);  
75                 }finally{
76                         try{
77                                 session.close();
78                         }catch(Exception e){
79                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
80                         }
81                 }
82                 return data;
83         }
84
85
86         @SuppressWarnings({ "rawtypes", "unchecked" })
87         @Override
88         public List<Object> getDataById(Class className, String columnName, String key) {
89                 Session session = sessionFactory.openSession();
90                 List<Object> data = null;
91                 try {
92                         Criteria cr = session.createCriteria(className);
93                         if(columnName.contains(":") && key.contains(":")){
94                                 String[] columns = columnName.split(":");
95                                 String[] keys = key.split(":");
96                                 for(int i=0; i < columns.length; i++){
97                                         cr.add(Restrictions.eq(columns[i], keys[i]));
98                                 }
99                         }else{
100                                 cr.add(Restrictions.eq(columnName, key));       
101                         }
102                         data = cr.list();
103                 } catch (Exception e) {
104                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);          
105                 }finally{
106                         try{
107                                 session.close();
108                         }catch(Exception e1){
109                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
110                         }
111                 }
112                 return data;
113         }
114         
115         @SuppressWarnings({ "unchecked", "rawtypes" })
116         @Override
117         public List<String> getDataByColumn(Class className, String columnName) {
118                 Session session = sessionFactory.openSession();
119                 List<String> data = null;
120                 try{
121                         Criteria cr = session.createCriteria(className);
122                         cr.setProjection(Projections.property(columnName));
123                         data = cr.list();
124                 }catch(Exception e){
125                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Table"+e);  
126                 }finally{
127                         try{
128                                 session.close();
129                         }catch(Exception e){
130                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e);
131                         }
132                 }
133                 return data;
134         }
135         
136         @Override
137         public void save(Object entity) {
138                 Session session = sessionFactory.openSession();
139                 Transaction tx = session.beginTransaction();
140                 try {
141                         session.persist(entity);
142                         tx.commit();    
143                 }catch(Exception e){
144                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving  data to Table"+e);   
145                 }finally{
146                         try{
147                                 session.close();
148                         }catch(Exception e1){
149                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
150                         }
151                 }
152                 
153         }
154
155         @Override
156         public void delete(Object entity) {
157                 Session session = sessionFactory.openSession();
158                 Transaction tx = session.beginTransaction();
159                 try {
160                         session.delete(entity);
161                         tx.commit();    
162                 }catch(Exception e){
163                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table"+e);        
164                 }finally{
165                         try{
166                                 session.close();
167                         }catch(Exception e1){
168                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
169                         }
170                 }
171                 
172         }
173
174
175         @Override
176         public void update(Object entity) {
177                 Session session = sessionFactory.openSession();
178                 Transaction tx = session.beginTransaction();
179                 try {
180                         session.update(entity);
181                         tx.commit();    
182                 }catch(Exception e){
183                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table"+e);  
184                 }finally{
185                         try{
186                                 session.close();
187                         }catch(Exception e1){
188                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
189                         }
190                 }
191                 
192         }
193
194
195         @SuppressWarnings({ "unchecked", "rawtypes" })
196         @Override
197         public List<Object> checkDuplicateEntry(String value, String columnName, Class className) {
198                 Session session = sessionFactory.openSession();
199                 Transaction tx = session.beginTransaction();
200                 List<Object> data = null;
201                 
202                 String[] columnNames = null;
203                 if(columnName != null && columnName.contains(":")){
204                         columnNames = columnName.split(":");
205                 }
206                 String[] values = null;
207                 if(value != null && value.contains(":")){
208                         values = value.split(":");
209                 }               
210                 try {
211                         Criteria cr = session.createCriteria(className);
212                         if(columnNames != null && values != null && columnNames.length == values.length){
213                                 for (int i = 0; i < columnNames.length; i++){
214                                         cr.add(Restrictions.eq(columnNames[i],values[i]));
215                                 }
216                         }else{
217                                 cr.add(Restrictions.eq(columnName,value));
218                         }
219
220                         data = cr.list();
221                         tx.commit();
222                 } catch (Exception e) {
223                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"+e + className);    
224                 }finally{
225                         try{
226                                 session.close();
227                         }catch(Exception e1){
228                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
229                         }
230                 }
231                 return data;
232         }
233
234         
235         @SuppressWarnings("unchecked")
236         @Override
237         public List<Object> getDataByQuery(String query, SimpleBindings params) {
238                 Session session = sessionFactory.openSession();
239                 Transaction tx = session.beginTransaction();
240                 List<Object> data = null;
241                 try {
242                         Query hbquery = session.createQuery(query);
243                         for (Map.Entry<String, Object> paramPair : params.entrySet()) {
244                                 hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
245                         }
246                         data = hbquery.list();
247                         tx.commit();
248                 } catch (Exception e) {
249                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
250                         throw e;
251                 }finally{
252                         try{
253                                 session.close();
254                         }catch(Exception e1){
255                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
256                                 throw e1;
257                         }
258                 }
259                 return data;
260         }
261
262
263         @Override
264         public void updateQuery(String query) {
265                 Session session = sessionFactory.openSession();
266                 Transaction tx = session.beginTransaction();    
267                 try {
268                         Query hbquery = session.createQuery(query);
269                         hbquery.executeUpdate();
270                         tx.commit();
271                 } catch (Exception e) {
272                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table"+e); 
273                 }finally{
274                         try{
275                                 session.close();
276                         }catch(Exception e1){
277                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
278                         }
279                 }
280         }
281
282
283         @SuppressWarnings("rawtypes")
284         @Override
285         public Object getEntityItem(Class className, String columnName, String key) {
286                 Session session = sessionFactory.openSession();
287                 Transaction tx = session.beginTransaction();
288                 Object data = null;
289                 try {
290                         Criteria cr = session.createCriteria(className);
291                         if(columnName.contains(":") && key.contains(":")){
292                                 String[] columns = columnName.split(":");
293                                 String[] keys = key.split(":");
294                                 for(int i=0; i < columns.length; i++){
295                                         cr.add(Restrictions.eq(columns[i], keys[i]));
296                                 }
297                         }else{
298                                 cr.add(Restrictions.eq(columnName, key));       
299                         }
300                         data = cr.list().get(0);
301                         tx.commit();
302                 } catch (Exception e) {
303                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e); 
304                 }finally{
305                         try{
306                                 session.close();
307                         }catch(Exception e1){
308                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
309                         }
310                 }
311                 return data;
312         }
313
314
315         @SuppressWarnings("unchecked")
316         @Override
317         public List<PolicyRoles> getUserRoles() {
318                 Session session = sessionFactory.openSession();
319                 Transaction tx = session.beginTransaction();
320                 List<PolicyRoles> rolesData = null;
321                 try {
322                         Criteria cr = session.createCriteria(PolicyRoles.class);                
323                         Disjunction disjunction = Restrictions.disjunction(); 
324                         Conjunction conjunction1  = Restrictions.conjunction();
325                         conjunction1.add(Restrictions.eq("role", "admin"));
326                         Conjunction conjunction2  = Restrictions.conjunction();
327                         conjunction2.add(Restrictions.eq("role", "editor"));    
328                         Conjunction conjunction3  = Restrictions.conjunction();
329                         conjunction3.add(Restrictions.eq("role", "guest"));     
330                         disjunction.add(conjunction1);
331                         disjunction.add(conjunction2);
332                         disjunction.add(conjunction3);
333                         rolesData = cr.add(disjunction).list();
334                         tx.commit();
335                 } catch (Exception e) {
336                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);      
337                 }finally{
338                         try{
339                                 session.close();
340                         }catch(Exception e1){
341                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
342                         }
343                 }
344                 return rolesData;
345         }
346
347
348         @SuppressWarnings("unchecked")
349         @Override
350         public void updateClAlarms(String clName, String alarms) {
351                 Session session = sessionFactory.openSession();
352                 List<ClosedLoops> closedloopsdata = null;
353                 Transaction tx = session.beginTransaction();
354                 try {
355                 Criteria cr = session.createCriteria(ClosedLoops.class);
356                 cr.add(Restrictions.eq("closedLoopControlName",clName));
357             closedloopsdata = cr.list();
358                         ClosedLoops closedloop = closedloopsdata.get(0);
359                         closedloop.setAlarmConditions(alarms);
360                         session.update(closedloop);
361                         tx.commit();    
362                 }catch(Exception e){
363                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);      
364                 }finally{
365                         session.close();
366                 }
367         }
368
369
370         @SuppressWarnings("unchecked")
371         @Override
372         public void updateClYaml(String clName, String yaml) {
373                 Session session = sessionFactory.openSession();
374         List<ClosedLoops> closedloopsdata = null;
375                 Transaction tx = session.beginTransaction();
376                 try {
377                 Criteria cr = session.createCriteria(ClosedLoops.class);
378                 cr.add(Restrictions.eq("closedLoopControlName",clName));
379             closedloopsdata = cr.list();
380                         ClosedLoops closedloop = closedloopsdata.get(0);
381                         closedloop.setYaml(yaml);
382                         session.update(closedloop);
383                         tx.commit();    
384                 }catch(Exception e){
385                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating ClosedLoops Table"+e);      
386                 }finally{
387                         session.close();
388                 }
389         }
390
391
392         @SuppressWarnings("unchecked")
393         @Override
394         public void deleteAll() {
395                 Session session = sessionFactory.openSession();
396                 Transaction tx = session.beginTransaction();
397                 List<ClosedLoops> closedloopsdata = null;
398                 try {
399                         Criteria cr = session.createCriteria(ClosedLoops.class);
400                         closedloopsdata = cr.list();
401
402                         if(closedloopsdata!=null && !closedloopsdata.isEmpty()) {
403                                 LOGGER.info("ClosedLoops exist in the database, we need to delete them in our first step to buildCache().");
404                                 for(int i=0; i < closedloopsdata.size(); i++) {
405                                         ClosedLoops cl = closedloopsdata.get(i);
406                                         session.delete(cl);
407                                 }
408                         } else {
409                                 LOGGER.info("No ClosedLoops exist in the database, no need to delete.");
410                         }
411
412                         tx.commit();
413                 }catch(Exception e) {
414                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while deleting ClosedLoops from the table"+e);
415                 }finally{
416                         session.close();
417                 }
418         }
419
420         @SuppressWarnings({ "unchecked"})
421         @Override
422         public List<Object> checkExistingGroupListforUpdate(String groupListValue, String groupNameValue) {
423                 Session session = sessionFactory.openSession();
424                 Transaction tx = session.beginTransaction();
425                 List<Object> data = null;
426                 try {
427                         Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
428                         cr.add(Restrictions.eq("groupList",groupListValue));    
429                         Criterion expression = Restrictions.eq("name", groupNameValue);
430                         cr.add(Restrictions.not(expression));
431                         data = cr.list();
432                         tx.commit();
433                 } catch (Exception e) {
434                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for GroupPolicyScopeList Table"+e);   
435                 }finally{
436                         try{
437                                 session.close();
438                         }catch(Exception e1){
439                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
440                         }
441                 }
442                 return data;
443         }
444
445
446         @Override
447         public List<Object> getMultipleDataOnAddingConjunction(@SuppressWarnings("rawtypes") Class className, String columnName, List<String> data) {
448                 return null;
449         }
450
451 }