From 59278e17a3911518c5e3601b6a38bfbbcbcf0fc8 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 7 May 2020 10:57:38 +0100 Subject: Fix simultaneous get access to policy provider When a "get" is executed while a "create" or "delete" is underway, the DB sessions overlp and data is lost in the database. "get" requests must also be synchronized. Issue-ID: POLICY-2533 Change-Id: Idbe155568e5d793fe82b7cc3aad5ef121e8209b9 Signed-off-by: liamfallon --- .../provider/AuthorativeToscaProvider.java | 157 +++++++++++---------- 1 file changed, 86 insertions(+), 71 deletions(-) (limited to 'models-tosca/src/main') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index c595b557e..fbc3b9b77 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -71,14 +71,16 @@ public class AuthorativeToscaProvider { public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { - LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version); + synchronized (providerLockObject) { + LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version); - JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version); + JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version); - ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative(); + ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative(); - LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); - return serviceTemplate; + LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); + return serviceTemplate; + } } /** @@ -93,19 +95,21 @@ public class AuthorativeToscaProvider { public List getPolicyTypeList(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { - LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version); + synchronized (providerLockObject) { + LOGGER.debug("->getPolicyTypeList: name={}, version={}", name, version); - List policyTypeList; + List policyTypeList; - try { - policyTypeList = new ArrayList<>( - new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative().getPolicyTypes().values()); - } catch (PfModelRuntimeException pfme) { - return handlePfModelRuntimeException(pfme); - } + try { + policyTypeList = new ArrayList<>(new SimpleToscaProvider().getPolicyTypes(dao, name, version) + .toAuthorative().getPolicyTypes().values()); + } catch (PfModelRuntimeException pfme) { + return handlePfModelRuntimeException(pfme); + } - LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList); - return policyTypeList; + LOGGER.debug("<-getPolicyTypeList: name={}, version={}, policyTypeList={}", name, version, policyTypeList); + return policyTypeList; + } } /** @@ -119,34 +123,35 @@ public class AuthorativeToscaProvider { public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull final PfDao dao, @NonNull final ToscaPolicyTypeFilter filter) throws PfModelException { - LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter); - SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider(); + synchronized (providerLockObject) { + LOGGER.debug("->getFilteredPolicyTypes: filter={}", filter); + SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider(); - final JpaToscaServiceTemplate dbServiceTemplate = simpleToscaProvider.getPolicyTypes(dao, null, null); + final JpaToscaServiceTemplate dbServiceTemplate = simpleToscaProvider.getPolicyTypes(dao, null, null); - List filteredPolicyTypes = dbServiceTemplate.getPolicyTypes().toAuthorativeList(); - filteredPolicyTypes = filter.filter(filteredPolicyTypes); + List filteredPolicyTypes = dbServiceTemplate.getPolicyTypes().toAuthorativeList(); + filteredPolicyTypes = filter.filter(filteredPolicyTypes); - if (CollectionUtils.isEmpty(filteredPolicyTypes)) { - throw new PfModelRuntimeException(Response.Status.NOT_FOUND, - "policy types for filter " + filter.toString() + " do not exist"); - } - - JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate(); + if (CollectionUtils.isEmpty(filteredPolicyTypes)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, + "policy types for filter " + filter.toString() + " do not exist"); + } - for (ToscaPolicyType policyType : filteredPolicyTypes) { - JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider - .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion()); + JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate(); - filteredServiceTemplate = - ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); - } + for (ToscaPolicyType policyType : filteredPolicyTypes) { + JpaToscaServiceTemplate cascadedServiceTemplate = simpleToscaProvider + .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion()); - ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative(); + filteredServiceTemplate = + ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); + } - LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate); - return returnServiceTemplate; + ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative(); + LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", filter, returnServiceTemplate); + return returnServiceTemplate; + } } /** @@ -249,13 +254,17 @@ public class AuthorativeToscaProvider { */ public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { - LOGGER.debug("->getPolicies: name={}, version={}", name, version); - ToscaServiceTemplate gotServiceTempalate = - new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative(); + synchronized (providerLockObject) { + LOGGER.debug("->getPolicies: name={}, version={}", name, version); + + ToscaServiceTemplate gotServiceTempalate = + new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative(); - LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, gotServiceTempalate); - return gotServiceTempalate; + LOGGER.debug("<-getPolicies: name={}, version={}, gotServiceTempalate={}", name, version, + gotServiceTempalate); + return gotServiceTempalate; + } } /** @@ -269,19 +278,22 @@ public class AuthorativeToscaProvider { */ public List getPolicyList(@NonNull final PfDao dao, final String name, final String version) throws PfModelException { - LOGGER.debug("->getPolicyList: name={}, version={}", name, version); - List policyList; + synchronized (providerLockObject) { + LOGGER.debug("->getPolicyList: name={}, version={}", name, version); - try { - policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative() - .getToscaTopologyTemplate().getPolicies()); - } catch (PfModelRuntimeException pfme) { - return handlePfModelRuntimeException(pfme); - } + List policyList; - LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList); - return policyList; + try { + policyList = asConceptList(new SimpleToscaProvider().getPolicies(dao, name, version).toAuthorative() + .getToscaTopologyTemplate().getPolicies()); + } catch (PfModelRuntimeException pfme) { + return handlePfModelRuntimeException(pfme); + } + + LOGGER.debug("<-getPolicyList: name={}, version={}, policyTypeList={}", name, version, policyList); + return policyList; + } } /** @@ -295,35 +307,38 @@ public class AuthorativeToscaProvider { public ToscaServiceTemplate getFilteredPolicies(@NonNull final PfDao dao, @NonNull final ToscaPolicyFilter filter) throws PfModelException { - LOGGER.debug("->getFilteredPolicies: filter={}", filter); - String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion(); + synchronized (providerLockObject) { + LOGGER.debug("->getFilteredPolicies: filter={}", filter); + String version = ToscaPolicyFilter.LATEST_VERSION.equals(filter.getVersion()) ? null : filter.getVersion(); - SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider(); - final JpaToscaServiceTemplate dbServiceTemplate = - simpleToscaProvider.getPolicies(dao, filter.getName(), version); + SimpleToscaProvider simpleToscaProvider = new SimpleToscaProvider(); + final JpaToscaServiceTemplate dbServiceTemplate = + simpleToscaProvider.getPolicies(dao, filter.getName(), version); - List filteredPolicies = dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList(); - filteredPolicies = filter.filter(filteredPolicies); + List filteredPolicies = + dbServiceTemplate.getTopologyTemplate().getPolicies().toAuthorativeList(); + filteredPolicies = filter.filter(filteredPolicies); - if (CollectionUtils.isEmpty(filteredPolicies)) { - throw new PfModelRuntimeException(Response.Status.NOT_FOUND, - "policies for filter " + filter.toString() + " do not exist"); - } + if (CollectionUtils.isEmpty(filteredPolicies)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, + "policies for filter " + filter.toString() + " do not exist"); + } - JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate(); + JpaToscaServiceTemplate filteredServiceTemplate = new JpaToscaServiceTemplate(); - for (ToscaPolicy policy : filteredPolicies) { - JpaToscaServiceTemplate cascadedServiceTemplate = - simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion()); + for (ToscaPolicy policy : filteredPolicies) { + JpaToscaServiceTemplate cascadedServiceTemplate = + simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion()); - filteredServiceTemplate = - ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); - } + filteredServiceTemplate = + ToscaServiceTemplateUtils.addFragment(filteredServiceTemplate, cascadedServiceTemplate); + } - ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative(); + ToscaServiceTemplate returnServiceTemplate = filteredServiceTemplate.toAuthorative(); - LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate); - return returnServiceTemplate; + LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", filter, returnServiceTemplate); + return returnServiceTemplate; + } } /** -- cgit 1.2.3-korg