Use ValidationResult for models v2.0
[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  *  Modifications Copyright (C) 2019-2020 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.persistence.provider;
23
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.common.parameters.BeanValidationResult;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfKey;
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.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
44 /**
45  * This class provides the provision of information on PAP concepts in the database to callers.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class PdpProvider {
50
51     /**
52      * Get PDP groups.
53      *
54      * @param dao the DAO to use to access the database
55      * @param name the name of the PDP group to get, null to get all PDP groups
56      * @return the PDP groups found
57      * @throws PfModelException on errors getting PDP groups
58      */
59     public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
60
61         return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_VERSION));
62     }
63
64     /**
65      * Get filtered PDP groups.
66      *
67      * @param dao the DAO to use to access the database
68      * @param filter the filter for the PDP groups to get
69      * @return the PDP groups found
70      * @throws PfModelException on errors getting policies
71      */
72     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroupFilter filter) {
73
74         return filter.filter(
75                         asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
76     }
77
78     /**
79      * Creates PDP groups.
80      *
81      * @param dao the DAO to use to access the database
82      * @param pdpGroups a specification of the PDP groups to create
83      * @return the PDP groups created
84      * @throws PfModelException on errors creating PDP groups
85      */
86     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
87             throws PfModelException {
88
89         for (PdpGroup pdpGroup : pdpGroups) {
90             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
91             jpaPdpGroup.fromAuthorative(pdpGroup);
92
93             BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
94             if (!validationResult.isValid()) {
95                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
96             }
97
98             dao.create(jpaPdpGroup);
99         }
100
101         // Return the created PDP groups
102         List<PdpGroup> returnPdpGroups = new ArrayList<>();
103
104         for (PdpGroup pdpGroup : pdpGroups) {
105             JpaPdpGroup jpaPdpGroup =
106                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
107             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
108         }
109
110         return returnPdpGroups;
111     }
112
113     /**
114      * Updates PDP groups.
115      *
116      * @param dao the DAO to use to access the database
117      * @param pdpGroups a specification of the PDP groups to update
118      * @return the PDP groups updated
119      * @throws PfModelException on errors updating PDP groups
120      */
121     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
122             throws PfModelException {
123
124         for (PdpGroup pdpGroup : pdpGroups) {
125             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
126             jpaPdpGroup.fromAuthorative(pdpGroup);
127
128             BeanValidationResult validationResult = jpaPdpGroup.validate("PDP group");
129             if (!validationResult.isValid()) {
130                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
131             }
132
133             dao.update(jpaPdpGroup);
134         }
135
136         // Return the created PDP groups
137         List<PdpGroup> returnPdpGroups = new ArrayList<>();
138
139         for (PdpGroup pdpGroup : pdpGroups) {
140             JpaPdpGroup jpaPdpGroup =
141                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
142             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
143         }
144
145         return returnPdpGroups;
146     }
147
148     /**
149      * Update a PDP subgroup.
150      *
151      * @param dao the DAO to use to access the database
152      * @param pdpGroupName the name of the PDP group of the PDP subgroup
153      * @param pdpSubGroup the PDP subgroup to be updated
154      * @throws PfModelException on errors updating PDP subgroups
155      */
156     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
157             @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
158
159         final PfReferenceKey subGroupKey =
160                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
161         final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
162         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
163
164         BeanValidationResult validationResult = jpaPdpSubgroup.validate("PDP sub group");
165         if (!validationResult.isValid()) {
166             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
167         }
168
169         dao.update(jpaPdpSubgroup);
170     }
171
172     /**
173      * Update a PDP.
174      *
175      * @param dao the DAO to use to access the database
176      * @param pdpGroupName the name of the PDP group of the PDP subgroup
177      * @param pdpSubGroup the PDP subgroup to be updated
178      * @param pdp the PDP to be updated
179      * @throws PfModelException on errors updating PDP subgroups
180      */
181     public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
182             @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
183
184         final PfReferenceKey pdpKey =
185                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
186         final JpaPdp jpaPdp = new JpaPdp(pdpKey);
187         jpaPdp.fromAuthorative(pdp);
188
189         BeanValidationResult validationResult = jpaPdp.validate("PDP");
190         if (!validationResult.isValid()) {
191             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
192         }
193
194         dao.update(jpaPdp);
195     }
196
197     /**
198      * Delete a PDP group.
199      *
200      * @param dao the DAO to use to access the database
201      * @param name the name of the policy to get, null to get all PDP groups
202      * @return the PDP group deleted
203      * @throws PfModelException on errors deleting PDP groups
204      */
205     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
206
207         PfConceptKey pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
208
209         JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
210
211         if (jpaDeletePdpGroup == null) {
212             String errorMessage =
213                     "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
214             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
215         }
216
217         dao.delete(jpaDeletePdpGroup);
218
219         return jpaDeletePdpGroup.toAuthorative();
220     }
221
222     /**
223      * Get PDP statistics.
224      *
225      * @param dao the DAO to use to access the database
226      * @param name the name of the PDP group to get statistics for, null to get all PDP groups
227      * @return the statistics found
228      * @throws PfModelException on errors getting statistics
229      */
230     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException {
231         return new ArrayList<>();
232     }
233
234     /**
235      * Update PDP statistics for a PDP.
236      *
237      * @param dao the DAO to use to access the database
238      * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
239      * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
240      * @param pdpInstanceId the instance ID of the PDP to update statistics for
241      * @param pdpStatistics the statistics to update
242      * @throws PfModelException on errors updating statistics
243      */
244     public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
245             @NonNull final String pdpType, @NonNull final String pdpInstanceId,
246             @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
247         // Not implemented yet
248     }
249
250     /**
251      * Convert JPA PDP group list to an authorative PDP group list.
252      *
253      * @param foundPdpGroups the list to convert
254      * @return the authorative list
255      */
256     private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
257         List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
258
259         for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
260             pdpGroupList.add(jpaPdpGroup.toAuthorative());
261         }
262
263         return pdpGroupList;
264     }
265 }