Update API for changes in TOSCA provider
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyTypeProvider.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.Entry;\r
29 \r
30 import javax.ws.rs.core.Response;\r
31 \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
38 \r
39 /**\r
40  * Class to provide all kinds of policy type operations.\r
41  *\r
42  * @author Chenfei Gao (cgao@research.att.com)\r
43  */\r
44 public class PolicyTypeProvider extends CommonModelProvider {\r
45 \r
46     /**\r
47      * Default constructor.\r
48      */\r
49     public PolicyTypeProvider() throws PfModelException {\r
50         super();\r
51     }\r
52 \r
53     /**\r
54      * Retrieves a list of policy types matching specified policy type ID and version.\r
55      *\r
56      * @param policyTypeId the ID of policy type\r
57      * @param policyTypeVersion the version of policy type\r
58      *\r
59      * @return the ToscaServiceTemplate object\r
60      *\r
61      * @throws PfModelException the PfModel parsing exception\r
62      */\r
63     public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)\r
64             throws PfModelException {\r
65 \r
66         ToscaServiceTemplate serviceTemplate = getFilteredPolicyTypes(policyTypeId, policyTypeVersion);\r
67 \r
68         if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {\r
69             throw new PfModelException(Response.Status.NOT_FOUND,\r
70                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
71         }\r
72 \r
73         return serviceTemplate;\r
74     }\r
75 \r
76     /**\r
77      * Retrieves a list of policy types with the latest versions.\r
78      *\r
79      * @param policyTypeId the ID of policy type\r
80      *\r
81      * @return the ToscaServiceTemplate object\r
82      *\r
83      * @throws PfModelException the PfModel parsing exception\r
84      */\r
85     public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {\r
86 \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
91         }\r
92 \r
93         return serviceTemplate;\r
94     }\r
95 \r
96     /**\r
97      * Creates a new policy type.\r
98      *\r
99      * @param body the entity body of policy type\r
100      *\r
101      * @return the ToscaServiceTemplate object\r
102      * @throws PfModelException the PfModel parsing exception\r
103      */\r
104     public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {\r
105 \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
109         }\r
110         validatePolicyTypeVersionExist(body);\r
111         return modelsProvider.createPolicyTypes(body);\r
112     }\r
113 \r
114     /**\r
115      * Delete the policy type matching specified policy type ID and version.\r
116      *\r
117      * @param policyTypeId the ID of policy type\r
118      * @param policyTypeVersion the version of policy type\r
119      *\r
120      * @return the ToscaServiceTemplate object\r
121      *\r
122      * @throws PfModelException the PfModel parsing exception\r
123      */\r
124     public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)\r
125             throws PfModelException {\r
126 \r
127         validateDeleteEligibility(policyTypeId, policyTypeVersion);\r
128 \r
129         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);\r
130 \r
131         if (!hasPolicyType(serviceTemplate)) {\r
132             throw new PfModelException(Response.Status.NOT_FOUND,\r
133                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
134         }\r
135 \r
136         return serviceTemplate;\r
137     }\r
138 \r
139     /**\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
142      *\r
143      * @param policyTypeId the ID of policy type\r
144      * @param policyTypeVersion the version of policy type\r
145      *\r
146      * @throws PfModelException the PfModel parsing exception\r
147      */\r
148     private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
149 \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
156         }\r
157     }\r
158 \r
159     /**\r
160      * Validates that each policy type has a version specified in the payload.\r
161      *\r
162      * @param body the TOSCA service template payload to check against\r
163      *\r
164      * @throws PfModelException the PfModel parsing exception\r
165      */\r
166     private void validatePolicyTypeVersionExist(ToscaServiceTemplate body) throws PfModelException {\r
167 \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
173             }\r
174         }\r
175 \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
180         }\r
181     }\r
182 \r
183     /**\r
184      * Retrieves the specified version of the policy type.\r
185      *\r
186      * @param policyTypeName the name of the policy type\r
187      * @param policyTypeVersion the version of the policy type\r
188      *\r
189      * @return the TOSCA service template containing the specified version of the policy type\r
190      *\r
191      * @throws PfModelException the PfModel parsing exception\r
192      */\r
193     private ToscaServiceTemplate getFilteredPolicyTypes(String policyTypeName, String policyTypeVersion)\r
194             throws PfModelException {\r
195 \r
196         ToscaPolicyTypeFilter policyTypeFilter =\r
197                 ToscaPolicyTypeFilter.builder().name(policyTypeName).version(policyTypeVersion).build();\r
198         return modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
199     }\r
200 \r
201     /**\r
202      * Constructs returned message for not found resource.\r
203      *\r
204      * @param policyTypeId the ID of policy type\r
205      * @param policyTypeVersion the version of policy type\r
206      *\r
207      * @return constructed message\r
208      */\r
209     private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {\r
210 \r
211         return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";\r
212     }\r
213 }\r