Java 17 Upgrade
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / provider / PdpProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2023 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.pdp.persistence.provider;
24
25 import jakarta.ws.rs.core.Response;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 import lombok.NonNull;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfKey;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.dao.PfDao;
40 import org.onap.policy.models.dao.PfFilterParameters;
41 import org.onap.policy.models.pdp.concepts.Pdp;
42 import org.onap.policy.models.pdp.concepts.PdpGroup;
43 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
44 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
45 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
46 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
47 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
48 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus;
49 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
51
52 /**
53  * This class provides the provision of information on PAP concepts in the database to callers.
54  *
55  * @author Liam Fallon (liam.fallon@est.tech)
56  */
57 public class PdpProvider {
58     private static final Object statusLock = new Object();
59
60     /**
61      * Get PDP groups.
62      *
63      * @param dao the DAO to use to access the database
64      * @param name the name of the PDP group to get, null to get all PDP groups
65      * @return the PDP groups found
66      */
67     public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) {
68
69         return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
70     }
71
72     /**
73      * Get filtered PDP groups.
74      *
75      * @param dao the DAO to use to access the database
76      * @param filter the filter for the PDP groups to get
77      * @return the PDP groups found
78      */
79     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
80
81         return filter.filter(
82                         asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
83     }
84
85     /**
86      * Creates PDP groups.
87      *
88      * @param dao the DAO to use to access the database
89      * @param pdpGroups a specification of the PDP groups to create
90      * @return the PDP groups created
91      */
92     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
93
94         for (PdpGroup pdpGroup : pdpGroups) {
95             var jpaPdpGroup = new JpaPdpGroup();
96             jpaPdpGroup.fromAuthorative(pdpGroup);
97
98             BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
99             if (!validationResult.isValid()) {
100                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
101             }
102
103             dao.create(jpaPdpGroup);
104         }
105
106         // Return the created PDP groups
107         List<PdpGroup> returnPdpGroups = new ArrayList<>();
108
109         for (PdpGroup pdpGroup : pdpGroups) {
110             var jpaPdpGroup = dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
111             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
112         }
113
114         return returnPdpGroups;
115     }
116
117     /**
118      * Updates PDP groups.
119      *
120      * @param dao the DAO to use to access the database
121      * @param pdpGroups a specification of the PDP groups to update
122      * @return the PDP groups updated
123      */
124     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
125
126         for (PdpGroup pdpGroup : pdpGroups) {
127             var jpaPdpGroup = new JpaPdpGroup();
128             jpaPdpGroup.fromAuthorative(pdpGroup);
129
130             BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
131             if (!validationResult.isValid()) {
132                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
133             }
134
135             dao.update(jpaPdpGroup);
136         }
137
138         // Return the created PDP groups
139         List<PdpGroup> returnPdpGroups = new ArrayList<>();
140
141         for (PdpGroup pdpGroup : pdpGroups) {
142             var jpaPdpGroup =
143                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
144             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
145         }
146
147         return returnPdpGroups;
148     }
149
150     /**
151      * Update a PDP subgroup.
152      *
153      * @param dao the DAO to use to access the database
154      * @param pdpGroupName the name of the PDP group of the PDP subgroup
155      * @param pdpSubGroup the PDP subgroup to be updated
156      */
157     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
158             @NonNull final PdpSubGroup pdpSubGroup) {
159
160         final var subGroupKey =
161                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
162         final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
163         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
164
165         BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
166         if (!validationResult.isValid()) {
167             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
168         }
169
170         dao.update(jpaPdpSubgroup);
171     }
172
173     /**
174      * Update a PDP.
175      *
176      * @param dao the DAO to use to access the database
177      * @param pdpGroupName the name of the PDP group of the PDP subgroup
178      * @param pdpSubGroup the PDP subgroup to be updated
179      * @param pdp the PDP to be updated
180      */
181     public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
182             @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
183
184         final var pdpKey =
185                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
186         final var jpaPdp = new JpaPdp(pdpKey);
187         jpaPdp.fromAuthorative(pdp);
188
189         BeanValidationResult validationResult = jpaPdp.validate("PDP");
190         if (!validationResult.isValid()) {
191             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
192         }
193
194         dao.update(jpaPdp);
195     }
196
197     /**
198      * Delete a PDP group.
199      *
200      * @param dao the DAO to use to access the database
201      * @param name the name of the policy to get, null to get all PDP groups
202      * @return the PDP group deleted
203      */
204     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
205
206         var pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
207
208         JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
209
210         if (jpaDeletePdpGroup == null) {
211             String errorMessage =
212                     "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
213             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
214         }
215
216         dao.delete(jpaDeletePdpGroup);
217
218         return jpaDeletePdpGroup.toAuthorative();
219     }
220
221     /**
222      * Gets all policy deployments.
223      *
224      * @param dao the DAO to use to access the database
225      * @return the deployments found
226      */
227     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao) {
228         return dao.getAll(JpaPdpPolicyStatus.class).stream().map(JpaPdpPolicyStatus::toAuthorative)
229                         .collect(Collectors.toList());
230     }
231
232     /**
233      * Gets all deployments for a policy.
234      *
235      * @param dao the DAO to use to access the database
236      * @return the deployments found
237      */
238     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao,
239                     @NonNull ToscaConceptIdentifierOptVersion policy) {
240         if (policy.getVersion() != null) {
241             return dao.getAll(JpaPdpPolicyStatus.class, new PfConceptKey(policy.getName(), policy.getVersion()))
242                             .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
243
244         } else {
245             return dao.getAllVersionsByParent(JpaPdpPolicyStatus.class, policy.getName()).stream()
246                             .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
247         }
248     }
249
250     /**
251      * Gets the policy deployments for a PDP group.
252      *
253      * @param dao the DAO to use to access the database
254      * @param groupName the name of the PDP group of interest, null to get results for all
255      *        PDP groups
256      * @return the deployments found
257      */
258     public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName) {
259         PfFilterParameters params = PfFilterParameters.builder().filterMap(Map.of("pdpGroup", groupName)).build();
260
261         return dao.getFiltered(JpaPdpPolicyStatus.class, params)
262                         .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
263     }
264
265     /**
266      * Creates, updates, and deletes collections of policy status.
267      *
268      * @param dao the DAO to use to access the database
269      * @param createObjs the objects to create
270      * @param updateObjs the objects to update
271      * @param deleteObjs the objects to delete
272      */
273     public void cudPolicyStatus(@NonNull final PfDao dao, Collection<PdpPolicyStatus> createObjs,
274                     Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
275
276         synchronized (statusLock) {
277             dao.deleteCollection(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
278             dao.createCollection(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
279             dao.createCollection(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
280         }
281     }
282
283     /**
284      * Converts a collection of authorative policy status to a collection of JPA policy
285      * status.  Validates the resulting list.
286      *
287      * @param objs authorative policy status to convert
288      * @param fieldName name of the field containing the collection
289      * @return a collection of JPA policy status
290      */
291     private Collection<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
292         if (objs == null) {
293             return Collections.emptyList();
294         }
295
296         List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());
297
298         // validate the objects
299         var result = new BeanValidationResult(fieldName, jpas);
300
301         var count = 0;
302         for (JpaPdpPolicyStatus jpa: jpas) {
303             result.addResult(jpa.validate(String.valueOf(count++)));
304         }
305
306         if (!result.isValid()) {
307             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
308         }
309
310         return jpas;
311     }
312
313     /**
314      * Convert JPA PDP group list to an authorative PDP group list.
315      *
316      * @param jpaPdpGroupList the list to convert
317      * @return the authorative list
318      */
319     private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
320         List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
321
322         for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
323             pdpGroupList.add(jpaPdpGroup.toAuthorative());
324         }
325
326         return pdpGroupList;
327     }
328 }