Merge "Add impl of more PDP persistence"
[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.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.ws.rs.core.Response;
29
30 import lombok.NonNull;
31
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;
49
50 /**
51  * This class provides the provision of information on PAP concepts in the database to callers.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55 public class PdpProvider {
56     private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
57
58     // Recurring string constants
59     private static final String NOT_VALID = "\" is not valid \n";
60
61     /**
62      * Get PDP groups.
63      *
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
69      */
70     public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name, final String version)
71             throws PfModelException {
72
73         List<JpaPdpGroup> foundPdpGroups = dao.getFiltered(JpaPdpGroup.class, new PfConceptKey(name, version));
74
75         if (foundPdpGroups != null) {
76             return asPdpGroupList(foundPdpGroups);
77         } else {
78             String errorMessage = "no PDP groups found for filter " + name + ":" + version;
79             LOGGER.warn(errorMessage);
80             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
81         }
82     }
83
84     /**
85      * Get latest PDP Groups, returns PDP groups in all states.
86      *
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
91      */
92     public List<PdpGroup> getLatestPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
93         List<JpaPdpGroup> returnList = new ArrayList<>();
94
95         if (name == null) {
96             returnList.add(dao.getLatestVersion(JpaPdpGroup.class, name));
97         }
98         else {
99             returnList.addAll(dao.getLatestVersions(JpaPdpGroup.class));
100         }
101
102         return asPdpGroupList(returnList);
103     }
104
105     /**
106      * Get a filtered list of PDP groups, returns only active PDP groups.
107      *
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
112      */
113     public List<PdpGroup> getFilteredPdpGroups(@NonNull final PfDao dao, final String pdpType,
114             @NonNull final List<Pair<String, String>> supportedPolicyTypes) {
115         return new ArrayList<>();
116     }
117
118     /**
119      * Creates PDP groups.
120      *
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
125      */
126     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
127             throws PfModelException {
128
129         for (PdpGroup pdpGroup : pdpGroups) {
130             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
131             jpaPdpGroup.fromAuthorative(pdpGroup);
132
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);
138             }
139
140             dao.create(jpaPdpGroup);
141         }
142
143         // Return the created PDP groups
144         List<PdpGroup> returnPdpGroups = new ArrayList<>();
145
146         for (PdpGroup pdpGroup : pdpGroups) {
147             JpaPdpGroup jpaPdpGroup =
148                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
149             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
150         }
151
152         return returnPdpGroups;
153     }
154
155     /**
156      * Updates PDP groups.
157      *
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
162      */
163     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
164             throws PfModelException {
165
166         for (PdpGroup pdpGroup : pdpGroups) {
167             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();;
168             jpaPdpGroup.fromAuthorative(pdpGroup);
169
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);
175             }
176
177             dao.update(jpaPdpGroup);
178         }
179
180         // Return the created PDP groups
181         List<PdpGroup> returnPdpGroups = new ArrayList<>();
182
183         for (PdpGroup pdpGroup : pdpGroups) {
184             JpaPdpGroup jpaPdpGroup =
185                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
186             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
187         }
188
189         return returnPdpGroups;
190     }
191
192     /**
193      * Update a PDP subgroup.
194      *
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
200      */
201     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
202             @NonNull final String pdpGroupVersion, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
203
204         final PfReferenceKey subGroupKey = new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup.getPdpType());
205         final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
206         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
207
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);
213         }
214
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);
218         }
219     }
220
221     /**
222      * Update a PDP.
223      *
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
230      */
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 {
234
235         final PfReferenceKey pdpKey =
236                 new PfReferenceKey(pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp.getInstanceId());
237         final JpaPdp jpaPdp = new JpaPdp(pdpKey);
238         jpaPdp.fromAuthorative(pdp);
239
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);
245         }
246
247         if (dao.update(jpaPdp) == null) {
248             String errorMessage = "update of PDP \"" + jpaPdp.getId() + "\" failed";
249             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
250         }
251     }
252
253     /**
254      * Delete a PDP group.
255      *
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
261      */
262     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name, @NonNull final String version)
263             throws PfModelException {
264
265         PfConceptKey pdpGroupKey = new PfConceptKey(name, version);
266
267         JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
268
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);
273         }
274
275         dao.delete(jpaDeletePdpGroup);
276
277         return jpaDeletePdpGroup.toAuthorative();
278     }
279
280     /**
281      * Get PDP statistics.
282      *
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
288      */
289     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final String version)
290             throws PfModelException {
291         return new ArrayList<>();
292     }
293
294     /**
295      * Update PDP statistics for a PDP.
296      *
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
303      */
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
308     }
309
310     /**
311      * Get deployed policies.
312      *
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
317      */
318     public Map<Pair<String, String>, List<ToscaPolicy>> getDeployedPolicyList(@NonNull final PfDao dao,
319             final String name) throws PfModelException {
320         return new LinkedHashMap<>();
321     }
322
323     /**
324      * Convert JPA PDP group list to an authorative PDP group list.
325      *
326      * @param foundPdpGroups the list to convert
327      * @return the authorative list
328      */
329     private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
330         List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
331
332         for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
333             pdpGroupList.add(jpaPdpGroup.toAuthorative());
334         }
335
336         return pdpGroupList;
337     }
338 }