Exposing the Audit provider through Models provider.
[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  * ================================================================================
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.models.pap.persistence.provider;
22
23 import java.time.Instant;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response;
29 import lombok.Builder;
30 import lombok.Data;
31 import lombok.NonNull;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.dao.PfDao;
36 import org.onap.policy.models.pap.concepts.PolicyAudit;
37 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
38 import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit;
39
40 /**
41  * Provider for Policy Audit.
42  *
43  * @author Adheli Tavares (adheli.tavares@est.tech)
44  *
45  */
46 public class PolicyAuditProvider {
47
48     private static final Integer DEFAULT_MAX_RECORDS = 100;
49     private static final String DESCENDING_ORDER = "DESC";
50
51     /**
52      * Create audit records.
53      *
54      * @param audits list of policy audit
55      */
56     public void createAuditRecords(@NonNull PfDao dao, @NonNull final List<PolicyAudit> audits) {
57         List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
58
59         BeanValidationResult result = new BeanValidationResult("createAuditRecords", jpaAudits);
60
61         int 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
70         dao.createCollection(jpaAudits);
71     }
72
73     /**
74      * Collect all audit records.
75      *
76      * @param numRecords number of records to be collected
77      * @return list of {@link PolicyAudit} records
78      */
79     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull Integer numRecords) {
80         numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords;
81
82         // @formatter:off
83         return dao.getAll(JpaPolicyAudit.class, "timeStamp DESC", numRecords)
84                 .stream()
85                 .map(JpaPolicyAudit::toAuthorative)
86                 .collect(Collectors.toList());
87         // @formatter:on
88     }
89
90     /**
91      * Collect audit records based on filters at {@link AuditFilter}.
92      *
93      * @param auditFilter {@link AuditFilter} object with filters for search
94      * @param numRecords number of records to be collected
95      * @return list of {@link PolicyAudit} records
96      */
97     public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter,
98             @NonNull Integer numRecords) {
99         numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords;
100
101         Map<String, Object> filter = new HashMap<>();
102         if (StringUtils.isNotBlank(auditFilter.getPdpGroup())) {
103             filter.put("pdpGroup", auditFilter.getPdpGroup());
104         }
105
106         if (auditFilter.getAction() != null) {
107             filter.put("action", auditFilter.getAction());
108         }
109
110         // @formatter:off
111         return dao.getFiltered(JpaPolicyAudit.class,
112                 auditFilter.getName(), auditFilter.getVersion(),
113                 auditFilter.getFromDate(), auditFilter.getToDate(),
114                 filter, DESCENDING_ORDER, numRecords)
115                 .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
116         // @formatter:on
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 {
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
137         /**
138          * Check if even still using build(), none of the params were provided.
139          *
140          * @return {@code true} if all empty/null; {@code false} otherwise.
141          */
142         public boolean isEmpty() {
143             return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null
144                     && toDate == null;
145         }
146     }
147 }