Fix config files to remove outdated configuration for hibernate
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / ModelsProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021, 2023-2024 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 jakarta.ws.rs.core.Response;
24 import java.util.Properties;
25 import lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import org.onap.policy.models.base.PfModelException;
28 import org.onap.policy.models.dao.DaoParameters;
29 import org.onap.policy.models.dao.PfDao;
30 import org.onap.policy.models.dao.PfDaoFactory;
31 import org.onap.policy.models.dao.impl.DefaultPfDao;
32 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
33
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public final class ModelsProvider {
36
37     /**
38      * Initialise the provider.
39      *
40      * @throws PfModelException in initialisation errors
41      */
42     public static PfDao init(PolicyModelsProviderParameters parameters) throws PfModelException {
43         // Parameters for the DAO
44         final var daoParameters = new DaoParameters();
45         daoParameters.setPluginClass(DefaultPfDao.class.getName());
46         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
47
48         var jdbcProperties = new Properties();
49         jdbcProperties.setProperty("jakarta.persistence.jdbc.driver",   parameters.getDatabaseDriver());
50         jdbcProperties.setProperty("jakarta.persistence.jdbc.url",      parameters.getDatabaseUrl());
51         jdbcProperties.setProperty("jakarta.persistence.jdbc.user",     parameters.getDatabaseUser());
52         jdbcProperties.setProperty("jakarta.persistence.jdbc.password", parameters.getDatabasePassword());
53
54         daoParameters.setJdbcProperties(jdbcProperties);
55
56         PfDao pfDao = null;
57         try {
58             pfDao = new PfDaoFactory().createPfDao(daoParameters);
59             pfDao.init(daoParameters);
60         } catch (Exception exc) {
61             String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
62                     + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
63             if (pfDao != null) {
64                 pfDao.close();
65             }
66
67             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
68         }
69         return pfDao;
70     }
71
72 }