36c762e01738a40868e5fc74a079a3408f520095
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / ModelsProvider.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.util.Properties;
24 import javax.ws.rs.core.Response;
25 import lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import org.eclipse.persistence.config.PersistenceUnitProperties;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.dao.DaoParameters;
30 import org.onap.policy.models.dao.PfDao;
31 import org.onap.policy.models.dao.PfDaoFactory;
32 import org.onap.policy.models.dao.impl.DefaultPfDao;
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
34
35 @NoArgsConstructor(access = AccessLevel.PRIVATE)
36 public final class ModelsProvider {
37
38     /**
39      * Initialise the provider.
40      *
41      * @throws PfModelException in initialisation errors
42      */
43     public static PfDao init(PolicyModelsProviderParameters parameters) throws PfModelException {
44         // Parameters for the DAO
45         final var daoParameters = new DaoParameters();
46         daoParameters.setPluginClass(DefaultPfDao.class.getName());
47         daoParameters.setPersistenceUnit(parameters.getPersistenceUnit());
48
49         // @formatter:off
50         var jdbcProperties = new Properties();
51         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER,   parameters.getDatabaseDriver());
52         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,      parameters.getDatabaseUrl());
53         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER,     parameters.getDatabaseUser());
54         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, parameters.getDatabasePassword());
55         jdbcProperties.setProperty(PersistenceUnitProperties.TARGET_DATABASE,
56                         (parameters.getDatabaseType() == null ? "MySQL" : parameters.getDatabaseType()));
57         // @formatter:on
58
59         daoParameters.setJdbcProperties(jdbcProperties);
60
61         PfDao pfDao = null;
62         try {
63             pfDao = new PfDaoFactory().createPfDao(daoParameters);
64             pfDao.init(daoParameters);
65         } catch (Exception exc) {
66             String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl()
67                     + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\"";
68             if (pfDao != null) {
69                 pfDao.close();
70             }
71
72             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc);
73         }
74         return pfDao;
75     }
76
77 }