Fix sonar in policy/models
[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 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.Date;
26 import java.util.List;
27 import javax.persistence.Column;
28 import javax.persistence.EmbeddedId;
29 import javax.persistence.Entity;
30 import javax.persistence.Index;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.Table;
34 import javax.persistence.Temporal;
35 import javax.persistence.TemporalType;
36 import javax.validation.constraints.NotNull;
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import org.apache.commons.lang3.builder.CompareToBuilder;
40 import org.onap.policy.common.utils.validation.Assertions;
41 import org.onap.policy.models.base.PfAuthorative;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfGeneratedIdKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfReferenceKey;
46 import org.onap.policy.models.base.validation.annotations.VerifyKey;
47 import org.onap.policy.models.pap.concepts.PolicyAudit;
48 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50
51 /**
52  * Entity to keep the records on policy actions for audit.
53  *
54  * @author Adheli Tavares (adheli.tavares@est.tech)
55  *
56  */
57 @Entity
58 @Table(name = "JpaPolicyAudit", indexes = {@Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")})
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @Data
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
63     private static final long serialVersionUID = -2935734300607322191L;
64
65     @EmbeddedId
66     @Column
67     @NotNull
68     @VerifyKey(versionNotNull = true)
69     private PfGeneratedIdKey key;
70
71     @Column
72     private String pdpGroup;
73
74     @Column
75     private String pdpType;
76
77     @Column
78     @NotNull
79     private AuditAction action;
80
81     @Column
82     @Temporal(TemporalType.TIMESTAMP)
83     @NotNull
84     private Date timeStamp;
85
86     @Column
87     private String user;
88
89     /**
90      * Default constructor.
91      */
92     public JpaPolicyAudit() {
93         key = new PfGeneratedIdKey();
94     }
95
96     /**
97      * Constructor from an authorative.
98      *
99      * @param audit authorative model
100      */
101     public JpaPolicyAudit(PolicyAudit audit) {
102         fromAuthorative(audit);
103     }
104
105     /**
106      * Constructor as a copy.
107      *
108      * @param copyConcept original entity to be copied
109      */
110     public JpaPolicyAudit(JpaPolicyAudit copyConcept) {
111         this.key = new PfGeneratedIdKey(copyConcept.getKey());
112         this.pdpGroup = copyConcept.getPdpGroup();
113         this.pdpType = copyConcept.getPdpType();
114         this.action = copyConcept.getAction();
115         this.timeStamp = copyConcept.getTimeStamp();
116         this.user = copyConcept.getUser();
117     }
118
119     @Override
120     public int compareTo(PfConcept o) {
121         if (o == null) {
122             return -1;
123         }
124         if (this == o) {
125             return 0;
126         }
127         if (getClass() != o.getClass()) {
128             return getClass().getName().compareTo(o.getClass().getName());
129         }
130
131         final JpaPolicyAudit other = (JpaPolicyAudit) o;
132
133         // @formatter:off
134         return new CompareToBuilder()
135                         .append(key, other.key)
136                         .append(pdpGroup, other.pdpGroup)
137                         .append(pdpType, other.pdpType)
138                         .append(action, other.action)
139                         .append(timeStamp, other.timeStamp)
140                         .append(user, other.user)
141                         .toComparison();
142         // @formatter:on
143     }
144
145     @Override
146     public PolicyAudit toAuthorative() {
147         var policyIdent = new ToscaConceptIdentifier(key.getName(), key.getVersion());
148
149         // @formatter:off
150         return PolicyAudit.builder()
151                         .auditId(key.getGeneratedId())
152                         .pdpGroup(pdpGroup)
153                         .pdpType(pdpType)
154                         .policy(policyIdent)
155                         .action(action)
156                         .timestamp(timeStamp == null ? null : timeStamp.toInstant())
157                         .user(user)
158                         .build();
159         // @formatter:on
160     }
161
162     @Override
163     public void fromAuthorative(PolicyAudit authorativeConcept) {
164         if (authorativeConcept.getPolicy() != null) {
165             final ToscaConceptIdentifier policy = authorativeConcept.getPolicy();
166             key = new PfGeneratedIdKey(policy.getName(), policy.getVersion(), authorativeConcept.getAuditId());
167         } else {
168             key = new PfGeneratedIdKey();
169         }
170
171         pdpGroup = authorativeConcept.getPdpGroup();
172         pdpType = authorativeConcept.getPdpType();
173         action = authorativeConcept.getAction();
174         timeStamp = authorativeConcept.getTimestamp() == null ? Date.from(Instant.now())
175                 : Date.from(authorativeConcept.getTimestamp());
176         user = authorativeConcept.getUser();
177     }
178
179     @Override
180     public List<PfKey> getKeys() {
181         return getKey().getKeys();
182     }
183
184     @Override
185     public void clean() {
186         key.clean();
187
188         pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
189         pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
190         user = Assertions.validateStringParameter("user", user, PfReferenceKey.LOCAL_NAME_REGEXP);
191     }
192 }