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;
26 import javax.ws.rs.core.Response;
27 import lombok.NonNull;
28 import org.onap.policy.models.base.PfConceptKey;
29 import org.onap.policy.models.base.PfKey;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.base.PfReferenceKey;
33 import org.onap.policy.models.base.PfValidationResult;
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;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * This class provides the provision of information on PAP concepts in the database to callers.
49 * @author Liam Fallon (liam.fallon@est.tech)
51 public class PdpProvider {
52 private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
54 // Recurring string constants
55 private static final String NOT_VALID = "\" is not valid \n";
60 * @param dao the DAO to use to access the database
61 * @param name the name of the PDP group to get, null to get all PDP groups
62 * @return the PDP groups found
63 * @throws PfModelException on errors getting PDP groups
65 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
67 return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
71 * Get filtered PDP groups.
73 * @param dao the DAO to use to access the database
74 * @param filter the filter for the PDP groups to get
75 * @return the PDP groups found
76 * @throws PfModelException on errors getting policies
78 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
81 asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
87 * @param dao the DAO to use to access the database
88 * @param pdpGroups a specification of the PDP groups to create
89 * @return the PDP groups created
90 * @throws PfModelException on errors creating PDP groups
92 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
93 throws PfModelException {
95 for (PdpGroup pdpGroup : pdpGroups) {
96 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
97 jpaPdpGroup.fromAuthorative(pdpGroup);
99 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
100 if (!validationResult.isOk()) {
101 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
102 LOGGER.warn(errorMessage);
103 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
106 dao.create(jpaPdpGroup);
109 // Return the created PDP groups
110 List<PdpGroup> returnPdpGroups = new ArrayList<>();
112 for (PdpGroup pdpGroup : pdpGroups) {
113 JpaPdpGroup jpaPdpGroup =
114 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
115 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
118 return returnPdpGroups;
122 * Updates PDP groups.
124 * @param dao the DAO to use to access the database
125 * @param pdpGroups a specification of the PDP groups to update
126 * @return the PDP groups updated
127 * @throws PfModelException on errors updating PDP groups
129 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
130 throws PfModelException {
132 for (PdpGroup pdpGroup : pdpGroups) {
133 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
134 jpaPdpGroup.fromAuthorative(pdpGroup);
136 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
137 if (!validationResult.isOk()) {
138 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
139 LOGGER.warn(errorMessage);
140 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
143 dao.update(jpaPdpGroup);
146 // Return the created PDP groups
147 List<PdpGroup> returnPdpGroups = new ArrayList<>();
149 for (PdpGroup pdpGroup : pdpGroups) {
150 JpaPdpGroup jpaPdpGroup =
151 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
152 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
155 return returnPdpGroups;
159 * Update a PDP subgroup.
161 * @param dao the DAO to use to access the database
162 * @param pdpGroupName the name of the PDP group of the PDP subgroup
163 * @param pdpSubGroup the PDP subgroup to be updated
164 * @throws PfModelException on errors updating PDP subgroups
166 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
167 @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
169 final PfReferenceKey subGroupKey =
170 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
171 final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
172 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
174 PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
175 if (!validationResult.isOk()) {
176 String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
177 LOGGER.warn(errorMessage);
178 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
181 dao.update(jpaPdpSubgroup);
187 * @param dao the DAO to use to access the database
188 * @param pdpGroupName the name of the PDP group of the PDP subgroup
189 * @param pdpSubGroup the PDP subgroup to be updated
190 * @param pdp the PDP to be updated
191 * @throws PfModelException on errors updating PDP subgroups
193 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
194 @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
196 final PfReferenceKey pdpKey =
197 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
198 final JpaPdp jpaPdp = new JpaPdp(pdpKey);
199 jpaPdp.fromAuthorative(pdp);
201 PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
202 if (!validationResult.isOk()) {
203 String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
204 LOGGER.warn(errorMessage);
205 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
212 * Delete a PDP group.
214 * @param dao the DAO to use to access the database
215 * @param name the name of the policy to get, null to get all PDP groups
216 * @return the PDP group deleted
217 * @throws PfModelException on errors deleting PDP groups
219 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
221 PfConceptKey pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
223 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
225 if (jpaDeletePdpGroup == null) {
226 String errorMessage =
227 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
228 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
231 dao.delete(jpaDeletePdpGroup);
233 return jpaDeletePdpGroup.toAuthorative();
237 * Get PDP statistics.
239 * @param dao the DAO to use to access the database
240 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
241 * @return the statistics found
242 * @throws PfModelException on errors getting statistics
244 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException {
245 return new ArrayList<>();
249 * Update PDP statistics for a PDP.
251 * @param dao the DAO to use to access the database
252 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
253 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
254 * @param pdpInstanceId the instance ID of the PDP to update statistics for
255 * @param pdpStatistics the statistics to update
256 * @throws PfModelException on errors updating statistics
258 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
259 @NonNull final String pdpType, @NonNull final String pdpInstanceId,
260 @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
261 // Not implemented yet
265 * Convert JPA PDP group list to an authorative PDP group list.
267 * @param foundPdpGroups the list to convert
268 * @return the authorative list
270 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
271 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
273 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
274 pdpGroupList.add(jpaPdpGroup.toAuthorative());