Complete unit test for models-pdp
[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         return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, version));
71     }
72
73     /**
74      * Get filtered PDP groups.
75      *
76      * @param dao the DAO to use to access the database
77      * @param filter the filter for the PDP groups to get
78      * @return the PDP groups found
79      * @throws PfModelException on errors getting policies
80      */
81     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
82
83         List<JpaPdpGroup> jpaPdpGroupList = dao.getAll(JpaPdpGroup.class);
84
85         return filter.filter(asPdpGroupList(jpaPdpGroupList));
86     }
87
88     /**
89      * Creates PDP groups.
90      *
91      * @param dao the DAO to use to access the database
92      * @param pdpGroups a specification of the PDP groups to create
93      * @return the PDP groups created
94      * @throws PfModelException on errors creating PDP groups
95      */
96     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
97             throws PfModelException {
98
99         for (PdpGroup pdpGroup : pdpGroups) {
100             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
101             jpaPdpGroup.fromAuthorative(pdpGroup);
102
103             PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
104             if (!validationResult.isOk()) {
105                 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
106                 LOGGER.warn(errorMessage);
107                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
108             }
109
110             dao.create(jpaPdpGroup);
111         }
112
113         // Return the created PDP groups
114         List<PdpGroup> returnPdpGroups = new ArrayList<>();
115
116         for (PdpGroup pdpGroup : pdpGroups) {
117             JpaPdpGroup jpaPdpGroup =
118                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
119             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
120         }
121
122         return returnPdpGroups;
123     }
124
125     /**
126      * Updates PDP groups.
127      *
128      * @param dao the DAO to use to access the database
129      * @param pdpGroups a specification of the PDP groups to update
130      * @return the PDP groups updated
131      * @throws PfModelException on errors updating PDP groups
132      */
133     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
134             throws PfModelException {
135
136         for (PdpGroup pdpGroup : pdpGroups) {
137             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
138             jpaPdpGroup.fromAuthorative(pdpGroup);
139
140             PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
141             if (!validationResult.isOk()) {
142                 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
143                 LOGGER.warn(errorMessage);
144                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
145             }
146
147             dao.update(jpaPdpGroup);
148         }
149
150         // Return the created PDP groups
151         List<PdpGroup> returnPdpGroups = new ArrayList<>();
152
153         for (PdpGroup pdpGroup : pdpGroups) {
154             JpaPdpGroup jpaPdpGroup =
155                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
156             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
157         }
158
159         return returnPdpGroups;
160     }
161
162     /**
163      * Update a PDP subgroup.
164      *
165      * @param dao the DAO to use to access the database
166      * @param pdpGroupName the name of the PDP group of the PDP subgroup
167      * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
168      * @param pdpSubGroup the PDP subgroup to be updated
169      * @throws PfModelException on errors updating PDP subgroups
170      */
171     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
172             @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
173
174         final PfReferenceKey subGroupKey = new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup.getPdpType());
175         final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
176         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
177
178         PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
179         if (!validationResult.isOk()) {
180             String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
181             LOGGER.warn(errorMessage);
182             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
183         }
184
185         dao.update(jpaPdpSubgroup);
186     }
187
188     /**
189      * Update a PDP.
190      *
191      * @param dao the DAO to use to access the database
192      * @param pdpGroupName the name of the PDP group of the PDP subgroup
193      * @param pdpGroupVersion the version of the PDP group of the PDP subgroup
194      * @param pdpSubGroup the PDP subgroup to be updated
195      * @param pdp the PDP to be updated
196      * @throws PfModelException on errors updating PDP subgroups
197      */
198     public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
199             @NonNull final String pdpGroupVersion, @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
200
201         final PfReferenceKey pdpKey =
202                 new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp.getInstanceId());
203         final JpaPdp jpaPdp = new JpaPdp(pdpKey);
204         jpaPdp.fromAuthorative(pdp);
205
206         PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
207         if (!validationResult.isOk()) {
208             String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
209             LOGGER.warn(errorMessage);
210             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
211         }
212
213         dao.update(jpaPdp);
214     }
215
216     /**
217      * Delete a PDP group.
218      *
219      * @param dao the DAO to use to access the database
220      * @param name the name of the policy to get, null to get all PDP groups
221      * @param version the version of the policy to get, null to get all versions of a PDP group
222      * @return the PDP group deleted
223      * @throws PfModelException on errors deleting PDP groups
224      */
225     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name,
226             @NonNull final String version) {
227
228         PfConceptKey pdpGroupKey = new PfConceptKey(name, version);
229
230         JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
231
232         if (jpaDeletePdpGroup == null) {
233             String errorMessage =
234                     "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
235             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
236         }
237
238         dao.delete(jpaDeletePdpGroup);
239
240         return jpaDeletePdpGroup.toAuthorative();
241     }
242
243     /**
244      * Get PDP statistics.
245      *
246      * @param dao the DAO to use to access the database
247      * @param name the name of the PDP group to get statistics for, null to get all PDP groups
248      * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group
249      * @return the statistics found
250      * @throws PfModelException on errors getting statistics
251      */
252     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
253             throws PfModelException {
254         return new ArrayList<>();
255     }
256
257     /**
258      * Update PDP statistics for a PDP.
259      *
260      * @param dao the DAO to use to access the database
261      * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
262      * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for
263      * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
264      * @param pdpInstanceId the instance ID of the PDP to update statistics for
265      * @param pdpStatistics the statistics to update
266      * @throws PfModelException on errors updating statistics
267      */
268     public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
269             @NonNull final String pdpGroupVersion, @NonNull final String pdpType, @NonNull final String pdpInstanceId,
270             @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
271         // Not implemented yet
272     }
273
274     /**
275      * Convert JPA PDP group list to an authorative PDP group list.
276      *
277      * @param foundPdpGroups the list to convert
278      * @return the authorative list
279      */
280     private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
281         List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
282
283         for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
284             pdpGroupList.add(jpaPdpGroup.toAuthorative());
285         }
286
287         return pdpGroupList;
288     }
289 }