2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 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;
26 import javax.ws.rs.core.Response;
27 import lombok.NonNull;
28 import org.onap.policy.common.parameters.BeanValidationResult;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfKey;
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.dao.PfDao;
35 import org.onap.policy.models.pdp.concepts.Pdp;
36 import org.onap.policy.models.pdp.concepts.PdpGroup;
37 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
38 import org.onap.policy.models.pdp.concepts.PdpStatistics;
39 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
40 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
41 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
42 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
45 * This class provides the provision of information on PAP concepts in the database to callers.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class PdpProvider {
54 * @param dao the DAO to use to access the database
55 * @param name the name of the PDP group to get, null to get all PDP groups
56 * @return the PDP groups found
57 * @throws PfModelException on errors getting PDP groups
59 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
61 return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
65 * Get filtered PDP groups.
67 * @param dao the DAO to use to access the database
68 * @param filter the filter for the PDP groups to get
69 * @return the PDP groups found
70 * @throws PfModelException on errors getting policies
72 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
75 asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
81 * @param dao the DAO to use to access the database
82 * @param pdpGroups a specification of the PDP groups to create
83 * @return the PDP groups created
84 * @throws PfModelException on errors creating PDP groups
86 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
87 throws PfModelException {
89 for (PdpGroup pdpGroup : pdpGroups) {
90 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
91 jpaPdpGroup.fromAuthorative(pdpGroup);
93 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
94 if (!validationResult.isValid()) {
95 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
98 dao.create(jpaPdpGroup);
101 // Return the created PDP groups
102 List<PdpGroup> returnPdpGroups = new ArrayList<>();
104 for (PdpGroup pdpGroup : pdpGroups) {
105 JpaPdpGroup jpaPdpGroup =
106 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
107 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
110 return returnPdpGroups;
114 * Updates PDP groups.
116 * @param dao the DAO to use to access the database
117 * @param pdpGroups a specification of the PDP groups to update
118 * @return the PDP groups updated
119 * @throws PfModelException on errors updating PDP groups
121 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
122 throws PfModelException {
124 for (PdpGroup pdpGroup : pdpGroups) {
125 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
126 jpaPdpGroup.fromAuthorative(pdpGroup);
128 BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
129 if (!validationResult.isValid()) {
130 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
133 dao.update(jpaPdpGroup);
136 // Return the created PDP groups
137 List<PdpGroup> returnPdpGroups = new ArrayList<>();
139 for (PdpGroup pdpGroup : pdpGroups) {
140 JpaPdpGroup jpaPdpGroup =
141 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
142 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
145 return returnPdpGroups;
149 * Update a PDP subgroup.
151 * @param dao the DAO to use to access the database
152 * @param pdpGroupName the name of the PDP group of the PDP subgroup
153 * @param pdpSubGroup the PDP subgroup to be updated
154 * @throws PfModelException on errors updating PDP subgroups
156 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
157 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
159 final PfReferenceKey subGroupKey =
160 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
161 final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
162 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
164 BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
165 if (!validationResult.isValid()) {
166 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
169 dao.update(jpaPdpSubgroup);
175 * @param dao the DAO to use to access the database
176 * @param pdpGroupName the name of the PDP group of the PDP subgroup
177 * @param pdpSubGroup the PDP subgroup to be updated
178 * @param pdp the PDP to be updated
179 * @throws PfModelException on errors updating PDP subgroups
181 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
182 @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
184 final PfReferenceKey pdpKey =
185 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
186 final JpaPdp jpaPdp = new JpaPdp(pdpKey);
187 jpaPdp.fromAuthorative(pdp);
189 BeanValidationResult validationResult = jpaPdp.validate("PDP");
190 if (!validationResult.isValid()) {
191 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
198 * Delete a PDP group.
200 * @param dao the DAO to use to access the database
201 * @param name the name of the policy to get, null to get all PDP groups
202 * @return the PDP group deleted
203 * @throws PfModelException on errors deleting PDP groups
205 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
207 PfConceptKey pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
209 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
211 if (jpaDeletePdpGroup == null) {
212 String errorMessage =
213 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
214 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
217 dao.delete(jpaDeletePdpGroup);
219 return jpaDeletePdpGroup.toAuthorative();
223 * Get PDP statistics.
225 * @param dao the DAO to use to access the database
226 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
227 * @return the statistics found
228 * @throws PfModelException on errors getting statistics
230 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException {
231 return new ArrayList<>();
235 * Update PDP statistics for a PDP.
237 * @param dao the DAO to use to access the database
238 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
239 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
240 * @param pdpInstanceId the instance ID of the PDP to update statistics for
241 * @param pdpStatistics the statistics to update
242 * @throws PfModelException on errors updating statistics
244 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
245 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
246 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
247 // Not implemented yet
251 * Convert JPA PDP group list to an authorative PDP group list.
253 * @param foundPdpGroups the list to convert
254 * @return the authorative list
256 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
257 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
259 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
260 pdpGroupList.add(jpaPdpGroup.toAuthorative());