Allow providers other than PolicyModelsProvider
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / AbstractModelsProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.provider.impl;
22
23 import java.io.Closeable;
24 import java.util.Properties;
25 import javax.ws.rs.core.Response;
26 import lombok.Getter;
27 import lombok.NonNull;
28 import org.eclipse.persistence.config.PersistenceUnitProperties;
29 import org.onap.policy.models.base.PfModelException;
30 import org.onap.policy.models.dao.DaoParameters;
31 import org.onap.policy.models.dao.PfDao;
32 import org.onap.policy.models.dao.PfDaoFactory;
33 import org.onap.policy.models.dao.impl.DefaultPfDao;
34 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * This class provides an abstract models provider that can be used to initialise the database for any specialisation of
40  * the class.
41  */
42 public abstract class AbstractModelsProvider implements Closeable {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractModelsProvider.class);
45
46     private final PolicyModelsProviderParameters parameters;
47
48     // Database connection and the DAO for reading and writing Policy Framework concepts
49     @Getter
50     private PfDao pfDao;
51
52     /**
53      * Constructor that takes the parameters.
54      *
55      * @param parameters the parameters for the provider
56      */
57     protected AbstractModelsProvider(@NonNull final PolicyModelsProviderParameters parameters) {
58         this.parameters = parameters;
59     }
60
61     /**
62      * Initialise the provider.
63      *
64      * @throws PfModelException in initialisation errors
65      */
66     public synchronized void init() throws PfModelException {
67         LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
68                 parameters.getPersistenceUnit());
69
70         if (pfDao != null) {
71             String errorMessage = "provider is already initialized";
72             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
73         }
74
75         // Parameters for the DAO
76         final DaoParameters daoParameters = new DaoParameters();
77         daoParameters.setPluginClass(DefaultPfDao.class.getName());
78         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
79
80         // @formatter:off
81         Properties jdbcProperties = new Properties();
82         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER,   parameters.getDatabaseDriver());
83         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,      parameters.getDatabaseUrl());
84         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER,     parameters.getDatabaseUser());
85         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
86         // @formatter:on
87
88         daoParameters.setJdbcProperties(jdbcProperties);
89
90         try {
91             pfDao = new PfDaoFactory().createPfDao(daoParameters);
92             pfDao.init(daoParameters);
93         } catch (Exception exc) {
94             String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
95                     + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
96
97             this.close();
98             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
99         }
100     }
101
102     @Override
103     public synchronized void close() {
104         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
105                 parameters.getPersistenceUnit());
106
107         if (pfDao != null) {
108             pfDao.close();
109             pfDao = null;
110         }
111
112         LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
113                 parameters.getPersistenceUnit());
114     }
115 }