9dafc411c3d1612004b0d5955eefd07ff728719c
[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 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
27 import javax.ws.rs.core.Response;
28
29 import lombok.NonNull;
30
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfKey;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.base.PfReferenceKey;
36 import org.onap.policy.models.base.PfValidationResult;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.pdp.concepts.Pdp;
39 import org.onap.policy.models.pdp.concepts.PdpGroup;
40 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
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.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * This class provides the provision of information on PAP concepts in the database to callers.
51  *
52  * @author Liam Fallon (liam.fallon@est.tech)
53  */
54 public class PdpProvider {
55     private static final Logger LOGGER = LoggerFactory.getLogger(PdpProvider.class);
56
57     // Recurring string constants
58     private static final String NOT_VALID = "\" is not valid \n";
59
60     /**
61      * Get PDP groups.
62      *
63      * @param dao the DAO to use to access the database
64      * @param name the name of the PDP group to get, null to get all PDP groups
65      * @return the PDP groups found
66      * @throws PfModelException on errors getting PDP groups
67      */
68     public List<PdpGroup> getPdpGroups(@NonNull final PfDao dao, final String name) throws PfModelException {
69
70         return asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, name, PfKey.NULL_KEY_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         return filter.filter(
84                         asPdpGroupList(dao.getFiltered(JpaPdpGroup.class, filter.getName(), PfKey.NULL_KEY_VERSION)));
85     }
86
87     /**
88      * Creates PDP groups.
89      *
90      * @param dao the DAO to use to access the database
91      * @param pdpGroups a specification of the PDP groups to create
92      * @return the PDP groups created
93      * @throws PfModelException on errors creating PDP groups
94      */
95     public List<PdpGroup> createPdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
96             throws PfModelException {
97
98         for (PdpGroup pdpGroup : pdpGroups) {
99             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
100             jpaPdpGroup.fromAuthorative(pdpGroup);
101
102             PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
103             if (!validationResult.isOk()) {
104                 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
105                 LOGGER.warn(errorMessage);
106                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
107             }
108
109             dao.create(jpaPdpGroup);
110         }
111
112         // Return the created PDP groups
113         List<PdpGroup> returnPdpGroups = new ArrayList<>();
114
115         for (PdpGroup pdpGroup : pdpGroups) {
116             JpaPdpGroup jpaPdpGroup =
117                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
118             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
119         }
120
121         return returnPdpGroups;
122     }
123
124     /**
125      * Updates PDP groups.
126      *
127      * @param dao the DAO to use to access the database
128      * @param pdpGroups a specification of the PDP groups to update
129      * @return the PDP groups updated
130      * @throws PfModelException on errors updating PDP groups
131      */
132     public List<PdpGroup> updatePdpGroups(@NonNull final PfDao dao, @NonNull final List<PdpGroup> pdpGroups)
133             throws PfModelException {
134
135         for (PdpGroup pdpGroup : pdpGroups) {
136             JpaPdpGroup jpaPdpGroup = new JpaPdpGroup();
137             jpaPdpGroup.fromAuthorative(pdpGroup);
138
139             PfValidationResult validationResult = jpaPdpGroup.validate(new PfValidationResult());
140             if (!validationResult.isOk()) {
141                 String errorMessage = "pdp group \"" + jpaPdpGroup.getId() + NOT_VALID + validationResult;
142                 LOGGER.warn(errorMessage);
143                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
144             }
145
146             dao.update(jpaPdpGroup);
147         }
148
149         // Return the created PDP groups
150         List<PdpGroup> returnPdpGroups = new ArrayList<>();
151
152         for (PdpGroup pdpGroup : pdpGroups) {
153             JpaPdpGroup jpaPdpGroup =
154                     dao.get(JpaPdpGroup.class, new PfConceptKey(pdpGroup.getName(), PfKey.NULL_KEY_VERSION));
155             returnPdpGroups.add(jpaPdpGroup.toAuthorative());
156         }
157
158         return returnPdpGroups;
159     }
160
161     /**
162      * Update a PDP subgroup.
163      *
164      * @param dao the DAO to use to access the database
165      * @param pdpGroupName the name of the PDP group of the PDP subgroup
166      * @param pdpSubGroup the PDP subgroup to be updated
167      * @throws PfModelException on errors updating PDP subgroups
168      */
169     public void updatePdpSubGroup(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
170             @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
171
172         final PfReferenceKey subGroupKey =
173                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup.getPdpType());
174         final JpaPdpSubGroup jpaPdpSubgroup = new JpaPdpSubGroup(subGroupKey);
175         jpaPdpSubgroup.fromAuthorative(pdpSubGroup);
176
177         PfValidationResult validationResult = jpaPdpSubgroup.validate(new PfValidationResult());
178         if (!validationResult.isOk()) {
179             String errorMessage = "PDP subgroup \"" + jpaPdpSubgroup.getId() + NOT_VALID + validationResult;
180             LOGGER.warn(errorMessage);
181             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
182         }
183
184         dao.update(jpaPdpSubgroup);
185     }
186
187     /**
188      * Update a PDP.
189      *
190      * @param dao the DAO to use to access the database
191      * @param pdpGroupName the name of the PDP group of the PDP subgroup
192      * @param pdpSubGroup the PDP subgroup to be updated
193      * @param pdp the PDP to be updated
194      * @throws PfModelException on errors updating PDP subgroups
195      */
196     public void updatePdp(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
197             @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) {
198
199         final PfReferenceKey pdpKey =
200                 new PfReferenceKey(pdpGroupName, PfKey.NULL_KEY_VERSION, pdpSubGroup, pdp.getInstanceId());
201         final JpaPdp jpaPdp = new JpaPdp(pdpKey);
202         jpaPdp.fromAuthorative(pdp);
203
204         PfValidationResult validationResult = jpaPdp.validate(new PfValidationResult());
205         if (!validationResult.isOk()) {
206             String errorMessage = "PDP \"" + jpaPdp.getId() + NOT_VALID + validationResult;
207             LOGGER.warn(errorMessage);
208             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
209         }
210
211         dao.update(jpaPdp);
212     }
213
214     /**
215      * Delete a PDP group.
216      *
217      * @param dao the DAO to use to access the database
218      * @param name the name of the policy to get, null to get all PDP groups
219      * @return the PDP group deleted
220      * @throws PfModelException on errors deleting PDP groups
221      */
222     public PdpGroup deletePdpGroup(@NonNull final PfDao dao, @NonNull final String name) {
223
224         PfConceptKey pdpGroupKey = new PfConceptKey(name, PfKey.NULL_KEY_VERSION);
225
226         JpaPdpGroup jpaDeletePdpGroup = dao.get(JpaPdpGroup.class, pdpGroupKey);
227
228         if (jpaDeletePdpGroup == null) {
229             String errorMessage =
230                     "delete of PDP group \"" + pdpGroupKey.getId() + "\" failed, PDP group does not exist";
231             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
232         }
233
234         dao.delete(jpaDeletePdpGroup);
235
236         return jpaDeletePdpGroup.toAuthorative();
237     }
238
239     /**
240      * Get PDP statistics.
241      *
242      * @param dao the DAO to use to access the database
243      * @param name the name of the PDP group to get statistics for, null to get all PDP groups
244      * @return the statistics found
245      * @throws PfModelException on errors getting statistics
246      */
247     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException {
248         return new ArrayList<>();
249     }
250
251     /**
252      * Update PDP statistics for a PDP.
253      *
254      * @param dao the DAO to use to access the database
255      * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for
256      * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for
257      * @param pdpInstanceId the instance ID of the PDP to update statistics for
258      * @param pdpStatistics the statistics to update
259      * @throws PfModelException on errors updating statistics
260      */
261     public void updatePdpStatistics(@NonNull final PfDao dao, @NonNull final String pdpGroupName,
262             @NonNull final String pdpType, @NonNull final String pdpInstanceId,
263             @NonNull final PdpStatistics pdpStatistics) throws PfModelException {
264         // Not implemented yet
265     }
266
267     /**
268      * Convert JPA PDP group list to an authorative PDP group list.
269      *
270      * @param foundPdpGroups the list to convert
271      * @return the authorative list
272      */
273     private List<PdpGroup> asPdpGroupList(List<JpaPdpGroup> jpaPdpGroupList) {
274         List<PdpGroup> pdpGroupList = new ArrayList<>(jpaPdpGroupList.size());
275
276         for (JpaPdpGroup jpaPdpGroup : jpaPdpGroupList) {
277             pdpGroupList.add(jpaPdpGroup.toAuthorative());
278         }
279
280         return pdpGroupList;
281     }
282 }