Address sonar issues in models
[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  *  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 javax.ws.rs.core.Response;
25 import lombok.NonNull;
26 import org.onap.policy.models.base.PfModelException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation.
32  *
33  * @author Liam Fallon (liam.fallon@est.tech)
34  */
35 public class PolicyModelsProviderFactory {
36     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyModelsProviderFactory.class);
37
38     /**
39      * Creates a new PolicyModelsProvider object from its implementation.
40      *
41      * @param parameters The parameters for the implementation of the PolicyModelProvider
42      * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
43      */
44     public PolicyModelsProvider createPolicyModelsProvider(@NonNull final PolicyModelsProviderParameters parameters)
45             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             LOGGER.warn(errorMessage);
55             throw new PfModelException(Response.Status.NOT_FOUND, errorMessage, exc);
56         }
57
58         // It is, now check if it is a PolicyModelsProvider
59         if (!PolicyModelsProvider.class.isAssignableFrom(implementationClass)) {
60             String errorMessage = "the class \"" + implementationClass.getName()
61                     + "\" is not an implementation of the \"PolicyModelsProvider\" interface";
62             LOGGER.warn(errorMessage);
63             throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage);
64         }
65
66         try {
67             PolicyModelsProvider provider = (PolicyModelsProvider) implementationClass
68                     .getConstructor(PolicyModelsProviderParameters.class).newInstance(parameters);
69
70             provider.init();
71
72             return provider;
73         } catch (Exception exc) {
74             String errorMessage =
75                     "could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\"";
76             LOGGER.warn(errorMessage);
77             throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
78         }
79     }
80 }