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