2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pdp.persistence.provider;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.stream.Collectors;
30 import javax.ws.rs.core.Response;
31 import lombok.NonNull;
32 import org.onap.policy.common.parameters.BeanValidationResult;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfKey;
35 import org.onap.policy.models.base.PfModelException;
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.PdpStatistics;
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 * Get PDP statistics.
233 * @param dao the DAO to use to access the database
234 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
235 * @return the statistics found
236 * @throws PfModelException on errors getting statistics
238 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException {
239 return new ArrayList<>();
243 * Update PDP statistics for a PDP.
245 * @param dao the DAO to use to access the database
246 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
247 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
248 * @param pdpInstanceId the instance ID of the PDP to update statistics for
249 * @param pdpStatistics the statistics to update
250 * @throws PfModelException on errors updating statistics
252 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
253 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
254 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
255 // Not implemented yet
259 * Gets all policy deployments.
261 * @param dao the DAO to use to access the database
262 * @return the deployments found
263 * @throws PfModelException on errors getting PDP groups
265 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao)
266 throws PfModelException {
268 return dao.getAll(JpaPdpPolicyStatus.class).stream().map(JpaPdpPolicyStatus::toAuthorative)
269 .collect(Collectors.toList());
273 * Gets all deployments for a policy.
275 * @param dao the DAO to use to access the database
276 * @return the deployments found
277 * @throws PfModelException on errors getting PDP groups
279 public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull final PfDao dao,
280 @NonNull ToscaConceptIdentifierOptVersion policy) throws PfModelException {
282 if (policy.getVersion() != null) {
283 return dao.getAll(JpaPdpPolicyStatus.class, new PfConceptKey(policy.getName(), policy.getVersion()))
284 .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
287 return dao.getAllVersionsByParent(JpaPdpPolicyStatus.class, policy.getName()).stream()
288 .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
293 * Gets the policy deployments for a PDP group.
295 * @param dao the DAO to use to access the database
296 * @param groupName the name of the PDP group of interest, null to get results for all
298 * @return the deployments found
299 * @throws PfModelException on errors getting PDP groups
301 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName)
302 throws PfModelException {
304 PfFilterParameters params = PfFilterParameters.builder().filterMap(Map.of("pdpGroup", groupName)).build();
306 return dao.getFiltered(JpaPdpPolicyStatus.class, params)
307 .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
311 * Creates, updates, and deletes collections of policy status.
313 * @param dao the DAO to use to access the database
314 * @param createObjs the objects to create
315 * @param updateObjs the objects to update
316 * @param deleteObjs the objects to delete
318 public void cudPolicyStatus(@NonNull final PfDao dao, Collection<PdpPolicyStatus> createObjs,
319 Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
321 synchronized (statusLock) {
322 dao.deleteCollection(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
323 dao.createCollection(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
324 dao.createCollection(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
329 * Converts a collection of authorative policy status to a collection of JPA policy
330 * status. Validates the resulting list.
332 * @param objs authorative policy status to convert
333 * @param fieldName name of the field containing the collection
334 * @return a collection of JPA policy status
336 private Collection<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
338 return Collections.emptyList();
341 List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());
343 // validate the objects
344 var result = new BeanValidationResult(fieldName, jpas);
347 for (JpaPdpPolicyStatus jpa: jpas) {
348 result.addResult(jpa.validate(String.valueOf(count++)));
351 if (!result.isValid()) {
352 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
359 * Convert JPA PDP group list to an authorative PDP group list.
361 * @param jpaPdpGroupList the list to convert
362 * @return the authorative list
364 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
365 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
367 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
368 pdpGroupList.add(jpaPdpGroup.toAuthorative());