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.pdp.concepts.Pdp;
40 import org.onap.policy.models.pdp.concepts.PdpGroup;
41 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
42 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
43 import org.onap.policy.models.pdp.concepts.PdpStatistics;
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;
51 * This class provides the provision of information on PAP concepts in the database to callers.
53 * @author Liam Fallon (liam.fallon@est.tech)
55 public class PdpProvider {
56 private static final Object statusLock = new Object();
61 * @param dao the DAO to use to access the database
62 * @param name the name of the PDP group to get, null to get all PDP groups
63 * @return the PDP groups found
64 * @throws PfModelException on errors getting PDP groups
66 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
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
77 * @throws PfModelException on errors getting policies
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
91 * @throws PfModelException on errors creating PDP groups
93 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
94 throws PfModelException {
96 for (PdpGroup pdpGroup : pdpGroups) {
97 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
98 jpaPdpGroup.fromAuthorative(pdpGroup);
100 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
101 if (!validationResult.isValid()) {
102 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
105 dao.create(jpaPdpGroup);
108 // Return the created PDP groups
109 List<PdpGroup> returnPdpGroups = new ArrayList<>();
111 for (PdpGroup pdpGroup : pdpGroups) {
112 JpaPdpGroup jpaPdpGroup =
113 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
114 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
117 return returnPdpGroups;
121 * Updates PDP groups.
123 * @param dao the DAO to use to access the database
124 * @param pdpGroups a specification of the PDP groups to update
125 * @return the PDP groups updated
126 * @throws PfModelException on errors updating PDP groups
128 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
129 throws PfModelException {
131 for (PdpGroup pdpGroup : pdpGroups) {
132 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
133 jpaPdpGroup.fromAuthorative(pdpGroup);
135 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
136 if (!validationResult.isValid()) {
137 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
140 dao.update(jpaPdpGroup);
143 // Return the created PDP groups
144 List<PdpGroup> returnPdpGroups = new ArrayList<>();
146 for (PdpGroup pdpGroup : pdpGroups) {
147 JpaPdpGroup jpaPdpGroup =
148 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
149 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
152 return returnPdpGroups;
156 * Update a PDP subgroup.
158 * @param dao the DAO to use to access the database
159 * @param pdpGroupName the name of the PDP group of the PDP subgroup
160 * @param pdpSubGroup the PDP subgroup to be updated
161 * @throws PfModelException on errors updating PDP subgroups
163 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
164 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
166 final PfReferenceKey subGroupKey =
167 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
168 final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
169 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
171 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
172 if (!validationResult.isValid()) {
173 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
176 dao.update(jpaPdpSubgroup);
182 * @param dao the DAO to use to access the database
183 * @param pdpGroupName the name of the PDP group of the PDP subgroup
184 * @param pdpSubGroup the PDP subgroup to be updated
185 * @param pdp the PDP to be updated
186 * @throws PfModelException on errors updating PDP subgroups
188 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
189 @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
191 final PfReferenceKey pdpKey =
192 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
193 final JpaPdp jpaPdp = new JpaPdp(pdpKey);
194 jpaPdp.fromAuthorative(pdp);
196 BeanValidationResult validationResult = jpaPdp.validate("PDP");
197 if (!validationResult.isValid()) {
198 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
205 * Delete a PDP group.
207 * @param dao the DAO to use to access the database
208 * @param name the name of the policy to get, null to get all PDP groups
209 * @return the PDP group deleted
210 * @throws PfModelException on errors deleting PDP groups
212 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
214 PfConceptKey pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
216 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
218 if (jpaDeletePdpGroup == null) {
219 String errorMessage =
220 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
221 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
224 dao.delete(jpaDeletePdpGroup);
226 return jpaDeletePdpGroup.toAuthorative();
230 * Get PDP statistics.
232 * @param dao the DAO to use to access the database
233 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
234 * @return the statistics found
235 * @throws PfModelException on errors getting statistics
237 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException {
238 return new ArrayList<>();
242 * Update PDP statistics for a PDP.
244 * @param dao the DAO to use to access the database
245 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
246 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
247 * @param pdpInstanceId the instance ID of the PDP to update statistics for
248 * @param pdpStatistics the statistics to update
249 * @throws PfModelException on errors updating statistics
251 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
252 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
253 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
254 // Not implemented yet
258 * Gets the policy deployments for a PDP group.
260 * @param dao the DAO to use to access the database
261 * @param groupName the name of the PDP group of interest, null to get results for all
263 * @return the deployments found
264 * @throws PfModelException on errors getting PDP groups
266 public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull final PfDao dao, @NonNull final String groupName)
267 throws PfModelException {
269 Map<String, Object> filter = Map.of("pdpGroup", groupName);
271 return dao.getFiltered(JpaPdpPolicyStatus.class, null, null, null, null, filter, null, 0).stream()
272 .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList());
276 * Creates, updates, and deletes collections of policy status.
278 * @param dao the DAO to use to access the database
279 * @param createObjs the objects to create
280 * @param updateObjs the objects to update
281 * @param deleteObjs the objects to delete
283 public void cudPolicyStatus(@NonNull final PfDao dao, Collection<PdpPolicyStatus> createObjs,
284 Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs) {
286 synchronized (statusLock) {
287 dao.deleteCollection(fromAuthorativeStatus(deleteObjs, "deletePdpPolicyStatusList"));
288 dao.createCollection(fromAuthorativeStatus(createObjs, "createPdpPolicyStatusList"));
289 dao.createCollection(fromAuthorativeStatus(updateObjs, "updatePdpPolicyStatusList"));
294 * Converts a collection of authorative policy status to a collection of JPA policy
295 * status. Validates the resulting list.
297 * @param objs authorative policy status to convert
298 * @param fieldName name of the field containing the collection
299 * @return a collection of JPA policy status
301 private Collection<JpaPdpPolicyStatus> fromAuthorativeStatus(Collection<PdpPolicyStatus> objs, String fieldName) {
303 return Collections.emptyList();
306 List<JpaPdpPolicyStatus> jpas = objs.stream().map(JpaPdpPolicyStatus::new).collect(Collectors.toList());
308 // validate the objects
309 BeanValidationResult result = new BeanValidationResult(fieldName, jpas);
312 for (JpaPdpPolicyStatus jpa: jpas) {
313 result.addResult(jpa.validate(String.valueOf(count++)));
316 if (!result.isValid()) {
317 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
324 * Convert JPA PDP group list to an authorative PDP group list.
326 * @param foundPdpGroups the list to convert
327 * @return the authorative list
329 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
330 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
332 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
333 pdpGroupList.add(jpaPdpGroup.toAuthorative());