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.tosca.authorative.concepts.ToscaPolicy;
\r
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
\r
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
40 * Class to provide all kinds of policy operations.
\r
42 * @author Chenfei Gao (cgao@research.att.com)
\r
44 public class PolicyProvider extends CommonModelProvider {
\r
47 * Default constructor.
\r
49 public PolicyProvider() throws PfModelException {
\r
54 * Retrieves a list of policies matching specified ID and version of both policy type and policy.
\r
56 * @param policyTypeId the ID of policy type
\r
57 * @param policyTypeVersion the version of policy type
\r
58 * @param policyId the ID of policy
\r
59 * @param policyVersion the version of policy
\r
61 * @return the ToscaServiceTemplate object
\r
63 * @throws PfModelException the PfModel parsing exception
\r
65 public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,
\r
66 String policyVersion) throws PfModelException {
\r
68 ToscaServiceTemplate serviceTemplate =
\r
69 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);
\r
71 if (!hasPolicy(serviceTemplate)) {
\r
72 throw new PfModelException(Response.Status.NOT_FOUND,
\r
73 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));
\r
76 return serviceTemplate;
\r
80 * Retrieves a list of policies with the latest versions that match specified policy type id and version.
\r
82 * @param policyTypeId the ID of policy type
\r
83 * @param policyTypeVersion the version of policy type
\r
84 * @param policyId the ID of the policy
\r
86 * @return the ToscaServiceTemplate object
\r
88 * @throws PfModelException the PfModel parsing exception
\r
90 public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)
\r
91 throws PfModelException {
\r
93 ToscaServiceTemplate serviceTemplate =
\r
94 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);
\r
96 if (!hasPolicy(serviceTemplate)) {
\r
97 throw new PfModelException(Response.Status.NOT_FOUND,
\r
98 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, null));
\r
101 return serviceTemplate;
\r
105 * Retrieves a list of deployed policies in each pdp group.
\r
107 * @param policyTypeId the ID of policy type
\r
108 * @param policyTypeVersion the version of policy type
\r
109 * @param policyId the ID of the policy
\r
111 * @return a list of deployed policies in each pdp group
\r
113 * @throws PfModelException the PfModel parsing exception
\r
115 public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,
\r
116 String policyTypeVersion, String policyId) throws PfModelException {
\r
118 return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),
\r
119 modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));
\r
123 * Creates one or more new policies for the same policy type ID and version.
\r
125 * @param policyTypeId the ID of policy type
\r
126 * @param policyTypeVersion the version of policy type
\r
127 * @param body the entity body of polic(ies)
\r
129 * @return the ToscaServiceTemplate object
\r
131 * @throws PfModelException the PfModel parsing exception
\r
133 public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
\r
134 throws PfModelException {
\r
136 return modelsProvider.createPolicies(body);
\r
140 * Creates one or more new policies.
\r
142 * @param body the entity body of policy
\r
144 * @return the ToscaServiceTemplate object
\r
146 * @throws PfModelException the PfModel parsing exception
\r
148 public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {
\r
149 return modelsProvider.createPolicies(body);
\r
153 * Deletes the policy matching specified ID and version of both policy type and policy.
\r
155 * @param policyTypeId the ID of policy type
\r
156 * @param policyTypeVersion the version of policy type
\r
157 * @param policyId the ID of policy
\r
158 * @param policyVersion the version of policy
\r
160 * @return the ToscaServiceTemplate object
\r
162 * @throws PfModelException the PfModel parsing exception
\r
164 public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,
\r
165 String policyVersion) throws PfModelException {
\r
167 ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);
\r
169 if (!hasPolicy(serviceTemplate)) {
\r
170 throw new PfModelException(Response.Status.NOT_FOUND,
\r
171 constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));
\r
174 return serviceTemplate;
\r
178 * Retrieves the specified version of the policy.
\r
180 * @param policyTypeName the name of the policy type
\r
181 * @param policyTypeVersion the version of the policy type
\r
182 * @param policyName the name of the policy
\r
183 * @param policyVersion the version of the policy
\r
185 * @return the TOSCA service template containing the specified version of the policy
\r
187 * @throws PfModelException the PfModel parsing exception
\r
189 private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,
\r
190 String policyVersion) throws PfModelException {
\r
192 ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)
\r
193 .type(policyTypeName).typeVersion(policyTypeVersion).build();
\r
194 return modelsProvider.getFilteredPolicies(policyFilter);
\r
198 * Constructs returned message for not found resource.
\r
200 * @param policyTypeId the ID of policy type
\r
201 * @param policyTypeVersion the version of policy type
\r
202 * @param policyId the ID of policy
\r
203 * @param policyVersion the version of policy
\r
205 * @return constructed message
\r
207 private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion, String policyId,
\r
208 String policyVersion) {
\r
210 return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":"
\r
211 + policyTypeVersion + " does not exist";
\r