2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019, 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.pdp.persistence.concepts;
26 import jakarta.persistence.Column;
27 import jakarta.persistence.EmbeddedId;
28 import jakarta.persistence.Entity;
29 import jakarta.persistence.Inheritance;
30 import jakarta.persistence.InheritanceType;
31 import jakarta.persistence.Table;
32 import jakarta.persistence.Temporal;
33 import jakarta.persistence.TemporalType;
34 import java.io.Serial;
35 import java.io.Serializable;
36 import java.util.Date;
37 import java.util.List;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.builder.CompareToBuilder;
42 import org.onap.policy.common.parameters.BeanValidationResult;
43 import org.onap.policy.common.parameters.annotations.NotBlank;
44 import org.onap.policy.common.parameters.annotations.NotNull;
45 import org.onap.policy.models.base.PfAuthorative;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfReferenceKey;
49 import org.onap.policy.models.base.validation.annotations.VerifyKey;
50 import org.onap.policy.models.pdp.concepts.Pdp;
51 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
52 import org.onap.policy.models.pdp.enums.PdpState;
55 * Class to represent a PDP in the database.
57 * @author Liam Fallon (liam.fallon@est.tech)
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @EqualsAndHashCode(callSuper = false)
64 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
66 private static final long serialVersionUID = -357224425637789775L;
71 private PfReferenceKey key;
75 private PdpState pdpState;
79 private PdpHealthStatus healthy;
83 private String message;
85 @Column(precision = 0)
86 @Temporal(TemporalType.TIMESTAMP)
87 private Date lastUpdate;
90 * The Default Constructor creates a {@link JpaPdp} object with a null key.
93 this(new PfReferenceKey());
97 * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
101 public JpaPdp(@NonNull final PfReferenceKey key) {
102 this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
106 * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
109 * @param pdpState the state of the PDP
110 * @param healthy the health state of the PDP
112 public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
113 @NonNull PdpHealthStatus healthy) {
115 this.pdpState = pdpState;
116 this.healthy = healthy;
117 this.lastUpdate = new Date();
123 * @param copyConcept the concept to copy from
125 public JpaPdp(@NonNull final JpaPdp copyConcept) {
127 this.key = new PfReferenceKey(copyConcept.key);
128 this.pdpState = copyConcept.pdpState;
129 this.healthy = copyConcept.healthy;
130 this.message = copyConcept.message;
131 this.lastUpdate = copyConcept.lastUpdate;
135 * Authorative constructor.
137 * @param authorativeConcept the authorative concept to copy from
139 public JpaPdp(@NonNull final Pdp authorativeConcept) {
140 this.fromAuthorative(authorativeConcept);
144 public Pdp toAuthorative() {
147 pdp.setInstanceId(key.getLocalName());
148 pdp.setPdpState(pdpState);
149 pdp.setHealthy(healthy);
150 pdp.setMessage(message);
151 pdp.setLastUpdate(lastUpdate.toInstant());
157 public void fromAuthorative(@NonNull final Pdp pdp) {
158 if (this.key == null || this.getKey().isNullKey()) {
159 this.setKey(new PfReferenceKey());
160 getKey().setLocalName(pdp.getInstanceId());
163 this.setPdpState(pdp.getPdpState());
164 this.setHealthy(pdp.getHealthy());
165 this.setMessage(pdp.getMessage());
167 if (pdp.getLastUpdate() == null) {
168 this.setLastUpdate(new Date());
170 this.setLastUpdate(Date.from(pdp.getLastUpdate()));
175 public List<PfKey> getKeys() {
176 return getKey().getKeys();
180 public void clean() {
183 if (message != null) {
184 message = message.trim();
189 public BeanValidationResult validate(@NonNull String fieldName) {
190 BeanValidationResult result = super.validate(fieldName);
192 validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
194 if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
195 addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
202 public int compareTo(final PfConcept otherConcept) {
203 if (otherConcept == null) {
206 if (this == otherConcept) {
209 if (getClass() != otherConcept.getClass()) {
210 return getClass().getName().compareTo(otherConcept.getClass().getName());
213 final JpaPdp other = (JpaPdp) otherConcept;
216 return new CompareToBuilder()
217 .append(this.key, other.key)
218 .append(this.pdpState, other.pdpState)
219 .append(this.healthy, other.healthy)
220 .append(this.message, other.message)
221 .append(this.lastUpdate, other.lastUpdate)