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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pap.persistence.concepts;
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;
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;
52 * Entity to keep the records on policy actions for audit.
54 * @author Adheli Tavares (adheli.tavares@est.tech)
58 @Table(name = "JpaPolicyAudit", indexes = {@Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")})
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
63 private static final long serialVersionUID = -2935734300607322191L;
68 @VerifyKey(versionNotNull = true)
69 private PfGeneratedIdKey key;
72 private String pdpGroup;
75 private String pdpType;
79 private AuditAction action;
82 @Temporal(TemporalType.TIMESTAMP)
84 private Date timeStamp;
90 * Default constructor.
92 public JpaPolicyAudit() {
93 key = new PfGeneratedIdKey();
97 * Constructor from an authorative.
99 * @param audit authorative model
101 public JpaPolicyAudit(PolicyAudit audit) {
102 fromAuthorative(audit);
106 * Constructor as a copy.
108 * @param copyConcept original entity to be copied
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();
120 public int compareTo(PfConcept o) {
127 if (getClass() != o.getClass()) {
128 return getClass().getName().compareTo(o.getClass().getName());
131 final JpaPolicyAudit other = (JpaPolicyAudit) o;
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)
146 public PolicyAudit toAuthorative() {
147 var policyIdent = new ToscaConceptIdentifier(key.getName(), key.getVersion());
150 return PolicyAudit.builder()
151 .auditId(key.getGeneratedId())
156 .timestamp(timeStamp == null ? null : timeStamp.toInstant())
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());
168 key = new PfGeneratedIdKey();
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();
180 public List<PfKey> getKeys() {
181 return getKey().getKeys();
185 public void clean() {
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);