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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.pap.persistence.provider;
25 import java.time.Instant;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.stream.Collectors;
30 import javax.ws.rs.core.Response;
31 import lombok.Builder;
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;
44 * Provider for Policy Audit.
46 * @author Adheli Tavares (adheli.tavares@est.tech)
49 public class PolicyAuditProvider {
51 private static final Integer DEFAULT_MAX_RECORDS = 100;
54 * Create audit records.
56 * @param audits list of policy audit
58 public void createAuditRecords(@NonNull PfDao dao, @NonNull final List<PolicyAudit> audits) {
59 List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
61 var result = new BeanValidationResult("createAuditRecords", jpaAudits);
64 for (JpaPolicyAudit jpaAudit : jpaAudits) {
65 result.addResult(jpaAudit.validate(String.valueOf(count++)));
68 if (!result.isValid()) {
69 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
72 dao.createCollection(jpaAudits);
76 * Collect all audit records.
78 * @param numRecords number of records to be collected
79 * @return list of {@link PolicyAudit} records
81 public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull Integer numRecords) {
82 numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords;
85 return dao.getAll(JpaPolicyAudit.class, "timeStamp DESC", numRecords)
87 .map(JpaPolicyAudit::toAuthorative)
88 .collect(Collectors.toList());
93 * Collect audit records based on filters at {@link AuditFilter}.
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
99 public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter,
100 @NonNull Integer numRecords) {
102 auditFilter.setRecordNum(Math.min(numRecords, DEFAULT_MAX_RECORDS));
104 return getAuditRecords(dao, auditFilter);
108 * Collect audit records based on filters at {@link AuditFilter}.
110 * @param auditFilter {@link AuditFilter} object with filters for search
111 * @return list of {@link PolicyAudit} records
113 public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) {
115 return dao.getFiltered(JpaPolicyAudit.class, auditFilter)
116 .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList());
120 * Create a filter for looking for audit records.
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
129 public static class AuditFilter implements PfFilterParametersIntfc {
131 private String version;
132 private AuditAction action;
133 private String pdpGroup;
134 private Instant fromDate;
135 private Instant toDate;
136 private int recordNum;
138 private String sortOrder = "DESC";
140 // initialized lazily, if not set via the builder
141 private Map<String, Object> filterMap;
144 * Check if even still using build(), none of the params were provided.
146 * @return {@code true} if all empty/null; {@code false} otherwise.
148 public boolean isEmpty() {
149 return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null
154 public Instant getStartTime() {
159 public Instant getEndTime() {
164 public Map<String, Object> getFilterMap() {
165 if (filterMap != null) {
169 filterMap = new HashMap<>();
171 if (StringUtils.isNotBlank(pdpGroup)) {
172 filterMap.put("pdpGroup", pdpGroup);
175 if (action != null) {
176 filterMap.put("action", action);