Fix sonars in policy-models
[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  * ================================================================================
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.models.pap.persistence.provider;
23
24 import java.time.Instant;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 import javax.ws.rs.core.Response;
30 import lombok.Builder;
31 import lombok.Data;
32 import lombok.NonNull;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.policy.common.parameters.BeanValidationResult;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.dao.PfDao;
37 import org.onap.policy.models.dao.PfFilterParametersIntfc;
38 import org.onap.policy.models.pap.concepts.PolicyAudit;
39 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
40 import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit;
41
42 /**
43  * Provider for Policy Audit.
44  *
45  * @author Adheli Tavares (adheli.tavares@est.tech)
46  *
47  */
48 public class PolicyAuditProvider {
49
50     private static final Integer DEFAULT_MAX_RECORDS = 100;
51
52     /**
53      * Create audit records.
54      *
55      * @param audits list of policy audit
56      */
57     public void createAuditRecords(@NonNull PfDao dao, @NonNull final List<PolicyAudit> audits) {
58         List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
59
60         BeanValidationResult result = new BeanValidationResult("createAuditRecords", jpaAudits);
61
62         int 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
71         dao.createCollection(jpaAudits);
72     }
73
74     /**
75      * Collect all audit records.
76      *
77      * @param numRecords number of records to be collected
78      * @return list of {@link PolicyAudit} records
79      */
80     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull Integer numRecords) {
81         numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords;
82
83         // @formatter:off
84         return dao.getAll(JpaPolicyAudit.class, "timeStamp DESC", numRecords)
85                 .stream()
86                 .map(JpaPolicyAudit::toAuthorative)
87                 .collect(Collectors.toList());
88         // @formatter:on
89     }
90
91     /**
92      * Collect audit records based on filters at {@link AuditFilter}.
93      *
94      * @param auditFilter {@link AuditFilter} object with filters for search
95      * @param numRecords number of records to be collected
96      * @return list of {@link PolicyAudit} records
97      */
98     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter,
99             @NonNull Integer numRecords) {
100
101         auditFilter.setRecordNum(Math.min(numRecords, DEFAULT_MAX_RECORDS));
102
103         return getAuditRecords(dao, auditFilter);
104     }
105
106     /**
107      * Collect audit records based on filters at {@link AuditFilter}.
108      *
109      * @param auditFilter {@link AuditFilter} object with filters for search
110      * @return list of {@link PolicyAudit} records
111      */
112     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) {
113
114         return dao.getFiltered(JpaPolicyAudit.class, auditFilter)
115                     .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
116     }
117
118     /**
119      * Create a filter for looking for audit records.
120      * name - policy name
121      * version - policy version
122      * pdpGroup - PDP group that policy might be related
123      * action - type of action/operation realized on policy
124      * fromDate - start of period in case of time interval search
125      */
126     @Data
127     @Builder
128     public static class AuditFilter implements PfFilterParametersIntfc {
129         private String name;
130         private String version;
131         private AuditAction action;
132         private String pdpGroup;
133         private Instant fromDate;
134         private Instant toDate;
135         private int recordNum;
136         @Builder.Default
137         private String sortOrder = "DESC";
138
139         // initialized lazily, if not set via the builder
140         private Map<String, Object> filterMap;
141
142         /**
143          * Check if even still using build(), none of the params were provided.
144          *
145          * @return {@code true} if all empty/null; {@code false} otherwise.
146          */
147         public boolean isEmpty() {
148             return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null
149                     && toDate == null;
150         }
151
152         @Override
153         public Instant getStartTime() {
154             return fromDate;
155         }
156
157         @Override
158         public Instant getEndTime() {
159             return toDate;
160         }
161
162         @Override
163         public Map<String, Object> getFilterMap() {
164             if (filterMap != null) {
165                 return filterMap;
166             }
167
168             filterMap = new HashMap<>();
169
170             if (StringUtils.isNotBlank(pdpGroup)) {
171                 filterMap.put("pdpGroup", pdpGroup);
172             }
173
174             if (action != null) {
175                 filterMap.put("action", action);
176             }
177
178             return filterMap;
179         }
180     }
181 }