2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.List;
27 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfKey;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.base.PfReferenceKey;
36 import org.onap.policy.models.base.PfValidationResult;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.pdp.concepts.Pdp;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
41 import org.onap.policy.models.pdp.concepts.PdpStatistics;
42 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
43 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
44 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
45 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * This class provides the provision of information on PAP concepts in the database to callers.
52 * @author Liam Fallon (liam.fallon@est.tech)
54 public class PdpProvider {
55 private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
57 // Recurring string constants
58 private static final String NOT_VALID = "\" is not valid \n";
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 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
100 jpaPdpGroup.fromAuthorative(pdpGroup);
102 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
103 if (!validationResult.isOk()) {
104 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
105 LOGGER.warn(errorMessage);
106 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
109 dao.create(jpaPdpGroup);
112 // Return the created PDP groups
113 List<PdpGroup> returnPdpGroups = new ArrayList<>();
115 for (PdpGroup pdpGroup : pdpGroups) {
116 JpaPdpGroup jpaPdpGroup =
117 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
118 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
121 return returnPdpGroups;
125 * Updates PDP groups.
127 * @param dao the DAO to use to access the database
128 * @param pdpGroups a specification of the PDP groups to update
129 * @return the PDP groups updated
130 * @throws PfModelException on errors updating PDP groups
132 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
133 throws PfModelException {
135 for (PdpGroup pdpGroup : pdpGroups) {
136 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
137 jpaPdpGroup.fromAuthorative(pdpGroup);
139 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
140 if (!validationResult.isOk()) {
141 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
142 LOGGER.warn(errorMessage);
143 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
146 dao.update(jpaPdpGroup);
149 // Return the created PDP groups
150 List<PdpGroup> returnPdpGroups = new ArrayList<>();
152 for (PdpGroup pdpGroup : pdpGroups) {
153 JpaPdpGroup jpaPdpGroup =
154 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
155 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
158 return returnPdpGroups;
162 * Update a PDP subgroup.
164 * @param dao the DAO to use to access the database
165 * @param pdpGroupName the name of the PDP group of the PDP subgroup
166 * @param pdpSubGroup the PDP subgroup to be updated
167 * @throws PfModelException on errors updating PDP subgroups
169 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
170 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
172 final PfReferenceKey subGroupKey =
173 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
174 final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
175 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
177 PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
178 if (!validationResult.isOk()) {
179 String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
180 LOGGER.warn(errorMessage);
181 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
184 dao.update(jpaPdpSubgroup);
190 * @param dao the DAO to use to access the database
191 * @param pdpGroupName the name of the PDP group of the PDP subgroup
192 * @param pdpSubGroup the PDP subgroup to be updated
193 * @param pdp the PDP to be updated
194 * @throws PfModelException on errors updating PDP subgroups
196 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
197 @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
199 final PfReferenceKey pdpKey =
200 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
201 final JpaPdp jpaPdp = new JpaPdp(pdpKey);
202 jpaPdp.fromAuthorative(pdp);
204 PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
205 if (!validationResult.isOk()) {
206 String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
207 LOGGER.warn(errorMessage);
208 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
215 * Delete a PDP group.
217 * @param dao the DAO to use to access the database
218 * @param name the name of the policy to get, null to get all PDP groups
219 * @return the PDP group deleted
220 * @throws PfModelException on errors deleting PDP groups
222 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
224 PfConceptKey pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
226 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
228 if (jpaDeletePdpGroup == null) {
229 String errorMessage =
230 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
231 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
234 dao.delete(jpaDeletePdpGroup);
236 return jpaDeletePdpGroup.toAuthorative();
240 * Get PDP statistics.
242 * @param dao the DAO to use to access the database
243 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
244 * @return the statistics found
245 * @throws PfModelException on errors getting statistics
247 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException {
248 return new ArrayList<>();
252 * Update PDP statistics for a PDP.
254 * @param dao the DAO to use to access the database
255 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
256 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
257 * @param pdpInstanceId the instance ID of the PDP to update statistics for
258 * @param pdpStatistics the statistics to update
259 * @throws PfModelException on errors updating statistics
261 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
262 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
263 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
264 // Not implemented yet
268 * Convert JPA PDP group list to an authorative PDP group list.
270 * @param foundPdpGroups the list to convert
271 * @return the authorative list
273 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
274 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
276 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
277 pdpGroupList.add(jpaPdpGroup.toAuthorative());