d7d1784eb3084776071bbbe5b93be6a9e34577ab
[policy/models.git] / models-dao / src / main / java / org / onap / policy / models / dao / PfDaoFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 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.dao;
23
24 import java.lang.reflect.InvocationTargetException;
25 import javax.ws.rs.core.Response;
26
27 import org.onap.policy.common.utils.validation.Assertions;
28 import org.onap.policy.models.base.PfModelException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * This factory class returns a Policy Framework DAO for the configured persistence mechanism. The
34  * factory uses the plugin class specified in {@link DaoParameters} to instantiate a DAO instance.
35  *
36  * @author Liam Fallon (liam.fallon@est.tech)
37  */
38 public class PfDaoFactory {
39     // Get a reference to the logger
40     private static final Logger LOGGER = LoggerFactory.getLogger(PfDaoFactory.class);
41
42     /**
43      * Return a Policy Framework DAO for the required Policy Framework DAO plugin class.
44      *
45      * @param daoParameters parameters to use to read the database configuration information
46      * @return the Policy Framework DAO
47      * @throws PfModelException on invalid JPA plugins
48      */
49     public PfDao createPfDao(final DaoParameters daoParameters) throws PfModelException {
50         Assertions.argumentOfClassNotNull(daoParameters, PfModelException.class,
51                 "Parameter \"daoParameters\" may not be null");
52
53         // Get the class for the DAO using reflection
54         Object pfDaoObject = null;
55         try {
56             pfDaoObject = Class.forName(daoParameters.getPluginClass()).getDeclaredConstructor().newInstance();
57         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException
58                         | InvocationTargetException | NoSuchMethodException | SecurityException e) {
59             String errorMessage =
60                     "Policy Framework DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"";
61             LOGGER.error(errorMessage);
62             throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, e);
63         }
64
65         // Check the class is a Policy Framework DAO
66         if (!(pfDaoObject instanceof PfDao)) {
67             String errorMessage = "Specified DAO plugin class \"" + daoParameters.getPluginClass()
68                     + "\" does not implement the PfDao interface";
69             LOGGER.error(errorMessage);
70             throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage);
71         }
72
73         return (PfDao) pfDaoObject;
74     }
75 }