diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2021-05-11 14:35:20 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2021-05-28 15:45:52 +0100 |
commit | 09e2bc57b947fb71c4dde9a8c0ded52f695abda1 (patch) | |
tree | f2259c0b5a0968792d6f3fa51bc4c9ae31ae2756 /models-pap/src/main | |
parent | e6bc3b407aadcab3b54074e39afc4c4dfe142b06 (diff) |
Handling Policy deploy/undeploy audit models
Added a new Entity to store information when a Pdp Policy is deployed or
undeployed and its create/retrieve methods.
Change-Id: I35b3608c878bbfbbee0a99a124d100a48fe08131
Issue-ID: POLICY-2899
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'models-pap/src/main')
3 files changed, 378 insertions, 0 deletions
diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PolicyAudit.java b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PolicyAudit.java new file mode 100644 index 000000000..765d246a6 --- /dev/null +++ b/models-pap/src/main/java/org/onap/policy/models/pap/concepts/PolicyAudit.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.pap.concepts; + +import java.time.Instant; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +/** + * Audit info on policy actions. + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class PolicyAudit { + + public enum AuditAction { + DEPLOYMENT, UNDEPLOYMENT + } + + private Long auditId; + private String pdpGroup; + private String pdpType; + private ToscaConceptIdentifier policy; + private AuditAction action; + private Instant timestamp; + private String user; +} diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/concepts/JpaPolicyAudit.java b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/concepts/JpaPolicyAudit.java new file mode 100644 index 000000000..b31af8ea0 --- /dev/null +++ b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/concepts/JpaPolicyAudit.java @@ -0,0 +1,191 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.pap.persistence.concepts; + +import java.time.Instant; +import java.util.Date; +import java.util.List; +import javax.persistence.Column; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.Index; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.validation.constraints.NotNull; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.apache.commons.lang3.builder.CompareToBuilder; +import org.onap.policy.common.utils.validation.Assertions; +import org.onap.policy.models.base.PfAuthorative; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfGeneratedIdKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.validation.annotations.VerifyKey; +import org.onap.policy.models.pap.concepts.PolicyAudit; +import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +/** + * Entity to keep the records on policy actions for audit. + * + * @author Adheli Tavares (adheli.tavares@est.tech) + * + */ +@Entity +@Table(name = "JpaPolicyAudit", indexes = {@Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")}) +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Data +@EqualsAndHashCode(callSuper = false) +public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> { + private static final long serialVersionUID = -2935734300607322191L; + + @EmbeddedId + @Column + @NotNull + @VerifyKey(versionNotNull = true) + private PfGeneratedIdKey key; + + @Column + private String pdpGroup; + + @Column + private String pdpType; + + @Column + @NotNull + private AuditAction action; + + @Column + @Temporal(TemporalType.TIMESTAMP) + @NotNull + private Date timeStamp; + + @Column + private String user; + + /** + * Default constructor. + */ + public JpaPolicyAudit() { + key = new PfGeneratedIdKey(); + } + + /** + * Constructor from an authorative. + * + * @param audit authorative model + */ + public JpaPolicyAudit(PolicyAudit audit) { + fromAuthorative(audit); + } + + /** + * Constructor as a copy. + * + * @param copyConcept original entity to be copied + */ + public JpaPolicyAudit(JpaPolicyAudit copyConcept) { + this.key = new PfGeneratedIdKey(copyConcept.getKey()); + this.pdpGroup = copyConcept.getPdpGroup(); + this.pdpType = copyConcept.getPdpType(); + this.action = copyConcept.getAction(); + this.timeStamp = copyConcept.getTimeStamp(); + this.user = copyConcept.getUser(); + } + + @Override + public int compareTo(PfConcept o) { + if (o == null) { + return -1; + } + if (this == o) { + return 0; + } + if (getClass() != o.getClass()) { + return getClass().getName().compareTo(o.getClass().getName()); + } + + final JpaPolicyAudit other = (JpaPolicyAudit) o; + + // @formatter:off + return new CompareToBuilder() + .append(key, other.key) + .append(pdpGroup, other.pdpGroup) + .append(pdpType, other.pdpType) + .append(action, other.action) + .append(timeStamp, other.timeStamp) + .append(user, other.user) + .toComparison(); + // @formatter:on + } + + @Override + public PolicyAudit toAuthorative() { + ToscaConceptIdentifier policyIdent = new ToscaConceptIdentifier(key.getName(), key.getVersion()); + + // @formatter:off + return PolicyAudit.builder() + .auditId(key.getGeneratedId()) + .pdpGroup(pdpGroup) + .pdpType(pdpType) + .policy(policyIdent) + .action(action) + .timestamp(timeStamp == null ? null : timeStamp.toInstant()) + .user(user) + .build(); + // @formatter:on + } + + @Override + public void fromAuthorative(PolicyAudit authorativeConcept) { + if (authorativeConcept.getPolicy() != null) { + final ToscaConceptIdentifier policy = authorativeConcept.getPolicy(); + key = new PfGeneratedIdKey(policy.getName(), policy.getVersion(), authorativeConcept.getAuditId()); + } else { + key = new PfGeneratedIdKey(); + } + + pdpGroup = authorativeConcept.getPdpGroup(); + pdpType = authorativeConcept.getPdpType(); + action = authorativeConcept.getAction(); + timeStamp = authorativeConcept.getTimestamp() == null ? Date.from(Instant.now()) + : Date.from(authorativeConcept.getTimestamp()); + user = authorativeConcept.getUser(); + } + + @Override + public List<PfKey> getKeys() { + return getKey().getKeys(); + } + + @Override + public void clean() { + key.clean(); + + pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP); + pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP); + user = Assertions.validateStringParameter("user", user, PfReferenceKey.LOCAL_NAME_REGEXP); + } +} diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java new file mode 100644 index 000000000..c117beefa --- /dev/null +++ b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.pap.persistence.provider; + +import java.time.Instant; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import javax.ws.rs.core.Response; +import lombok.Builder; +import lombok.Data; +import lombok.NonNull; +import org.apache.commons.lang3.StringUtils; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.pap.concepts.PolicyAudit; +import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction; +import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit; + +/** + * Provider for Policy Audit. + * + * @author Adheli Tavares (adheli.tavares@est.tech) + * + */ +public class PolicyAuditProvider { + + private static final Integer DEFAULT_MAX_RECORDS = 100; + private static final String DESCENDING_ORDER = "DESC"; + + /** + * Create audit records. + * + * @param audits list of policy audit + */ + public void createAuditRecords(@NonNull PfDao dao, @NonNull final List<PolicyAudit> audits) { + List<JpaPolicyAudit> jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList()); + + BeanValidationResult result = new BeanValidationResult("createAuditRecords", jpaAudits); + + int count = 0; + for (JpaPolicyAudit jpaAudit: jpaAudits) { + result.addResult(jpaAudit.validate(String.valueOf(count++))); + } + + if (!result.isValid()) { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult()); + } + + dao.createCollection(jpaAudits); + } + + /** + * Collect all audit records. + * + * @param numRecords number of records to be collected + * @return list of {@link PolicyAudit} records + */ + public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull Integer numRecords) { + numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords; + + // @formatter:off + return dao.getAll(JpaPolicyAudit.class, "timeStamp DESC", numRecords) + .stream() + .map(JpaPolicyAudit::toAuthorative) + .collect(Collectors.toList()); + // @formatter:on + } + + /** + * Collect audit records based on filters at {@link AuditFilter}. + * + * @param auditFilter {@link AuditFilter} object with filters for search + * @param numRecords number of records to be collected + * @return list of {@link PolicyAudit} records + */ + public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter, + @NonNull Integer numRecords) { + numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords; + + Map<String, Object> filter = new HashMap<>(); + if (StringUtils.isNotBlank(auditFilter.getPdpGroup())) { + filter.put("pdpGroup", auditFilter.getPdpGroup()); + } + + if (auditFilter.getAction() != null) { + filter.put("action", auditFilter.getAction()); + } + + // @formatter:off + return dao.getFiltered(JpaPolicyAudit.class, + auditFilter.getName(), auditFilter.getVersion(), + auditFilter.getFromDate(), auditFilter.getToDate(), + filter, DESCENDING_ORDER, numRecords) + .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList()); + // @formatter:on + } + + /** + * Create a filter for looking for audit records. + * name - policy name + * version - policy version + * pdpGroup - PDP group that policy might be related + * action - type of action/operation realized on policy + * fromDate - start of period in case of time interval search + */ + @Data + @Builder + protected static class AuditFilter { + private String name; + private String version; + private AuditAction action; + private String pdpGroup; + private Instant fromDate; + private Instant toDate; + } +} |