2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.persistence.provider;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.base.PfReferenceKey;
34 import org.onap.policy.models.base.PfValidationResult;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.pdp.concepts.Pdp;
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
38 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
41 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
42 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
43 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * This class provides the provision of information on PAP concepts in the database to callers.
50 * @author Liam Fallon (liam.fallon@est.tech)
52 public class PdpProvider {
53 private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
55 // Recurring string constants
56 private static final String NOT_VALID = "\" is not valid \n";
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 * @param version the version of the policy to get, null to get all versions of a PDP group
64 * @return the PDP groups found
65 * @throws PfModelException on errors getting PDP groups
67 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name, final String version)
68 throws PfModelException {
70 List<JpaPdpGroup> foundPdpGroups = dao.getFiltered(JpaPdpGroup.class, name, version);
72 if (foundPdpGroups != null) {
73 return asPdpGroupList(foundPdpGroups);
75 String errorMessage = "no PDP groups found for filter " + name + ":" + version;
76 LOGGER.warn(errorMessage);
77 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
82 * Get latest PDP Groups, returns PDP groups in all states.
84 * @param dao the DAO to use to access the database
85 * @param name the name of the PDP group to get, null to get all PDP groups
86 * @return the PDP groups found
87 * @throws PfModelException on errors getting policies
89 public List<PdpGroup> getLatestPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
90 List<JpaPdpGroup> jpaPdpGroupList = new ArrayList<>();
93 jpaPdpGroupList.addAll(dao.getAll(JpaPdpGroup.class));
95 jpaPdpGroupList.addAll(dao.getAllVersions(JpaPdpGroup.class, name));
98 return asPdpGroupList(jpaPdpGroupList);
102 * Get filtered PDP groups.
104 * @param dao the DAO to use to access the database
105 * @param filter the filter for the PDP groups to get
106 * @return the PDP groups found
107 * @throws PfModelException on errors getting policies
109 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter)
110 throws PfModelException {
112 List<JpaPdpGroup> jpaPdpGroupList = dao.getAll(JpaPdpGroup.class);
114 return asPdpGroupList(jpaPdpGroupList);
118 * Creates PDP groups.
120 * @param dao the DAO to use to access the database
121 * @param pdpGroups a specification of the PDP groups to create
122 * @return the PDP groups created
123 * @throws PfModelException on errors creating PDP groups
125 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
126 throws PfModelException {
128 for (PdpGroup pdpGroup : pdpGroups) {
129 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
130 jpaPdpGroup.fromAuthorative(pdpGroup);
132 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
133 if (!validationResult.isOk()) {
134 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
135 LOGGER.warn(errorMessage);
136 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
139 dao.create(jpaPdpGroup);
142 // Return the created PDP groups
143 List<PdpGroup> returnPdpGroups = new ArrayList<>();
145 for (PdpGroup pdpGroup : pdpGroups) {
146 JpaPdpGroup jpaPdpGroup =
147 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
148 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
151 return returnPdpGroups;
155 * Updates PDP groups.
157 * @param dao the DAO to use to access the database
158 * @param pdpGroups a specification of the PDP groups to update
159 * @return the PDP groups updated
160 * @throws PfModelException on errors updating PDP groups
162 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
163 throws PfModelException {
165 for (PdpGroup pdpGroup : pdpGroups) {
166 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
167 jpaPdpGroup.fromAuthorative(pdpGroup);
169 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
170 if (!validationResult.isOk()) {
171 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
172 LOGGER.warn(errorMessage);
173 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
176 dao.update(jpaPdpGroup);
179 // Return the created PDP groups
180 List<PdpGroup> returnPdpGroups = new ArrayList<>();
182 for (PdpGroup pdpGroup : pdpGroups) {
183 JpaPdpGroup jpaPdpGroup =
184 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
185 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
188 return returnPdpGroups;
192 * Update a PDP subgroup.
194 * @param dao the DAO to use to access the database
195 * @param pdpGroupName the name of the PDP group of the PDP subgroup
196 * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
197 * @param pdpSubGroup the PDP subgroup to be updated
198 * @throws PfModelException on errors updating PDP subgroups
200 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
201 @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
203 final PfReferenceKey subGroupKey = new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup.getPdpType());
204 final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
205 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
207 PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
208 if (!validationResult.isOk()) {
209 String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
210 LOGGER.warn(errorMessage);
211 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
214 if (dao.update(jpaPdpSubgroup) == null) {
215 String errorMessage = "update of PDP subgroup \"" + jpaPdpSubgroup.getId() + "\" failed";
216 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
223 * @param dao the DAO to use to access the database
224 * @param pdpGroupName the name of the PDP group of the PDP subgroup
225 * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
226 * @param pdpSubGroup the PDP subgroup to be updated
227 * @param pdp the PDP to be updated
228 * @throws PfModelException on errors updating PDP subgroups
230 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
231 @NonNull final String pdpGroupVersion, @NonNull final String pdpSubGroup, @NonNull final Pdp pdp)
232 throws PfModelException {
234 final PfReferenceKey pdpKey =
235 new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp.getInstanceId());
236 final JpaPdp jpaPdp = new JpaPdp(pdpKey);
237 jpaPdp.fromAuthorative(pdp);
239 PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
240 if (!validationResult.isOk()) {
241 String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
242 LOGGER.warn(errorMessage);
243 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
246 if (dao.update(jpaPdp) == null) {
247 String errorMessage = "update of PDP \"" + jpaPdp.getId() + "\" failed";
248 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
253 * Delete a PDP group.
255 * @param dao the DAO to use to access the database
256 * @param name the name of the policy to get, null to get all PDP groups
257 * @param version the version of the policy to get, null to get all versions of a PDP group
258 * @return the PDP group deleted
259 * @throws PfModelException on errors deleting PDP groups
261 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name, @NonNull final String version)
262 throws PfModelException {
264 PfConceptKey pdpGroupKey = new PfConceptKey(name, version);
266 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
268 if (jpaDeletePdpGroup == null) {
269 String errorMessage =
270 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
271 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
274 dao.delete(jpaDeletePdpGroup);
276 return jpaDeletePdpGroup.toAuthorative();
280 * Get PDP statistics.
282 * @param dao the DAO to use to access the database
283 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
284 * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group
285 * @return the statistics found
286 * @throws PfModelException on errors getting statistics
288 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
289 throws PfModelException {
290 return new ArrayList<>();
294 * Update PDP statistics for a PDP.
296 * @param dao the DAO to use to access the database
297 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
298 * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for
299 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
300 * @param pdpInstanceId the instance ID of the PDP to update statistics for
301 * @throws PfModelException on errors updating statistics
303 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
304 @NonNull final String pdpGroupVersion, @NonNull final String pdpType, @NonNull final String pdpInstanceId,
305 @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
306 // Not implemented yet
310 * Convert JPA PDP group list to an authorative PDP group list.
312 * @param foundPdpGroups the list to convert
313 * @return the authorative list
315 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
316 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
318 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
319 pdpGroupList.add(jpaPdpGroup.toAuthorative());