Update API for changes in TOSCA provider
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy API\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
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
19  *\r
20  * SPDX-License-Identifier: Apache-2.0\r
21  * ============LICENSE_END=========================================================\r
22  */\r
23 \r
24 package org.onap.policy.api.main.rest.provider;\r
25 \r
26 import java.util.ArrayList;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 \r
30 import javax.ws.rs.core.Response;\r
31 \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
42 \r
43 /**\r
44  * Class to provide all kinds of policy operations.\r
45  *\r
46  * @author Chenfei Gao (cgao@research.att.com)\r
47  */\r
48 public class PolicyProvider extends CommonModelProvider {\r
49 \r
50     /**\r
51      * Default constructor.\r
52      */\r
53     public PolicyProvider() throws PfModelException {\r
54         super();\r
55     }\r
56 \r
57     /**\r
58      * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
59      *\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
64      *\r
65      * @return the ToscaServiceTemplate object\r
66      *\r
67      * @throws PfModelException the PfModel parsing exception\r
68      */\r
69     public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,\r
70             String policyVersion) throws PfModelException {\r
71 \r
72         ToscaServiceTemplate serviceTemplate =\r
73                 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
74 \r
75         if (!hasPolicy(serviceTemplate)) {\r
76             throw new PfModelException(Response.Status.NOT_FOUND,\r
77                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
78         }\r
79 \r
80         return serviceTemplate;\r
81     }\r
82 \r
83     /**\r
84      * Retrieves a list of policies with the latest versions that match specified policy type id and version.\r
85      *\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
89      *\r
90      * @return the ToscaServiceTemplate object\r
91      *\r
92      * @throws PfModelException the PfModel parsing exception\r
93      */\r
94     public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)\r
95             throws PfModelException {\r
96 \r
97         ToscaServiceTemplate serviceTemplate =\r
98                 getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);\r
99 \r
100         if (!hasPolicy(serviceTemplate)) {\r
101             throw new PfModelException(Response.Status.NOT_FOUND,\r
102                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, null));\r
103         }\r
104 \r
105         return serviceTemplate;\r
106     }\r
107 \r
108     /**\r
109      * Retrieves a list of deployed policies in each pdp group.\r
110      *\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
114      *\r
115      * @return a list of deployed policies in each pdp group\r
116      *\r
117      * @throws PfModelException the PfModel parsing exception\r
118      */\r
119     public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,\r
120             String policyTypeVersion, String policyId) throws PfModelException {\r
121 \r
122         return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),\r
123                 modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));\r
124     }\r
125 \r
126     /**\r
127      * Creates one or more new policies for the same policy type ID and version.\r
128      *\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
132      *\r
133      * @return the ToscaServiceTemplate object\r
134      *\r
135      * @throws PfModelException the PfModel parsing exception\r
136      */\r
137     public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
138             throws PfModelException {\r
139 \r
140         return modelsProvider.createPolicies(body);\r
141     }\r
142 \r
143     /**\r
144      * Creates one or more new policies.\r
145      *\r
146      * @param body the entity body of policy\r
147      *\r
148      * @return the ToscaServiceTemplate object\r
149      *\r
150      * @throws PfModelException the PfModel parsing exception\r
151      */\r
152     public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
153         return modelsProvider.createPolicies(body);\r
154     }\r
155 \r
156     /**\r
157      * Deletes the policy matching specified ID and version of both policy type and policy.\r
158      *\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
163      *\r
164      * @return the ToscaServiceTemplate object\r
165      *\r
166      * @throws PfModelException the PfModel parsing exception\r
167      */\r
168     public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
169             String policyVersion) throws PfModelException {\r
170 \r
171         validateDeleteEligibility(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
172 \r
173         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
174 \r
175         if (!hasPolicy(serviceTemplate)) {\r
176             throw new PfModelException(Response.Status.NOT_FOUND,\r
177                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion, policyId, policyVersion));\r
178         }\r
179 \r
180         return serviceTemplate;\r
181     }\r
182 \r
183     /**\r
184      * Validates whether specified policy can be deleted based on the rule that deployed policy cannot be deleted.\r
185      *\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
190      *\r
191      * @throws PfModelException the PfModel parsing exception\r
192      */\r
193     private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion, String policyId,\r
194             String policyVersion) throws PfModelException {\r
195 \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
203 \r
204         List<PdpGroup> pdpGroups = modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
205 \r
206         if (!pdpGroups.isEmpty()) {\r
207             throw new PfModelException(Response.Status.CONFLICT,\r
208                     constructDeletePolicyViolationMessage(policyId, policyVersion, pdpGroups));\r
209         }\r
210     }\r
211 \r
212     /**\r
213      * Retrieves the specified version of the policy.\r
214      *\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
219      *\r
220      * @return the TOSCA service template containing the specified version of the policy\r
221      *\r
222      * @throws PfModelException the PfModel parsing exception\r
223      */\r
224     private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,\r
225             String policyVersion) throws PfModelException {\r
226 \r
227         ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)\r
228                 .type(policyTypeName).typeVersion(policyTypeVersion).build();\r
229         return modelsProvider.getFilteredPolicies(policyFilter);\r
230     }\r
231 \r
232     /**\r
233      * Constructs returned message for not found resource.\r
234      *\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
239      *\r
240      * @return constructed message\r
241      */\r
242     private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion, String policyId,\r
243             String policyVersion) {\r
244 \r
245         return "policy with ID " + policyId + ":" + policyVersion + " and type " + policyTypeId + ":"\r
246                 + policyTypeVersion + " does not exist";\r
247     }\r
248 }\r