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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.provider;
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;
31 * A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation.
33 * @author Liam Fallon (liam.fallon@est.tech)
35 public class PolicyModelsProviderFactory {
36 private static final Logger LOGGER = LoggerFactory.getLogger(PolicyModelsProviderFactory.class);
39 * Creates a new PolicyModelsProvider object from its implementation.
41 * @param parameters The parameters for the implementation of the PolicyModelProvider
42 * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
44 public PolicyModelsProvider createPolicyModelsProvider(@NonNull final PolicyModelsProviderParameters parameters)
45 throws PfModelException {
46 // Get the class for the PolicyModelsProvider
47 Class<?> implementationClass = null;
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);
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);
67 PolicyModelsProvider provider = (PolicyModelsProvider) implementationClass
68 .getConstructor(PolicyModelsProviderParameters.class).newInstance(parameters);
73 } catch (Exception exc) {
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);