2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.provider.impl;
24 import java.io.Closeable;
25 import javax.ws.rs.core.Response;
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;
35 * This class provides an abstract models provider that can be used to initialise the database for any specialisation of
38 public abstract class AbstractModelsProvider implements Closeable {
40 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractModelsProvider.class);
42 private final PolicyModelsProviderParameters parameters;
44 // Database connection and the DAO for reading and writing Policy Framework concepts
49 * Constructor that takes the parameters.
51 * @param parameters the parameters for the provider
53 protected AbstractModelsProvider(@NonNull final PolicyModelsProviderParameters parameters) {
54 this.parameters = parameters;
58 * Initialise the provider.
60 * @throws PfModelException in initialisation errors
62 public synchronized void init() throws PfModelException {
63 LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
64 parameters.getPersistenceUnit());
67 var errorMessage = "provider is already initialized";
68 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage);
71 pfDao = ModelsProvider.init(parameters);
75 public synchronized void close() {
76 LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
77 parameters.getPersistenceUnit());
84 LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(),
85 parameters.getPersistenceUnit());