2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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 = {@Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")})
67 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
69 @EqualsAndHashCode(callSuper = false)
70 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
71 private static final long serialVersionUID = -2935734300607322191L;
75 @GeneratedValue(strategy = GenerationType.TABLE, generator = "auditIdGen")
79 pkColumnName = "SEQ_NAME",
80 valueColumnName = "SEQ_COUNT",
81 pkColumnValue = "SEQ_GEN")
82 private Long generatedId;
84 @Column(name = "name", length = 120)
85 @Pattern(regexp = PfKey.NAME_REGEXP)
88 @Column(name = "version", length = 20)
89 @Pattern(regexp = PfKey.VERSION_REGEXP)
90 private String version;
93 private String pdpGroup;
96 private String pdpType;
100 private AuditAction action;
103 @Temporal(TemporalType.TIMESTAMP)
105 private Date timeStamp;
111 * Default constructor.
113 public JpaPolicyAudit() {
114 this.setName(PfKey.NULL_KEY_NAME);
115 this.setVersion(PfKey.NULL_KEY_VERSION);
119 * Constructor from an authorative.
121 * @param audit authorative model
123 public JpaPolicyAudit(PolicyAudit audit) {
124 fromAuthorative(audit);
128 * Constructor as a copy.
130 * @param copyConcept original entity to be copied
132 public JpaPolicyAudit(JpaPolicyAudit copyConcept) {
133 this.name = copyConcept.name;
134 this.version = copyConcept.version;
135 this.generatedId = copyConcept.generatedId;
136 this.pdpGroup = copyConcept.getPdpGroup();
137 this.pdpType = copyConcept.getPdpType();
138 this.action = copyConcept.getAction();
139 this.timeStamp = copyConcept.getTimeStamp();
140 this.user = copyConcept.getUser();
144 public int compareTo(PfConcept o) {
151 if (getClass() != o.getClass()) {
152 return getClass().getName().compareTo(o.getClass().getName());
155 final JpaPolicyAudit other = (JpaPolicyAudit) o;
158 return new CompareToBuilder()
159 .append(name, other.name)
160 .append(version, other.version)
161 .append(generatedId, other.generatedId)
162 .append(pdpGroup, other.pdpGroup)
163 .append(pdpType, other.pdpType)
164 .append(action, other.action)
165 .append(timeStamp, other.timeStamp)
166 .append(user, other.user)
172 public PolicyAudit toAuthorative() {
173 var policyIdent = new ToscaConceptIdentifier(name, version);
176 return PolicyAudit.builder()
177 .auditId(generatedId)
182 .timestamp(timeStamp == null ? null : timeStamp.toInstant())
189 public void fromAuthorative(PolicyAudit authorativeConcept) {
190 if (authorativeConcept.getPolicy() != null) {
191 final ToscaConceptIdentifier policy = authorativeConcept.getPolicy();
192 this.setName(policy.getName());
193 this.setVersion(policy.getVersion());
195 this.setName(PfKey.NULL_KEY_NAME);
196 this.setVersion(PfKey.NULL_KEY_VERSION);
198 this.setGeneratedId(authorativeConcept.getAuditId());
199 pdpGroup = authorativeConcept.getPdpGroup();
200 pdpType = authorativeConcept.getPdpType();
201 action = authorativeConcept.getAction();
202 timeStamp = authorativeConcept.getTimestamp() == null ? Date.from(Instant.now())
203 : Date.from(authorativeConcept.getTimestamp());
204 user = authorativeConcept.getUser();
209 public List<PfKey> getKeys() {
210 final List<PfKey> keyList = new ArrayList<>();
211 keyList.add(getKey());
216 public PfKey getKey() {
217 return new PfConceptKey(name, version);
221 public void clean() {
222 pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
223 pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
224 user = Assertions.validateStringParameter("user", user, PfReferenceKey.LOCAL_NAME_REGEXP);
228 public BeanValidationResult validate(@NonNull String fieldName) {
229 BeanValidationResult result = super.validate(fieldName);
230 if (PfKey.NULL_KEY_NAME.equals(name)) {
231 result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);