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.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
67 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) {
69 return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
73 * Get filtered PDP groups.
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
79 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
82 asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
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
92 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
94 for (PdpGroup pdpGroup : pdpGroups) {
95 var jpaPdpGroup = new JpaPdpGroup();
96 jpaPdpGroup.fromAuthorative(pdpGroup);
98 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
99 if (!validationResult.isValid()) {
100 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
103 dao.create(jpaPdpGroup);
106 // Return the created PDP groups
107 List<PdpGroup> returnPdpGroups = new ArrayList<>();
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());
114 return returnPdpGroups;
118 * Updates PDP groups.
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
124 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups) {
126 for (PdpGroup pdpGroup : pdpGroups) {
127 var jpaPdpGroup = new JpaPdpGroup();
128 jpaPdpGroup.fromAuthorative(pdpGroup);
130 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
131 if (!validationResult.isValid()) {
132 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
135 dao.update(jpaPdpGroup);
138 // Return the created PDP groups
139 List<PdpGroup> returnPdpGroups = new ArrayList<>();
141 for (PdpGroup pdpGroup : pdpGroups) {
143 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
144 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
147 return returnPdpGroups;
151 * Update a PDP subgroup.
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
157 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
158 @NonNull final PdpSubGroup pdpSubGroup) {
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);
165 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
166 if (!validationResult.isValid()) {
167 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
170 dao.update(jpaPdpSubgroup);
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
181 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
182 @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
185 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
186 final var jpaPdp = new JpaPdp(pdpKey);
187 jpaPdp.fromAuthorative(pdp);
189 BeanValidationResult validationResult = jpaPdp.validate("PDP");
190 if (!validationResult.isValid()) {
191 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
198 * Delete a PDP group.
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
204 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
206 var pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
208 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
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);
216 dao.delete(jpaDeletePdpGroup);
218 return jpaDeletePdpGroup.toAuthorative();
222 * Gets all policy deployments.
224 * @param dao the DAO to use to access the database
225 * @return the deployments found
227 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao) {
228 return dao.getAll(JpaPdpPolicyStatus.class).stream().map(JpaPdpPolicyStatus::toAuthorative)
229 .collect(Collectors.toList());
233 * Gets all deployments for a policy.
235 * @param dao the DAO to use to access the database
236 * @return the deployments found
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());
245 return dao.getAllVersionsByParent(JpaPdpPolicyStatus.class, policy.getName()).stream()
246 .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
251 * Gets the policy deployments for a PDP group.
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
256 * @return the deployments found
258 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName) {
259 PfFilterParameters params = PfFilterParameters.builder().filterMap(Map.of("pdpGroup", groupName)).build();
261 return dao.getFiltered(JpaPdpPolicyStatus.class, params)
262 .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
266 * Creates, updates, and deletes collections of policy status.
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
273 public void cudPolicyStatus(@NonNull final PfDao dao, Collection<PdpPolicyStatus> createObjs,
274 Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
276 synchronized (statusLock) {
277 dao.deleteCollection(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
278 dao.createCollection(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
279 dao.createCollection(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
284 * Converts a collection of authorative policy status to a collection of JPA policy
285 * status. Validates the resulting list.
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
291 private Collection<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
293 return Collections.emptyList();
296 List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).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());