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.List;
\r
26 import javax.ws.rs.core.Response;
\r
27 import org.onap.policy.models.base.PfModelException;
\r
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
\r
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
\r
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
34 * Class to provide all kinds of policy type operations.
\r
36 * @author Chenfei Gao (cgao@research.att.com)
\r
38 public class PolicyTypeProvider extends CommonModelProvider {
\r
41 * Default constructor.
\r
43 public PolicyTypeProvider() throws PfModelException {
\r
48 * Retrieves a list of policy types matching specified policy type ID and version.
\r
50 * @param policyTypeId the ID of policy type
\r
51 * @param policyTypeVersion the version of policy type
\r
53 * @return the ToscaServiceTemplate object
\r
55 * @throws PfModelException the PfModel parsing exception
\r
57 public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)
\r
58 throws PfModelException {
\r
60 ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()
\r
61 .name(policyTypeId).version(policyTypeVersion).build();
\r
62 ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
\r
64 if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {
\r
65 throw new PfModelException(Response.Status.NOT_FOUND,
\r
66 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
69 return serviceTemplate;
\r
73 * Retrieves a list of policy types with the latest versions.
\r
75 * @param policyTypeId the ID of policy type
\r
77 * @return the ToscaServiceTemplate object
\r
79 * @throws PfModelException the PfModel parsing exception
\r
81 public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {
\r
83 ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()
\r
84 .name(policyTypeId).version(ToscaPolicyTypeFilter.LATEST_VERSION).build();
\r
85 ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
\r
86 if (!hasPolicyType(serviceTemplate)) {
\r
87 throw new PfModelException(Response.Status.NOT_FOUND,
\r
88 constructResourceNotFoundMessage(policyTypeId, null));
\r
91 return serviceTemplate;
\r
95 * Creates a new policy type.
\r
97 * @param body the entity body of policy type
\r
99 * @return the ToscaServiceTemplate object
\r
100 * @throws PfModelException the PfModel parsing exception
\r
102 public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {
\r
104 return modelsProvider.createPolicyTypes(body);
\r
108 * Delete the policy type matching specified policy type ID and version.
\r
110 * @param policyTypeId the ID of policy type
\r
111 * @param policyTypeVersion the version of policy type
\r
113 * @return the ToscaServiceTemplate object
\r
115 * @throws PfModelException the PfModel parsing exception
\r
117 public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)
\r
118 throws PfModelException {
\r
120 validateDeleteEligibility(policyTypeId, policyTypeVersion);
\r
122 ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);
\r
124 if (!hasPolicyType(serviceTemplate)) {
\r
125 throw new PfModelException(Response.Status.NOT_FOUND,
\r
126 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
129 return serviceTemplate;
\r
133 * Validates whether specified policy type can be deleted based on the rule that
\r
134 * policy type parameterized by at least one policies cannot be deleted.
\r
136 * @param policyTypeId the ID of policy type
\r
137 * @param policyTypeVersion the version of policy type
\r
139 * @throws PfModelException the PfModel parsing exception
\r
141 private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {
\r
143 ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()
\r
144 .type(policyTypeId).typeVersion(policyTypeVersion).build();
\r
145 List<ToscaPolicy> policies = modelsProvider.getFilteredPolicyList(policyFilter);
\r
146 if (!policies.isEmpty()) {
\r
147 throw new PfModelException(Response.Status.CONFLICT,
\r
148 constructDeletePolicyTypeViolationMessage(policyTypeId, policyTypeVersion, policies));
\r
153 * Constructs returned message for not found resource.
\r
155 * @param policyTypeId the ID of policy type
\r
156 * @param policyTypeVersion the version of policy type
\r
158 * @return constructed message
\r
160 private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {
\r
162 return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";
\r