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