2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.util;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.Query;
31 import javax.servlet.ServletException;
33 import org.onap.policy.rest.XacmlAdminAuthorization;
34 import org.onap.policy.rest.jpa.Attribute;
35 import org.onap.policy.rest.jpa.Datatype;
36 import org.onap.policy.rest.jpa.FunctionDefinition;
37 import org.onap.policy.rest.jpa.GlobalRoleSettings;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeSelectorType;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
45 public class JPAUtils {
46 private static final Logger LOGGER = FlexLogger.getLogger(JPAUtils.class);
48 private static EntityManagerFactory emf;
49 private static final Object mapAccess = new Object();
50 private static Map<Datatype, List<FunctionDefinition>> mapDatatype2Function = null;
51 private static Map<String, FunctionDefinition> mapID2Function = null;
52 private static JPAUtils currentInstance = null;
56 * Get an instance of a JPAUtils. It creates one if it does not exist.
57 * Only one instance is allowed to be created per server.
58 * @param emf The EntityFactoryManager to be used for database connections
59 * @return The new instance of JPAUtils or throw exception if the given emf is null.
60 * @throws IllegalStateException if a JPAUtils has already been constructed. Call getJPAUtilsInstance() to get this.
62 public static JPAUtils getJPAUtilsInstance(EntityManagerFactory emf){
63 LOGGER.debug("getJPAUtilsInstance(EntityManagerFactory emf) as getJPAUtilsInstance("+emf+") called");
64 if(currentInstance == null){
66 currentInstance = new JPAUtils(emf);
67 return currentInstance;
69 throw new IllegalStateException("The EntityManagerFactory is Null");
71 return currentInstance;
74 private JPAUtils(EntityManagerFactory emf){
75 LOGGER.debug("JPAUtils(EntityManagerFactory emf) as JPAUtils("+emf+") called");
80 * Gets the current instance of JPAUtils.
81 * @return The instance of JPAUtils or throws exception if the given instance is null.
82 * @throws IllegalStateException if a JPAUtils instance is null. Call getJPAUtilsInstance(EntityManagerFactory emf) to get this.
84 public static JPAUtils getJPAUtilsInstance(){
85 LOGGER.debug("getJPAUtilsInstance() as getJPAUtilsInstance() called");
86 if(currentInstance != null){
87 return currentInstance;
89 throw new IllegalStateException("The JPAUtils.currentInstance is Null. Use getJPAUtilsInstance(EntityManagerFactory emf)");
92 public static AttributeDesignatorType createDesignator(Attribute attribute) {
93 AttributeDesignatorType designator = new AttributeDesignatorType();
94 designator.setAttributeId(attribute.getXacmlId());
95 if (attribute.getCategoryBean() != null) {
96 designator.setCategory(attribute.getCategoryBean().getXacmlId());
98 LOGGER.warn("No category bean");
100 if (attribute.getDatatypeBean() != null) {
101 designator.setDataType(attribute.getDatatypeBean().getXacmlId());
103 LOGGER.warn("No datatype bean");
105 designator.setIssuer(attribute.getIssuer());
106 designator.setMustBePresent(attribute.isMustBePresent());
110 public static AttributeSelectorType createSelector(Attribute attribute) {
111 AttributeSelectorType selector = new AttributeSelectorType();
112 selector.setContextSelectorId(attribute.getXacmlId());
113 selector.setPath(attribute.getSelectorPath());
114 if (attribute.getCategoryBean() != null) {
115 selector.setCategory(attribute.getCategoryBean().getXacmlId());
117 LOGGER.warn("No category bean");
119 if (attribute.getDatatypeBean() != null) {
120 selector.setDataType(attribute.getDatatypeBean().getXacmlId());
122 LOGGER.warn("No datatype bean");
124 selector.setMustBePresent(attribute.isMustBePresent());
129 * Builds a map in memory of a functions return datatype to function definition. Useful in limiting the number
130 * of SQL calls to DB especially when we don't expect these to change much.
132 * @return - A HashMap of Datatype JPA Container ID's to FunctionDefinition objects
134 public Map<Datatype, List<FunctionDefinition>> getFunctionDatatypeMap() {
136 synchronized(mapAccess) {
137 if (mapDatatype2Function == null||mapDatatype2Function.isEmpty()) {
140 } catch (ServletException e) {
141 LOGGER.error("Exception Occured"+e);
145 return mapDatatype2Function;
148 public Map<String, FunctionDefinition> getFunctionIDMap() {
149 synchronized(mapAccess) {
150 if (mapID2Function == null||mapID2Function.isEmpty()) {
153 } catch (ServletException e) {
154 LOGGER.error("Exception Occured"+e);
158 return mapID2Function;
161 private static void buildFunctionMaps() throws ServletException {
162 mapDatatype2Function = new HashMap<>();
163 mapID2Function = new HashMap<>();
165 EntityManager em = emf.createEntityManager();
166 Query getFunctionDefinitions = em.createNamedQuery("FunctionDefinition.findAll");
167 List<?> functionList = getFunctionDefinitions.getResultList();
169 for (Object id : functionList) {
170 FunctionDefinition value = (FunctionDefinition)id;
171 mapID2Function.put(value.getXacmlid(), value);
172 if (!mapDatatype2Function.containsKey(value.getDatatypeBean())) {
173 mapDatatype2Function.put(value.getDatatypeBean(), new ArrayList<FunctionDefinition>());
175 mapDatatype2Function.get(value.getDatatypeBean()).add(value);
183 * Returns the lockdown value, in case of exception it is assumed that lockdown functionality
184 * is not supported and returns false.
187 * @throws ReadOnlyException
188 * @throws ConversionException
190 public boolean dbLockdownIgnoreErrors() {
191 if (LOGGER.isTraceEnabled())
192 LOGGER.trace("ENTER");
194 boolean lockdown = false;
196 lockdown = dbLockdown();
197 } catch (Exception e) {
198 LOGGER.warn("Cannot access DB lockdown value", e);
204 * Returns the lockdown value from the database.
206 * @throws ReadOnlyException
207 * @throws ConversionException
209 public boolean dbLockdown()
210 throws IllegalAccessException {
211 if (LOGGER.isTraceEnabled())
212 LOGGER.trace("ENTER");
214 EntityManager em = emf.createEntityManager();
215 Query globalRoleSettingsJPA = em.createNamedQuery("GlobalRoleSettings.findAll");
217 GlobalRoleSettings globalRoleSettings = (GlobalRoleSettings) globalRoleSettingsJPA.getSingleResult();
219 if (globalRoleSettings == null) {
220 // this should not happen
221 String msg = "NO GlobalSetttings for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
222 if (LOGGER.isErrorEnabled())
224 throw new IllegalAccessException(msg);
227 if (!globalRoleSettings.getRole().equals(XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString())) {
228 String msg = "NOT FOUND db data for " + XacmlAdminAuthorization.Role.ROLE_SUPERADMIN.toString();
229 if (LOGGER.isErrorEnabled())
231 throw new IllegalAccessException(msg);
234 return globalRoleSettings.isLockdown();