Implement policy provider functions
[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 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.List;\r
26 import java.util.Map;\r
27 import javax.ws.rs.core.Response;\r
28 import org.apache.commons.lang3.tuple.Pair;\r
29 import org.onap.policy.api.main.parameters.ApiParameterGroup;\r
30 import org.onap.policy.common.parameters.ParameterService;\r
31 import org.onap.policy.models.base.PfModelException;\r
32 import org.onap.policy.models.provider.PolicyModelsProvider;\r
33 import org.onap.policy.models.provider.PolicyModelsProviderFactory;\r
34 import org.onap.policy.models.provider.PolicyModelsProviderParameters;\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
37 \r
38 /**\r
39  * Class to provide all kinds of policy operations.\r
40  *\r
41  * @author Chenfei Gao (cgao@research.att.com)\r
42  */\r
43 public class PolicyProvider {\r
44 \r
45     private PolicyModelsProvider modelsProvider;\r
46 \r
47     /**\r
48      * Default constructor.\r
49      */\r
50     public PolicyProvider() throws PfModelException {\r
51 \r
52         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
53         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
54         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
55         modelsProvider.init();\r
56     }\r
57 \r
58     /**\r
59      * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
60      *\r
61      * @param policyTypeId the ID of policy type\r
62      * @param policyTypeVersion the version of policy type\r
63      * @param policyId the ID of policy\r
64      * @param policyVersion the version of policy\r
65      *\r
66      * @return the ToscaServiceTemplate object\r
67      *\r
68      * @throws PfModelException the PfModel parsing exception\r
69      */\r
70     public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion,\r
71             String policyId, String policyVersion) throws PfModelException {\r
72 \r
73         validatePathParam(policyTypeId, policyTypeVersion);\r
74         ToscaServiceTemplate serviceTemplate;\r
75         if (policyId == null) {\r
76             serviceTemplate = modelsProvider.getPolicies4PolicyType(policyTypeId, policyTypeVersion);\r
77         } else {\r
78             serviceTemplate = modelsProvider.getPolicies(policyId, policyVersion);\r
79         }\r
80         close();\r
81         return serviceTemplate;\r
82     }\r
83 \r
84     /**\r
85      * Retrieves a list of policies with the latest versions that match specified policy type id and version.\r
86      *\r
87      * @param policyTypeId the ID of policy type\r
88      * @param policyTypeVersion the version of policy type\r
89      * @param policyId the ID of the policy\r
90      *\r
91      * @return the ToscaServiceTemplate object\r
92      *\r
93      * @throws PfModelException the PfModel parsing exception\r
94      */\r
95     public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion,\r
96             String policyId) throws PfModelException {\r
97 \r
98         validatePathParam(policyTypeId, policyTypeVersion);\r
99         ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicies(policyId);\r
100         close();\r
101         return serviceTemplate;\r
102     }\r
103 \r
104     /**\r
105      * Retrieves a list of deployed policies in each pdp group.\r
106      *\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
110      *\r
111      * @return a list of deployed policies in each pdp group\r
112      *\r
113      * @throws PfModelException the PfModel parsing exception\r
114      */\r
115     public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(\r
116             String policyTypeId, String policyTypeVersion, String policyId) throws PfModelException {\r
117 \r
118         validatePathParam(policyTypeId, policyTypeVersion);\r
119         Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicies = modelsProvider.getDeployedPolicyList(policyId);\r
120         close();\r
121         return deployedPolicies;\r
122     }\r
123 \r
124     /**\r
125      * Creates a new policy for a policy type ID and version.\r
126      *\r
127      * @param policyTypeId the ID of policy type\r
128      * @param policyTypeVersion the version of policy type\r
129      * @param body the entity body of policy\r
130      *\r
131      * @return the ToscaServiceTemplate object\r
132      *\r
133      * @throws PfModelException the PfModel parsing exception\r
134      */\r
135     public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion,\r
136                                              ToscaServiceTemplate body) throws PfModelException {\r
137 \r
138         validatePathParam(policyTypeId, policyTypeVersion);\r
139         validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);\r
140         ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicies(body);\r
141         close();\r
142         return serviceTemplate;\r
143     }\r
144 \r
145     /**\r
146      * Deletes the policy matching specified ID and version of both policy type and policy.\r
147      *\r
148      * @param policyTypeId the ID of policy type\r
149      * @param policyTypeVersion the version of policy type\r
150      * @param policyId the ID of policy\r
151      * @param policyVersion the version of policy\r
152      *\r
153      * @return the ToscaServiceTemplate object\r
154      *\r
155      * @throws PfModelException the PfModel parsing exception\r
156      */\r
157     public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion,\r
158                                  String policyId, String policyVersion) throws PfModelException {\r
159 \r
160         validatePathParam(policyTypeId, policyTypeVersion);\r
161         ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);\r
162         close();\r
163         return serviceTemplate;\r
164     }\r
165 \r
166     /**\r
167      * Checks the validation of policy type info passed in as path param.\r
168      *\r
169      * @param policyTypeId the ID of policy type\r
170      * @param policyTypeVersion the version of policy type\r
171      *\r
172      * @throws PfModelException the PfModel parsing exception\r
173      */\r
174     private void validatePathParam(String policyTypeId, String policyTypeVersion) throws PfModelException {\r
175 \r
176         // Check policy type existence\r
177         try {\r
178             modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);\r
179         } catch (Exception e) {\r
180             throw new PfModelException(Response.Status.NOT_FOUND, "specified policy type does not exist", e);\r
181         }\r
182     }\r
183 \r
184     /**\r
185      * Validates the match between policy type specified in path param and the one specified in type of policy.\r
186      *\r
187      * @param body the ToscaServiceTemplate to create\r
188      *\r
189      * @throws PfModelException the PfModel parsing exception\r
190      */\r
191     private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
192             throws PfModelException {\r
193 \r
194         List<Map<String, ToscaPolicy>> policies = body.getToscaTopologyTemplate().getPolicies();\r
195         for (Map<String, ToscaPolicy> policy : policies) {\r
196             if (policy.size() != 1) {\r
197                 throw new PfModelException(Response.Status.BAD_REQUEST,\r
198                         "one policy block contains more than one policies");\r
199             }\r
200             ToscaPolicy policyContent = policy.values().iterator().next();\r
201             if (!policyTypeId.equalsIgnoreCase(policyContent.getType())\r
202                     || !policyTypeVersion.equalsIgnoreCase(policyContent.getVersion())) {\r
203                 throw new PfModelException(Response.Status.BAD_REQUEST, "policy type info does not match");\r
204             }\r
205         }\r
206     }\r
207 \r
208     /**\r
209      * Closes the connection to database.\r
210      *\r
211      * @throws PfModelException the PfModel parsing exception\r
212      */\r
213     private void close() throws PfModelException {\r
214         try {\r
215             modelsProvider.close();\r
216         } catch (Exception e) {\r
217             throw new PfModelException(\r
218                     Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);\r
219         }\r
220     }\r
221 }