Fix sonar 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  *  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 all audit records.
77      *
78      * @param numRecords number of records to be collected
79      * @return list of {@link PolicyAudit} records
80      */
81     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull Integer numRecords) {
82         numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords;
83
84         // @formatter:off
85         return dao.getAll(JpaPolicyAudit.class, "timeStamp DESC", numRecords)
86                 .stream()
87                 .map(JpaPolicyAudit::toAuthorative)
88                 .collect(Collectors.toList());
89         // @formatter:on
90     }
91
92     /**
93      * Collect audit records based on filters at {@link AuditFilter}.
94      *
95      * @param auditFilter {@link AuditFilter} object with filters for search
96      * @param numRecords number of records to be collected
97      * @return list of {@link PolicyAudit} records
98      */
99     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter,
100             @NonNull Integer numRecords) {
101
102         auditFilter.setRecordNum(Math.min(numRecords, DEFAULT_MAX_RECORDS));
103
104         return getAuditRecords(dao, auditFilter);
105     }
106
107     /**
108      * Collect audit records based on filters at {@link AuditFilter}.
109      *
110      * @param auditFilter {@link AuditFilter} object with filters for search
111      * @return list of {@link PolicyAudit} records
112      */
113     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) {
114
115         return dao.getFiltered(JpaPolicyAudit.class, auditFilter)
116                     .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
117     }
118
119     /**
120      * Create a filter for looking for audit records.
121      * name - policy name
122      * version - policy version
123      * pdpGroup - PDP group that policy might be related
124      * action - type of action/operation realized on policy
125      * fromDate - start of period in case of time interval search
126      */
127     @Data
128     @Builder
129     public static class AuditFilter implements PfFilterParametersIntfc {
130         private String name;
131         private String version;
132         private AuditAction action;
133         private String pdpGroup;
134         private Instant fromDate;
135         private Instant toDate;
136         private int recordNum;
137         @Builder.Default
138         private String sortOrder = "DESC";
139
140         // initialized lazily, if not set via the builder
141         private Map<String, Object> filterMap;
142
143         /**
144          * Check if even still using build(), none of the params were provided.
145          *
146          * @return {@code true} if all empty/null; {@code false} otherwise.
147          */
148         public boolean isEmpty() {
149             return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null
150                     && toDate == null;
151         }
152
153         @Override
154         public Instant getStartTime() {
155             return fromDate;
156         }
157
158         @Override
159         public Instant getEndTime() {
160             return toDate;
161         }
162
163         @Override
164         public Map<String, Object> getFilterMap() {
165             if (filterMap != null) {
166                 return filterMap;
167             }
168
169             filterMap = new HashMap<>();
170
171             if (StringUtils.isNotBlank(pdpGroup)) {
172                 filterMap.put("pdpGroup", pdpGroup);
173             }
174
175             if (action != null) {
176                 filterMap.put("action", action);
177             }
178
179             return filterMap;
180         }
181     }
182 }