2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pap.persistence.concepts;
24 import jakarta.persistence.Column;
25 import jakarta.persistence.Entity;
26 import jakarta.persistence.GeneratedValue;
27 import jakarta.persistence.GenerationType;
28 import jakarta.persistence.Id;
29 import jakarta.persistence.Index;
30 import jakarta.persistence.Inheritance;
31 import jakarta.persistence.InheritanceType;
32 import jakarta.persistence.Table;
33 import jakarta.persistence.TableGenerator;
34 import jakarta.persistence.Temporal;
35 import jakarta.persistence.TemporalType;
36 import jakarta.validation.constraints.NotNull;
37 import java.io.Serial;
38 import java.time.Instant;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
43 import lombok.EqualsAndHashCode;
44 import lombok.NonNull;
45 import org.apache.commons.lang3.builder.CompareToBuilder;
46 import org.onap.policy.common.parameters.BeanValidationResult;
47 import org.onap.policy.common.parameters.ValidationStatus;
48 import org.onap.policy.common.parameters.annotations.Pattern;
49 import org.onap.policy.common.utils.validation.Assertions;
50 import org.onap.policy.models.base.PfAuthorative;
51 import org.onap.policy.models.base.PfConcept;
52 import org.onap.policy.models.base.PfConceptKey;
53 import org.onap.policy.models.base.PfKey;
54 import org.onap.policy.models.base.PfReferenceKey;
55 import org.onap.policy.models.base.Validated;
56 import org.onap.policy.models.pap.concepts.PolicyAudit;
57 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
61 * Entity to keep the records on policy actions for audit.
63 * @author Adheli Tavares (adheli.tavares@est.tech)
67 @Table(name = "JpaPolicyAudit", indexes = {
68 @Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")
70 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
72 @EqualsAndHashCode(callSuper = false)
73 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
75 private static final long serialVersionUID = -2935734300607322191L;
79 @GeneratedValue(strategy = GenerationType.TABLE, generator = "auditIdGen")
82 table = "audit_sequence",
83 pkColumnName = "SEQ_NAME",
84 valueColumnName = "SEQ_COUNT",
85 pkColumnValue = "SEQ_GEN")
86 private Long generatedId;
88 @Column(name = "name", length = 120)
89 @Pattern(regexp = PfKey.NAME_REGEXP)
92 @Column(name = "version", length = 20)
93 @Pattern(regexp = PfKey.VERSION_REGEXP)
94 private String version;
97 private String pdpGroup;
100 private String pdpType;
104 private AuditAction action;
107 @Temporal(TemporalType.TIMESTAMP)
109 private Date timeStamp;
112 private String userName;
115 * Default constructor.
117 public JpaPolicyAudit() {
118 this.setName(PfKey.NULL_KEY_NAME);
119 this.setVersion(PfKey.NULL_KEY_VERSION);
123 * Constructor from an authorative.
125 * @param audit authorative model
127 public JpaPolicyAudit(PolicyAudit audit) {
128 fromAuthorative(audit);
132 * Constructor as a copy.
134 * @param copyConcept original entity to be copied
136 public JpaPolicyAudit(JpaPolicyAudit copyConcept) {
137 this.name = copyConcept.name;
138 this.version = copyConcept.version;
139 this.generatedId = copyConcept.generatedId;
140 this.pdpGroup = copyConcept.getPdpGroup();
141 this.pdpType = copyConcept.getPdpType();
142 this.action = copyConcept.getAction();
143 this.timeStamp = copyConcept.getTimeStamp();
144 this.userName = copyConcept.getUserName();
148 public int compareTo(PfConcept o) {
155 if (getClass() != o.getClass()) {
156 return getClass().getName().compareTo(o.getClass().getName());
159 final JpaPolicyAudit other = (JpaPolicyAudit) o;
162 return new CompareToBuilder()
163 .append(name, other.name)
164 .append(version, other.version)
165 .append(generatedId, other.generatedId)
166 .append(pdpGroup, other.pdpGroup)
167 .append(pdpType, other.pdpType)
168 .append(action, other.action)
169 .append(timeStamp, other.timeStamp)
170 .append(userName, other.userName)
176 public PolicyAudit toAuthorative() {
177 var policyIdent = new ToscaConceptIdentifier(name, version);
180 return PolicyAudit.builder()
181 .auditId(generatedId)
186 .timestamp(timeStamp == null ? null : timeStamp.toInstant())
193 public void fromAuthorative(PolicyAudit authorativeConcept) {
194 if (authorativeConcept.getPolicy() != null) {
195 final ToscaConceptIdentifier policy = authorativeConcept.getPolicy();
196 this.setName(policy.getName());
197 this.setVersion(policy.getVersion());
199 this.setName(PfKey.NULL_KEY_NAME);
200 this.setVersion(PfKey.NULL_KEY_VERSION);
202 this.setGeneratedId(authorativeConcept.getAuditId());
203 pdpGroup = authorativeConcept.getPdpGroup();
204 pdpType = authorativeConcept.getPdpType();
205 action = authorativeConcept.getAction();
206 timeStamp = authorativeConcept.getTimestamp() == null ? Date.from(Instant.now())
207 : Date.from(authorativeConcept.getTimestamp());
208 userName = authorativeConcept.getUser();
212 public List<PfKey> getKeys() {
213 final List<PfKey> keyList = new ArrayList<>();
214 keyList.add(getKey());
219 public PfKey getKey() {
220 return new PfConceptKey(name, version);
224 public void clean() {
225 pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
226 pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
227 userName = Assertions.validateStringParameter("user", userName, PfReferenceKey.LOCAL_NAME_REGEXP);
231 public BeanValidationResult validate(@NonNull String fieldName) {
232 BeanValidationResult result = super.validate(fieldName);
233 if (PfKey.NULL_KEY_NAME.equals(name)) {
234 result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);