Refactor policy/api csit tests
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / CommonModelProvider.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) 2019-2021 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.HashMap;\r
28 import java.util.List;\r
29 import java.util.Map;\r
30 import java.util.function.BiConsumer;\r
31 import javax.ws.rs.core.Response;\r
32 import org.apache.commons.lang3.tuple.Pair;\r
33 import org.onap.policy.api.main.parameters.ApiParameterGroup;\r
34 import org.onap.policy.common.parameters.ParameterService;\r
35 import org.onap.policy.models.base.PfConceptKey;\r
36 import org.onap.policy.models.base.PfModelException;\r
37 import org.onap.policy.models.pdp.concepts.PdpGroup;\r
38 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;\r
39 import org.onap.policy.models.pdp.concepts.PdpSubGroup;\r
40 import org.onap.policy.models.pdp.enums.PdpState;\r
41 import org.onap.policy.models.provider.PolicyModelsProvider;\r
42 import org.onap.policy.models.provider.PolicyModelsProviderFactory;\r
43 import org.onap.policy.models.provider.PolicyModelsProviderParameters;\r
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;\r
45 \r
46 /**\r
47  * Super class for providers that use a model provider.\r
48  */\r
49 public class CommonModelProvider implements AutoCloseable {\r
50 \r
51     protected final PolicyModelsProvider modelsProvider;\r
52 \r
53     /**\r
54      * Constructs the object, populating {@link #modelsProvider}.\r
55      *\r
56      * @throws PfModelException if an error occurs\r
57      */\r
58     public CommonModelProvider() throws PfModelException {\r
59 \r
60         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");\r
61         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();\r
62         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);\r
63     }\r
64 \r
65     /**\r
66      * Closes the connection to database.\r
67      *\r
68      * @throws PfModelException the PfModel parsing exception\r
69      */\r
70     @Override\r
71     public void close() throws PfModelException {\r
72 \r
73         modelsProvider.close();\r
74     }\r
75 \r
76     /**\r
77      * Collects all deployed versions of specified policy in all pdp groups.\r
78      *\r
79      * @param policyId the ID of policy\r
80      * @param policyType the concept key of policy type\r
81      * @param getter the custom generic getter Bifunction\r
82      * @param consumer the BiConsumer\r
83      * @param data the data structure storing retrieved deployed policies\r
84      *\r
85      * @return a map between pdp group and deployed versions of specified policy in that group\r
86      *\r
87      * @throws PfModelException the PfModel parsing exception\r
88      */\r
89     protected <T, R> Map<Pair<String, String>, T> collectDeployedPolicies(String policyId, PfConceptKey policyType,\r
90             BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {\r
91 \r
92         List<PdpGroup> pdpGroups = getPolicyTypeFilteredPdpGroups(policyType);\r
93         hasActivePdpGroup(pdpGroups, policyType, policyId);\r
94         return constructDeployedPolicyMap(pdpGroups, policyId, policyType, getter, consumer, data);\r
95     }\r
96 \r
97     @FunctionalInterface\r
98     protected interface BiFunctionWithEx<T, U, R> {\r
99         public R apply(T value1, U value2) throws PfModelException;\r
100     }\r
101 \r
102     /**\r
103      * Checks if the list of pdp groups is empty. If so, throws exception saying specified policy deployment is not\r
104      * found in all existing pdp groups.\r
105      *\r
106      * @param pdpGroups the list of pdp groups to check against\r
107      * @param policyType the concept key of policy type\r
108      * @param policyId the ID of policy\r
109      *\r
110      * @throws PfModelException the PfModel parsing exception\r
111      */\r
112     private void hasActivePdpGroup(List<PdpGroup> pdpGroups, PfConceptKey policyType, String policyId)\r
113             throws PfModelException {\r
114 \r
115         if (pdpGroups.isEmpty()) {\r
116             throw new PfModelException(Response.Status.NOT_FOUND,\r
117                     constructDeploymentNotFoundMessage(policyType, policyId));\r
118         }\r
119     }\r
120 \r
121     /**\r
122      * Retrieves all pdp groups supporting specified policy type.\r
123      *\r
124      * @param policyTypeId the ID of policy type\r
125      * @param policyTypeVersion the version of policy type\r
126      *\r
127      * @return a list of pdp groups supporting specified policy type\r
128      *\r
129      * @throws PfModelException the PfModel parsing exception\r
130      */\r
131     private List<PdpGroup> getPolicyTypeFilteredPdpGroups(PfConceptKey policyType) throws PfModelException {\r
132 \r
133         List<ToscaConceptIdentifier> policyTypes = new ArrayList<>();\r
134         policyTypes.add(new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion()));\r
135         PdpGroupFilter pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes).groupState(PdpState.ACTIVE)\r
136                 .pdpState(PdpState.ACTIVE).build();\r
137         return modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
138     }\r
139 \r
140     /**\r
141      * Constructs the map of deployed pdp groups and deployed policies.\r
142      *\r
143      * @param pdpGroups the list of pdp groups that contain the specified policy\r
144      * @param policyId the ID of policy\r
145      * @param policyType the concept key of policy type\r
146      * @param getter the custom generic getter BiFunction\r
147      * @param consumer the BiConsumer\r
148      * @param data the data structure storing retrieved deployed policies\r
149      *\r
150      * @return the constructed map of pdp groups and deployed policies\r
151      *\r
152      * @throws PfModelException the PfModel parsing exception\r
153      */\r
154     private <T, R> Map<Pair<String, String>, T> constructDeployedPolicyMap(List<PdpGroup> pdpGroups, String policyId,\r
155             PfConceptKey policyType, BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data)\r
156             throws PfModelException {\r
157 \r
158         Map<Pair<String, String>, T> deployedPolicyMap = new HashMap<>();\r
159         for (PdpGroup pdpGroup : pdpGroups) {\r
160             List<ToscaConceptIdentifier> policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup, policyType);\r
161             T deployedPolicies = getDeployedPolicies(policyIdentifiers, policyType, getter, consumer, data);\r
162             deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);\r
163         }\r
164         return deployedPolicyMap;\r
165     }\r
166 \r
167     /**\r
168      * Extracts policy identifiers matching specified policy ID from specified pdp group.\r
169      *\r
170      * @param policyId the ID of policy to match\r
171      * @param pdpGroup the target pdp group to search\r
172      * @param policyType the concept key of policy type\r
173      *\r
174      * @return the list of policy identifiers\r
175      *\r
176      * @throws PfModelException the PfModel parsing exception\r
177      */\r
178     private List<ToscaConceptIdentifier> extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup,\r
179             PfConceptKey policyType) throws PfModelException {\r
180 \r
181         List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>();\r
182         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {\r
183             for (ToscaConceptIdentifier policyIdentifier : pdpSubGroup.getPolicies()) {\r
184                 if (policyId.equalsIgnoreCase(policyIdentifier.getName())) {\r
185                     policyIdentifiers.add(policyIdentifier);\r
186                 }\r
187             }\r
188         }\r
189         if (policyIdentifiers.isEmpty()) {\r
190             throw new PfModelException(Response.Status.NOT_FOUND,\r
191                     constructDeploymentNotFoundMessage(policyType, policyId));\r
192         }\r
193         return policyIdentifiers;\r
194     }\r
195 \r
196     /**\r
197      * Retrieves deployed policies in a generic way.\r
198      *\r
199      * @param policyIdentifiers the identifiers of the policies to return\r
200      * @param policyType the concept key of current policy type\r
201      * @param getter the method reference of getting deployed policies\r
202      * @param consumer the method reference of consuming the returned policies\r
203      * @param data the data structure of deployed policies to return\r
204      *\r
205      * @return the generic type of policy data structure to return\r
206      *\r
207      * @throws PfModelException the PfModel parsing exception\r
208      */\r
209     private <T, R> T getDeployedPolicies(List<ToscaConceptIdentifier> policyIdentifiers, PfConceptKey policyType,\r
210             BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {\r
211 \r
212         for (ToscaConceptIdentifier policyIdentifier : policyIdentifiers) {\r
213             R result = getter.apply(policyIdentifier.getName(),\r
214                     getTrimedVersionForLegacyType(policyIdentifier.getVersion(), policyType));\r
215             consumer.accept(data, result);\r
216         }\r
217         return data;\r
218     }\r
219 \r
220     /**\r
221      * Trims the version for legacy policies.\r
222      *\r
223      * @param fullVersion the full version format with major, minor, patch\r
224      * @param policyType the concept key of policy type\r
225      *\r
226      * @return the trimmed version\r
227      */\r
228     private String getTrimedVersionForLegacyType(String fullVersion, PfConceptKey policyType) {\r
229         return (policyType.getName().contains("guard") || policyType.getName().contains("Operational"))\r
230                 ? fullVersion.split("\\.")[0]\r
231                 : fullVersion;\r
232     }\r
233 \r
234     /**\r
235      * Constructs returned message for not found policy deployment.\r
236      *\r
237      * @param policyType the concept key of policy type\r
238      * @param policyId the ID of policy\r
239      *\r
240      * @return constructed message\r
241      */\r
242     private String constructDeploymentNotFoundMessage(PfConceptKey policyType, String policyId) {\r
243 \r
244         return "could not find policy with ID " + policyId + " and type " + policyType.getName() + ":"\r
245                 + policyType.getVersion() + " deployed in any pdp group";\r
246     }\r
247 }\r