summaryrefslogtreecommitdiffstats
path: root/models-provider
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2021-10-27 10:18:32 +0100
committerFrancescoFioraEst <francesco.fiora@est.tech>2021-11-03 12:58:18 +0000
commit083868f1eab56b45baff7278ffb9acfd4126e02a (patch)
tree8cadaec703d6a169efd41bed5d42cd8ac1fd28b8 /models-provider
parent567201748af88c9120e623e531ace50382060e00 (diff)
Fix Db connection issues in TOSCA control loop
Issue-ID: POLICY-3153 Change-Id: I28a7962027a9cb383238a6d3765a46a905f8e58b Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'models-provider')
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java44
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractModelsProvider.java33
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractPolicyModelsProvider.java352
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java341
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/impl/DbPolicyModelsProviderImpl.java51
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/impl/ModelsProvider.java77
6 files changed, 550 insertions, 348 deletions
diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java
index 858b61477..7cd36a526 100644
--- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java
+++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java
@@ -24,8 +24,7 @@ package org.onap.policy.models.provider;
import javax.ws.rs.core.Response;
import lombok.NonNull;
import org.onap.policy.models.base.PfModelException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.onap.policy.models.dao.impl.ProxyDao;
/**
* A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation.
@@ -33,7 +32,43 @@ import org.slf4j.LoggerFactory;
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class PolicyModelsProviderFactory {
- private static final Logger LOGGER = LoggerFactory.getLogger(PolicyModelsProviderFactory.class);
+
+ /**
+ * Create PolicyModelsProvider.
+ *
+ * @param pfDao the ProxyDao
+ * @param parameters the PolicyModelsProviderParameters
+ * @return the PolicyModelsProvider
+ * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
+ */
+ public PolicyModelsProvider createPolicyModelsProvider(@NonNull final ProxyDao pfDao,
+ @NonNull final PolicyModelsProviderParameters parameters) throws PfModelException {
+ // Get the class for the PolicyModelsProvider
+ Class<?> implementationClass = null;
+ try {
+ // Check if the implementation class is on the classpath
+ implementationClass = Class.forName(parameters.getImplementation());
+ } catch (final Exception exc) {
+ String errorMessage = "could not find implementation of the \"PolicyModelsProvider\" interface \""
+ + parameters.getImplementation() + "\"";
+ throw new PfModelException(Response.Status.NOT_FOUND, errorMessage, exc);
+ }
+
+ // It is, now check if it is a PolicyModelsProvider
+ if (!PolicyModelsProvider.class.isAssignableFrom(implementationClass)) {
+ String errorMessage = "the class \"" + implementationClass.getName()
+ + "\" is not an implementation of the \"PolicyModelsProvider\" interface";
+ throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+
+ try {
+ return (PolicyModelsProvider) implementationClass.getConstructor(ProxyDao.class).newInstance(pfDao);
+ } catch (Exception exc) {
+ String errorMessage =
+ "could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\"";
+ throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
+ }
+ }
/**
* Creates a new PolicyModelsProvider object from its implementation.
@@ -51,7 +86,6 @@ public class PolicyModelsProviderFactory {
} catch (final Exception exc) {
String errorMessage = "could not find implementation of the \"PolicyModelsProvider\" interface \""
+ parameters.getImplementation() + "\"";
- LOGGER.warn(errorMessage);
throw new PfModelException(Response.Status.NOT_FOUND, errorMessage, exc);
}
@@ -59,7 +93,6 @@ public class PolicyModelsProviderFactory {
if (!PolicyModelsProvider.class.isAssignableFrom(implementationClass)) {
String errorMessage = "the class \"" + implementationClass.getName()
+ "\" is not an implementation of the \"PolicyModelsProvider\" interface";
- LOGGER.warn(errorMessage);
throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
}
@@ -73,7 +106,6 @@ public class PolicyModelsProviderFactory {
} catch (Exception exc) {
String errorMessage =
"could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\"";
- LOGGER.warn(errorMessage);
throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
}
}
diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractModelsProvider.java
index dc0a1f62e..7f6c4e5f8 100644
--- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractModelsProvider.java
+++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractModelsProvider.java
@@ -22,16 +22,11 @@
package org.onap.policy.models.provider.impl;
import java.io.Closeable;
-import java.util.Properties;
import javax.ws.rs.core.Response;
import lombok.Getter;
import lombok.NonNull;
-import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
-import org.onap.policy.models.dao.PfDaoFactory;
-import org.onap.policy.models.dao.impl.DefaultPfDao;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -73,33 +68,7 @@ public abstract class AbstractModelsProvider implements Closeable {
throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
}
- // Parameters for the DAO
- final var daoParameters = new DaoParameters();
- daoParameters.setPluginClass(DefaultPfDao.class.getName());
- daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
-
- // @formatter:off
- var jdbcProperties = new Properties();
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
- jdbcProperties.setProperty(PersistenceUnitProperties.TARGET_DATABASE,
- (parameters.getDatabaseType() == null ? "MySQL" : parameters.getDatabaseType()));
- // @formatter:on
-
- daoParameters.setJdbcProperties(jdbcProperties);
-
- try {
- pfDao = new PfDaoFactory().createPfDao(daoParameters);
- pfDao.init(daoParameters);
- } catch (Exception exc) {
- String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
- + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
-
- this.close();
- throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
- }
+ pfDao = ModelsProvider.init(parameters);
}
@Override
diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractPolicyModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractPolicyModelsProvider.java
new file mode 100644
index 000000000..fa7658355
--- /dev/null
+++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/AbstractPolicyModelsProvider.java
@@ -0,0 +1,352 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.provider.impl;
+
+import java.time.Instant;
+import java.util.Collection;
+import java.util.List;
+import javax.ws.rs.core.Response;
+import lombok.NonNull;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.pap.concepts.PolicyAudit;
+import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider;
+import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter;
+import org.onap.policy.models.pdp.concepts.Pdp;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
+import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
+import org.onap.policy.models.pdp.concepts.PdpStatistics;
+import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
+import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
+import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
+import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
+import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
+
+public abstract class AbstractPolicyModelsProvider implements PolicyModelsProvider {
+
+ protected abstract PfDao getPfDao();
+
+ @Override
+ public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
+ }
+
+
+ @Override
+ public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
+ @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
+ }
+
+ @Override
+ public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
+ }
+
+ @Override
+ public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
+ }
+
+ @Override
+ public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
+ throws PfModelException {
+ assertInitialized();
+
+ return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
+ }
+
+ @Override
+ public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
+ }
+
+ @Override
+ public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
+ }
+
+ @Override
+ public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
+ }
+
+ @Override
+ public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
+ }
+
+ @Override
+ public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
+ }
+
+ @Override
+ public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
+ }
+
+ @Override
+ public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
+ throws PfModelException {
+ assertInitialized();
+
+ var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
+ assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
+
+ return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
+ }
+
+ @Override
+ public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
+ }
+
+ @Override
+ public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
+ }
+
+ @Override
+ public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
+ }
+
+ @Override
+ public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
+ }
+
+ @Override
+ public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
+ }
+
+ @Override
+ public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
+ throws PfModelException {
+ assertInitialized();
+ return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
+ }
+
+ @Override
+ public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
+ throws PfModelException {
+ assertInitialized();
+
+ var policyIdentifier = new ToscaConceptIdentifier(name, version);
+ assertPolicyNotDeployedInPdpGroup(policyIdentifier);
+
+ return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
+ }
+
+ @Override
+ public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().getPdpGroups(getPfDao(), name);
+ }
+
+ @Override
+ public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
+ }
+
+ @Override
+ public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
+ }
+
+ @Override
+ public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
+ }
+
+ @Override
+ public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
+ throws PfModelException {
+ assertInitialized();
+ new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
+ }
+
+ @Override
+ public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
+ throws PfModelException {
+ new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
+ }
+
+ @Override
+ public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().deletePdpGroup(getPfDao(), name);
+ }
+
+ @Override
+ public List<PdpStatistics> getFilteredPdpStatistics(PdpFilterParameters filterParams) throws PfModelException {
+ assertInitialized();
+ return new PdpStatisticsProvider().getFilteredPdpStatistics(getPfDao(), filterParams);
+ }
+
+ @Override
+ public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
+ throws PfModelException {
+ assertInitialized();
+ return new PdpStatisticsProvider().createPdpStatistics(getPfDao(), pdpStatisticsList);
+ }
+
+ @Override
+ public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
+ throws PfModelException {
+ assertInitialized();
+ return new PdpStatisticsProvider().updatePdpStatistics(getPfDao(), pdpStatisticsList);
+ }
+
+ @Override
+ public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Instant timestamp)
+ throws PfModelException {
+ assertInitialized();
+ return new PdpStatisticsProvider().deletePdpStatistics(getPfDao(), name, timestamp);
+ }
+
+ @Override
+ public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().getAllPolicyStatus(getPfDao());
+ }
+
+ @Override
+ public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
+ throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
+ }
+
+ @Override
+ public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
+ assertInitialized();
+ return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
+ }
+
+ @Override
+ public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
+ Collection<PdpPolicyStatus> deleteObjs) {
+ assertInitialized();
+ new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
+ }
+
+ @Override
+ public void createAuditRecords(List<PolicyAudit> auditRecords) {
+ assertInitialized();
+ new PolicyAuditProvider().createAuditRecords(getPfDao(), auditRecords);
+ }
+
+ @Override
+ public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter) {
+ assertInitialized();
+ return new PolicyAuditProvider().getAuditRecords(getPfDao(), auditFilter);
+ }
+
+ /**
+ * Check if the model provider is initialized.
+ */
+ private void assertInitialized() {
+ if (getPfDao() == null) {
+ var errorMessage = "policy models provider is not initilaized";
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+ }
+
+ /**
+ * Assert that the policy type is not supported in any PDP group.
+ *
+ * @param policyTypeIdentifier the policy type identifier
+ * @throws PfModelException if the policy type is supported in a PDP group
+ */
+ private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
+ throws PfModelException {
+ for (PdpGroup pdpGroup : getPdpGroups(null)) {
+ for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+ if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+ "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
+ + pdpSubGroup.getPdpType());
+ }
+ }
+ }
+ }
+
+ /**
+ * Assert that the policy is not deployed in a PDP group.
+ *
+ * @param policyIdentifier the identifier of the policy
+ * @throws PfModelException thrown if the policy is deployed in a PDP group
+ */
+ private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
+ throws PfModelException {
+ for (PdpGroup pdpGroup : getPdpGroups(null)) {
+ for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+ if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+ "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
+ + pdpSubGroup.getPdpType());
+ }
+ }
+ }
+ }
+
+}
diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java
index 05722d71b..af03fdbcf 100644
--- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java
+++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java
@@ -22,35 +22,14 @@
package org.onap.policy.models.provider.impl;
-import java.time.Instant;
-import java.util.Collection;
-import java.util.List;
import javax.ws.rs.core.Response;
+import lombok.Getter;
import lombok.NonNull;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.pap.concepts.PolicyAudit;
-import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider;
-import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter;
-import org.onap.policy.models.pdp.concepts.Pdp;
-import org.onap.policy.models.pdp.concepts.PdpGroup;
-import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
-import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
-import org.onap.policy.models.pdp.concepts.PdpStatistics;
-import org.onap.policy.models.pdp.concepts.PdpSubGroup;
-import org.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
-import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
-import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider;
-import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
-import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
@@ -58,308 +37,50 @@ import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvide
*
* @author Liam Fallon (liam.fallon@est.tech)
*/
-public class DatabasePolicyModelsProviderImpl extends AbstractModelsProvider implements PolicyModelsProvider {
+public class DatabasePolicyModelsProviderImpl extends AbstractPolicyModelsProvider {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
+
+ private final PolicyModelsProviderParameters parameters;
+
+ // Database connection and the DAO for reading and writing Policy Framework concepts
+ @Getter
+ private PfDao pfDao;
+
/**
* Constructor that takes the parameters.
*
* @param parameters the parameters for the provider
*/
public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
- super(parameters);
- }
-
- @Override
- public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getServiceTemplateList(getPfDao(), name, version);
- }
-
-
- @Override
- public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
- @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getFilteredServiceTemplateList(getPfDao(), filter);
- }
-
- @Override
- public ToscaServiceTemplate createServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().createServiceTemplate(getPfDao(), serviceTemplate);
- }
-
- @Override
- public ToscaServiceTemplate updateServiceTemplate(@NonNull final ToscaServiceTemplate serviceTemplate)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().updateServiceTemplate(getPfDao(), serviceTemplate);
- }
-
- @Override
- public ToscaServiceTemplate deleteServiceTemplate(@NonNull final String name, @NonNull final String version)
- throws PfModelException {
- assertInitialized();
-
- return new AuthorativeToscaProvider().deleteServiceTemplate(getPfDao(), name, version);
- }
-
- @Override
- public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getPolicyTypes(getPfDao(), name, version);
- }
-
- @Override
- public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getPolicyTypeList(getPfDao(), name, version);
- }
-
- @Override
- public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getFilteredPolicyTypes(getPfDao(), filter);
- }
-
- @Override
- public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaEntityFilter<ToscaPolicyType> filter)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getFilteredPolicyTypeList(getPfDao(), filter);
- }
-
- @Override
- public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().createPolicyTypes(getPfDao(), serviceTemplate);
- }
-
- @Override
- public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().updatePolicyTypes(getPfDao(), serviceTemplate);
- }
-
- @Override
- public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
- throws PfModelException {
- assertInitialized();
-
- var policyTypeIdentifier = new ToscaConceptIdentifier(name, version);
- assertPolicyTypeNotSupportedInPdpGroup(policyTypeIdentifier);
-
- return new AuthorativeToscaProvider().deletePolicyType(getPfDao(), name, version);
- }
-
- @Override
- public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getPolicies(getPfDao(), name, version);
- }
-
- @Override
- public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getPolicyList(getPfDao(), name, version);
- }
-
- @Override
- public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getFilteredPolicies(getPfDao(), filter);
- }
-
- @Override
- public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaTypedEntityFilter<ToscaPolicy> filter)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().getFilteredPolicyList(getPfDao(), filter);
- }
-
- @Override
- public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().createPolicies(getPfDao(), serviceTemplate);
- }
-
- @Override
- public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
- throws PfModelException {
- assertInitialized();
- return new AuthorativeToscaProvider().updatePolicies(getPfDao(), serviceTemplate);
+ this.parameters = parameters;
}
@Override
- public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
- throws PfModelException {
- assertInitialized();
+ public synchronized void init() throws PfModelException {
+ LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
+ parameters.getPersistenceUnit());
- var policyIdentifier = new ToscaConceptIdentifier(name, version);
- assertPolicyNotDeployedInPdpGroup(policyIdentifier);
-
- return new AuthorativeToscaProvider().deletePolicy(getPfDao(), name, version);
- }
-
- @Override
- public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
- assertInitialized();
- return new PdpProvider().getPdpGroups(getPfDao(), name);
- }
-
- @Override
- public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException {
- assertInitialized();
- return new PdpProvider().getFilteredPdpGroups(getPfDao(), filter);
- }
-
- @Override
- public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
- assertInitialized();
- return new PdpProvider().createPdpGroups(getPfDao(), pdpGroups);
- }
-
- @Override
- public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
- assertInitialized();
- return new PdpProvider().updatePdpGroups(getPfDao(), pdpGroups);
- }
-
- @Override
- public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup)
- throws PfModelException {
- assertInitialized();
- new PdpProvider().updatePdpSubGroup(getPfDao(), pdpGroupName, pdpSubGroup);
- }
-
- @Override
- public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp)
- throws PfModelException {
- new PdpProvider().updatePdp(getPfDao(), pdpGroupName, pdpSubGroup, pdp);
- }
-
- @Override
- public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException {
- assertInitialized();
- return new PdpProvider().deletePdpGroup(getPfDao(), name);
- }
-
- @Override
- public List<PdpStatistics> getFilteredPdpStatistics(PdpFilterParameters filterParams) throws PfModelException {
- assertInitialized();
- return new PdpStatisticsProvider().getFilteredPdpStatistics(getPfDao(), filterParams);
- }
-
- @Override
- public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
- throws PfModelException {
- assertInitialized();
- return new PdpStatisticsProvider().createPdpStatistics(getPfDao(), pdpStatisticsList);
- }
-
- @Override
- public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList)
- throws PfModelException {
- assertInitialized();
- return new PdpStatisticsProvider().updatePdpStatistics(getPfDao(), pdpStatisticsList);
- }
-
- @Override
- public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Instant timestamp)
- throws PfModelException {
- assertInitialized();
- return new PdpStatisticsProvider().deletePdpStatistics(getPfDao(), name, timestamp);
- }
-
- @Override
- public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
- assertInitialized();
- return new PdpProvider().getAllPolicyStatus(getPfDao());
- }
-
- @Override
- public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
- throws PfModelException {
- assertInitialized();
- return new PdpProvider().getAllPolicyStatus(getPfDao(), policy);
- }
-
- @Override
- public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
- assertInitialized();
- return new PdpProvider().getGroupPolicyStatus(getPfDao(), groupName);
- }
-
- @Override
- public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
- Collection<PdpPolicyStatus> deleteObjs) {
- assertInitialized();
- new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs);
- }
+ if (pfDao != null) {
+ var errorMessage = "provider is already initialized";
+ throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
+ }
- @Override
- public void createAuditRecords(List<PolicyAudit> auditRecords) {
- assertInitialized();
- new PolicyAuditProvider().createAuditRecords(getPfDao(), auditRecords);
+ pfDao = ModelsProvider.init(parameters);
}
@Override
- public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter) {
- assertInitialized();
- return new PolicyAuditProvider().getAuditRecords(getPfDao(), auditFilter);
- }
+ public synchronized void close() throws PfModelException {
+ LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
+ parameters.getPersistenceUnit());
- /**
- * Check if the model provider is initialized.
- */
- private void assertInitialized() {
- if (getPfDao() == null) {
- var errorMessage = "policy models provider is not initilaized";
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ if (pfDao != null) {
+ pfDao.close();
+ pfDao = null;
}
- }
- /**
- * Assert that the policy type is not supported in any PDP group.
- *
- * @param policyTypeIdentifier the policy type identifier
- * @throws PfModelException if the policy type is supported in a PDP group
- */
- private void assertPolicyTypeNotSupportedInPdpGroup(ToscaConceptIdentifier policyTypeIdentifier)
- throws PfModelException {
- for (PdpGroup pdpGroup : getPdpGroups(null)) {
- for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
- if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
- throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
- "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
- + pdpSubGroup.getPdpType());
- }
- }
- }
+ LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
+ parameters.getPersistenceUnit());
}
- /**
- * Assert that the policy is not deployed in a PDP group.
- *
- * @param policyIdentifier the identifier of the policy
- * @throws PfModelException thrown if the policy is deployed in a PDP group
- */
- private void assertPolicyNotDeployedInPdpGroup(final ToscaConceptIdentifier policyIdentifier)
- throws PfModelException {
- for (PdpGroup pdpGroup : getPdpGroups(null)) {
- for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
- if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
- throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
- "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
- + pdpSubGroup.getPdpType());
- }
- }
- }
- }
}
diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DbPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DbPolicyModelsProviderImpl.java
new file mode 100644
index 000000000..1c34c300c
--- /dev/null
+++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DbPolicyModelsProviderImpl.java
@@ -0,0 +1,51 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.provider.impl;
+
+import lombok.Getter;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.dao.impl.ProxyDao;
+
+public class DbPolicyModelsProviderImpl extends AbstractPolicyModelsProvider {
+
+ // Database connection and the DAO for reading and writing Policy Framework concepts
+ @Getter
+ private final ProxyDao pfDao;
+
+ /**
+ * Constructor.
+ *
+ * @param pfDao the ProxyDao
+ */
+ public DbPolicyModelsProviderImpl(ProxyDao pfDao) {
+ this.pfDao = pfDao;
+ }
+
+ @Override
+ public void init() throws PfModelException {
+ // Not needs
+ }
+
+ @Override
+ public void close() throws PfModelException {
+ // Not needs
+ }
+}
diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/ModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/ModelsProvider.java
new file mode 100644
index 000000000..36c762e01
--- /dev/null
+++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/ModelsProvider.java
@@ -0,0 +1,77 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.provider.impl;
+
+import java.util.Properties;
+import javax.ws.rs.core.Response;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.dao.DaoParameters;
+import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfDaoFactory;
+import org.onap.policy.models.dao.impl.DefaultPfDao;
+import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ModelsProvider {
+
+ /**
+ * Initialise the provider.
+ *
+ * @throws PfModelException in initialisation errors
+ */
+ public static PfDao init(PolicyModelsProviderParameters parameters) throws PfModelException {
+ // Parameters for the DAO
+ final var daoParameters = new DaoParameters();
+ daoParameters.setPluginClass(DefaultPfDao.class.getName());
+ daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
+
+ // @formatter:off
+ var jdbcProperties = new Properties();
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver());
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl());
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser());
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
+ jdbcProperties.setProperty(PersistenceUnitProperties.TARGET_DATABASE,
+ (parameters.getDatabaseType() == null ? "MySQL" : parameters.getDatabaseType()));
+ // @formatter:on
+
+ daoParameters.setJdbcProperties(jdbcProperties);
+
+ PfDao pfDao = null;
+ try {
+ pfDao = new PfDaoFactory().createPfDao(daoParameters);
+ pfDao.init(daoParameters);
+ } catch (Exception exc) {
+ String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
+ + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
+ if (pfDao != null) {
+ pfDao.close();
+ }
+
+ throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
+ }
+ return pfDao;
+ }
+
+}