Java 17 Upgrade
[policy/models.git] / models-pap / src / main / java / org / onap / policy / models / pap / persistence / concepts / JpaPolicyAudit.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pap.persistence.concepts;
23
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;
42 import lombok.Data;
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;
59
60 /**
61  * Entity to keep the records on policy actions for audit.
62  *
63  * @author Adheli Tavares (adheli.tavares@est.tech)
64  *
65  */
66 @Entity
67 @Table(name = "JpaPolicyAudit", indexes = {
68     @Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")
69 })
70 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
71 @Data
72 @EqualsAndHashCode(callSuper = false)
73 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
74     @Serial
75     private static final long serialVersionUID = -2935734300607322191L;
76
77     @Id
78     @Column(name = "ID")
79     @GeneratedValue(strategy = GenerationType.TABLE, generator = "auditIdGen")
80     @TableGenerator(
81         name = "auditIdGen",
82         table = "audit_sequence",
83         pkColumnName = "SEQ_NAME",
84         valueColumnName = "SEQ_COUNT",
85         pkColumnValue = "SEQ_GEN")
86     private Long generatedId;
87
88     @Column(name = "name", length = 120)
89     @Pattern(regexp = PfKey.NAME_REGEXP)
90     private String name;
91
92     @Column(name = "version", length = 20)
93     @Pattern(regexp = PfKey.VERSION_REGEXP)
94     private String version;
95
96     @Column
97     private String pdpGroup;
98
99     @Column
100     private String pdpType;
101
102     @Column
103     @NotNull
104     private AuditAction action;
105
106     @Column
107     @Temporal(TemporalType.TIMESTAMP)
108     @NotNull
109     private Date timeStamp;
110
111     @Column
112     private String userName;
113
114     /**
115      * Default constructor.
116      */
117     public JpaPolicyAudit() {
118         this.setName(PfKey.NULL_KEY_NAME);
119         this.setVersion(PfKey.NULL_KEY_VERSION);
120     }
121
122     /**
123      * Constructor from an authorative.
124      *
125      * @param audit authorative model
126      */
127     public JpaPolicyAudit(PolicyAudit audit) {
128         fromAuthorative(audit);
129     }
130
131     /**
132      * Constructor as a copy.
133      *
134      * @param copyConcept original entity to be copied
135      */
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();
145     }
146
147     @Override
148     public int compareTo(PfConcept o) {
149         if (o == null) {
150             return -1;
151         }
152         if (this == o) {
153             return 0;
154         }
155         if (getClass() != o.getClass()) {
156             return getClass().getName().compareTo(o.getClass().getName());
157         }
158
159         final JpaPolicyAudit other = (JpaPolicyAudit) o;
160
161         // @formatter:off
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)
171                         .toComparison();
172         // @formatter:on
173     }
174
175     @Override
176     public PolicyAudit toAuthorative() {
177         var policyIdent = new ToscaConceptIdentifier(name, version);
178
179         // @formatter:off
180         return PolicyAudit.builder()
181                         .auditId(generatedId)
182                         .pdpGroup(pdpGroup)
183                         .pdpType(pdpType)
184                         .policy(policyIdent)
185                         .action(action)
186                         .timestamp(timeStamp == null ? null : timeStamp.toInstant())
187                         .user(userName)
188                         .build();
189         // @formatter:on
190     }
191
192     @Override
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());
198         } else {
199             this.setName(PfKey.NULL_KEY_NAME);
200             this.setVersion(PfKey.NULL_KEY_VERSION);
201         }
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();
209     }
210
211     @Override
212     public List<PfKey> getKeys() {
213         final List<PfKey> keyList = new ArrayList<>();
214         keyList.add(getKey());
215         return keyList;
216     }
217
218     @Override
219     public PfKey getKey() {
220         return new PfConceptKey(name, version);
221     }
222
223     @Override
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);
228     }
229
230     @Override
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);
235         }
236         return result;
237     }
238 }