summaryrefslogtreecommitdiffstats
path: root/models-provider/src/main/java/org
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-04-11 15:59:47 +0000
committerliamfallon <liam.fallon@est.tech>2019-04-11 15:59:47 +0000
commitd010fb918de5215fd5ff9219041ea11c77a4059a (patch)
tree12c8d44b160788fc07d6d921de116504823b4ff9 /models-provider/src/main/java/org
parent891bffd9f13177d96ad26acdfa7148d09e1d682a (diff)
Fix database properties
Issue-ID: POLICY-1095 Change-Id: I3edd70898836d3bd978643857d1ba29599b1cf6c Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-provider/src/main/java/org')
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java6
-rw-r--r--models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java69
2 files changed, 33 insertions, 42 deletions
diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java
index 5abafed22..65aa72eb2 100644
--- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java
+++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java
@@ -55,6 +55,7 @@ public class PolicyModelsProviderParameters implements ParameterGroup {
private String name;
private String implementation = DEFAULT_IMPLEMENTATION;
+ private String databaseDriver;
private String databaseUrl;
private String databaseUser;
private String databasePassword;
@@ -73,6 +74,11 @@ public class PolicyModelsProviderParameters implements ParameterGroup {
"a PolicyModelsProvider implementation must be specified");
}
+ if (!ParameterValidationUtils.validateStringParameter(databaseDriver)) {
+ validationResult.setResult("databaseUrl", ValidationStatus.INVALID,
+ "a driver must be specified for the JDBC connection to the database");
+ }
+
if (!ParameterValidationUtils.validateStringParameter(databaseUrl)) {
validationResult.setResult("databaseUrl", ValidationStatus.INVALID,
"a URL must be specified for the JDBC connection to the database");
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 cc70df903..475e5769d 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
@@ -20,11 +20,10 @@
package org.onap.policy.models.provider.impl;
-import java.sql.Connection;
-import java.sql.DriverManager;
import java.util.Base64;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import javax.ws.rs.core.Response;
@@ -64,12 +63,20 @@ import org.slf4j.LoggerFactory;
* @author Liam Fallon (liam.fallon@est.tech)
*/
public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
+
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
+ // Constants for persistence properties
+ // @formatter:off
+ private static final String JAVAX_PERSISTENCE_JDBC_DRIVER = "javax.persistence.jdbc.driver";
+ private static final String JAVAX_PERSISTENCE_JDBC_URL = "javax.persistence.jdbc.url";
+ private static final String JAVAX_PERSISTENCE_JDBC_USER = "javax.persistence.jdbc.user";
+ private static final String JAVAX_PERSISTENCE_JDBC_PWORD = "javax.persistence.jdbc.password";
+ // @formatter:on
+
private final PolicyModelsProviderParameters parameters;
// Database connection and the DAO for reading and writing Policy Framework concepts
- private Connection connection;
private PfDao pfDao;
/**
@@ -86,30 +93,31 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
parameters.getPersistenceUnit());
- if (connection != null || pfDao != null) {
+ if (pfDao != null) {
String errorMessage = "provider is already initialized";
LOGGER.warn(errorMessage);
throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
}
- // Decode the password using Base64
- String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
-
- // Connect to the database, call does not implement AutoCloseable for try-with-resources
- try {
- connection = DriverManager.getConnection(parameters.getDatabaseUrl(), parameters.getDatabaseUser(),
- decodedPassword);
- } catch (Exception exc) {
- String errorMessage = "could not connect to database with URL \"" + parameters.getDatabaseUrl() + "\"";
- LOGGER.warn(errorMessage, exc);
- throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
- }
-
// Parameters for the DAO
final DaoParameters daoParameters = new DaoParameters();
daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
+ // Decode the password using Base64
+ String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword()));
+
+ // @formatter:off
+ Properties jdbcProperties = new Properties();
+ jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_DRIVER, parameters.getDatabaseDriver());
+ jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_URL, parameters.getDatabaseUrl());
+ jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_USER, parameters.getDatabaseUser());
+ jdbcProperties.setProperty(JAVAX_PERSISTENCE_JDBC_PWORD, decodedPassword);
+ // @formatter:on
+
+ daoParameters.setJdbcProperties(jdbcProperties);
+
+ pfDao = new PfDaoFactory().createPfDao(daoParameters);
try {
pfDao = new PfDaoFactory().createPfDao(daoParameters);
pfDao.init(daoParameters);
@@ -133,20 +141,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
pfDao = null;
}
- if (connection != null) {
- try {
- connection.close();
- } catch (Exception exc) {
-
- String errorMessage =
- "could not close connection to database with URL \"" + parameters.getDatabaseUrl() + "\"";
- LOGGER.warn(errorMessage, exc);
- throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
- } finally {
- connection = null;
- }
- }
-
LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
parameters.getPersistenceUnit());
}
@@ -328,8 +322,8 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
}
@Override
- public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion,
- @NonNull String pdpSubGroup, @NonNull Pdp pdp) throws PfModelException {
+ public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, @NonNull String pdpSubGroup,
+ @NonNull Pdp pdp) throws PfModelException {
new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp);
}
@@ -364,13 +358,4 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
}
-
- /**
- * Hook for unit test mocking of database connection.
- *
- * @param client the mocked client
- */
- protected void setConnection(final Connection connection) {
- this.connection = connection;
- }
}