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.LinkedHashMap;
25 import java.util.List;
28 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.base.PfReferenceKey;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.pdp.concepts.Pdp;
40 import org.onap.policy.models.pdp.concepts.PdpGroup;
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.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * This class provides the provision of information on PAP concepts in the database to callers.
53 * @author Liam Fallon (liam.fallon@est.tech)
55 public class PdpProvider {
56 private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
58 // Recurring string constants
59 private static final String NOT_VALID = "\" is not valid \n";
64 * @param dao the DAO to use to access the database
65 * @param name the name of the PDP group to get, null to get all PDP groups
66 * @param version the version of the policy to get, null to get all versions of a PDP group
67 * @return the PDP groups found
68 * @throws PfModelException on errors getting PDP groups
70 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name, final String version)
71 throws PfModelException {
73 List<JpaPdpGroup> foundPdpGroups = dao.getFiltered(JpaPdpGroup.class, new PfConceptKey(name, version));
75 if (foundPdpGroups != null) {
76 return asPdpGroupList(foundPdpGroups);
78 String errorMessage = "no PDP groups found for filter " + name + ":" + version;
79 LOGGER.warn(errorMessage);
80 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
85 * Get latest PDP Groups, returns PDP groups in all states.
87 * @param dao the DAO to use to access the database
88 * @param name the name of the PDP group to get, null to get all PDP groups
89 * @return the PDP groups found
90 * @throws PfModelException on errors getting policies
92 public List<PdpGroup> getLatestPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
93 List<JpaPdpGroup> returnList = new ArrayList<>();
96 returnList.add(dao.getLatestVersion(JpaPdpGroup.class, name));
99 returnList.addAll(dao.getLatestVersions(JpaPdpGroup.class));
102 return asPdpGroupList(returnList);
106 * Get a filtered list of PDP groups, returns only active PDP groups.
108 * @param dao the DAO to use to access the database
109 * @param pdpType The PDP type filter for the returned PDP groups, null to get policy types across PDP subgroups
110 * @param supportedPolicyTypes a list of policy type name/version pairs that the PDP groups must support.
111 * @return the PDP groups found
113 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, final String pdpType,
114 @NonNull final List<Pair<String, String>> supportedPolicyTypes) {
115 return new ArrayList<>();
119 * Creates PDP groups.
121 * @param dao the DAO to use to access the database
122 * @param pdpGroups a specification of the PDP groups to create
123 * @return the PDP groups created
124 * @throws PfModelException on errors creating PDP groups
126 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
127 throws PfModelException {
129 for (PdpGroup pdpGroup : pdpGroups) {
130 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
131 jpaPdpGroup.fromAuthorative(pdpGroup);
133 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
134 if (!validationResult.isOk()) {
135 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
136 LOGGER.warn(errorMessage);
137 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
140 dao.create(jpaPdpGroup);
143 // Return the created PDP groups
144 List<PdpGroup> returnPdpGroups = new ArrayList<>();
146 for (PdpGroup pdpGroup : pdpGroups) {
147 JpaPdpGroup jpaPdpGroup =
148 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
149 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
152 return returnPdpGroups;
156 * Updates PDP groups.
158 * @param dao the DAO to use to access the database
159 * @param pdpGroups a specification of the PDP groups to update
160 * @return the PDP groups updated
161 * @throws PfModelException on errors updating PDP groups
163 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
164 throws PfModelException {
166 for (PdpGroup pdpGroup : pdpGroups) {
167 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
168 jpaPdpGroup.fromAuthorative(pdpGroup);
170 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
171 if (!validationResult.isOk()) {
172 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
173 LOGGER.warn(errorMessage);
174 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
177 dao.update(jpaPdpGroup);
180 // Return the created PDP groups
181 List<PdpGroup> returnPdpGroups = new ArrayList<>();
183 for (PdpGroup pdpGroup : pdpGroups) {
184 JpaPdpGroup jpaPdpGroup =
185 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
186 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
189 return returnPdpGroups;
193 * Update a PDP subgroup.
195 * @param dao the DAO to use to access the database
196 * @param pdpGroupName the name of the PDP group of the PDP subgroup
197 * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
198 * @param pdpSubGroup the PDP subgroup to be updated
199 * @throws PfModelException on errors updating PDP subgroups
201 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
202 @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
204 final PfReferenceKey subGroupKey = new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup.getPdpType());
205 final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
206 jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
208 PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
209 if (!validationResult.isOk()) {
210 String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
211 LOGGER.warn(errorMessage);
212 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
215 if (dao.update(jpaPdpSubgroup) == null) {
216 String errorMessage = "update of PDP subgroup \"" + jpaPdpSubgroup.getId() + "\" failed";
217 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
224 * @param dao the DAO to use to access the database
225 * @param pdpGroupName the name of the PDP group of the PDP subgroup
226 * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
227 * @param pdpSubGroup the PDP subgroup to be updated
228 * @param pdp the PDP to be updated
229 * @throws PfModelException on errors updating PDP subgroups
231 public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
232 @NonNull final String pdpGroupVersion, @NonNull final String pdpSubGroup, @NonNull final Pdp pdp)
233 throws PfModelException {
235 final PfReferenceKey pdpKey =
236 new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp.getInstanceId());
237 final JpaPdp jpaPdp = new JpaPdp(pdpKey);
238 jpaPdp.fromAuthorative(pdp);
240 PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
241 if (!validationResult.isOk()) {
242 String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
243 LOGGER.warn(errorMessage);
244 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
247 if (dao.update(jpaPdp) == null) {
248 String errorMessage = "update of PDP \"" + jpaPdp.getId() + "\" failed";
249 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
254 * Delete a PDP group.
256 * @param dao the DAO to use to access the database
257 * @param name the name of the policy to get, null to get all PDP groups
258 * @param version the version of the policy to get, null to get all versions of a PDP group
259 * @return the PDP group deleted
260 * @throws PfModelException on errors deleting PDP groups
262 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name, @NonNull final String version)
263 throws PfModelException {
265 PfConceptKey pdpGroupKey = new PfConceptKey(name, version);
267 JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
269 if (jpaDeletePdpGroup == null) {
270 String errorMessage =
271 "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
272 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
275 dao.delete(jpaDeletePdpGroup);
277 return jpaDeletePdpGroup.toAuthorative();
281 * Get PDP statistics.
283 * @param dao the DAO to use to access the database
284 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
285 * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group
286 * @return the statistics found
287 * @throws PfModelException on errors getting statistics
289 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
290 throws PfModelException {
291 return new ArrayList<>();
295 * Update PDP statistics for a PDP.
297 * @param dao the DAO to use to access the database
298 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
299 * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for
300 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
301 * @param pdpInstanceId the instance ID of the PDP to update statistics for
302 * @throws PfModelException on errors updating statistics
304 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
305 @NonNull final String pdpGroupVersion, @NonNull final String pdpType, @NonNull final String pdpInstanceId,
306 @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
307 // Not implemented yet
311 * Get deployed policies.
313 * @param dao the DAO to use to access the database
314 * @param name the name of the policy to get deployed policies for, null to get all deployed policies
315 * @return the policies deployed as a map of policy lists keyed by PDP group name and version
316 * @throws PfModelException on errors getting policies
318 public Map<Pair<String, String>, List<ToscaPolicy>> getDeployedPolicyList(@NonNull final PfDao dao,
319 final String name) throws PfModelException {
320 return new LinkedHashMap<>();
324 * Convert JPA PDP group list to an authorative PDP group list.
326 * @param foundPdpGroups the list to convert
327 * @return the authorative list
329 private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
330 List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
332 for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
333 pdpGroupList.add(jpaPdpGroup.toAuthorative());