2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.pdp.persistence.provider;
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;
44 * This class provides the provision of information on PAP concepts in the database to callers.
46 * @author Ning Xi (ning.xi@est.tech)
48 public class PdpStatisticsProvider {
49 private static final int DEFAULT_RECORD_COUNT = 10;
50 private static final int MAX_RECORD_COUNT = 100;
53 * Get filtered PDP statistics.
55 * @param dao the DAO to use to access the database
56 * @param filterParams filter parameters
57 * @return the PDP statistics found
58 * @throws PfModelException on errors getting policies
60 public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao,
61 PdpFilterParameters filterParams) {
63 if (filterParams.getRecordNum() <= 0) {
64 filterParams.setRecordNum(DEFAULT_RECORD_COUNT);
66 } else if (filterParams.getRecordNum() > MAX_RECORD_COUNT) {
67 filterParams.setRecordNum(MAX_RECORD_COUNT);
70 return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, filterParams));
74 * Creates PDP statistics.
76 * @param dao the DAO to use to access the database
77 * @param pdpStatisticsList a specification of the PDP statistics to create
78 * @return the PDP statistics created
79 * @throws PfModelException on errors creating PDP statistics
81 public List<PdpStatistics> createPdpStatistics(@NonNull final PfDao dao,
82 @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
83 for (PdpStatistics pdpStatistics : pdpStatisticsList) {
84 var jpaPdpStatistics = new JpaPdpStatistics();
85 jpaPdpStatistics.fromAuthorative(pdpStatistics);
86 BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
87 if (!validationResult.isValid()) {
88 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
91 dao.create(jpaPdpStatistics);
92 pdpStatistics.setGeneratedId(jpaPdpStatistics.getKey().getGeneratedId());
95 // Return the created PDP statistics
96 List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
98 for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
99 var jpaPdpStatistics =
100 dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
101 PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
102 pdpStatistics.add(jpaPdpStatistics.toAuthorative());
104 return pdpStatistics;
108 * Updates PDP statistics.
110 * @param dao the DAO to use to access the database
111 * @param pdpStatisticsList a specification of the PDP statistics to update
112 * @return the PDP statistics updated
113 * @throws PfModelException on errors updating PDP statistics
115 public List<PdpStatistics> updatePdpStatistics(@NonNull final PfDao dao,
116 @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException {
118 for (PdpStatistics pdpStatistics : pdpStatisticsList) {
119 var jpaPdpStatistics = new JpaPdpStatistics();
120 jpaPdpStatistics.fromAuthorative(pdpStatistics);
122 BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics");
123 if (!validationResult.isValid()) {
124 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
127 dao.update(jpaPdpStatistics);
130 // Return the created PDP statistics
131 List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size());
133 for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) {
134 var jpaPdpStatistics =
135 dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(),
136 PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId()));
137 pdpStatistics.add(jpaPdpStatistics.toAuthorative());
140 return pdpStatistics;
144 * Delete a PDP statistics.
146 * @param dao the DAO to use to access the database
147 * @param name the name of the policy to get, null to get all PDP statistics
148 * @param timestamp the timeStamp of statistics to delete, null to delete all statistics record of given PDP
149 * @return the PDP statistics list deleted
150 * @throws PfModelException on errors deleting PDP statistics
152 public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name,
153 final Instant timestamp) {
154 List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class,
155 PfFilterParameters.builder().name(name).startTime(timestamp).endTime(timestamp).build()));
157 pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class,
158 new PfGeneratedIdKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getGeneratedId())));
160 return pdpStatisticsListToDel;
164 * Convert JPA PDP statistics list to an PDP statistics list.
166 * @param jpaPdpStatisticsList the list to convert
167 * @return the PDP statistics list
169 private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) {
170 return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList());