Migrate policy api startup & config, controller to springboot
[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-2021 AT&T Intellectual Property. All rights reserved.\r
6  * Modifications Copyright (C) 2019-2021 Nordix Foundation.\r
7  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *      http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  *\r
21  * SPDX-License-Identifier: Apache-2.0\r
22  * ============LICENSE_END=========================================================\r
23  */\r
24 \r
25 package org.onap.policy.api.main.rest.provider;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.HashMap;\r
29 import java.util.List;\r
30 import java.util.Map;\r
31 import java.util.function.BiConsumer;\r
32 import javax.ws.rs.core.Response;\r
33 import org.apache.commons.lang3.tuple.Pair;\r
34 import org.onap.policy.models.base.PfConceptKey;\r
35 import org.onap.policy.models.base.PfModelException;\r
36 import org.onap.policy.models.pdp.concepts.PdpGroup;\r
37 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;\r
38 import org.onap.policy.models.pdp.concepts.PdpSubGroup;\r
39 import org.onap.policy.models.pdp.enums.PdpState;\r
40 import org.onap.policy.models.provider.PolicyModelsProvider;\r
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;\r
42 \r
43 /**\r
44  * Super class for providers that use a model provider.\r
45  */\r
46 public class CommonModelProvider {\r
47 \r
48     protected final PolicyModelsProvider modelsProvider;\r
49 \r
50     /**\r
51      * Constructs the object, populating {@link #modelsProvider}.\r
52      *\r
53      */\r
54     public CommonModelProvider(PolicyModelsProvider modelsProvider) {\r
55         this.modelsProvider = modelsProvider;\r
56     }\r
57 \r
58     /**\r
59      * Collects all deployed versions of specified policy in all pdp groups.\r
60      *\r
61      * @param policyId the ID of policy\r
62      * @param policyType the concept key of policy type\r
63      * @param getter the custom generic getter Bifunction\r
64      * @param consumer the BiConsumer\r
65      * @param data the data structure storing retrieved deployed policies\r
66      *\r
67      * @return a map between pdp group and deployed versions of specified policy in that group\r
68      *\r
69      * @throws PfModelException the PfModel parsing exception\r
70      */\r
71     protected <T, R> Map<Pair<String, String>, T> collectDeployedPolicies(String policyId, PfConceptKey policyType,\r
72             BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {\r
73 \r
74         List<PdpGroup> pdpGroups = getPolicyTypeFilteredPdpGroups(policyType);\r
75         hasActivePdpGroup(pdpGroups, policyType, policyId);\r
76         return constructDeployedPolicyMap(pdpGroups, policyId, policyType, getter, consumer, data);\r
77     }\r
78 \r
79     @FunctionalInterface\r
80     protected interface BiFunctionWithEx<T, U, R> {\r
81         public R apply(T value1, U value2) throws PfModelException;\r
82     }\r
83 \r
84     /**\r
85      * Checks if the list of pdp groups is empty. If so, throws exception saying specified policy deployment is not\r
86      * found in all existing pdp groups.\r
87      *\r
88      * @param pdpGroups the list of pdp groups to check against\r
89      * @param policyType the concept key of policy type\r
90      * @param policyId the ID of policy\r
91      *\r
92      * @throws PfModelException the PfModel parsing exception\r
93      */\r
94     private void hasActivePdpGroup(List<PdpGroup> pdpGroups, PfConceptKey policyType, String policyId)\r
95             throws PfModelException {\r
96 \r
97         if (pdpGroups.isEmpty()) {\r
98             throw new PfModelException(Response.Status.NOT_FOUND,\r
99                     constructDeploymentNotFoundMessage(policyType, policyId));\r
100         }\r
101     }\r
102 \r
103     /**\r
104      * Retrieves all pdp groups supporting specified policy type.\r
105      *\r
106      * @param policyType the policy type\r
107      *\r
108      * @return a list of pdp groups supporting specified policy type\r
109      *\r
110      * @throws PfModelException the PfModel parsing exception\r
111      */\r
112     private List<PdpGroup> getPolicyTypeFilteredPdpGroups(PfConceptKey policyType) throws PfModelException {\r
113 \r
114         List<ToscaConceptIdentifier> policyTypes = new ArrayList<>();\r
115         policyTypes.add(new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion()));\r
116         var pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes).groupState(PdpState.ACTIVE)\r
117                 .pdpState(PdpState.ACTIVE).build();\r
118         return modelsProvider.getFilteredPdpGroups(pdpGroupFilter);\r
119     }\r
120 \r
121     /**\r
122      * Constructs the map of deployed pdp groups and deployed policies.\r
123      *\r
124      * @param pdpGroups the list of pdp groups that contain the specified policy\r
125      * @param policyId the ID of policy\r
126      * @param policyType the concept key of policy type\r
127      * @param getter the custom generic getter BiFunction\r
128      * @param consumer the BiConsumer\r
129      * @param data the data structure storing retrieved deployed policies\r
130      *\r
131      * @return the constructed map of pdp groups and deployed policies\r
132      *\r
133      * @throws PfModelException the PfModel parsing exception\r
134      */\r
135     private <T, R> Map<Pair<String, String>, T> constructDeployedPolicyMap(List<PdpGroup> pdpGroups, String policyId,\r
136             PfConceptKey policyType, BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data)\r
137             throws PfModelException {\r
138 \r
139         Map<Pair<String, String>, T> deployedPolicyMap = new HashMap<>();\r
140         for (PdpGroup pdpGroup : pdpGroups) {\r
141             List<ToscaConceptIdentifier> policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup, policyType);\r
142             var deployedPolicies = getDeployedPolicies(policyIdentifiers, policyType, getter, consumer, data);\r
143             deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);\r
144         }\r
145         return deployedPolicyMap;\r
146     }\r
147 \r
148     /**\r
149      * Extracts policy identifiers matching specified policy ID from specified pdp group.\r
150      *\r
151      * @param policyId the ID of policy to match\r
152      * @param pdpGroup the target pdp group to search\r
153      * @param policyType the concept key of policy type\r
154      *\r
155      * @return the list of policy identifiers\r
156      *\r
157      * @throws PfModelException the PfModel parsing exception\r
158      */\r
159     private List<ToscaConceptIdentifier> extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup,\r
160             PfConceptKey policyType) throws PfModelException {\r
161 \r
162         List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>();\r
163         for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {\r
164             for (ToscaConceptIdentifier policyIdentifier : pdpSubGroup.getPolicies()) {\r
165                 if (policyId.equalsIgnoreCase(policyIdentifier.getName())) {\r
166                     policyIdentifiers.add(policyIdentifier);\r
167                 }\r
168             }\r
169         }\r
170         if (policyIdentifiers.isEmpty()) {\r
171             throw new PfModelException(Response.Status.NOT_FOUND,\r
172                     constructDeploymentNotFoundMessage(policyType, policyId));\r
173         }\r
174         return policyIdentifiers;\r
175     }\r
176 \r
177     /**\r
178      * Retrieves deployed policies in a generic way.\r
179      *\r
180      * @param policyIdentifiers the identifiers of the policies to return\r
181      * @param policyType the concept key of current policy type\r
182      * @param getter the method reference of getting deployed policies\r
183      * @param consumer the method reference of consuming the returned policies\r
184      * @param data the data structure of deployed policies to return\r
185      *\r
186      * @return the generic type of policy data structure to return\r
187      *\r
188      * @throws PfModelException the PfModel parsing exception\r
189      */\r
190     private <T, R> T getDeployedPolicies(List<ToscaConceptIdentifier> policyIdentifiers, PfConceptKey policyType,\r
191             BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {\r
192 \r
193         for (ToscaConceptIdentifier policyIdentifier : policyIdentifiers) {\r
194             var result = getter.apply(policyIdentifier.getName(),\r
195                     getTrimedVersionForLegacyType(policyIdentifier.getVersion(), policyType));\r
196             consumer.accept(data, result);\r
197         }\r
198         return data;\r
199     }\r
200 \r
201     /**\r
202      * Trims the version for legacy policies.\r
203      *\r
204      * @param fullVersion the full version format with major, minor, patch\r
205      * @param policyType the concept key of policy type\r
206      *\r
207      * @return the trimmed version\r
208      */\r
209     private String getTrimedVersionForLegacyType(String fullVersion, PfConceptKey policyType) {\r
210         return (policyType.getName().contains("guard") || policyType.getName().contains("Operational"))\r
211                 ? fullVersion.split("\\.")[0]\r
212                 : fullVersion;\r
213     }\r
214 \r
215     /**\r
216      * Constructs returned message for not found policy deployment.\r
217      *\r
218      * @param policyType the concept key of policy type\r
219      * @param policyId the ID of policy\r
220      *\r
221      * @return constructed message\r
222      */\r
223     private String constructDeploymentNotFoundMessage(PfConceptKey policyType, String policyId) {\r
224 \r
225         return "could not find policy with ID " + policyId + " and type " + policyType.getName() + ":"\r
226                 + policyType.getVersion() + " deployed in any pdp group";\r
227     }\r
228 }