summaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/util/JPAUtils.java
blob: 22c44a8ab302bbcc70fd72dc2cdb2d9337f655fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PAP-REST
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.xacml.rest.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.onap.policy.rest.XacmlAdminAuthorization;
import org.onap.policy.rest.jpa.GlobalRoleSettings;

import org.onap.policy.common.logging.flexlogger.FlexLogger; 
import org.onap.policy.common.logging.flexlogger.Logger;

public class JPAUtils {
	private static final Logger LOGGER	= FlexLogger.getLogger(JPAUtils.class);
	
	private static EntityManagerFactory emf;
	private static JPAUtils currentInstance = null;
	
	
	/**
	 * Get an instance of a JPAUtils. It creates one if it does not exist.
	 * Only one instance is allowed to be created per server.
	 * @param emf The EntityFactoryManager to be used for database connections
	 * @return The new instance of JPAUtils or throw exception if the given emf is null.
	 * @throws IllegalStateException if a JPAUtils has already been constructed. Call getJPAUtilsInstance() to get this.
	 */
	public static JPAUtils getJPAUtilsInstance(EntityManagerFactory emf){
		LOGGER.debug("getJPAUtilsInstance(EntityManagerFactory emf) as getJPAUtilsInstance("+emf+") called");
		if(currentInstance == null){
			if(emf != null){
				currentInstance = new JPAUtils(emf);
				return currentInstance;
			}
			throw new IllegalStateException("The EntityManagerFactory is Null");
		}
		return currentInstance;
	}
	
	private JPAUtils(EntityManagerFactory emf){
		LOGGER.debug("JPAUtils(EntityManagerFactory emf) as JPAUtils("+emf+") called");
		JPAUtils.emf = emf;	
	}
	
	/**
	 * Returns the lockdown value, in case of exception it is assumed that lockdown functionality
	 * is not supported and returns false.
	 * 
	 * 
	 * @throws ReadOnlyException
	 * @throws ConversionException
	 */
	public boolean dbLockdownIgnoreErrors() {
		if (LOGGER.isTraceEnabled())
			LOGGER.trace("ENTER");
		
		boolean lockdown = false;
		try {
			lockdown = dbLockdown();
		} catch (Exception e) {
			LOGGER.warn("Cannot access DB lockdown value", e);
		}
		return lockdown;
	}
	
	/**
	 * Returns the lockdown value from the database.
	 * 
	 * @throws ReadOnlyException
	 * @throws ConversionException
	 */
	public boolean dbLockdown() 
			throws  IllegalAccessException {
		if (LOGGER.isTraceEnabled())
			LOGGER.trace("ENTER");
		
		EntityManager em = emf.createEntityManager();
		Query globalRoleSettingsJPA = em.createNamedQuery("GlobalRoleSettings.findAll");	
		
		GlobalRoleSettings globalRoleSettings = (GlobalRoleSettings) globalRoleSettingsJPA.getSingleResult();
		
		if (globalRoleSettings == null) {
			// this should not happen
			String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
			if (LOGGER.isErrorEnabled())
				LOGGER.error(msg);
			throw new IllegalAccessException(msg);
		}
		
		if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) {
			String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
			if (LOGGER.isErrorEnabled())
				LOGGER.error(msg);
			throw new IllegalAccessException(msg);
		}
		
		return globalRoleSettings.isLockdown();
	}
}