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