Java 17 Upgrade
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / PolicyModelsProviderFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-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.provider;
23
24 import jakarta.ws.rs.core.Response;
25 import lombok.NonNull;
26 import org.onap.policy.models.base.PfModelException;
27 import org.onap.policy.models.dao.impl.ProxyDao;
28
29 /**
30  * A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation.
31  *
32  * @author Liam Fallon (liam.fallon@est.tech)
33  */
34 public class PolicyModelsProviderFactory {
35
36     /**
37      * Create PolicyModelsProvider.
38      *
39      * @param pfDao the ProxyDao
40      * @param parameters the PolicyModelsProviderParameters
41      * @return the PolicyModelsProvider
42      * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
43      */
44     public PolicyModelsProvider createPolicyModelsProvider(@NonNull final ProxyDao pfDao,
45             @NonNull final PolicyModelsProviderParameters parameters) throws PfModelException {
46         // Get the class for the PolicyModelsProvider
47         Class<?> implementationClass = null;
48         try {
49             // Check if the implementation class is on the classpath
50             implementationClass = Class.forName(parameters.getImplementation());
51         } catch (final Exception exc) {
52             String errorMessage = "could not find implementation of the \"PolicyModelsProvider\" interface \""
53                     + parameters.getImplementation() + "\"";
54             throw new PfModelException(Response.Status.NOT_FOUND, errorMessage, exc);
55         }
56
57         // It is, now check if it is a PolicyModelsProvider
58         if (!PolicyModelsProvider.class.isAssignableFrom(implementationClass)) {
59             String errorMessage = "the class \"" + implementationClass.getName()
60                     + "\" is not an implementation of the \"PolicyModelsProvider\" interface";
61             throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
62         }
63
64         try {
65             return (PolicyModelsProvider) implementationClass.getConstructor(ProxyDao.class).newInstance(pfDao);
66         } catch (Exception exc) {
67             String errorMessage =
68                     "could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\"";
69             throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
70         }
71     }
72
73     /**
74      * Creates a new PolicyModelsProvider object from its implementation.
75      *
76      * @param parameters The parameters for the implementation of the PolicyModelProvider
77      * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
78      */
79     public PolicyModelsProvider createPolicyModelsProvider(@NonNull final PolicyModelsProviderParameters parameters)
80             throws PfModelException {
81         // Get the class for the PolicyModelsProvider
82         Class<?> implementationClass = null;
83         try {
84             // Check if the implementation class is on the classpath
85             implementationClass = Class.forName(parameters.getImplementation());
86         } catch (final Exception exc) {
87             String errorMessage = "could not find implementation of the \"PolicyModelsProvider\" interface \""
88                     + parameters.getImplementation() + "\"";
89             throw new PfModelException(Response.Status.NOT_FOUND, errorMessage, exc);
90         }
91
92         // It is, now check if it is a PolicyModelsProvider
93         if (!PolicyModelsProvider.class.isAssignableFrom(implementationClass)) {
94             String errorMessage = "the class \"" + implementationClass.getName()
95                     + "\" is not an implementation of the \"PolicyModelsProvider\" interface";
96             throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
97         }
98
99         try {
100             PolicyModelsProvider provider = (PolicyModelsProvider) implementationClass
101                     .getConstructor(PolicyModelsProviderParameters.class).newInstance(parameters);
102
103             provider.init();
104
105             return provider;
106         } catch (Exception exc) {
107             String errorMessage =
108                     "could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\"";
109             throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
110         }
111     }
112 }