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.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
29 import javax.ws.rs.core.Response;
31 import lombok.NonNull;
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.concepts.PdpStatistics;
41 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
42 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
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);
58 * @param dao the DAO to use to access the database
59 * @param name the name of the policy to get, null to get all PDP groups
60 * @param version the version of the policy to get, null to get all versions of a PDP group
61 * @return the PDP groups found
62 * @throws PfModelException on errors getting PDP groups
64 public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name, final String version)
65 throws PfModelException {
67 PfConceptKey jpaPdpGroupKey = new PfConceptKey(name, version);
68 JpaPdpGroup jpaPdpGroup = dao.get(JpaPdpGroup.class, jpaPdpGroupKey);
70 if (jpaPdpGroup != null) {
71 return Collections.singletonList(jpaPdpGroup.toAuthorative());
73 String errorMessage = "PDP group not found: " + jpaPdpGroupKey.getId();
74 LOGGER.warn(errorMessage);
75 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
80 * Get latest PDP Groups.
82 * @param dao the DAO to use to access the database
83 * @param name the name of the PDP group to get, null to get all PDP groups
84 * @return the PDP groups found
85 * @throws PfModelException on errors getting policies
87 public List<PdpGroup> getLatestPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
88 return new ArrayList<>();
92 * Get a filtered list of PDP groups.
94 * @param dao the DAO to use to access the database
95 * @param pdpType The PDP type filter for the returned PDP groups, null to get policy types across PDP subgroups
96 * @param supportedPolicyTypes a list of policy type name/version pairs that the PDP groups must support.
97 * @return the PDP groups found
99 public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, final String pdpType,
100 @NonNull final List<Pair<String, String>> supportedPolicyTypes) {
101 return new ArrayList<>();
105 * Creates PDP groups.
107 * @param dao the DAO to use to access the database
108 * @param pdpGroups a specification of the PDP groups to create
109 * @return the PDP groups created
110 * @throws PfModelException on errors creating PDP groups
112 public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
113 throws PfModelException {
115 for (PdpGroup pdpGroup : pdpGroups) {
116 JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
117 jpaPdpGroup.fromAuthorative(pdpGroup);
119 PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
120 if (!validationResult.isOk()) {
121 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + "\" is not valid \n" + validationResult;
122 LOGGER.warn(errorMessage);
123 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
126 dao.create(jpaPdpGroup);
129 // Return the created PDP groups
130 List<PdpGroup> returnPdpGroups = new ArrayList<>();
132 for (PdpGroup pdpGroup : pdpGroups) {
133 JpaPdpGroup jpaPdpGroup =
134 dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
135 returnPdpGroups.add(jpaPdpGroup.toAuthorative());
138 return returnPdpGroups;
142 * Updates PDP groups.
144 * @param dao the DAO to use to access the database
145 * @param pdpGroups a specification of the PDP groups to update
146 * @return the PDP groups updated
147 * @throws PfModelException on errors updating PDP groups
149 public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
150 throws PfModelException {
151 return new ArrayList<>();
156 * Update a PDP subgroup.
158 * @param dao the DAO to use to access the database
159 * @param pdpGroupName the name of the PDP group of the PDP subgroup
160 * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
161 * @param pdpSubGroup the PDP subgroup to be updated
162 * @throws PfModelException on errors updating PDP subgroups
164 public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
165 @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
166 // Not implemented yet
170 * Delete a PDP group.
172 * @param dao the DAO to use to access the database
173 * @param name the name of the policy to get, null to get all PDP groups
174 * @param version the version of the policy to get, null to get all versions of a PDP group
175 * @return the PDP group deleted
176 * @throws PfModelException on errors deleting PDP groups
178 public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name, @NonNull final String version)
179 throws PfModelException {
180 return new PdpGroup();
185 * Get PDP statistics.
187 * @param dao the DAO to use to access the database
188 * @param name the name of the PDP group to get statistics for, null to get all PDP groups
189 * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group
190 * @return the statistics found
191 * @throws PfModelException on errors getting statistics
193 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
194 throws PfModelException {
195 return new ArrayList<>();
199 * Update PDP statistics for a PDP.
201 * @param dao the DAO to use to access the database
202 * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
203 * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for
204 * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
205 * @param pdpInstanceId the instance ID of the PDP to update statistics for
206 * @throws PfModelException on errors updating statistics
208 public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
209 @NonNull final String pdpGroupVersion, @NonNull final String pdpType, @NonNull final String pdpInstanceId,
210 @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
211 // Not implemented yet
215 * Get deployed policies.
217 * @param dao the DAO to use to access the database
218 * @param name the name of the policy to get deployed policies for, null to get all deployed policies
219 * @return the policies deployed as a map of policy lists keyed by PDP group name and version
220 * @throws PfModelException on errors getting policies
222 public Map<Pair<String, String>, List<ToscaPolicy>> getDeployedPolicyList(@NonNull final PfDao dao,
223 final String name) throws PfModelException {
224 return new LinkedHashMap<>();