ed47ae0a0edf4009b51bfb9229ebaab7d031817a
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DatabasePolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020, 2022 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.provider.impl;
24
25 import javax.ws.rs.core.Response;
26 import lombok.Getter;
27 import lombok.NonNull;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.dao.PfDao;
30 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards
36  * a relational database.
37  *
38  * @author Liam Fallon (liam.fallon@est.tech)
39  */
40 public class DatabasePolicyModelsProviderImpl extends AbstractPolicyModelsProvider {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class);
43
44     private final PolicyModelsProviderParameters parameters;
45
46     // Database connection and the DAO for reading and writing Policy Framework concepts
47     @Getter
48     private PfDao pfDao;
49
50     /**
51      * Constructor that takes the parameters.
52      *
53      * @param parameters the parameters for the provider
54      */
55     public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
56         this.parameters = parameters;
57     }
58
59     @Override
60     public synchronized void init() throws PfModelException {
61         LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
62                 parameters.getPersistenceUnit());
63
64         if (pfDao != null) {
65             var errorMessage = "provider is already initialized";
66             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
67         }
68
69         pfDao = ModelsProvider.init(parameters);
70     }
71
72     @Override
73     public synchronized void close() throws PfModelException {
74         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
75                 parameters.getPersistenceUnit());
76
77         if (pfDao != null) {
78             pfDao.close();
79             pfDao = null;
80         }
81
82         LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
83                 parameters.getPersistenceUnit());
84     }
85
86 }