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;
\r
30 import javax.ws.rs.core.Response;
\r
32 import org.apache.commons.lang3.tuple.Pair;
\r
33 import org.onap.policy.models.base.PfConceptKey;
\r
34 import org.onap.policy.models.base.PfModelException;
\r
35 import org.onap.policy.models.pdp.concepts.PdpGroup;
\r
36 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
\r
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
\r
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
\r
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
\r
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
44 * Class to provide all kinds of policy operations.
\r
46 * @author Chenfei Gao (cgao@research.att.com)
\r
48 public class PolicyProvider extends CommonModelProvider {
\r
51 * Default constructor.
\r
53 public PolicyProvider() throws PfModelException {
\r
58 * Retrieves a list of policies matching specified ID and version of both policy type and policy.
\r
60 * @param policyTypeId the ID of policy type
\r
61 * @param policyTypeVersion the version of policy type
\r
62 * @param policyId the ID of policy
\r
63 * @param policyVersion the version of policy
\r
65 * @return the ToscaServiceTemplate object
\r
67 * @throws PfModelException the PfModel parsing exception
\r
69 public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,
\r
70 String policyVersion) throws PfModelException {
\r
72 ToscaServiceTemplate serviceTemplate =
\r
73 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);
\r
75 if (!hasPolicy(serviceTemplate)) {
\r
76 throw new PfModelException(Response.Status.NOT_FOUND,
\r
77 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));
\r
80 return serviceTemplate;
\r
84 * Retrieves a list of policies with the latest versions that match specified policy type id and version.
\r
86 * @param policyTypeId the ID of policy type
\r
87 * @param policyTypeVersion the version of policy type
\r
88 * @param policyId the ID of the policy
\r
90 * @return the ToscaServiceTemplate object
\r
92 * @throws PfModelException the PfModel parsing exception
\r
94 public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)
\r
95 throws PfModelException {
\r
97 ToscaServiceTemplate serviceTemplate =
\r
98 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);
\r
100 if (!hasPolicy(serviceTemplate)) {
\r
101 throw new PfModelException(Response.Status.NOT_FOUND,
\r
102 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, null));
\r
105 return serviceTemplate;
\r
109 * Retrieves a list of deployed policies in each pdp group.
\r
111 * @param policyTypeId the ID of policy type
\r
112 * @param policyTypeVersion the version of policy type
\r
113 * @param policyId the ID of the policy
\r
115 * @return a list of deployed policies in each pdp group
\r
117 * @throws PfModelException the PfModel parsing exception
\r
119 public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,
\r
120 String policyTypeVersion, String policyId) throws PfModelException {
\r
122 return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),
\r
123 modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));
\r
127 * Creates one or more new policies for the same policy type ID and version.
\r
129 * @param policyTypeId the ID of policy type
\r
130 * @param policyTypeVersion the version of policy type
\r
131 * @param body the entity body of polic(ies)
\r
133 * @return the ToscaServiceTemplate object
\r
135 * @throws PfModelException the PfModel parsing exception
\r
137 public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
\r
138 throws PfModelException {
\r
140 return modelsProvider.createPolicies(body);
\r
144 * Creates one or more new policies.
\r
146 * @param body the entity body of policy
\r
148 * @return the ToscaServiceTemplate object
\r
150 * @throws PfModelException the PfModel parsing exception
\r
152 public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {
\r
153 return modelsProvider.createPolicies(body);
\r
157 * Deletes the policy matching specified ID and version of both policy type and policy.
\r
159 * @param policyTypeId the ID of policy type
\r
160 * @param policyTypeVersion the version of policy type
\r
161 * @param policyId the ID of policy
\r
162 * @param policyVersion the version of policy
\r
164 * @return the ToscaServiceTemplate object
\r
166 * @throws PfModelException the PfModel parsing exception
\r
168 public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,
\r
169 String policyVersion) throws PfModelException {
\r
171 validateDeleteEligibility(policyTypeId, policyTypeVersion, policyId, policyVersion);
\r
173 ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);
\r
175 if (!hasPolicy(serviceTemplate)) {
\r
176 throw new PfModelException(Response.Status.NOT_FOUND,
\r
177 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));
\r
180 return serviceTemplate;
\r
184 * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.
\r
186 * @param policyTypeId the ID of policy type
\r
187 * @param policyTypeVersion the version of policy type
\r
188 * @param policyId the ID of policy
\r
189 * @param policyVersion the version of policy
\r
191 * @throws PfModelException the PfModel parsing exception
\r
193 private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion, String policyId,
\r
194 String policyVersion) throws PfModelException {
\r
196 // TODO: Remove this method when delete validation is implemented in the tosca provider
\r
197 List<ToscaPolicyTypeIdentifier> policyTypes = new ArrayList<>(1);
\r
198 policyTypes.add(new ToscaPolicyTypeIdentifier(policyTypeId, policyTypeVersion));
\r
199 List<ToscaPolicyIdentifier> policies = new ArrayList<>(1);
\r
200 policies.add(new ToscaPolicyIdentifier(policyId, policyVersion));
\r
201 PdpGroupFilter pdpGroupFilter =
\r
202 PdpGroupFilter.builder().policyTypeList(policyTypes).policyList(policies).build();
\r
204 List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
\r
206 if (!pdpGroups.isEmpty()) {
\r
207 throw new PfModelException(Response.Status.CONFLICT,
\r
208 constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));
\r
213 * Retrieves the specified version of the policy.
\r
215 * @param policyTypeName the name of the policy type
\r
216 * @param policyTypeVersion the version of the policy type
\r
217 * @param policyName the name of the policy
\r
218 * @param policyVersion the version of the policy
\r
220 * @return the TOSCA service template containing the specified version of the policy
\r
222 * @throws PfModelException the PfModel parsing exception
\r
224 private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,
\r
225 String policyVersion) throws PfModelException {
\r
227 ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)
\r
228 .type(policyTypeName).typeVersion(policyTypeVersion).build();
\r
229 return modelsProvider.getFilteredPolicies(policyFilter);
\r
233 * Constructs returned message for not found resource.
\r
235 * @param policyTypeId the ID of policy type
\r
236 * @param policyTypeVersion the version of policy type
\r
237 * @param policyId the ID of policy
\r
238 * @param policyVersion the version of policy
\r
240 * @return constructed message
\r
242 private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion, String policyId,
\r
243 String policyVersion) {
\r
245 return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":"
\r
246 + policyTypeVersion + " does not exist";
\r