2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
19 * SPDX-License-Identifier: Apache-2.0
\r
20 * ============LICENSE_END=========================================================
\r
23 package org.onap.policy.api.main.rest.provider;
\r
25 import java.util.ArrayList;
\r
26 import java.util.List;
\r
27 import javax.ws.rs.core.Response;
\r
28 import org.onap.policy.api.main.parameters.ApiParameterGroup;
\r
29 import org.onap.policy.common.parameters.ParameterService;
\r
30 import org.onap.policy.models.base.PfModelException;
\r
31 import org.onap.policy.models.provider.PolicyModelsProvider;
\r
32 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
\r
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
\r
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
\r
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
\r
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
40 * Class to provide all kinds of policy type operations.
\r
42 * @author Chenfei Gao (cgao@research.att.com)
\r
44 public class PolicyTypeProvider implements AutoCloseable {
\r
46 private PolicyModelsProvider modelsProvider;
\r
49 * Default constructor.
\r
51 public PolicyTypeProvider() throws PfModelException {
\r
53 ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
\r
54 PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
\r
55 modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
\r
59 * Retrieves a list of policy types matching specified policy type ID and version.
\r
61 * @param policyTypeId the ID of policy type
\r
62 * @param policyTypeVersion the version of policy type
\r
64 * @return the ToscaServiceTemplate object
\r
66 * @throws PfModelException the PfModel parsing exception
\r
68 public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)
\r
69 throws PfModelException {
\r
71 ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()
\r
72 .name(policyTypeId).version(policyTypeVersion).build();
\r
73 ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
\r
75 if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {
\r
76 throw new PfModelException(Response.Status.NOT_FOUND,
\r
77 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
80 return serviceTemplate;
\r
84 * Retrieves a list of policy types with the latest versions.
\r
86 * @param policyTypeId the ID of policy type
\r
88 * @return the ToscaServiceTemplate object
\r
90 * @throws PfModelException the PfModel parsing exception
\r
92 public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {
\r
94 ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()
\r
95 .name(policyTypeId).version(ToscaPolicyTypeFilter.LATEST_VERSION).build();
\r
96 ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
\r
97 if (!hasPolicyType(serviceTemplate)) {
\r
98 throw new PfModelException(Response.Status.NOT_FOUND,
\r
99 constructResourceNotFoundMessage(policyTypeId, null));
\r
102 return serviceTemplate;
\r
106 * Creates a new policy type.
\r
108 * @param body the entity body of policy type
\r
110 * @return the ToscaServiceTemplate object
\r
111 * @throws PfModelException the PfModel parsing exception
\r
113 public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {
\r
115 return modelsProvider.createPolicyTypes(body);
\r
119 * Delete the policy type matching specified policy type ID and version.
\r
121 * @param policyTypeId the ID of policy type
\r
122 * @param policyTypeVersion the version of policy type
\r
124 * @return the ToscaServiceTemplate object
\r
126 * @throws PfModelException the PfModel parsing exception
\r
128 public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)
\r
129 throws PfModelException {
\r
131 validateDeleteEligibility(policyTypeId, policyTypeVersion);
\r
133 ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);
\r
135 if (!hasPolicyType(serviceTemplate)) {
\r
136 throw new PfModelException(Response.Status.NOT_FOUND,
\r
137 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
140 return serviceTemplate;
\r
144 * Validates whether specified policy type can be deleted based on the rule that
\r
145 * policy type parameterized by at least one policies cannot be deleted.
\r
147 * @param policyTypeId the ID of policy type
\r
148 * @param policyTypeVersion the version of policy type
\r
150 * @throws PfModelException the PfModel parsing exception
\r
152 private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {
\r
154 ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()
\r
155 .type(policyTypeId).typeVersion(policyTypeVersion).build();
\r
156 List<ToscaPolicy> policies = modelsProvider.getFilteredPolicyList(policyFilter);
\r
157 if (!policies.isEmpty()) {
\r
158 throw new PfModelException(Response.Status.CONFLICT,
\r
159 constructDeleteRuleViolationMessage(policyTypeId, policyTypeVersion, policies));
\r
164 * Constructs returned message for policy type delete rule violation.
\r
166 * @param policyTypeId the ID of policy type
\r
167 * @param policyTypeVersion the version of policy type
\r
168 * @param policies the list of policies that parameterizes specified policy type
\r
170 * @return the constructed message
\r
172 private String constructDeleteRuleViolationMessage(
\r
173 String policyTypeId, String policyTypeVersion, List<ToscaPolicy> policies) {
\r
175 List<String> policyNameVersionList = new ArrayList<>();
\r
176 for (ToscaPolicy policy : policies) {
\r
177 policyNameVersionList.add(policy.getName() + ":" + policy.getVersion());
\r
179 String parameterizedPolicies = String.join(",", policyNameVersionList);
\r
180 return "policy type with ID " + policyTypeId + ":" + policyTypeVersion
\r
181 + " cannot be deleted as it is parameterized by policies " + parameterizedPolicies;
\r
185 * Constructs returned message for not found resource.
\r
187 * @param policyTypeId the ID of policy type
\r
188 * @param policyTypeVersion the version of policy type
\r
190 * @return constructed message
\r
192 private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {
\r
194 return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";
\r
198 * Checks if service template contains any policy type.
\r
200 * @param serviceTemplate the service template to check against
\r
202 * @return boolean whether service template contains any policy type
\r
204 private boolean hasPolicyType(ToscaServiceTemplate serviceTemplate) {
\r
206 if (serviceTemplate.getPolicyTypes() == null) {
\r
208 } else if (serviceTemplate.getPolicyTypes().isEmpty()) {
\r
210 } else if (serviceTemplate.getPolicyTypes().get(0).isEmpty()) {
\r
218 * Closes the connection to database.
\r
220 * @throws PfModelException the PfModel parsing exception
\r
223 public void close() throws PfModelException {
\r
225 modelsProvider.close();
\r