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 return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, 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) {
83 List<JpaPdpGroup> jpaPdpGroupList = dao.getAll(JpaPdpGroup.class);
85 return filter.filter(asPdpGroupList(jpaPdpGroupList));
91 * @param dao the DAO to use to access the database
92 * @param pdpGroups a specification of the PDP groups to create
93 * @return the PDP groups created
94 * @throws PfModelException on errors creating PDP groups
96 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
97 throws PfModelException {
99 for (PdpGroup pdpGroup : pdpGroups) {
100 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
101 jpaPdpGroup.fromAuthorative(pdpGroup);
103 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
104 if (!validationResult.isOk()) {
105 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
106 LOGGER.warn(errorMessage);
107 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
110 dao.create(jpaPdpGroup);
113 // Return the created PDP groups
114 List<PdpGroup> returnPdpGroups = new ArrayList<>();
116 for (PdpGroup pdpGroup : pdpGroups) {
117 JpaPdpGroup jpaPdpGroup =
118 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
119 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
122 return returnPdpGroups;
126 * Updates PDP groups.
128 * @param dao the DAO to use to access the database
129 * @param pdpGroups a specification of the PDP groups to update
130 * @return the PDP groups updated
131 * @throws PfModelException on errors updating PDP groups
133 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
134 throws PfModelException {
136 for (PdpGroup pdpGroup : pdpGroups) {
137 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
138 jpaPdpGroup.fromAuthorative(pdpGroup);
140 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
141 if (!validationResult.isOk()) {
142 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
143 LOGGER.warn(errorMessage);
144 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
147 dao.update(jpaPdpGroup);
150 // Return the created PDP groups
151 List<PdpGroup> returnPdpGroups = new ArrayList<>();
153 for (PdpGroup pdpGroup : pdpGroups) {
154 JpaPdpGroup jpaPdpGroup =
155 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
156 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
159 return returnPdpGroups;
163 * Update a PDP subgroup.
165 * @param dao the DAO to use to access the database
166 * @param pdpGroupName the name of the PDP group of the PDP subgroup
167 * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
168 * @param pdpSubGroup the PDP subgroup to be updated
169 * @throws PfModelException on errors updating PDP subgroups
171 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
172 @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
174 final PfReferenceKey subGroupKey = new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup.getPdpType());
175 final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
176 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
178 PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
179 if (!validationResult.isOk()) {
180 String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
181 LOGGER.warn(errorMessage);
182 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
185 dao.update(jpaPdpSubgroup);
191 * @param dao the DAO to use to access the database
192 * @param pdpGroupName the name of the PDP group of the PDP subgroup
193 * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
194 * @param pdpSubGroup the PDP subgroup to be updated
195 * @param pdp the PDP to be updated
196 * @throws PfModelException on errors updating PDP subgroups
198 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
199 @NonNull final String pdpGroupVersion, @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
201 final PfReferenceKey pdpKey =
202 new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp.getInstanceId());
203 final JpaPdp jpaPdp = new JpaPdp(pdpKey);
204 jpaPdp.fromAuthorative(pdp);
206 PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
207 if (!validationResult.isOk()) {
208 String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
209 LOGGER.warn(errorMessage);
210 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
217 * Delete a PDP group.
219 * @param dao the DAO to use to access the database
220 * @param name the name of the policy to get, null to get all PDP groups
221 * @param version the version of the policy to get, null to get all versions of a PDP group
222 * @return the PDP group deleted
223 * @throws PfModelException on errors deleting PDP groups
225 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name,
226 @NonNull final String version) {
228 PfConceptKey pdpGroupKey = new PfConceptKey(name, version);
230 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
232 if (jpaDeletePdpGroup == null) {
233 String errorMessage =
234 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
235 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
238 dao.delete(jpaDeletePdpGroup);
240 return jpaDeletePdpGroup.toAuthorative();
244 * Get PDP statistics.
246 * @param dao the DAO to use to access the database
247 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
248 * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group
249 * @return the statistics found
250 * @throws PfModelException on errors getting statistics
252 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
253 throws PfModelException {
254 return new ArrayList<>();
258 * Update PDP statistics for a PDP.
260 * @param dao the DAO to use to access the database
261 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
262 * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for
263 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
264 * @param pdpInstanceId the instance ID of the PDP to update statistics for
265 * @param pdpStatistics the statistics to update
266 * @throws PfModelException on errors updating statistics
268 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
269 @NonNull final String pdpGroupVersion, @NonNull final String pdpType, @NonNull final String pdpInstanceId,
270 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
271 // Not implemented yet
275 * Convert JPA PDP group list to an authorative PDP group list.
277 * @param foundPdpGroups the list to convert
278 * @return the authorative list
280 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
281 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
283 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
284 pdpGroupList.add(jpaPdpGroup.toAuthorative());