Add a new key class which uses @GeneratedValue to base classes
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / provider / PdpStatisticsProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2021 Nordix Foundation.
6  * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.pdp.persistence.provider;
25
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 import javax.ws.rs.core.Response;
33 import lombok.NonNull;
34 import org.onap.policy.common.parameters.BeanValidationResult;
35 import org.onap.policy.models.base.PfGeneratedIdKey;
36 import org.onap.policy.models.base.PfKey;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.onap.policy.models.dao.PfDao;
40 import org.onap.policy.models.pdp.concepts.PdpStatistics;
41 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics;
42
43
44 /**
45  * This class provides the provision of information on PAP concepts in the database to callers.
46  *
47  * @author Ning Xi (ning.xi@est.tech)
48  */
49 public class PdpStatisticsProvider {
50     // Recurring string constants
51     private static final String DESC_ORDER = "DESC";
52
53     /**
54      * Get PDP statistics.
55      *
56      * @param dao the DAO to use to access the database
57      * @param name the name of the PDP statistics to get, null to get all PDPs
58      * @return the PDP statistics found
59      * @throws PfModelException on errors getting PDP statistics
60      */
61     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final Instant timeStamp)
62             throws PfModelException {
63         List<PdpStatistics> pdpStatistics = new ArrayList<>();
64         if (name != null && timeStamp != null) {
65             return asPdpStatisticsList(dao.getByTimestamp(JpaPdpStatistics.class,
66                     new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION), timeStamp));
67         } else {
68             return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
69         }
70     }
71
72     /**
73      * Get PDP statistics.
74      *
75      * @param dao the DAO to use to access the database
76      * @param name the name of the PDP statistics to get, null to get all PDPs
77      * @return the PDP statistics found
78      * @throws PfModelException on errors getting PDP statistics
79      */
80     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name)
81             throws PfModelException {
82
83         List<PdpStatistics> pdpStatistics = new ArrayList<>();
84         if (name != null) {
85             pdpStatistics
86                     .add(dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION))
87                             .toAuthorative());
88         } else {
89             return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
90         }
91         return pdpStatistics;
92     }
93
94     /**
95      * Get filtered PDP statistics.
96      *
97      * @param dao the DAO to use to access the database
98      * @param name the pdpInstance name for the PDP statistics to get
99      * @param pdpGroupName pdpGroupName to filter statistics
100      * @param pdpSubGroup pdpSubGroupType name to filter statistics
101      * @param startTimeStamp startTimeStamp to filter statistics
102      * @param endTimeStamp endTimeStamp to filter statistics
103      * @param sortOrder sortOrder to query database
104      * @param getRecordNum Total query count from database
105      * @return the PDP statistics found
106      * @throws PfModelException on errors getting policies
107      */
108     public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao, final String name,
109             @NonNull final String pdpGroupName, final String pdpSubGroup, final Instant startTimeStamp,
110             final Instant endTimeStamp, final String sortOrder, final int getRecordNum) {
111         Map<String, Object> filterMap = new HashMap<>();
112         filterMap.put("pdpGroupName", pdpGroupName);
113         if (pdpSubGroup != null) {
114             filterMap.put("pdpSubGroupName", pdpSubGroup);
115         }
116
117         return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name,
118                 PfKey.NULL_KEY_VERSION, startTimeStamp,
119                 endTimeStamp, filterMap, sortOrder, getRecordNum));
120     }
121
122     /**
123      * Creates PDP statistics.
124      *
125      * @param dao the DAO to use to access the database
126      * @param pdpStatisticsList a specification of the PDP statistics to create
127      * @return the PDP statistics created
128      * @throws PfModelException on errors creating PDP statistics
129      */
130     public List<PdpStatistics> createPdpStatistics(@NonNull final PfDao dao,
131             @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
132         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
133             JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics();
134             jpaPdpStatistics.fromAuthorative(pdpStatistics);
135             BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
136             if (!validationResult.isValid()) {
137                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
138             }
139
140             dao.create(jpaPdpStatistics);
141             pdpStatistics.setGeneratedId(jpaPdpStatistics.getKey().getGeneratedId());
142         }
143
144         // Return the created PDP statistics
145         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
146
147         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
148             JpaPdpStatistics jpaPdpStatistics =
149                     dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
150                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
151             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
152         }
153         return pdpStatistics;
154     }
155
156     /**
157      * Updates PDP statistics.
158      *
159      * @param dao the DAO to use to access the database
160      * @param pdpStatisticsList a specification of the PDP statistics to update
161      * @return the PDP statistics updated
162      * @throws PfModelException on errors updating PDP statistics
163      */
164     public List<PdpStatistics> updatePdpStatistics(@NonNull final PfDao dao,
165             @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
166
167         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
168             JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics();
169             jpaPdpStatistics.fromAuthorative(pdpStatistics);
170
171             BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
172             if (!validationResult.isValid()) {
173                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
174             }
175
176             dao.update(jpaPdpStatistics);
177         }
178
179         // Return the created PDP statistics
180         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
181
182         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
183             JpaPdpStatistics jpaPdpStatistics =
184                     dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
185                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
186             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
187         }
188
189         return pdpStatistics;
190     }
191
192     /**
193      * Delete a PDP statistics.
194      *
195      * @param dao the DAO to use to access the database
196      * @param name the name of the policy to get, null to get all PDP statistics
197      * @param timestamp the timeStamp of statistics to delete, null to delete all statistics record of given PDP
198      * @return the PDP statistics list deleted
199      * @throws PfModelException on errors deleting PDP statistics
200      */
201     public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name,
202             final Instant timestamp) {
203         List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(
204                 dao.getFiltered(JpaPdpStatistics.class, name,
205                 PfKey.NULL_KEY_VERSION, timestamp, timestamp, null, DESC_ORDER, 0));
206
207         pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class,
208                 new PfGeneratedIdKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getGeneratedId())));
209
210         return pdpStatisticsListToDel;
211     }
212
213     /**
214      * Convert JPA PDP statistics list to an PDP statistics list.
215      *
216      * @param jpaPdpStatisticsList the list to convert
217      * @return the PDP statistics list
218      */
219     private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
220         return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());
221     }
222 }