Java 17 Upgrade
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderImpl.java
index 85c4d13..8f4cdbc 100644 (file)
@@ -1,6 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2021, 2023 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2020, 2022 Bell Canada. 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.
 
 package org.onap.policy.models.provider.impl;
 
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.util.Base64;
-import java.util.Map;
-
-import javax.ws.rs.core.Response;
-
+import jakarta.ws.rs.core.Response;
+import lombok.Getter;
 import lombok.NonNull;
-
-import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.base.PfModelRuntimeException;
-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.pdp.concepts.PdpGroups;
-import org.onap.policy.models.pdp.persistence.provider.PdpProvider;
-import org.onap.policy.models.provider.PolicyModelsProvider;
 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
-import org.onap.policy.models.tosca.legacy.provider.LegacyProvider;
-import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
-import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -55,13 +37,14 @@ 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);
+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
-    private Connection connection;
+    @Getter
     private PfDao pfDao;
 
     /**
@@ -74,43 +57,20 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider {
     }
 
     @Override
-    public void init() throws PfModelException {
+    public synchronized void init() throws PfModelException {
         LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
                 parameters.getPersistenceUnit());
 
-        // 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);
+        if (pfDao != null) {
+            var errorMessage = "provider is already initialized";
+            throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
         }
 
-        // Parameters for the DAO
-        final DaoParameters daoParameters = new DaoParameters();
-        daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
-        daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
-
-        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() + "\"";
-            LOGGER.warn(errorMessage, exc);
-
-            this.close();
-            throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
-        }
+        pfDao = ModelsProvider.init(parameters);
     }
 
     @Override
-    public void close() throws PfModelException {
+    public synchronized void close() throws PfModelException {
         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
                 parameters.getPersistenceUnit());
 
@@ -119,171 +79,8 @@ 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());
     }
 
-    @Override
-    public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().getPolicyTypes(pfDao, policyTypeKey);
-    }
-
-    @Override
-    public JpaToscaServiceTemplate createPolicyTypes(@NonNull final JpaToscaServiceTemplate serviceTemplate)
-            throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate);
-    }
-
-    @Override
-    public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final JpaToscaServiceTemplate serviceTemplate)
-            throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate);
-    }
-
-    @Override
-    public JpaToscaServiceTemplate deletePolicyTypes(@NonNull final PfConceptKey policyTypeKey)
-            throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().deletePolicyTypes(pfDao, policyTypeKey);
-    }
-
-    @Override
-    public JpaToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().getPolicies(pfDao, policyKey);
-    }
-
-    @Override
-    public JpaToscaServiceTemplate createPolicies(@NonNull final JpaToscaServiceTemplate serviceTemplate)
-            throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate);
-    }
-
-    @Override
-    public JpaToscaServiceTemplate updatePolicies(@NonNull final JpaToscaServiceTemplate serviceTemplate)
-            throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().updatePolicies(pfDao, serviceTemplate);
-    }
-
-    @Override
-    public JpaToscaServiceTemplate deletePolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
-        assertInitilized();
-        return new SimpleToscaProvider().deletePolicies(pfDao, policyKey);
-    }
-
-    @Override
-    public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().getOperationalPolicy(pfDao, policyId);
-    }
-
-    @Override
-    public LegacyOperationalPolicy createOperationalPolicy(
-            @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy);
-    }
-
-    @Override
-    public LegacyOperationalPolicy updateOperationalPolicy(
-            @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy);
-    }
-
-    @Override
-    public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId);
-    }
-
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().getGuardPolicy(pfDao, policyId);
-    }
-
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
-            @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy);
-    }
-
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
-            @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy);
-    }
-
-    @Override
-    public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
-            throws PfModelException {
-        assertInitilized();
-        return new LegacyProvider().deleteGuardPolicy(pfDao, policyId);
-    }
-
-    @Override
-    public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
-        assertInitilized();
-        return new PdpProvider().getPdpGroups(pfDao, pdpGroupFilter);
-    }
-
-    @Override
-    public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
-        assertInitilized();
-        return new PdpProvider().createPdpGroups(pfDao, pdpGroups);
-    }
-
-    @Override
-    public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
-        assertInitilized();
-        return new PdpProvider().updatePdpGroups(pfDao, pdpGroups);
-    }
-
-    @Override
-    public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
-        assertInitilized();
-        return new PdpProvider().deletePdpGroups(pfDao, pdpGroupFilter);
-    }
-
-    /**
-     * Check if the model provider is initialized.
-     */
-    private void assertInitilized() {
-        if (pfDao == null) {
-            String errorMessage = "policy models provider is not initilaized";
-            LOGGER.warn(errorMessage);
-            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;
-    }
 }