Modify ONAP PAP REST classes basic checkstyle
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / util / JPAUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.util;
22
23 import javax.persistence.EntityManager;
24 import javax.persistence.EntityManagerFactory;
25 import javax.persistence.Query;
26
27 import org.onap.policy.rest.XacmlAdminAuthorization;
28 import org.onap.policy.rest.jpa.GlobalRoleSettings;
29
30 import org.onap.policy.common.logging.flexlogger.FlexLogger; 
31 import org.onap.policy.common.logging.flexlogger.Logger;
32
33 public class JPAUtils {
34     private static final Logger LOGGER  = FlexLogger.getLogger(JPAUtils.class);
35
36     private static EntityManagerFactory emf;
37     private static JPAUtils currentInstance = null;
38
39
40     /**
41      * Get an instance of a JPAUtils. It creates one if it does not exist.
42      * Only one instance is allowed to be created per server.
43      * @param emf The EntityFactoryManager to be used for database connections
44      * @return The new instance of JPAUtils or throw exception if the given emf is null.
45      * @throws IllegalStateException if a JPAUtils has already been constructed. Call getJPAUtilsInstance() to get this.
46      */
47     public static JPAUtils getJPAUtilsInstance(EntityManagerFactory emf){
48         LOGGER.debug("getJPAUtilsInstance(EntityManagerFactory emf) as getJPAUtilsInstance("+emf+") called");
49         if(currentInstance == null){
50             if(emf != null){
51                 currentInstance = new JPAUtils(emf);
52                 return currentInstance;
53             }
54             throw new IllegalStateException("The EntityManagerFactory is Null");
55         }
56         return currentInstance;
57     }
58
59     private JPAUtils(EntityManagerFactory emf){
60         LOGGER.debug("JPAUtils(EntityManagerFactory emf) as JPAUtils("+emf+") called");
61         JPAUtils.emf = emf;
62     }
63
64     /**
65      * Returns the lockdown value, in case of exception it is assumed that lockdown functionality
66      * is not supported and returns false.
67      *
68      *
69      * @throws ReadOnlyException
70      * @throws ConversionException
71      */
72     public boolean dbLockdownIgnoreErrors() {
73         if (LOGGER.isTraceEnabled())
74             LOGGER.trace("ENTER");
75
76         boolean lockdown = false;
77         try {
78             lockdown = dbLockdown();
79         } catch (Exception e) {
80             LOGGER.warn("Cannot access DB lockdown value", e);
81         }
82         return lockdown;
83     }
84
85     /**
86      * Returns the lockdown value from the database.
87      *
88      * @throws ReadOnlyException
89      * @throws ConversionException
90      */
91     public boolean dbLockdown()
92             throws  IllegalAccessException {
93         if (LOGGER.isTraceEnabled())
94             LOGGER.trace("ENTER");
95
96         EntityManager em = emf.createEntityManager();
97         Query globalRoleSettingsJPA = em.createNamedQuery("GlobalRoleSettings.findAll");
98
99         GlobalRoleSettings globalRoleSettings = (GlobalRoleSettings) globalRoleSettingsJPA.getSingleResult();
100
101         if (globalRoleSettings == null) {
102             // this should not happen
103             String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
104             if (LOGGER.isErrorEnabled())
105                 LOGGER.error(msg);
106             throw new IllegalAccessException(msg);
107         }
108
109         if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) {
110             String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
111             if (LOGGER.isErrorEnabled())
112                 LOGGER.error(msg);
113             throw new IllegalAccessException(msg);
114         }
115
116         return globalRoleSettings.isLockdown();
117     }
118 }