2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
\r
6 * Modifications Copyright (C) 2020 Nordix Foundation.
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
20 * SPDX-License-Identifier: Apache-2.0
\r
21 * ============LICENSE_END=========================================================
\r
24 package org.onap.policy.api.main.rest.provider;
\r
26 import java.util.ArrayList;
\r
27 import java.util.List;
\r
28 import java.util.Map.Entry;
\r
30 import javax.ws.rs.core.Response;
\r
32 import org.onap.policy.models.base.PfModelException;
\r
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
\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 extends CommonModelProvider {
\r
47 * Default constructor.
\r
49 public PolicyTypeProvider() throws PfModelException {
\r
54 * Retrieves a list of policy types matching specified policy type ID and version.
\r
56 * @param policyTypeId the ID of policy type
\r
57 * @param policyTypeVersion the version of policy type
\r
59 * @return the ToscaServiceTemplate object
\r
61 * @throws PfModelException the PfModel parsing exception
\r
63 public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)
\r
64 throws PfModelException {
\r
66 ToscaServiceTemplate serviceTemplate = getFilteredPolicyTypes(policyTypeId, policyTypeVersion);
\r
68 if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {
\r
69 throw new PfModelException(Response.Status.NOT_FOUND,
\r
70 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
73 return serviceTemplate;
\r
77 * Retrieves a list of policy types with the latest versions.
\r
79 * @param policyTypeId the ID of policy type
\r
81 * @return the ToscaServiceTemplate object
\r
83 * @throws PfModelException the PfModel parsing exception
\r
85 public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {
\r
87 ToscaServiceTemplate serviceTemplate =
\r
88 getFilteredPolicyTypes(policyTypeId, ToscaPolicyTypeFilter.LATEST_VERSION);
\r
89 if (!hasPolicyType(serviceTemplate)) {
\r
90 throw new PfModelException(Response.Status.NOT_FOUND, constructResourceNotFoundMessage(policyTypeId, null));
\r
93 return serviceTemplate;
\r
97 * Creates a new policy type.
\r
99 * @param body the entity body of policy type
\r
101 * @return the ToscaServiceTemplate object
\r
102 * @throws PfModelException the PfModel parsing exception
\r
104 public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {
\r
106 if (!hasPolicyType(body)) {
\r
107 throw new PfModelException(Response.Status.BAD_REQUEST,
\r
108 "no policy types specified in the service template");
\r
110 validatePolicyTypeVersionExist(body);
\r
111 return modelsProvider.createPolicyTypes(body);
\r
115 * Delete the policy type matching specified policy type ID and version.
\r
117 * @param policyTypeId the ID of policy type
\r
118 * @param policyTypeVersion the version of policy type
\r
120 * @return the ToscaServiceTemplate object
\r
122 * @throws PfModelException the PfModel parsing exception
\r
124 public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)
\r
125 throws PfModelException {
\r
127 validateDeleteEligibility(policyTypeId, policyTypeVersion);
\r
129 ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);
\r
131 if (!hasPolicyType(serviceTemplate)) {
\r
132 throw new PfModelException(Response.Status.NOT_FOUND,
\r
133 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));
\r
136 return serviceTemplate;
\r
140 * Validates whether specified policy type can be deleted based on the rule that policy type parameterized by at
\r
141 * least one policies cannot be deleted.
\r
143 * @param policyTypeId the ID of policy type
\r
144 * @param policyTypeVersion the version of policy type
\r
146 * @throws PfModelException the PfModel parsing exception
\r
148 private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {
\r
150 ToscaPolicyFilter policyFilter =
\r
151 ToscaPolicyFilter.builder().type(policyTypeId).typeVersion(policyTypeVersion).build();
\r
152 List<ToscaPolicy> policies = modelsProvider.getFilteredPolicyList(policyFilter);
\r
153 if (!policies.isEmpty()) {
\r
154 throw new PfModelException(Response.Status.CONFLICT,
\r
155 constructDeletePolicyTypeViolationMessage(policyTypeId, policyTypeVersion, policies));
\r
160 * Validates that each policy type has a version specified in the payload.
\r
162 * @param body the TOSCA service template payload to check against
\r
164 * @throws PfModelException the PfModel parsing exception
\r
166 private void validatePolicyTypeVersionExist(ToscaServiceTemplate body) throws PfModelException {
\r
168 List<String> invalidPolicyTypeNames = new ArrayList<>();
\r
169 for (Entry<String, ToscaPolicyType> policyType : body.getPolicyTypes().entrySet()) {
\r
170 if (!"tosca.policies.Root".equals(policyType.getValue().getDerivedFrom())
\r
171 && policyType.getValue().getVersion() == null) {
\r
172 invalidPolicyTypeNames.add(policyType.getKey());
\r
176 if (!invalidPolicyTypeNames.isEmpty()) {
\r
177 String errorMsg = "mandatory 'version' field is missing in policy types: "
\r
178 + String.join(", ", invalidPolicyTypeNames);
\r
179 throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMsg);
\r
184 * Retrieves the specified version of the policy type.
\r
186 * @param policyTypeName the name of the policy type
\r
187 * @param policyTypeVersion the version of the policy type
\r
189 * @return the TOSCA service template containing the specified version of the policy type
\r
191 * @throws PfModelException the PfModel parsing exception
\r
193 private ToscaServiceTemplate getFilteredPolicyTypes(String policyTypeName, String policyTypeVersion)
\r
194 throws PfModelException {
\r
196 ToscaPolicyTypeFilter policyTypeFilter =
\r
197 ToscaPolicyTypeFilter.builder().name(policyTypeName).version(policyTypeVersion).build();
\r
198 return modelsProvider.getFilteredPolicyTypes(policyTypeFilter);
\r
202 * Constructs returned message for not found resource.
\r
204 * @param policyTypeId the ID of policy type
\r
205 * @param policyTypeVersion the version of policy type
\r
207 * @return constructed message
\r
209 private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {
\r
211 return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";
\r