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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pap.persistence.provider;
23 import java.time.Instant;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response;
29 import lombok.Builder;
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;
41 * Provider for Policy Audit.
43 * @author Adheli Tavares (adheli.tavares@est.tech)
46 public class PolicyAuditProvider {
48 private static final Integer DEFAULT_MAX_RECORDS = 100;
49 private static final String DESCENDING_ORDER = "DESC";
52 * Create audit records.
54 * @param audits list of policy audit
56 public void createAuditRecords(@NonNull PfDao dao, @NonNull final List<PolicyAudit> audits) {
57 List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList());
59 BeanValidationResult result = new BeanValidationResult("createAuditRecords", jpaAudits);
62 for (JpaPolicyAudit jpaAudit : jpaAudits) {
63 result.addResult(jpaAudit.validate(String.valueOf(count++)));
66 if (!result.isValid()) {
67 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult());
70 dao.createCollection(jpaAudits);
74 * Collect all audit records.
76 * @param numRecords number of records to be collected
77 * @return list of {@link PolicyAudit} records
79 public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull Integer numRecords) {
80 numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords;
83 return dao.getAll(JpaPolicyAudit.class, "timeStamp DESC", numRecords)
85 .map(JpaPolicyAudit::toAuthorative)
86 .collect(Collectors.toList());
91 * Collect audit records based on filters at {@link AuditFilter}.
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
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;
101 Map<String, Object> filter = new HashMap<>();
102 if (StringUtils.isNotBlank(auditFilter.getPdpGroup())) {
103 filter.put("pdpGroup", auditFilter.getPdpGroup());
106 if (auditFilter.getAction() != null) {
107 filter.put("action", auditFilter.getAction());
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());
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 {
131 private String version;
132 private AuditAction action;
133 private String pdpGroup;
134 private Instant fromDate;
135 private Instant toDate;
138 * Check if even still using build(), none of the params were provided.
140 * @return {@code true} if all empty/null; {@code false} otherwise.
142 public boolean isEmpty() {
143 return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null