2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.pdp.persistence.provider;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
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;
46 * This class provides the provision of information on PAP concepts in the database to callers.
48 * @author Ning Xi (ning.xi@est.tech)
50 public class PdpStatisticsProvider {
51 private static final Logger LOGGER = LoggerFactory.getLogger(PdpStatisticsProvider.class);
53 // Recurring string constants
54 private static final String NOT_VALID = "\" is not valid \n";
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
64 public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final Date timestamp)
65 throws PfModelException {
67 List<PdpStatistics> pdpStatistics = new ArrayList<>();
70 .add(dao.get(JpaPdpStatistics.class, new PfTimestampKey(name, PfKey.NULL_KEY_VERSION, timestamp))
73 return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class));
79 * Get filtered PDP statistics.
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
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);
99 return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, startTimeStamp,
100 endTimeStamp, filterMap));
104 * Creates PDP statistics.
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
111 public List<PdpStatistics> createPdpStatistics(@NonNull final PfDao dao,
112 @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
114 for (PdpStatistics pdpStatistics : pdpStatisticsList) {
115 JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics();
116 jpaPdpStatistics.fromAuthorative(pdpStatistics);
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);
125 dao.create(jpaPdpStatistics);
128 // Return the created PDP statistics
129 List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
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());
138 return pdpStatistics;
142 * Updates PDP statistics.
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
149 public List<PdpStatistics> updatePdpStatistics(@NonNull final PfDao dao,
150 @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
152 for (PdpStatistics pdpStatistics : pdpStatisticsList) {
153 JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics();
154 jpaPdpStatistics.fromAuthorative(pdpStatistics);
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);
163 dao.update(jpaPdpStatistics);
166 // Return the created PDP statistics
167 List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
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());
176 return pdpStatistics;
180 * Delete a PDP statistics.
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
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));
193 pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class,
194 new PfTimestampKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getTimeStamp())));
196 return pdpStatisticsListToDel;
200 * Convert JPA PDP statistics list to an PDP statistics list.
202 * @param jpaPdpStatisticsList the list to convert
203 * @return the PDP statistics list
205 private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
206 return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());