Merge "pdp statistics database provider implementation"
[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 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.pdp.persistence.provider;
24
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.core.Response;
32 import lombok.NonNull;
33 import org.onap.policy.models.base.PfKey;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.base.PfTimestampKey;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44
45 /**
46  * This class provides the provision of information on PAP concepts in the database to callers.
47  *
48  * @author Ning Xi (ning.xi@est.tech)
49  */
50 public class PdpStatisticsProvider {
51     private static final Logger LOGGER = LoggerFactory.getLogger(PdpStatisticsProvider.class);
52
53     // Recurring string constants
54     private static final String NOT_VALID = "\" is not valid \n";
55
56     /**
57      * Get PDP statistics.
58      *
59      * @param dao the DAO to use to access the database
60      * @param name the name of the PDP statistics to get, null to get all PDPs
61      * @return the PDP statistics found
62      * @throws PfModelException on errors getting PDP statistics
63      */
64     public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final Date timestamp)
65             throws PfModelException {
66
67         List<PdpStatistics> pdpStatistics = new ArrayList<>();
68         if (name != null) {
69             pdpStatistics
70                     .add(dao.get(JpaPdpStatistics.class, new PfTimestampKey(name, PfKey.NULL_KEY_VERSION, timestamp))
71                             .toAuthorative());
72         } else {
73             return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
74         }
75         return pdpStatistics;
76     }
77
78     /**
79      * Get filtered PDP statistics.
80      *
81      * @param dao the DAO to use to access the database
82      * @param name the pdpInstance name for the PDP statistics to get
83      * @param pdpGroupName pdpGroupName to filter statistics
84      * @param pdpSubGroup pdpSubGroupType name to filter statistics
85      * @param startTimeStamp startTimeStamp to filter statistics
86      * @param endTimeStamp endTimeStamp to filter statistics
87      * @return the PDP statistics found
88      * @throws PfModelException on errors getting policies
89      */
90     public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao, final String name,
91             @NonNull final String pdpGroupName, final String pdpSubGroup, final Date startTimeStamp,
92             final Date endTimeStamp) {
93         Map<String, Object> filterMap = new HashMap<>();
94         filterMap.put("pdpGroupName", pdpGroupName);
95         if (pdpSubGroup != null) {
96             filterMap.put("pdpSubGroupName", pdpSubGroup);
97         }
98
99         return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, startTimeStamp,
100                 endTimeStamp, filterMap));
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
114         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
115             JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics();
116             jpaPdpStatistics.fromAuthorative(pdpStatistics);
117
118             PfValidationResult validationResult = jpaPdpStatistics.validate(new PfValidationResult());
119             if (!validationResult.isOk()) {
120                 String errorMessage = "pdp statictics \"" + jpaPdpStatistics.getName() + NOT_VALID + validationResult;
121                 LOGGER.warn(errorMessage);
122                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
123             }
124
125             dao.create(jpaPdpStatistics);
126         }
127
128         // Return the created PDP statistics
129         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
130
131         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
132             JpaPdpStatistics jpaPdpStatistics =
133                     dao.get(JpaPdpStatistics.class, new PfTimestampKey(pdpStatisticsItem.getPdpInstanceId(),
134                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getTimeStamp()));
135             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
136         }
137
138         return pdpStatistics;
139     }
140
141     /**
142      * Updates PDP statistics.
143      *
144      * @param dao the DAO to use to access the database
145      * @param pdpStatisticsList a specification of the PDP statistics to update
146      * @return the PDP statistics updated
147      * @throws PfModelException on errors updating PDP statistics
148      */
149     public List<PdpStatistics> updatePdpStatistics(@NonNull final PfDao dao,
150             @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
151
152         for (PdpStatistics pdpStatistics : pdpStatisticsList) {
153             JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics();
154             jpaPdpStatistics.fromAuthorative(pdpStatistics);
155
156             PfValidationResult validationResult = jpaPdpStatistics.validate(new PfValidationResult());
157             if (!validationResult.isOk()) {
158                 String errorMessage = "pdp statistics \"" + jpaPdpStatistics.getId() + NOT_VALID + validationResult;
159                 LOGGER.warn(errorMessage);
160                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
161             }
162
163             dao.update(jpaPdpStatistics);
164         }
165
166         // Return the created PDP statistics
167         List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
168
169         for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
170             JpaPdpStatistics jpaPdpStatistics =
171                     dao.get(JpaPdpStatistics.class, new PfTimestampKey(pdpStatisticsItem.getPdpInstanceId(),
172                             PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getTimeStamp()));
173             pdpStatistics.add(jpaPdpStatistics.toAuthorative());
174         }
175
176         return pdpStatistics;
177     }
178
179     /**
180      * Delete a PDP statistics.
181      *
182      * @param dao the DAO to use to access the database
183      * @param name the name of the policy to get, null to get all PDP statistics
184      * @param timestamp the timeStamp of statistics to delete, null to delete all statistics record of given PDP
185      * @return the PDP statistics list deleted
186      * @throws PfModelException on errors deleting PDP statistics
187      */
188     public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name,
189             final Date timestamp) {
190         List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(
191                 dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, timestamp, timestamp, null));
192
193         pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class,
194                 new PfTimestampKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getTimeStamp())));
195
196         return pdpStatisticsListToDel;
197     }
198
199     /**
200      * Convert JPA PDP statistics list to an PDP statistics list.
201      *
202      * @param jpaPdpStatisticsList the list to convert
203      * @return the PDP statistics list
204      */
205     private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
206         return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());
207     }
208 }