Handle numRecords default setting when built as 0.
[policy/models.git] / models-pap / src / main / java / org / onap / policy / models / pap / persistence / provider / PolicyAuditProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.pap.persistence.provider;
24
25 import java.time.Instant;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30 import javax.ws.rs.core.Response;
31 import lombok.Builder;
32 import lombok.Data;
33 import lombok.NonNull;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.dao.PfDao;
38 import org.onap.policy.models.dao.PfFilterParametersIntfc;
39 import org.onap.policy.models.pap.concepts.PolicyAudit;
40 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
41 import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit;
42
43 /**
44  * Provider for Policy Audit.
45  *
46  * @author Adheli Tavares (adheli.tavares@est.tech)
47  *
48  */
49 public class PolicyAuditProvider {
50
51     private static final Integer DEFAULT_MAX_RECORDS = 100;
52
53     /**
54      * Create audit records.
55      *
56      * @param audits list of policy audit
57      */
58     public void createAuditRecords(@NonNull PfDao dao, @NonNull final List<PolicyAudit> audits) {
59         List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
60
61         var result = new BeanValidationResult("createAuditRecords", jpaAudits);
62
63         var count = 0;
64         for (JpaPolicyAudit jpaAudit : jpaAudits) {
65             result.addResult(jpaAudit.validate(String.valueOf(count++)));
66         }
67
68         if (!result.isValid()) {
69             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
70         }
71
72         dao.createCollection(jpaAudits);
73     }
74
75     /**
76      * Collect audit records based on filters at {@link AuditFilter}.
77      *
78      * @param auditFilter {@link AuditFilter} object with filters for search
79      * @return list of {@link PolicyAudit} records
80      */
81     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) {
82         if (auditFilter.getRecordNum() < 1 || auditFilter.getRecordNum() > DEFAULT_MAX_RECORDS) {
83             auditFilter.setRecordNum(DEFAULT_MAX_RECORDS);
84         }
85
86         return dao.getFiltered(JpaPolicyAudit.class, auditFilter).stream().map(JpaPolicyAudit::toAuthorative)
87                 .collect(Collectors.toList());
88     }
89
90     /**
91      * Create a filter for looking for audit records.
92      * name - policy name
93      * version - policy version
94      * pdpGroup - PDP group that policy might be related
95      * action - type of action/operation realized on policy
96      * fromDate - start of period in case of time interval search
97      */
98     @Data
99     @Builder
100     public static class AuditFilter implements PfFilterParametersIntfc {
101         private String name;
102         private String version;
103         private AuditAction action;
104         private String pdpGroup;
105         private Instant fromDate;
106         private Instant toDate;
107         private int recordNum;
108         @Builder.Default
109         private String sortOrder = "DESC";
110
111         // initialized lazily, if not set via the builder
112         private Map<String, Object> filterMap;
113
114         @Override
115         public Instant getStartTime() {
116             return fromDate;
117         }
118
119         @Override
120         public Instant getEndTime() {
121             return toDate;
122         }
123
124         @Override
125         public Map<String, Object> getFilterMap() {
126             if (filterMap != null) {
127                 return filterMap;
128             }
129
130             filterMap = new HashMap<>();
131
132             if (StringUtils.isNotBlank(pdpGroup)) {
133                 filterMap.put("pdpGroup", pdpGroup);
134             }
135
136             if (action != null) {
137                 filterMap.put("action", action);
138             }
139
140             return filterMap;
141         }
142     }
143 }