Add filter obejcts for concepts
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / provider / PdpProvider.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.persistence.provider;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.ws.rs.core.Response;
27
28 import lombok.NonNull;
29
30 import org.onap.policy.models.base.PfConceptKey;
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.base.PfValidationResult;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.pdp.concepts.Pdp;
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
38 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
41 import org.onap.policy.models.pdp.persistence.concepts.JpaPdp;
42 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
43 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * This class provides the provision of information on PAP concepts in the database to callers.
49  *
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 public class PdpProvider {
53     private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
54
55     // Recurring string constants
56     private static final String NOT_VALID = "\" is not valid \n";
57
58     /**
59      * Get PDP groups.
60      *
61      * @param dao the DAO to use to access the database
62      * @param name the name of the PDP group to get, null to get all PDP groups
63      * @param version the version of the policy to get, null to get all versions of a PDP group
64      * @return the PDP groups found
65      * @throws PfModelException on errors getting PDP groups
66      */
67     public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name, final String version)
68             throws PfModelException {
69
70         List<JpaPdpGroup> foundPdpGroups = dao.getFiltered(JpaPdpGroup.class, name, version);
71
72         if (foundPdpGroups != null) {
73             return asPdpGroupList(foundPdpGroups);
74         } else {
75             String errorMessage = "no PDP groups found for filter " + name + ":" + version;
76             LOGGER.warn(errorMessage);
77             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
78         }
79     }
80
81     /**
82      * Get latest PDP Groups, returns PDP groups in all states.
83      *
84      * @param dao the DAO to use to access the database
85      * @param name the name of the PDP group to get, null to get all PDP groups
86      * @return the PDP groups found
87      * @throws PfModelException on errors getting policies
88      */
89     public List<PdpGroup> getLatestPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
90         List<JpaPdpGroup> jpaPdpGroupList = new ArrayList<>();
91
92         if (name == null) {
93             jpaPdpGroupList.addAll(dao.getAll(JpaPdpGroup.class));
94         } else {
95             jpaPdpGroupList.addAll(dao.getAllVersions(JpaPdpGroup.class, name));
96         }
97
98         return asPdpGroupList(jpaPdpGroupList);
99     }
100
101     /**
102      * Get filtered PDP groups.
103      *
104      * @param dao the DAO to use to access the database
105      * @param filter the filter for the PDP groups to get
106      * @return the PDP groups found
107      * @throws PfModelException on errors getting policies
108      */
109     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter)
110             throws PfModelException {
111
112         List<JpaPdpGroup> jpaPdpGroupList = dao.getAll(JpaPdpGroup.class);
113
114         return asPdpGroupList(jpaPdpGroupList);
115     }
116
117     /**
118      * Creates PDP groups.
119      *
120      * @param dao the DAO to use to access the database
121      * @param pdpGroups a specification of the PDP groups to create
122      * @return the PDP groups created
123      * @throws PfModelException on errors creating PDP groups
124      */
125     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
126             throws PfModelException {
127
128         for (PdpGroup pdpGroup : pdpGroups) {
129             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
130             jpaPdpGroup.fromAuthorative(pdpGroup);
131
132             PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
133             if (!validationResult.isOk()) {
134                 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
135                 LOGGER.warn(errorMessage);
136                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
137             }
138
139             dao.create(jpaPdpGroup);
140         }
141
142         // Return the created PDP groups
143         List<PdpGroup> returnPdpGroups = new ArrayList<>();
144
145         for (PdpGroup pdpGroup : pdpGroups) {
146             JpaPdpGroup jpaPdpGroup =
147                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
148             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
149         }
150
151         return returnPdpGroups;
152     }
153
154     /**
155      * Updates PDP groups.
156      *
157      * @param dao the DAO to use to access the database
158      * @param pdpGroups a specification of the PDP groups to update
159      * @return the PDP groups updated
160      * @throws PfModelException on errors updating PDP groups
161      */
162     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
163             throws PfModelException {
164
165         for (PdpGroup pdpGroup : pdpGroups) {
166             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
167             jpaPdpGroup.fromAuthorative(pdpGroup);
168
169             PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
170             if (!validationResult.isOk()) {
171                 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
172                 LOGGER.warn(errorMessage);
173                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
174             }
175
176             dao.update(jpaPdpGroup);
177         }
178
179         // Return the created PDP groups
180         List<PdpGroup> returnPdpGroups = new ArrayList<>();
181
182         for (PdpGroup pdpGroup : pdpGroups) {
183             JpaPdpGroup jpaPdpGroup =
184                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
185             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
186         }
187
188         return returnPdpGroups;
189     }
190
191     /**
192      * Update a PDP subgroup.
193      *
194      * @param dao the DAO to use to access the database
195      * @param pdpGroupName the name of the PDP group of the PDP subgroup
196      * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
197      * @param pdpSubGroup the PDP subgroup to be updated
198      * @throws PfModelException on errors updating PDP subgroups
199      */
200     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
201             @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
202
203         final PfReferenceKey subGroupKey = new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup.getPdpType());
204         final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
205         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
206
207         PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
208         if (!validationResult.isOk()) {
209             String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
210             LOGGER.warn(errorMessage);
211             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
212         }
213
214         if (dao.update(jpaPdpSubgroup) == null) {
215             String errorMessage = "update of PDP subgroup \"" + jpaPdpSubgroup.getId() + "\" failed";
216             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
217         }
218     }
219
220     /**
221      * Update a PDP.
222      *
223      * @param dao the DAO to use to access the database
224      * @param pdpGroupName the name of the PDP group of the PDP subgroup
225      * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
226      * @param pdpSubGroup the PDP subgroup to be updated
227      * @param pdp the PDP to be updated
228      * @throws PfModelException on errors updating PDP subgroups
229      */
230     public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
231             @NonNull final String pdpGroupVersion, @NonNull final String pdpSubGroup, @NonNull final Pdp pdp)
232             throws PfModelException {
233
234         final PfReferenceKey pdpKey =
235                 new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp.getInstanceId());
236         final JpaPdp jpaPdp = new JpaPdp(pdpKey);
237         jpaPdp.fromAuthorative(pdp);
238
239         PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
240         if (!validationResult.isOk()) {
241             String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
242             LOGGER.warn(errorMessage);
243             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
244         }
245
246         if (dao.update(jpaPdp) == null) {
247             String errorMessage = "update of PDP \"" + jpaPdp.getId() + "\" failed";
248             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
249         }
250     }
251
252     /**
253      * Delete a PDP group.
254      *
255      * @param dao the DAO to use to access the database
256      * @param name the name of the policy to get, null to get all PDP groups
257      * @param version the version of the policy to get, null to get all versions of a PDP group
258      * @return the PDP group deleted
259      * @throws PfModelException on errors deleting PDP groups
260      */
261     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name, @NonNull final String version)
262             throws PfModelException {
263
264         PfConceptKey pdpGroupKey = new PfConceptKey(name, version);
265
266         JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
267
268         if (jpaDeletePdpGroup == null) {
269             String errorMessage =
270                     "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
271             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
272         }
273
274         dao.delete(jpaDeletePdpGroup);
275
276         return jpaDeletePdpGroup.toAuthorative();
277     }
278
279     /**
280      * Get PDP statistics.
281      *
282      * @param dao the DAO to use to access the database
283      * @param name the name of the PDP group to get statistics for, null to get all PDP groups
284      * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group
285      * @return the statistics found
286      * @throws PfModelException on errors getting statistics
287      */
288     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
289             throws PfModelException {
290         return new ArrayList<>();
291     }
292
293     /**
294      * Update PDP statistics for a PDP.
295      *
296      * @param dao the DAO to use to access the database
297      * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
298      * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for
299      * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
300      * @param pdpInstanceId the instance ID of the PDP to update statistics for
301      * @throws PfModelException on errors updating statistics
302      */
303     public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
304             @NonNull final String pdpGroupVersion, @NonNull final String pdpType, @NonNull final String pdpInstanceId,
305             @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
306         // Not implemented yet
307     }
308
309     /**
310      * Convert JPA PDP group list to an authorative PDP group list.
311      *
312      * @param foundPdpGroups the list to convert
313      * @return the authorative list
314      */
315     private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
316         List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
317
318         for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
319             pdpGroupList.add(jpaPdpGroup.toAuthorative());
320         }
321
322         return pdpGroupList;
323     }
324 }