Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / service / PolicyAuditService.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
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;
41
42 @Service
43 @Transactional
44 @RequiredArgsConstructor
45 public class PolicyAuditService {
46
47     private static final Integer DEFAULT_MAX_RECORDS = 100;
48     private static final Integer DEFAULT_MIN_RECORDS = 10;
49
50     private final PolicyAuditRepository policyAuditRepository;
51
52     /**
53      * Create audit records.
54      *
55      * @param audits list of policy audit
56      */
57     public void createAuditRecords(@NonNull final List<PolicyAudit> audits) {
58         List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
59
60         var result = new BeanValidationResult("createAuditRecords", jpaAudits);
61
62         var count = 0;
63         for (JpaPolicyAudit jpaAudit : jpaAudits) {
64             result.addResult(jpaAudit.validate(String.valueOf(count++)));
65         }
66
67         if (!result.isValid()) {
68             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
69         }
70         policyAuditRepository.saveAll(jpaAudits);
71     }
72
73     /**
74      * Collect the audit records.
75      *
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
80      */
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));
91         } else {
92             return asPolicyAuditList(
93                 policyAuditRepository.findByTimeStampLessThanEqual(Date.from(endTime), recordSize));
94         }
95     }
96
97     /**
98      * Collect the audit records.
99      *
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
105      */
106     public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, int recordCount, Instant startTime,
107         Instant endTime) {
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));
117         } else {
118             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampLessThanEqual(pdpGroup,
119                 Date.from(endTime), recordSize));
120         }
121     }
122
123     /**
124      * Collect the audit records.
125      *
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
133      */
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));
146         } else {
147             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampLessThanEqual(
148                 pdpGroup, policyName, policyVersion, Date.from(endTime), recordSize));
149         }
150     }
151
152     /**
153      * Collect the audit records.
154      *
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
161      */
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));
173         } else {
174             return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampLessThanEqual(policyName,
175                 policyVersion, Date.from(endTime), recordSize));
176         }
177     }
178
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;
184         }
185         return PageRequest.of(0, recordCount, Sort.by("timeStamp").descending());
186     }
187
188     private List<PolicyAudit> asPolicyAuditList(List<JpaPolicyAudit> jpaPolicyAuditList) {
189         return jpaPolicyAuditList.stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
190     }
191 }