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