718668b9788c5bf056f31f1585e512f331d1c0fb
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / PolicyModelsProviderFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.provider;
22
23 import javax.ws.rs.core.Response;
24
25 import lombok.NonNull;
26
27 import org.onap.policy.models.base.PfModelException;
28 import org.onap.policy.models.dao.impl.DefaultPfDao;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation.
34  *
35  * @author Liam Fallon (liam.fallon@est.tech)
36  */
37 public class PolicyModelsProviderFactory {
38     private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
39
40     /**
41      * Creates a new PolicyModelsProvider object from its implementation.
42      *
43      * @param parameters The parameters for the implementation of the PolicyModelProvider
44      * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
45      */
46     public PolicyModelsProvider createPolicyModelsProvider(@NonNull final PolicyModelsProviderParameters parameters)
47             throws PfModelException {
48         // Get the class for the PolicyModelsProvider
49         Class<?> implementationClass = null;
50         try {
51             // Check if the implementation class is on the classpath
52             implementationClass = Class.forName(parameters.getImplementation());
53         } catch (final Exception exc) {
54             String errorMessage = "could not find implementation of the \"PolicyModelsProvider\" interface \""
55                     + parameters.getImplementation() + "\"";
56             LOGGER.warn(errorMessage, exc);
57             throw new PfModelException(Response.Status.NOT_FOUND, errorMessage, exc);
58         }
59
60         // It is, now check if it is a PolicyModelsProvider
61         if (!PolicyModelsProvider.class.isAssignableFrom(implementationClass)) {
62             String errorMessage = "the class \"" + implementationClass.getCanonicalName()
63                     + "\" is not an implementation of the \"PolicyModelsProvider\" interface";
64             LOGGER.warn(errorMessage);
65             throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
66         }
67
68         try {
69             return (PolicyModelsProvider) implementationClass.getConstructor(PolicyModelsProviderParameters.class)
70                     .newInstance(parameters);
71         } catch (Exception exc) {
72             String errorMessage =
73                     "could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\"";
74             LOGGER.warn(errorMessage, exc);
75             throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
76         }
77     }
78 }