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
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 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;
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;
52 * This class provides the provision of information on PAP concepts in the database to callers.
54 * @author Liam Fallon (liam.fallon@est.tech)
56 public class PdpProvider {
57 private static final Object statusLock = new Object();
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
66 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) {
68 return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
72 * Get filtered PDP groups.
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
78 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
81 asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
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
91 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
93 for (PdpGroup pdpGroup : pdpGroups) {
94 var jpaPdpGroup = new JpaPdpGroup();
95 jpaPdpGroup.fromAuthorative(pdpGroup);
97 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
98 if (!validationResult.isValid()) {
99 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
102 dao.create(jpaPdpGroup);
105 // Return the created PDP groups
106 List<PdpGroup> returnPdpGroups = new ArrayList<>();
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());
113 return returnPdpGroups;
117 * Updates PDP groups.
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
123 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
125 for (PdpGroup pdpGroup : pdpGroups) {
126 var jpaPdpGroup = new JpaPdpGroup();
127 jpaPdpGroup.fromAuthorative(pdpGroup);
129 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
130 if (!validationResult.isValid()) {
131 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
134 dao.update(jpaPdpGroup);
137 // Return the created PDP groups
138 List<PdpGroup> returnPdpGroups = new ArrayList<>();
140 for (PdpGroup pdpGroup : pdpGroups) {
142 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
143 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
146 return returnPdpGroups;
150 * Update a PDP subgroup.
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
156 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
157 @NonNull final PdpSubGroup pdpSubGroup) {
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);
164 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
165 if (!validationResult.isValid()) {
166 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
169 dao.update(jpaPdpSubgroup);
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
180 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
181 @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
184 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
185 final var jpaPdp = new JpaPdp(pdpKey);
186 jpaPdp.fromAuthorative(pdp);
188 BeanValidationResult validationResult = jpaPdp.validate("PDP");
189 if (!validationResult.isValid()) {
190 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
197 * Delete a PDP group.
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
203 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
205 var pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
207 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
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);
215 dao.delete(jpaDeletePdpGroup);
217 return jpaDeletePdpGroup.toAuthorative();
221 * Gets all policy deployments.
223 * @param dao the DAO to use to access the database
224 * @return the deployments found
226 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao) {
227 return dao.getAll(JpaPdpPolicyStatus.class).stream().map(JpaPdpPolicyStatus::toAuthorative)
228 .collect(Collectors.toList());
232 * Gets all deployments for a policy.
234 * @param dao the DAO to use to access the database
235 * @return the deployments found
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());
244 return dao.getAllVersionsByParent(JpaPdpPolicyStatus.class, policy.getName()).stream()
245 .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
250 * Gets the policy deployments for a PDP group.
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
255 * @return the deployments found
257 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName) {
258 PfFilterParameters params = PfFilterParameters.builder().filterMap(Map.of("pdpGroup", groupName)).build();
260 return dao.getFiltered(JpaPdpPolicyStatus.class, params)
261 .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
265 * Creates, updates, and deletes collections of policy status.
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
272 public void cudPolicyStatus(@NonNull final PfDao dao, Collection<PdpPolicyStatus> createObjs,
273 Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
275 synchronized (statusLock) {
276 dao.deleteCollection(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
277 dao.createCollection(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
278 dao.createCollection(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
283 * Converts a collection of authorative policy status to a collection of JPA policy
284 * status. Validates the resulting list.
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
290 private Collection<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
292 return Collections.emptyList();
295 List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new)
296 .collect(Collectors.toList());
298 // validate the objects
299 var result = new BeanValidationResult(fieldName, jpas);
302 for (JpaPdpPolicyStatus jpa: jpas) {
303 result.addResult(jpa.validate(String.valueOf(count++)));
306 if (!result.isValid()) {
307 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
314 * Convert JPA PDP group list to an authorative PDP group list.
316 * @param jpaPdpGroupList the list to convert
317 * @return the authorative list
319 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
320 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
322 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
323 pdpGroupList.add(jpaPdpGroup.toAuthorative());