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