Handling Policy deploy/undeploy audit 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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pap.persistence.concepts;
22
23 import java.time.Instant;
24 import java.util.Date;
25 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Index;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
33 import javax.persistence.Temporal;
34 import javax.persistence.TemporalType;
35 import javax.validation.constraints.NotNull;
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import org.apache.commons.lang3.builder.CompareToBuilder;
39 import org.onap.policy.common.utils.validation.Assertions;
40 import org.onap.policy.models.base.PfAuthorative;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfGeneratedIdKey;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
46 import org.onap.policy.models.pap.concepts.PolicyAudit;
47 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49
50 /**
51  * Entity to keep the records on policy actions for audit.
52  *
53  * @author Adheli Tavares (adheli.tavares@est.tech)
54  *
55  */
56 @Entity
57 @Table(name = "JpaPolicyAudit", indexes = {@Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")})
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @Data
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
62     private static final long serialVersionUID = -2935734300607322191L;
63
64     @EmbeddedId
65     @Column
66     @NotNull
67     @VerifyKey(versionNotNull = true)
68     private PfGeneratedIdKey key;
69
70     @Column
71     private String pdpGroup;
72
73     @Column
74     private String pdpType;
75
76     @Column
77     @NotNull
78     private AuditAction action;
79
80     @Column
81     @Temporal(TemporalType.TIMESTAMP)
82     @NotNull
83     private Date timeStamp;
84
85     @Column
86     private String user;
87
88     /**
89      * Default constructor.
90      */
91     public JpaPolicyAudit() {
92         key = new PfGeneratedIdKey();
93     }
94
95     /**
96      * Constructor from an authorative.
97      *
98      * @param audit authorative model
99      */
100     public JpaPolicyAudit(PolicyAudit audit) {
101         fromAuthorative(audit);
102     }
103
104     /**
105      * Constructor as a copy.
106      *
107      * @param copyConcept original entity to be copied
108      */
109     public JpaPolicyAudit(JpaPolicyAudit copyConcept) {
110         this.key = new PfGeneratedIdKey(copyConcept.getKey());
111         this.pdpGroup = copyConcept.getPdpGroup();
112         this.pdpType = copyConcept.getPdpType();
113         this.action = copyConcept.getAction();
114         this.timeStamp = copyConcept.getTimeStamp();
115         this.user = copyConcept.getUser();
116     }
117
118     @Override
119     public int compareTo(PfConcept o) {
120         if (o == null) {
121             return -1;
122         }
123         if (this == o) {
124             return 0;
125         }
126         if (getClass() != o.getClass()) {
127             return getClass().getName().compareTo(o.getClass().getName());
128         }
129
130         final JpaPolicyAudit other = (JpaPolicyAudit) o;
131
132         // @formatter:off
133         return new CompareToBuilder()
134                         .append(key, other.key)
135                         .append(pdpGroup, other.pdpGroup)
136                         .append(pdpType, other.pdpType)
137                         .append(action, other.action)
138                         .append(timeStamp, other.timeStamp)
139                         .append(user, other.user)
140                         .toComparison();
141         // @formatter:on
142     }
143
144     @Override
145     public PolicyAudit toAuthorative() {
146         ToscaConceptIdentifier policyIdent = new ToscaConceptIdentifier(key.getName(), key.getVersion());
147
148         // @formatter:off
149         return PolicyAudit.builder()
150                         .auditId(key.getGeneratedId())
151                         .pdpGroup(pdpGroup)
152                         .pdpType(pdpType)
153                         .policy(policyIdent)
154                         .action(action)
155                         .timestamp(timeStamp == null ? null : timeStamp.toInstant())
156                         .user(user)
157                         .build();
158         // @formatter:on
159     }
160
161     @Override
162     public void fromAuthorative(PolicyAudit authorativeConcept) {
163         if (authorativeConcept.getPolicy() != null) {
164             final ToscaConceptIdentifier policy = authorativeConcept.getPolicy();
165             key = new PfGeneratedIdKey(policy.getName(), policy.getVersion(), authorativeConcept.getAuditId());
166         } else {
167             key = new PfGeneratedIdKey();
168         }
169
170         pdpGroup = authorativeConcept.getPdpGroup();
171         pdpType = authorativeConcept.getPdpType();
172         action = authorativeConcept.getAction();
173         timeStamp = authorativeConcept.getTimestamp() == null ? Date.from(Instant.now())
174                 : Date.from(authorativeConcept.getTimestamp());
175         user = authorativeConcept.getUser();
176     }
177
178     @Override
179     public List<PfKey> getKeys() {
180         return getKey().getKeys();
181     }
182
183     @Override
184     public void clean() {
185         key.clean();
186
187         pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
188         pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
189         user = Assertions.validateStringParameter("user", user, PfReferenceKey.LOCAL_NAME_REGEXP);
190     }
191 }