2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.service;
24 import jakarta.ws.rs.core.Response;
25 import java.time.Instant;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import lombok.NonNull;
30 import lombok.RequiredArgsConstructor;
31 import org.onap.policy.common.parameters.BeanValidationResult;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.pap.concepts.PolicyAudit;
34 import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit;
35 import org.onap.policy.pap.main.repository.PolicyAuditRepository;
36 import org.springframework.data.domain.PageRequest;
37 import org.springframework.data.domain.Pageable;
38 import org.springframework.data.domain.Sort;
39 import org.springframework.stereotype.Service;
40 import org.springframework.transaction.annotation.Transactional;
44 @RequiredArgsConstructor
45 public class PolicyAuditService {
47 private static final Integer DEFAULT_MAX_RECORDS = 100;
48 private static final Integer DEFAULT_MIN_RECORDS = 10;
50 private final PolicyAuditRepository policyAuditRepository;
53 * Create audit records.
55 * @param audits list of policy audit
57 public void createAuditRecords(@NonNull final List<PolicyAudit> audits) {
58 List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
60 var result = new BeanValidationResult("createAuditRecords", jpaAudits);
63 for (JpaPolicyAudit jpaAudit : jpaAudits) {
64 result.addResult(jpaAudit.validate(String.valueOf(count++)));
67 if (!result.isValid()) {
68 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
70 policyAuditRepository.saveAll(jpaAudits);
74 * Collect the audit records.
76 * @param recordCount the number of records to return
77 * @param startTime start time of the records to be returned
78 * @param endTime end time of the records to be returned
79 * @return list of {@link PolicyAudit} records found
81 public List<PolicyAudit> getAuditRecords(int recordCount, Instant startTime, Instant endTime) {
82 Pageable recordSize = getRecordSize(recordCount);
83 if (startTime != null && endTime != null) {
84 return asPolicyAuditList(
85 policyAuditRepository.findByTimeStampBetween(Date.from(startTime), Date.from(endTime), recordSize));
86 } else if (startTime == null && endTime == null) {
87 return asPolicyAuditList(policyAuditRepository.findAll(recordSize).toList());
88 } else if (startTime != null) {
89 return asPolicyAuditList(
90 policyAuditRepository.findByTimeStampGreaterThanEqual(Date.from(startTime), recordSize));
92 return asPolicyAuditList(
93 policyAuditRepository.findByTimeStampLessThanEqual(Date.from(endTime), recordSize));
98 * Collect the audit records.
100 * @param pdpGroup the name of the group
101 * @param recordCount the number of records to return
102 * @param startTime start time of the records to be returned
103 * @param endTime end time of the records to be returned
104 * @return list of {@link PolicyAudit} records found
106 public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, int recordCount, Instant startTime,
108 Pageable recordSize = getRecordSize(recordCount);
109 if (startTime != null && endTime != null) {
110 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampBetween(pdpGroup,
111 Date.from(startTime), Date.from(endTime), recordSize));
112 } else if (startTime == null && endTime == null) {
113 return asPolicyAuditList(policyAuditRepository.findByPdpGroup(pdpGroup, recordSize));
114 } else if (startTime != null) {
115 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampGreaterThanEqual(pdpGroup,
116 Date.from(startTime), recordSize));
118 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampLessThanEqual(pdpGroup,
119 Date.from(endTime), recordSize));
124 * Collect the audit records.
126 * @param pdpGroup the name of the group
127 * @param policyName the name of the policy
128 * @param policyVersion the version of the policy
129 * @param recordCount the number of records to return
130 * @param startTime start time of the records to be returned
131 * @param endTime end time of the records to be returned
132 * @return list of {@link PolicyAudit} records found
134 public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, @NonNull String policyName,
135 @NonNull String policyVersion, int recordCount, Instant startTime, Instant endTime) {
136 Pageable recordSize = getRecordSize(recordCount);
137 if (startTime != null && endTime != null) {
138 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampBetween(pdpGroup,
139 policyName, policyVersion, Date.from(startTime), Date.from(endTime), recordSize));
140 } else if (startTime == null && endTime == null) {
141 return asPolicyAuditList(
142 policyAuditRepository.findByPdpGroupAndNameAndVersion(pdpGroup, policyName, policyVersion, recordSize));
143 } else if (startTime != null) {
144 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampGreaterThanEqual(
145 pdpGroup, policyName, policyVersion, Date.from(startTime), recordSize));
147 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampLessThanEqual(
148 pdpGroup, policyName, policyVersion, Date.from(endTime), recordSize));
153 * Collect the audit records.
155 * @param policyName the name of the policy
156 * @param policyVersion the version of the policy
157 * @param recordCount the number of records to return
158 * @param startTime start time of the records to be returned
159 * @param endTime end time of the records to be returned
160 * @return list of {@link PolicyAudit} records found
162 public List<PolicyAudit> getAuditRecords(@NonNull String policyName, @NonNull String policyVersion, int recordCount,
163 Instant startTime, Instant endTime) {
164 Pageable recordSize = getRecordSize(recordCount);
165 if (startTime != null && endTime != null) {
166 return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampBetween(policyName,
167 policyVersion, Date.from(startTime), Date.from(endTime), recordSize));
168 } else if (startTime == null && endTime == null) {
169 return asPolicyAuditList(policyAuditRepository.findByNameAndVersion(policyName, policyVersion, recordSize));
170 } else if (startTime != null) {
171 return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampGreaterThanEqual(policyName,
172 policyVersion, Date.from(startTime), recordSize));
174 return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampLessThanEqual(policyName,
175 policyVersion, Date.from(endTime), recordSize));
179 private Pageable getRecordSize(int recordCount) {
180 if (recordCount < 1) {
181 recordCount = DEFAULT_MIN_RECORDS;
182 } else if (recordCount > DEFAULT_MAX_RECORDS) {
183 recordCount = DEFAULT_MAX_RECORDS;
185 return PageRequest.of(0, recordCount, Sort.by("timeStamp").descending());
188 private List<PolicyAudit> asPolicyAuditList(List<JpaPolicyAudit> jpaPolicyAuditList) {
189 return jpaPolicyAuditList.stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());