Consolidate PolicyRestAdapter setup
[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-2019 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 java.util.List;
24
25 import org.onap.policy.common.logging.flexlogger.FlexLogger;
26 import org.onap.policy.common.logging.flexlogger.Logger;
27 import org.onap.policy.rest.XacmlAdminAuthorization;
28 import org.onap.policy.rest.dao.CommonClassDao;
29 import org.onap.policy.rest.jpa.GlobalRoleSettings;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Component;
32
33 @Component
34 public class JPAUtils {
35     private static final Logger LOGGER = FlexLogger.getLogger(JPAUtils.class);
36
37     private static CommonClassDao commonClassDao;
38     private static JPAUtils currentInstance = null;
39
40     @Autowired
41     public JPAUtils(CommonClassDao commonClassDao) {
42         JPAUtils.commonClassDao = commonClassDao;
43     }
44
45     /**
46      * Get an instance of a JPAUtils. It creates one if it does not exist. Only one instance is allowed
47      * to be created per server.
48      *
49      * @param emf The EntityFactoryManager to be used for database connections
50      * @return The new instance of JPAUtils or throw exception if the given emf is null.
51      * @throws IllegalStateException if a JPAUtils has already been constructed. Call
52      *         getJPAUtilsInstance() to get this.
53      */
54     public static JPAUtils getJPAUtilsInstance() {
55         if (currentInstance == null) {
56             currentInstance = new JPAUtils();
57             return currentInstance;
58         }
59         return currentInstance;
60     }
61
62     private JPAUtils() {
63         // Default Constructor
64     }
65
66     /**
67      * Returns the lockdown value, in case of exception it is assumed that lockdown functionality is not
68      * supported and returns false.
69      *
70      *
71      * @throws ReadOnlyException
72      * @throws ConversionException
73      */
74     public boolean dbLockdownIgnoreErrors() {
75         if (LOGGER.isTraceEnabled())
76             LOGGER.trace("ENTER");
77
78         boolean lockdown = false;
79         try {
80             lockdown = dbLockdown();
81         } catch (Exception e) {
82             LOGGER.warn("Cannot access DB lockdown value", e);
83         }
84         return lockdown;
85     }
86
87     /**
88      * Returns the lockdown value from the database.
89      *
90      * @throws ReadOnlyException
91      * @throws ConversionException
92      */
93     public boolean dbLockdown() throws IllegalAccessException {
94         if (LOGGER.isTraceEnabled())
95             LOGGER.trace("ENTER");
96         List<Object> data = commonClassDao.getData(GlobalRoleSettings.class);
97
98         GlobalRoleSettings globalRoleSettings = null;
99
100         if (!data.isEmpty()) {
101             globalRoleSettings = (GlobalRoleSettings) data.get(0);
102         }
103
104         if (globalRoleSettings == null) {
105             // this should not happen
106             String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
107             if (LOGGER.isErrorEnabled())
108                 LOGGER.error(msg);
109             throw new IllegalAccessException(msg);
110         }
111
112         if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) {
113             String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
114             if (LOGGER.isErrorEnabled())
115                 LOGGER.error(msg);
116             throw new IllegalAccessException(msg);
117         }
118
119         return globalRoleSettings.isLockdown();
120     }
121 }