f582135d2d6a765252d7183d480d8f89db4c15e0
[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.stream.Collectors;
27 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.common.parameters.BeanValidationResult;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.pap.concepts.PolicyAudit;
33 import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit;
34 import org.onap.policy.pap.main.repository.PolicyAuditRepository;
35 import org.springframework.data.domain.PageRequest;
36 import org.springframework.data.domain.Pageable;
37 import org.springframework.data.domain.Sort;
38 import org.springframework.stereotype.Service;
39 import org.springframework.transaction.annotation.Transactional;
40
41 @Service
42 @Transactional
43 @RequiredArgsConstructor
44 public class PolicyAuditService {
45
46     private static final Integer DEFAULT_MAX_RECORDS = 100;
47     private static final Integer DEFAULT_MIN_RECORDS = 10;
48
49     private final PolicyAuditRepository policyAuditRepository;
50
51     /**
52      * Create audit records.
53      *
54      * @param audits list of policy audit
55      */
56     public void createAuditRecords(@NonNull final List<PolicyAudit> audits) {
57         List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
58
59         var result = new BeanValidationResult("createAuditRecords", jpaAudits);
60
61         var count = 0;
62         for (JpaPolicyAudit jpaAudit : jpaAudits) {
63             result.addResult(jpaAudit.validate(String.valueOf(count++)));
64         }
65
66         if (!result.isValid()) {
67             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
68         }
69         policyAuditRepository.saveAll(jpaAudits);
70     }
71
72     /**
73      * Collect the audit records.
74      *
75      * @param recordCount the number of records to return
76      * @param startTime start time of the records to be returned
77      * @param endTime end time of the records to be returned
78      * @return list of {@link PolicyAudit} records found
79      */
80     public List<PolicyAudit> getAuditRecords(int recordCount, Instant startTime, Instant endTime) {
81         Pageable recordSize = getRecordSize(recordCount);
82         if (startTime != null && endTime != null) {
83             return asPolicyAuditList(
84                 policyAuditRepository.findByTimeStampBetween(Date.from(startTime), Date.from(endTime), recordSize));
85         } else if (startTime == null && endTime == null) {
86             return asPolicyAuditList(policyAuditRepository.findAll(recordSize).toList());
87         } else if (startTime != null) {
88             return asPolicyAuditList(
89                 policyAuditRepository.findByTimeStampGreaterThanEqual(Date.from(startTime), recordSize));
90         } else {
91             return asPolicyAuditList(
92                 policyAuditRepository.findByTimeStampLessThanEqual(Date.from(endTime), recordSize));
93         }
94     }
95
96     /**
97      * Collect the audit records.
98      *
99      * @param pdpGroup the name of the group
100      * @param recordCount the number of records to return
101      * @param startTime start time of the records to be returned
102      * @param endTime end time of the records to be returned
103      * @return list of {@link PolicyAudit} records found
104      */
105     public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, int recordCount, Instant startTime,
106         Instant endTime) {
107         Pageable recordSize = getRecordSize(recordCount);
108         if (startTime != null && endTime != null) {
109             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampBetween(pdpGroup,
110                 Date.from(startTime), Date.from(endTime), recordSize));
111         } else if (startTime == null && endTime == null) {
112             return asPolicyAuditList(policyAuditRepository.findByPdpGroup(pdpGroup, recordSize));
113         } else if (startTime != null) {
114             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampGreaterThanEqual(pdpGroup,
115                 Date.from(startTime), recordSize));
116         } else {
117             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndTimeStampLessThanEqual(pdpGroup,
118                 Date.from(endTime), recordSize));
119         }
120     }
121
122     /**
123      * Collect the audit records.
124      *
125      * @param pdpGroup the name of the group
126      * @param policyName the name of the policy
127      * @param policyVersion the version of the policy
128      * @param recordCount the number of records to return
129      * @param startTime start time of the records to be returned
130      * @param endTime end time of the records to be returned
131      * @return list of {@link PolicyAudit} records found
132      */
133     public List<PolicyAudit> getAuditRecords(@NonNull String pdpGroup, @NonNull String policyName,
134         @NonNull String policyVersion, int recordCount, Instant startTime, Instant endTime) {
135         Pageable recordSize = getRecordSize(recordCount);
136         if (startTime != null && endTime != null) {
137             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampBetween(pdpGroup,
138                 policyName, policyVersion, Date.from(startTime), Date.from(endTime), recordSize));
139         } else if (startTime == null && endTime == null) {
140             return asPolicyAuditList(
141                 policyAuditRepository.findByPdpGroupAndNameAndVersion(pdpGroup, policyName, policyVersion, recordSize));
142         } else if (startTime != null) {
143             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampGreaterThanEqual(
144                 pdpGroup, policyName, policyVersion, Date.from(startTime), recordSize));
145         } else {
146             return asPolicyAuditList(policyAuditRepository.findByPdpGroupAndNameAndVersionAndTimeStampLessThanEqual(
147                 pdpGroup, policyName, policyVersion, Date.from(endTime), recordSize));
148         }
149     }
150
151     /**
152      * Collect the audit records.
153      *
154      * @param policyName the name of the policy
155      * @param policyVersion the version of the policy
156      * @param recordCount the number of records to return
157      * @param startTime start time of the records to be returned
158      * @param endTime end time of the records to be returned
159      * @return list of {@link PolicyAudit} records found
160      */
161     public List<PolicyAudit> getAuditRecords(@NonNull String policyName, @NonNull String policyVersion, int recordCount,
162         Instant startTime, Instant endTime) {
163         Pageable recordSize = getRecordSize(recordCount);
164         if (startTime != null && endTime != null) {
165             return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampBetween(policyName,
166                 policyVersion, Date.from(startTime), Date.from(endTime), recordSize));
167         } else if (startTime == null && endTime == null) {
168             return asPolicyAuditList(policyAuditRepository.findByNameAndVersion(policyName, policyVersion, recordSize));
169         } else if (startTime != null) {
170             return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampGreaterThanEqual(policyName,
171                 policyVersion, Date.from(startTime), recordSize));
172         } else {
173             return asPolicyAuditList(policyAuditRepository.findByNameAndVersionAndTimeStampLessThanEqual(policyName,
174                 policyVersion, Date.from(endTime), recordSize));
175         }
176     }
177
178     private Pageable getRecordSize(int recordCount) {
179         if (recordCount < 1) {
180             recordCount = DEFAULT_MIN_RECORDS;
181         } else if (recordCount > DEFAULT_MAX_RECORDS) {
182             recordCount = DEFAULT_MAX_RECORDS;
183         }
184         return PageRequest.of(0, recordCount, Sort.by("timeStamp").descending());
185     }
186
187     private List<PolicyAudit> asPolicyAuditList(List<JpaPolicyAudit> jpaPolicyAuditList) {
188         return jpaPolicyAuditList.stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
189     }
190 }