2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.service;
23 import java.time.Instant;
24 import java.util.Date;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicLong;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response;
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 AtomicLong generatedId = new AtomicLong();
52 private final PolicyAuditRepository policyAuditRepository;
55 * Create audit records.
57 * @param audits list of policy audit
59 public void createAuditRecords(@NonNull final List<PolicyAudit> audits) {
60 List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
62 var result = new BeanValidationResult("createAuditRecords", jpaAudits);
65 for (JpaPolicyAudit jpaAudit : jpaAudits) {
66 result.addResult(jpaAudit.validate(String.valueOf(count++)));
67 // TODO: Fix this as part of POLICY-3897
68 jpaAudit.getKey().setGeneratedId(generatedId.incrementAndGet());
71 if (!result.isValid()) {
72 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
74 policyAuditRepository.saveAll(jpaAudits);
78 * Collect the audit records.
80 * @param recordCount the number of records to return
81 * @param startTime start time of the records to be returned
82 * @param endTime end time of the records to be returned
83 * @return list of {@link PolicyAudit} records found
85 public List<PolicyAudit> getAuditRecords(int recordCount, Instant startTime, Instant endTime) {
86 Pageable recordSize = getRecordSize(recordCount);
87 if (startTime != null && endTime != null) {
88 return asPolicyAuditList(
89 policyAuditRepository.findByTimeStampBetween(Date.from(startTime), Date.from(endTime), recordSize));
90 } else if (startTime == null && endTime == null) {
91 return asPolicyAuditList(policyAuditRepository.findAll(recordSize).toList());
92 } else if (startTime != null) {
93 return asPolicyAuditList(
94 policyAuditRepository.findByTimeStampGreaterThanEqual(Date.from(startTime), recordSize));
96 return asPolicyAuditList(
97 policyAuditRepository.findByTimeStampLessThanEqual(Date.from(endTime), recordSize));
102 * Collect the audit records.
104 * @param pdpGroup the name of the group
105 * @param recordCount the number of records to return
106 * @param startTime start time of the records to be returned
107 * @param endTime end time of the records to be returned
108 * @return list of {@link PolicyAudit} records found
110 public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, int recordCount, Instant startTime,
112 Pageable recordSize = getRecordSize(recordCount);
113 if (startTime != null && endTime != null) {
114 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampBetween(pdpGroup,
115 Date.from(startTime), Date.from(endTime), recordSize));
116 } else if (startTime == null && endTime == null) {
117 return asPolicyAuditList(policyAuditRepository.findByPdpGroup(pdpGroup, recordSize));
118 } else if (startTime != null) {
119 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampGreaterThanEqual(pdpGroup,
120 Date.from(startTime), recordSize));
122 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampLessThanEqual(pdpGroup,
123 Date.from(endTime), recordSize));
128 * Collect the audit records.
130 * @param pdpGroup the name of the group
131 * @param policyName the name of the policy
132 * @param policyVersion the version of the policy
133 * @param recordCount the number of records to return
134 * @param startTime start time of the records to be returned
135 * @param endTime end time of the records to be returned
136 * @return list of {@link PolicyAudit} records found
138 public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, @NonNull String policyName,
139 @NonNull String policyVersion, int recordCount, Instant startTime, Instant endTime) {
140 Pageable recordSize = getRecordSize(recordCount);
141 if (startTime != null && endTime != null) {
142 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampBetween(
143 pdpGroup, policyName, policyVersion, Date.from(startTime), Date.from(endTime), recordSize));
144 } else if (startTime == null && endTime == null) {
145 return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersion(pdpGroup, policyName,
146 policyVersion, recordSize));
147 } else if (startTime != null) {
148 return asPolicyAuditList(
149 policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampGreaterThanEqual(pdpGroup,
150 policyName, policyVersion, Date.from(startTime), recordSize));
152 return asPolicyAuditList(
153 policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampLessThanEqual(pdpGroup,
154 policyName, policyVersion, Date.from(endTime), recordSize));
159 * Collect the audit records.
161 * @param policyName the name of the policy
162 * @param policyVersion the version of the policy
163 * @param recordCount the number of records to return
164 * @param startTime start time of the records to be returned
165 * @param endTime end time of the records to be returned
166 * @return list of {@link PolicyAudit} records found
168 public List<PolicyAudit> getAuditRecords(@NonNull String policyName, @NonNull String policyVersion, int recordCount,
169 Instant startTime, Instant endTime) {
170 Pageable recordSize = getRecordSize(recordCount);
171 if (startTime != null && endTime != null) {
172 return asPolicyAuditList(policyAuditRepository.findByKeyNameAndKeyVersionAndTimeStampBetween(policyName,
173 policyVersion, Date.from(startTime), Date.from(endTime), recordSize));
174 } else if (startTime == null && endTime == null) {
175 return asPolicyAuditList(
176 policyAuditRepository.findByKeyNameAndKeyVersion(policyName, policyVersion, recordSize));
177 } else if (startTime != null) {
178 return asPolicyAuditList(policyAuditRepository.findByKeyNameAndKeyVersionAndTimeStampGreaterThanEqual(
179 policyName, policyVersion, Date.from(startTime), recordSize));
181 return asPolicyAuditList(policyAuditRepository.findByKeyNameAndKeyVersionAndTimeStampLessThanEqual(
182 policyName, policyVersion, Date.from(endTime), recordSize));
186 private Pageable getRecordSize(int recordCount) {
187 if (recordCount < 1) {
188 recordCount = DEFAULT_MIN_RECORDS;
189 } else if (recordCount > DEFAULT_MAX_RECORDS) {
190 recordCount = DEFAULT_MAX_RECORDS;
192 return PageRequest.of(0, recordCount, Sort.by("timeStamp").descending());
195 private List<PolicyAudit> asPolicyAuditList(List<JpaPolicyAudit> jpaPolicyAuditList) {
196 return jpaPolicyAuditList.stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());