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.onap.policy.models.dao.impl.ProxyDao;
30 * A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation.
32 * @author Liam Fallon (liam.fallon@est.tech)
34 public class PolicyModelsProviderFactory {
37 * Create PolicyModelsProvider.
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
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;
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);
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);
65 return (PolicyModelsProvider) implementationClass.getConstructor(ProxyDao.class).newInstance(pfDao);
66 } catch (Exception exc) {
68 "could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\"";
69 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc);
74 * Creates a new PolicyModelsProvider object from its implementation.
76 * @param parameters The parameters for the implementation of the PolicyModelProvider
77 * @throws PfModelException on errors creating an implementation of the PolicyModelProvider
79 public PolicyModelsProvider createPolicyModelsProvider(@NonNull final PolicyModelsProviderParameters parameters)
80 throws PfModelException {
81 // Get the class for the PolicyModelsProvider
82 Class<?> implementationClass = null;
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);
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);
100 PolicyModelsProvider provider = (PolicyModelsProvider) implementationClass
101 .getConstructor(PolicyModelsProviderParameters.class).newInstance(parameters);
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);