24b8b1bdbed9dd590d07fecb9cf01293d34b8f96
[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 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  * SPDX-License-Identifier: Apache-2.0\r
20  * ============LICENSE_END=========================================================\r
21  */\r
22 \r
23 package org.onap.policy.api.main.rest.provider;\r
24 \r
25 import java.util.ArrayList;\r
26 import java.util.List;\r
27 import javax.ws.rs.core.Response;\r
28 import org.onap.policy.api.main.parameters.ApiParameterGroup;\r
29 import org.onap.policy.common.parameters.ParameterService;\r
30 import org.onap.policy.models.base.PfModelException;\r
31 import org.onap.policy.models.provider.PolicyModelsProvider;\r
32 import org.onap.policy.models.provider.PolicyModelsProviderFactory;\r
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;\r
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\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 implements AutoCloseable {\r
45 \r
46     private PolicyModelsProvider modelsProvider;\r
47 \r
48     /**\r
49      * Default constructor.\r
50      */\r
51     public PolicyTypeProvider() throws PfModelException {\r
52 \r
53         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
54         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
55         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
56     }\r
57 \r
58     /**\r
59      * Retrieves a list of policy types matching specified policy type ID and version.\r
60      *\r
61      * @param policyTypeId the ID of policy type\r
62      * @param policyTypeVersion the version of policy type\r
63      *\r
64      * @return the ToscaServiceTemplate object\r
65      *\r
66      * @throws PfModelException the PfModel parsing exception\r
67      */\r
68     public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)\r
69             throws PfModelException {\r
70 \r
71         ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
72                 .name(policyTypeId).version(policyTypeVersion).build();\r
73         ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
74 \r
75         if (policyTypeId != null && !hasPolicyType(serviceTemplate)) {\r
76             throw new PfModelException(Response.Status.NOT_FOUND,\r
77                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
78         }\r
79 \r
80         return serviceTemplate;\r
81     }\r
82 \r
83     /**\r
84      * Retrieves a list of policy types with the latest versions.\r
85      *\r
86      * @param policyTypeId the ID of policy type\r
87      *\r
88      * @return the ToscaServiceTemplate object\r
89      *\r
90      * @throws PfModelException the PfModel parsing exception\r
91      */\r
92     public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {\r
93 \r
94         ToscaPolicyTypeFilter policyTypeFilter = ToscaPolicyTypeFilter.builder()\r
95                 .name(policyTypeId).version(ToscaPolicyTypeFilter.LATEST_VERSION).build();\r
96         ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
97         if (!hasPolicyType(serviceTemplate)) {\r
98             throw new PfModelException(Response.Status.NOT_FOUND,\r
99                     constructResourceNotFoundMessage(policyTypeId, null));\r
100         }\r
101 \r
102         return serviceTemplate;\r
103     }\r
104 \r
105     /**\r
106      * Creates a new policy type.\r
107      *\r
108      * @param body the entity body of policy type\r
109      *\r
110      * @return the ToscaServiceTemplate object\r
111      * @throws PfModelException the PfModel parsing exception\r
112      */\r
113     public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {\r
114 \r
115         return modelsProvider.createPolicyTypes(body);\r
116     }\r
117 \r
118     /**\r
119      * Delete the policy type matching specified policy type ID and version.\r
120      *\r
121      * @param policyTypeId the ID of policy type\r
122      * @param policyTypeVersion the version of policy type\r
123      *\r
124      * @return the ToscaServiceTemplate object\r
125      *\r
126      * @throws PfModelException the PfModel parsing exception\r
127      */\r
128     public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)\r
129             throws PfModelException {\r
130 \r
131         validateDeleteEligibility(policyTypeId, policyTypeVersion);\r
132 \r
133         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);\r
134 \r
135         if (!hasPolicyType(serviceTemplate)) {\r
136             throw new PfModelException(Response.Status.NOT_FOUND,\r
137                     constructResourceNotFoundMessage(policyTypeId, policyTypeVersion));\r
138         }\r
139 \r
140         return serviceTemplate;\r
141     }\r
142 \r
143     /**\r
144      * Validates whether specified policy type can be deleted based on the rule that\r
145      * policy type parameterized by at least one policies cannot be deleted.\r
146      *\r
147      * @param policyTypeId the ID of policy type\r
148      * @param policyTypeVersion the version of policy type\r
149      *\r
150      * @throws PfModelException the PfModel parsing exception\r
151      */\r
152     private void validateDeleteEligibility(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
153 \r
154         ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder()\r
155                 .type(policyTypeId).typeVersion(policyTypeVersion).build();\r
156         List<ToscaPolicy> policies = modelsProvider.getFilteredPolicyList(policyFilter);\r
157         if (!policies.isEmpty()) {\r
158             throw new PfModelException(Response.Status.CONFLICT,\r
159                     constructDeleteRuleViolationMessage(policyTypeId, policyTypeVersion, policies));\r
160         }\r
161     }\r
162 \r
163     /**\r
164      * Constructs returned message for policy type delete rule violation.\r
165      *\r
166      * @param policyTypeId the ID of policy type\r
167      * @param policyTypeVersion the version of policy type\r
168      * @param policies the list of policies that parameterizes specified policy type\r
169      *\r
170      * @return the constructed message\r
171      */\r
172     private String constructDeleteRuleViolationMessage(\r
173             String policyTypeId, String policyTypeVersion, List<ToscaPolicy> policies) {\r
174 \r
175         List<String> policyNameVersionList = new ArrayList<>();\r
176         for (ToscaPolicy policy : policies) {\r
177             policyNameVersionList.add(policy.getName() + ":" + policy.getVersion());\r
178         }\r
179         String parameterizedPolicies = String.join(",", policyNameVersionList);\r
180         return "policy type with ID " + policyTypeId + ":" + policyTypeVersion\r
181                 + " cannot be deleted as it is parameterized by policies " + parameterizedPolicies;\r
182     }\r
183 \r
184     /**\r
185      * Constructs returned message for not found resource.\r
186      *\r
187      * @param policyTypeId the ID of policy type\r
188      * @param policyTypeVersion the version of policy type\r
189      *\r
190      * @return constructed message\r
191      */\r
192     private String constructResourceNotFoundMessage(String policyTypeId, String policyTypeVersion) {\r
193 \r
194         return "policy type with ID " + policyTypeId + ":" + policyTypeVersion + " does not exist";\r
195     }\r
196 \r
197     /**\r
198      * Checks if service template contains any policy type.\r
199      *\r
200      * @param serviceTemplate the service template to check against\r
201      *\r
202      * @return boolean whether service template contains any policy type\r
203      */\r
204     private boolean hasPolicyType(ToscaServiceTemplate serviceTemplate) {\r
205 \r
206         if (serviceTemplate.getPolicyTypes() == null) {\r
207             return false;\r
208         } else if (serviceTemplate.getPolicyTypes().isEmpty()) {\r
209             return false;\r
210         } else if (serviceTemplate.getPolicyTypes().get(0).isEmpty()) {\r
211             return false;\r
212         } else {\r
213             return true;\r
214         }\r
215     }\r
216 \r
217     /**\r
218      * Closes the connection to database.\r
219      *\r
220      * @throws PfModelException the PfModel parsing exception\r
221      */\r
222     @Override
223     public void close() throws PfModelException {\r
224 \r
225         modelsProvider.close();\r
226     }\r
227 }\r