Fix trailing space in Info.yaml models
[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.PfModelRuntimeException;
37 import org.onap.policy.models.base.PfReferenceKey;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.dao.PfFilterParameters;
40 import org.onap.policy.models.pdp.concepts.Pdp;
41 import org.onap.policy.models.pdp.concepts.PdpGroup;
42 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
43 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
44 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
45 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
46 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
47 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus;
48 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
50
51 /**
52  * This class provides the provision of information on PAP concepts in the database to callers.
53  *
54  * @author Liam Fallon (liam.fallon@est.tech)
55  */
56 public class PdpProvider {
57     private static final Object statusLock = new Object();
58
59     /**
60      * Get PDP groups.
61      *
62      * @param dao the DAO to use to access the database
63      * @param name the name of the PDP group to get, null to get all PDP groups
64      * @return the PDP groups found
65      */
66     public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) {
67
68         return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
69     }
70
71     /**
72      * Get filtered PDP groups.
73      *
74      * @param dao the DAO to use to access the database
75      * @param filter the filter for the PDP groups to get
76      * @return the PDP groups found
77      */
78     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
79
80         return filter.filter(
81                         asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
82     }
83
84     /**
85      * Creates PDP groups.
86      *
87      * @param dao the DAO to use to access the database
88      * @param pdpGroups a specification of the PDP groups to create
89      * @return the PDP groups created
90      */
91     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
92
93         for (PdpGroup pdpGroup : pdpGroups) {
94             var jpaPdpGroup = new JpaPdpGroup();
95             jpaPdpGroup.fromAuthorative(pdpGroup);
96
97             BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
98             if (!validationResult.isValid()) {
99                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
100             }
101
102             dao.create(jpaPdpGroup);
103         }
104
105         // Return the created PDP groups
106         List<PdpGroup> returnPdpGroups = new ArrayList<>();
107
108         for (PdpGroup pdpGroup : pdpGroups) {
109             var jpaPdpGroup = dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
110             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
111         }
112
113         return returnPdpGroups;
114     }
115
116     /**
117      * Updates PDP groups.
118      *
119      * @param dao the DAO to use to access the database
120      * @param pdpGroups a specification of the PDP groups to update
121      * @return the PDP groups updated
122      */
123     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
124
125         for (PdpGroup pdpGroup : pdpGroups) {
126             var jpaPdpGroup = new JpaPdpGroup();
127             jpaPdpGroup.fromAuthorative(pdpGroup);
128
129             BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
130             if (!validationResult.isValid()) {
131                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
132             }
133
134             dao.update(jpaPdpGroup);
135         }
136
137         // Return the created PDP groups
138         List<PdpGroup> returnPdpGroups = new ArrayList<>();
139
140         for (PdpGroup pdpGroup : pdpGroups) {
141             var jpaPdpGroup =
142                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
143             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
144         }
145
146         return returnPdpGroups;
147     }
148
149     /**
150      * Update a PDP subgroup.
151      *
152      * @param dao the DAO to use to access the database
153      * @param pdpGroupName the name of the PDP group of the PDP subgroup
154      * @param pdpSubGroup the PDP subgroup to be updated
155      */
156     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
157             @NonNull final PdpSubGroup pdpSubGroup) {
158
159         final var subGroupKey =
160                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
161         final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
162         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
163
164         BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
165         if (!validationResult.isValid()) {
166             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
167         }
168
169         dao.update(jpaPdpSubgroup);
170     }
171
172     /**
173      * Update a PDP.
174      *
175      * @param dao the DAO to use to access the database
176      * @param pdpGroupName the name of the PDP group of the PDP subgroup
177      * @param pdpSubGroup the PDP subgroup to be updated
178      * @param pdp the PDP to be updated
179      */
180     public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
181             @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
182
183         final var pdpKey =
184                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
185         final var jpaPdp = new JpaPdp(pdpKey);
186         jpaPdp.fromAuthorative(pdp);
187
188         BeanValidationResult validationResult = jpaPdp.validate("PDP");
189         if (!validationResult.isValid()) {
190             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
191         }
192
193         dao.update(jpaPdp);
194     }
195
196     /**
197      * Delete a PDP group.
198      *
199      * @param dao the DAO to use to access the database
200      * @param name the name of the policy to get, null to get all PDP groups
201      * @return the PDP group deleted
202      */
203     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
204
205         var pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
206
207         JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
208
209         if (jpaDeletePdpGroup == null) {
210             String errorMessage =
211                     "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
212             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
213         }
214
215         dao.delete(jpaDeletePdpGroup);
216
217         return jpaDeletePdpGroup.toAuthorative();
218     }
219
220     /**
221      * Gets all policy deployments.
222      *
223      * @param dao the DAO to use to access the database
224      * @return the deployments found
225      */
226     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao) {
227         return dao.getAll(JpaPdpPolicyStatus.class).stream().map(JpaPdpPolicyStatus::toAuthorative)
228                         .collect(Collectors.toList());
229     }
230
231     /**
232      * Gets all deployments for a policy.
233      *
234      * @param dao the DAO to use to access the database
235      * @return the deployments found
236      */
237     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao,
238                     @NonNull ToscaConceptIdentifierOptVersion policy) {
239         if (policy.getVersion() != null) {
240             return dao.getAll(JpaPdpPolicyStatus.class, new PfConceptKey(policy.getName(), policy.getVersion()))
241                             .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
242
243         } else {
244             return dao.getAllVersionsByParent(JpaPdpPolicyStatus.class, policy.getName()).stream()
245                             .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
246         }
247     }
248
249     /**
250      * Gets the policy deployments for a PDP group.
251      *
252      * @param dao the DAO to use to access the database
253      * @param groupName the name of the PDP group of interest, null to get results for all
254      *        PDP groups
255      * @return the deployments found
256      */
257     public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName) {
258         PfFilterParameters params = PfFilterParameters.builder().filterMap(Map.of("pdpGroup", groupName)).build();
259
260         return dao.getFiltered(JpaPdpPolicyStatus.class, params)
261                         .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
262     }
263
264     /**
265      * Creates, updates, and deletes collections of policy status.
266      *
267      * @param dao the DAO to use to access the database
268      * @param createObjs the objects to create
269      * @param updateObjs the objects to update
270      * @param deleteObjs the objects to delete
271      */
272     public void cudPolicyStatus(@NonNull final PfDao dao, Collection<PdpPolicyStatus> createObjs,
273                     Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
274
275         synchronized (statusLock) {
276             dao.deleteCollection(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
277             dao.createCollection(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
278             dao.createCollection(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
279         }
280     }
281
282     /**
283      * Converts a collection of authorative policy status to a collection of JPA policy
284      * status.  Validates the resulting list.
285      *
286      * @param objs authorative policy status to convert
287      * @param fieldName name of the field containing the collection
288      * @return a collection of JPA policy status
289      */
290     private Collection<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
291         if (objs == null) {
292             return Collections.emptyList();
293         }
294
295         List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new)
296             .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 }