Move PAP database provider to spring boot default
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.service;
22
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;
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 AtomicLong generatedId = new AtomicLong();
51
52     private final PolicyAuditRepository policyAuditRepository;
53
54     /**
55      * Create audit records.
56      *
57      * @param audits list of policy audit
58      */
59     public void createAuditRecords(@NonNull final List<PolicyAudit> audits) {
60         List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
61
62         var result = new BeanValidationResult("createAuditRecords", jpaAudits);
63
64         var count = 0;
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());
69         }
70
71         if (!result.isValid()) {
72             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
73         }
74         policyAuditRepository.saveAll(jpaAudits);
75     }
76
77     /**
78      * Collect the audit records.
79      *
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
84      */
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));
95         } else {
96             return asPolicyAuditList(
97                 policyAuditRepository.findByTimeStampLessThanEqual(Date.from(endTime), recordSize));
98         }
99     }
100
101     /**
102      * Collect the audit records.
103      *
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
109      */
110     public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, int recordCount, Instant startTime,
111         Instant endTime) {
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));
121         } else {
122             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampLessThanEqual(pdpGroup,
123                 Date.from(endTime), recordSize));
124         }
125     }
126
127     /**
128      * Collect the audit records.
129      *
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
137      */
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));
151         } else {
152             return asPolicyAuditList(
153                 policyAuditRepository.findByPdpGroupAndKeyNameAndKeyVersionAndTimeStampLessThanEqual(pdpGroup,
154                     policyName, policyVersion, Date.from(endTime), recordSize));
155         }
156     }
157
158     /**
159      * Collect the audit records.
160      *
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
167      */
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));
180         } else {
181             return asPolicyAuditList(policyAuditRepository.findByKeyNameAndKeyVersionAndTimeStampLessThanEqual(
182                 policyName, policyVersion, Date.from(endTime), recordSize));
183         }
184     }
185
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;
191         }
192         return PageRequest.of(0, recordCount, Sort.by("timeStamp").descending());
193     }
194
195     private List<PolicyAudit> asPolicyAuditList(List<JpaPolicyAudit> jpaPolicyAuditList) {
196         return jpaPolicyAuditList.stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
197     }
198 }