Fix sonars in policy models
[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.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         if (name != null && timeStamp != null) {
64             return asPdpStatisticsList(dao.getByTimestamp(JpaPdpStatistics.class,
65                     new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION), timeStamp));
66         } else {
67             return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
68         }
69     }
70
71     /**
72      * Get PDP statistics.
73      *
74      * @param dao the DAO to use to access the database
75      * @param name the name of the PDP statistics to get, null to get all PDPs
76      * @return the PDP statistics found
77      * @throws PfModelException on errors getting PDP statistics
78      */
79     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name)
80             throws PfModelException {
81
82         List<PdpStatistics> pdpStatistics = new ArrayList<>();
83         if (name != null) {
84             pdpStatistics
85                     .add(dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION))
86                             .toAuthorative());
87         } else {
88             return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
89         }
90         return pdpStatistics;
91     }
92
93     /**
94      * Get filtered PDP statistics.
95      *
96      * @param dao the DAO to use to access the database
97      * @param name the pdpInstance name for the PDP statistics to get
98      * @param pdpGroupName pdpGroupName to filter statistics
99      * @param pdpSubGroup pdpSubGroupType name to filter statistics
100      * @param startTimeStamp startTimeStamp to filter statistics
101      * @param endTimeStamp endTimeStamp to filter statistics
102      * @param sortOrder sortOrder to query database
103      * @param getRecordNum Total query count from database
104      * @return the PDP statistics found
105      * @throws PfModelException on errors getting policies
106      */
107     public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao, final String name,
108             @NonNull final String pdpGroupName, final String pdpSubGroup, final Instant startTimeStamp,
109             final Instant endTimeStamp, final String sortOrder, final int getRecordNum) {
110         Map<String, Object> filterMap = new HashMap<>();
111         filterMap.put("pdpGroupName", pdpGroupName);
112         if (pdpSubGroup != null) {
113             filterMap.put("pdpSubGroupName", pdpSubGroup);
114         }
115
116         return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name,
117                 PfKey.NULL_KEY_VERSION, startTimeStamp,
118                 endTimeStamp, filterMap, sortOrder, getRecordNum));
119     }
120
121     /**
122      * Creates PDP statistics.
123      *
124      * @param dao the DAO to use to access the database
125      * @param pdpStatisticsList a specification of the PDP statistics to create
126      * @return the PDP statistics created
127      * @throws PfModelException on errors creating PDP statistics
128      */
129     public List<PdpStatistics> createPdpStatistics(@NonNull final PfDao dao,
130             @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
131         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
132             var jpaPdpStatistics = new JpaPdpStatistics();
133             jpaPdpStatistics.fromAuthorative(pdpStatistics);
134             BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
135             if (!validationResult.isValid()) {
136                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
137             }
138
139             dao.create(jpaPdpStatistics);
140             pdpStatistics.setGeneratedId(jpaPdpStatistics.getKey().getGeneratedId());
141         }
142
143         // Return the created PDP statistics
144         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
145
146         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
147             var jpaPdpStatistics =
148                     dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
149                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
150             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
151         }
152         return pdpStatistics;
153     }
154
155     /**
156      * Updates PDP statistics.
157      *
158      * @param dao the DAO to use to access the database
159      * @param pdpStatisticsList a specification of the PDP statistics to update
160      * @return the PDP statistics updated
161      * @throws PfModelException on errors updating PDP statistics
162      */
163     public List<PdpStatistics> updatePdpStatistics(@NonNull final PfDao dao,
164             @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
165
166         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
167             var jpaPdpStatistics = new JpaPdpStatistics();
168             jpaPdpStatistics.fromAuthorative(pdpStatistics);
169
170             BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
171             if (!validationResult.isValid()) {
172                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
173             }
174
175             dao.update(jpaPdpStatistics);
176         }
177
178         // Return the created PDP statistics
179         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
180
181         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
182             var jpaPdpStatistics =
183                     dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
184                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
185             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
186         }
187
188         return pdpStatistics;
189     }
190
191     /**
192      * Delete a PDP statistics.
193      *
194      * @param dao the DAO to use to access the database
195      * @param name the name of the policy to get, null to get all PDP statistics
196      * @param timestamp the timeStamp of statistics to delete, null to delete all statistics record of given PDP
197      * @return the PDP statistics list deleted
198      * @throws PfModelException on errors deleting PDP statistics
199      */
200     public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name,
201             final Instant timestamp) {
202         List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(
203                 dao.getFiltered(JpaPdpStatistics.class, name,
204                 PfKey.NULL_KEY_VERSION, timestamp, timestamp, null, DESC_ORDER, 0));
205
206         pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class,
207                 new PfGeneratedIdKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getGeneratedId())));
208
209         return pdpStatisticsListToDel;
210     }
211
212     /**
213      * Convert JPA PDP statistics list to an PDP statistics list.
214      *
215      * @param jpaPdpStatisticsList the list to convert
216      * @return the PDP statistics list
217      */
218     private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
219         return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());
220     }
221 }