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