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