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