2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.pdp.persistence.provider;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
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;
53 * This class provides the provision of information on PAP concepts in the database to callers.
55 * @author Liam Fallon (liam.fallon@est.tech)
57 public class PdpProvider {
58 private static final Object statusLock = new Object();
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 * @throws PfModelException on errors getting PDP groups
68 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
70 return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
74 * Get filtered PDP groups.
76 * @param dao the DAO to use to access the database
77 * @param filter the filter for the PDP groups to get
78 * @return the PDP groups found
79 * @throws PfModelException on errors getting policies
81 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
84 asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
90 * @param dao the DAO to use to access the database
91 * @param pdpGroups a specification of the PDP groups to create
92 * @return the PDP groups created
93 * @throws PfModelException on errors creating PDP groups
95 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
96 throws PfModelException {
98 for (PdpGroup pdpGroup : pdpGroups) {
99 var jpaPdpGroup = new JpaPdpGroup();
100 jpaPdpGroup.fromAuthorative(pdpGroup);
102 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
103 if (!validationResult.isValid()) {
104 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
107 dao.create(jpaPdpGroup);
110 // Return the created PDP groups
111 List<PdpGroup> returnPdpGroups = new ArrayList<>();
113 for (PdpGroup pdpGroup : pdpGroups) {
114 var jpaPdpGroup = dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
115 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
118 return returnPdpGroups;
122 * Updates PDP groups.
124 * @param dao the DAO to use to access the database
125 * @param pdpGroups a specification of the PDP groups to update
126 * @return the PDP groups updated
127 * @throws PfModelException on errors updating PDP groups
129 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
130 throws PfModelException {
132 for (PdpGroup pdpGroup : pdpGroups) {
133 var jpaPdpGroup = new JpaPdpGroup();
134 jpaPdpGroup.fromAuthorative(pdpGroup);
136 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
137 if (!validationResult.isValid()) {
138 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
141 dao.update(jpaPdpGroup);
144 // Return the created PDP groups
145 List<PdpGroup> returnPdpGroups = new ArrayList<>();
147 for (PdpGroup pdpGroup : pdpGroups) {
149 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
150 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
153 return returnPdpGroups;
157 * Update a PDP subgroup.
159 * @param dao the DAO to use to access the database
160 * @param pdpGroupName the name of the PDP group of the PDP subgroup
161 * @param pdpSubGroup the PDP subgroup to be updated
162 * @throws PfModelException on errors updating PDP subgroups
164 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
165 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
167 final var subGroupKey =
168 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
169 final var jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
170 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
172 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
173 if (!validationResult.isValid()) {
174 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
177 dao.update(jpaPdpSubgroup);
183 * @param dao the DAO to use to access the database
184 * @param pdpGroupName the name of the PDP group of the PDP subgroup
185 * @param pdpSubGroup the PDP subgroup to be updated
186 * @param pdp the PDP to be updated
187 * @throws PfModelException on errors updating PDP subgroups
189 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
190 @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
193 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
194 final var jpaPdp = new JpaPdp(pdpKey);
195 jpaPdp.fromAuthorative(pdp);
197 BeanValidationResult validationResult = jpaPdp.validate("PDP");
198 if (!validationResult.isValid()) {
199 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
206 * Delete a PDP group.
208 * @param dao the DAO to use to access the database
209 * @param name the name of the policy to get, null to get all PDP groups
210 * @return the PDP group deleted
211 * @throws PfModelException on errors deleting PDP groups
213 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
215 var pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
217 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
219 if (jpaDeletePdpGroup == null) {
220 String errorMessage =
221 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
222 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
225 dao.delete(jpaDeletePdpGroup);
227 return jpaDeletePdpGroup.toAuthorative();
231 * Gets all policy deployments.
233 * @param dao the DAO to use to access the database
234 * @return the deployments found
235 * @throws PfModelException on errors getting PDP groups
237 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao)
238 throws PfModelException {
240 return dao.getAll(JpaPdpPolicyStatus.class).stream().map(JpaPdpPolicyStatus::toAuthorative)
241 .collect(Collectors.toList());
245 * Gets all deployments for a policy.
247 * @param dao the DAO to use to access the database
248 * @return the deployments found
249 * @throws PfModelException on errors getting PDP groups
251 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao,
252 @NonNull ToscaConceptIdentifierOptVersion policy) throws PfModelException {
254 if (policy.getVersion() != null) {
255 return dao.getAll(JpaPdpPolicyStatus.class, new PfConceptKey(policy.getName(), policy.getVersion()))
256 .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
259 return dao.getAllVersionsByParent(JpaPdpPolicyStatus.class, policy.getName()).stream()
260 .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
265 * Gets the policy deployments for a PDP group.
267 * @param dao the DAO to use to access the database
268 * @param groupName the name of the PDP group of interest, null to get results for all
270 * @return the deployments found
271 * @throws PfModelException on errors getting PDP groups
273 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName)
274 throws PfModelException {
276 PfFilterParameters params = PfFilterParameters.builder().filterMap(Map.of("pdpGroup", groupName)).build();
278 return dao.getFiltered(JpaPdpPolicyStatus.class, params)
279 .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
283 * Creates, updates, and deletes collections of policy status.
285 * @param dao the DAO to use to access the database
286 * @param createObjs the objects to create
287 * @param updateObjs the objects to update
288 * @param deleteObjs the objects to delete
290 public void cudPolicyStatus(@NonNull final PfDao dao, Collection<PdpPolicyStatus> createObjs,
291 Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
293 synchronized (statusLock) {
294 dao.deleteCollection(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
295 dao.createCollection(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
296 dao.createCollection(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
301 * Converts a collection of authorative policy status to a collection of JPA policy
302 * status. Validates the resulting list.
304 * @param objs authorative policy status to convert
305 * @param fieldName name of the field containing the collection
306 * @return a collection of JPA policy status
308 private Collection<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
310 return Collections.emptyList();
313 List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());
315 // validate the objects
316 var result = new BeanValidationResult(fieldName, jpas);
319 for (JpaPdpPolicyStatus jpa: jpas) {
320 result.addResult(jpa.validate(String.valueOf(count++)));
323 if (!result.isValid()) {
324 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
331 * Convert JPA PDP group list to an authorative PDP group list.
333 * @param jpaPdpGroupList the list to convert
334 * @return the authorative list
336 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
337 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
339 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
340 pdpGroupList.add(jpaPdpGroup.toAuthorative());