Java 17 Upgrade
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / AbstractModelsProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.provider.impl;
23
24 import jakarta.ws.rs.core.Response;
25 import java.io.Closeable;
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 abstract models provider that can be used to initialise the database for any specialisation of
36  * the class.
37  */
38 public abstract class AbstractModelsProvider implements Closeable {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractModelsProvider.class);
41
42     private final PolicyModelsProviderParameters parameters;
43
44     // Database connection and the DAO for reading and writing Policy Framework concepts
45     @Getter
46     private PfDao pfDao;
47
48     /**
49      * Constructor that takes the parameters.
50      *
51      * @param parameters the parameters for the provider
52      */
53     protected AbstractModelsProvider(@NonNull final PolicyModelsProviderParameters parameters) {
54         this.parameters = parameters;
55     }
56
57     /**
58      * Initialise the provider.
59      *
60      * @throws PfModelException in initialisation errors
61      */
62     public synchronized void init() throws PfModelException {
63         LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
64                 parameters.getPersistenceUnit());
65
66         if (pfDao != null) {
67             var errorMessage = "provider is already initialized";
68             throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
69         }
70
71         pfDao = ModelsProvider.init(parameters);
72     }
73
74     @Override
75     public synchronized void close() {
76         LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
77                 parameters.getPersistenceUnit());
78
79         if (pfDao != null) {
80             pfDao.close();
81             pfDao = null;
82         }
83
84         LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
85                 parameters.getPersistenceUnit());
86     }
87 }