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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pap.persistence.concepts;
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;
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;
51 * Entity to keep the records on policy actions for audit.
53 * @author Adheli Tavares (adheli.tavares@est.tech)
57 @Table(name = "JpaPolicyAudit", indexes = {@Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")})
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = false)
61 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
62 private static final long serialVersionUID = -2935734300607322191L;
67 @VerifyKey(versionNotNull = true)
68 private PfGeneratedIdKey key;
71 private String pdpGroup;
74 private String pdpType;
78 private AuditAction action;
81 @Temporal(TemporalType.TIMESTAMP)
83 private Date timeStamp;
89 * Default constructor.
91 public JpaPolicyAudit() {
92 key = new PfGeneratedIdKey();
96 * Constructor from an authorative.
98 * @param audit authorative model
100 public JpaPolicyAudit(PolicyAudit audit) {
101 fromAuthorative(audit);
105 * Constructor as a copy.
107 * @param copyConcept original entity to be copied
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();
119 public int compareTo(PfConcept o) {
126 if (getClass() != o.getClass()) {
127 return getClass().getName().compareTo(o.getClass().getName());
130 final JpaPolicyAudit other = (JpaPolicyAudit) o;
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)
145 public PolicyAudit toAuthorative() {
146 ToscaConceptIdentifier policyIdent = new ToscaConceptIdentifier(key.getName(), key.getVersion());
149 return PolicyAudit.builder()
150 .auditId(key.getGeneratedId())
155 .timestamp(timeStamp == null ? null : timeStamp.toInstant())
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());
167 key = new PfGeneratedIdKey();
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();
179 public List<PfKey> getKeys() {
180 return getKey().getKeys();
184 public void clean() {
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);