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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.provider;
23 import javax.ws.rs.core.Response;
25 import lombok.NonNull;
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;
33 * A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation.
35 * @author Liam Fallon (liam.fallon@est.tech)
37 public class PolicyModelsProviderFactory {
38 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
41 * Creates a new PolicyModelsProvider object from its implementation.
43 * @param parameters The parameters for the implementation of the PolicyModelProvider
44 * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
46 public PolicyModelsProvider createPolicyModelsProvider(@NonNull final PolicyModelsProviderParameters parameters)
47 throws PfModelException {
48 // Get the class for the PolicyModelsProvider
49 Class<?> implementationClass = null;
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);
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);
69 return (PolicyModelsProvider) implementationClass.getConstructor(PolicyModelsProviderParameters.class)
70 .newInstance(parameters);
71 } catch (Exception exc) {
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);