Remove unused DAO statistics methods
[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-2021 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.List;
29 import java.util.stream.Collectors;
30 import javax.ws.rs.core.Response;
31 import lombok.NonNull;
32 import org.onap.policy.common.parameters.BeanValidationResult;
33 import org.onap.policy.models.base.PfGeneratedIdKey;
34 import org.onap.policy.models.base.PfKey;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.dao.PfFilterParameters;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics;
41
42
43 /**
44  * This class provides the provision of information on PAP concepts in the database to callers.
45  *
46  * @author Ning Xi (ning.xi@est.tech)
47  */
48 public class PdpStatisticsProvider {
49
50     /**
51      * Get PDP statistics.
52      *
53      * @param dao the DAO to use to access the database
54      * @param name the name of the PDP statistics to get, null to get all PDPs
55      * @return the PDP statistics found
56      * @throws PfModelException on errors getting PDP statistics
57      */
58     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final Instant timeStamp)
59             throws PfModelException {
60         if (name != null && timeStamp != null) {
61             return asPdpStatisticsList(dao.getByTimestamp(JpaPdpStatistics.class,
62                     new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION), timeStamp));
63         } else {
64             return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
65         }
66     }
67
68     /**
69      * Get PDP statistics.
70      *
71      * @param dao the DAO to use to access the database
72      * @param name the name of the PDP statistics to get, null to get all PDPs
73      * @return the PDP statistics found
74      * @throws PfModelException on errors getting PDP statistics
75      */
76     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name)
77             throws PfModelException {
78
79         List<PdpStatistics> pdpStatistics = new ArrayList<>();
80         if (name != null) {
81             pdpStatistics
82                     .add(dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION))
83                             .toAuthorative());
84         } else {
85             return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
86         }
87         return pdpStatistics;
88     }
89
90     /**
91      * Get filtered PDP statistics.
92      *
93      * @param dao the DAO to use to access the database
94      * @param filterParams filter parameters
95      * @return the PDP statistics found
96      * @throws PfModelException on errors getting policies
97      */
98     public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao,
99                     PdpFilterParameters filterParams) {
100         return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, filterParams));
101     }
102
103     /**
104      * Creates PDP statistics.
105      *
106      * @param dao the DAO to use to access the database
107      * @param pdpStatisticsList a specification of the PDP statistics to create
108      * @return the PDP statistics created
109      * @throws PfModelException on errors creating PDP statistics
110      */
111     public List<PdpStatistics> createPdpStatistics(@NonNull final PfDao dao,
112             @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
113         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
114             var jpaPdpStatistics = new JpaPdpStatistics();
115             jpaPdpStatistics.fromAuthorative(pdpStatistics);
116             BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
117             if (!validationResult.isValid()) {
118                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
119             }
120
121             dao.create(jpaPdpStatistics);
122             pdpStatistics.setGeneratedId(jpaPdpStatistics.getKey().getGeneratedId());
123         }
124
125         // Return the created PDP statistics
126         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
127
128         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
129             var jpaPdpStatistics =
130                     dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
131                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
132             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
133         }
134         return pdpStatistics;
135     }
136
137     /**
138      * Updates PDP statistics.
139      *
140      * @param dao the DAO to use to access the database
141      * @param pdpStatisticsList a specification of the PDP statistics to update
142      * @return the PDP statistics updated
143      * @throws PfModelException on errors updating PDP statistics
144      */
145     public List<PdpStatistics> updatePdpStatistics(@NonNull final PfDao dao,
146             @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
147
148         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
149             var jpaPdpStatistics = new JpaPdpStatistics();
150             jpaPdpStatistics.fromAuthorative(pdpStatistics);
151
152             BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
153             if (!validationResult.isValid()) {
154                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
155             }
156
157             dao.update(jpaPdpStatistics);
158         }
159
160         // Return the created PDP statistics
161         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
162
163         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
164             var jpaPdpStatistics =
165                     dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
166                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
167             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
168         }
169
170         return pdpStatistics;
171     }
172
173     /**
174      * Delete a PDP statistics.
175      *
176      * @param dao the DAO to use to access the database
177      * @param name the name of the policy to get, null to get all PDP statistics
178      * @param timestamp the timeStamp of statistics to delete, null to delete all statistics record of given PDP
179      * @return the PDP statistics list deleted
180      * @throws PfModelException on errors deleting PDP statistics
181      */
182     public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name,
183                     final Instant timestamp) {
184         List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class,
185                         PfFilterParameters.builder().name(name).startTime(timestamp).endTime(timestamp).build()));
186
187         pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class,
188                 new PfGeneratedIdKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getGeneratedId())));
189
190         return pdpStatisticsListToDel;
191     }
192
193     /**
194      * Convert JPA PDP statistics list to an PDP statistics list.
195      *
196      * @param jpaPdpStatisticsList the list to convert
197      * @return the PDP statistics list
198      */
199     private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
200         return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());
201     }
202 }