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 java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.List;
28 import javax.persistence.Column;
29 import javax.persistence.Entity;
30 import javax.persistence.GeneratedValue;
31 import javax.persistence.GenerationType;
32 import javax.persistence.Id;
33 import javax.persistence.Index;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.Table;
37 import javax.persistence.TableGenerator;
38 import javax.persistence.Temporal;
39 import javax.persistence.TemporalType;
40 import javax.validation.constraints.NotNull;
42 import lombok.EqualsAndHashCode;
43 import lombok.NonNull;
44 import org.apache.commons.lang3.builder.CompareToBuilder;
45 import org.onap.policy.common.parameters.BeanValidationResult;
46 import org.onap.policy.common.parameters.ValidationStatus;
47 import org.onap.policy.common.parameters.annotations.Pattern;
48 import org.onap.policy.common.utils.validation.Assertions;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.Validated;
55 import org.onap.policy.models.pap.concepts.PolicyAudit;
56 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60 * Entity to keep the records on policy actions for audit.
62 * @author Adheli Tavares (adheli.tavares@est.tech)
66 @Table(name = "JpaPolicyAudit", indexes = {
67 @Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
71 @EqualsAndHashCode(callSuper = false)
72 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
73 private static final long serialVersionUID = -2935734300607322191L;
77 @GeneratedValue(strategy = GenerationType.TABLE, generator = "auditIdGen")
80 table = "audit_sequence",
81 pkColumnName = "SEQ_NAME",
82 valueColumnName = "SEQ_COUNT",
83 pkColumnValue = "SEQ_GEN")
84 private Long generatedId;
86 @Column(name = "name", length = 120)
87 @Pattern(regexp = PfKey.NAME_REGEXP)
90 @Column(name = "version", length = 20)
91 @Pattern(regexp = PfKey.VERSION_REGEXP)
92 private String version;
95 private String pdpGroup;
98 private String pdpType;
102 private AuditAction action;
105 @Temporal(TemporalType.TIMESTAMP)
107 private Date timeStamp;
110 private String userName;
113 * Default constructor.
115 public JpaPolicyAudit() {
116 this.setName(PfKey.NULL_KEY_NAME);
117 this.setVersion(PfKey.NULL_KEY_VERSION);
121 * Constructor from an authorative.
123 * @param audit authorative model
125 public JpaPolicyAudit(PolicyAudit audit) {
126 fromAuthorative(audit);
130 * Constructor as a copy.
132 * @param copyConcept original entity to be copied
134 public JpaPolicyAudit(JpaPolicyAudit copyConcept) {
135 this.name = copyConcept.name;
136 this.version = copyConcept.version;
137 this.generatedId = copyConcept.generatedId;
138 this.pdpGroup = copyConcept.getPdpGroup();
139 this.pdpType = copyConcept.getPdpType();
140 this.action = copyConcept.getAction();
141 this.timeStamp = copyConcept.getTimeStamp();
142 this.userName = copyConcept.getUserName();
146 public int compareTo(PfConcept o) {
153 if (getClass() != o.getClass()) {
154 return getClass().getName().compareTo(o.getClass().getName());
157 final JpaPolicyAudit other = (JpaPolicyAudit) o;
160 return new CompareToBuilder()
161 .append(name, other.name)
162 .append(version, other.version)
163 .append(generatedId, other.generatedId)
164 .append(pdpGroup, other.pdpGroup)
165 .append(pdpType, other.pdpType)
166 .append(action, other.action)
167 .append(timeStamp, other.timeStamp)
168 .append(userName, other.userName)
174 public PolicyAudit toAuthorative() {
175 var policyIdent = new ToscaConceptIdentifier(name, version);
178 return PolicyAudit.builder()
179 .auditId(generatedId)
184 .timestamp(timeStamp == null ? null : timeStamp.toInstant())
191 public void fromAuthorative(PolicyAudit authorativeConcept) {
192 if (authorativeConcept.getPolicy() != null) {
193 final ToscaConceptIdentifier policy = authorativeConcept.getPolicy();
194 this.setName(policy.getName());
195 this.setVersion(policy.getVersion());
197 this.setName(PfKey.NULL_KEY_NAME);
198 this.setVersion(PfKey.NULL_KEY_VERSION);
200 this.setGeneratedId(authorativeConcept.getAuditId());
201 pdpGroup = authorativeConcept.getPdpGroup();
202 pdpType = authorativeConcept.getPdpType();
203 action = authorativeConcept.getAction();
204 timeStamp = authorativeConcept.getTimestamp() == null ? Date.from(Instant.now())
205 : Date.from(authorativeConcept.getTimestamp());
206 userName = authorativeConcept.getUser();
210 public List<PfKey> getKeys() {
211 final List<PfKey> keyList = new ArrayList<>();
212 keyList.add(getKey());
217 public PfKey getKey() {
218 return new PfConceptKey(name, version);
222 public void clean() {
223 pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
224 pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
225 userName = Assertions.validateStringParameter("user", userName, PfReferenceKey.LOCAL_NAME_REGEXP);
229 public BeanValidationResult validate(@NonNull String fieldName) {
230 BeanValidationResult result = super.validate(fieldName);
231 if (PfKey.NULL_KEY_NAME.equals(name)) {
232 result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);