2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 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 java.io.Serializable;
27 import java.util.Date;
28 import java.util.List;
29 import javax.persistence.Column;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
35 import javax.persistence.Temporal;
36 import javax.persistence.TemporalType;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.lang3.builder.CompareToBuilder;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.common.parameters.annotations.NotBlank;
43 import org.onap.policy.common.parameters.annotations.NotNull;
44 import org.onap.policy.models.base.PfAuthorative;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfReferenceKey;
48 import org.onap.policy.models.base.validation.annotations.VerifyKey;
49 import org.onap.policy.models.pdp.concepts.Pdp;
50 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
51 import org.onap.policy.models.pdp.enums.PdpState;
54 * Class to represent a PDP in the database.
56 * @author Liam Fallon (liam.fallon@est.tech)
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializable {
64 private static final long serialVersionUID = -357224425637789775L;
69 private PfReferenceKey key;
73 private PdpState pdpState;
77 private PdpHealthStatus healthy;
81 private String message;
83 @Column(precision = 0)
84 @Temporal(TemporalType.TIMESTAMP)
85 private Date lastUpdate;
88 * The Default Constructor creates a {@link JpaPdp} object with a null key.
91 this(new PfReferenceKey());
95 * The Key Constructor creates a {@link JpaPdp} object with the given concept key.
99 public JpaPdp(@NonNull final PfReferenceKey key) {
100 this(key, PdpState.PASSIVE, PdpHealthStatus.UNKNOWN);
104 * The Key Constructor creates a {@link JpaPdp} object with all mandatory fields.
107 * @param pdpState the state of the PDP
108 * @param healthy the health state of the PDP
110 public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
111 @NonNull PdpHealthStatus healthy) {
113 this.pdpState = pdpState;
114 this.healthy = healthy;
115 this.lastUpdate = new Date();
121 * @param copyConcept the concept to copy from
123 public JpaPdp(@NonNull final JpaPdp copyConcept) {
125 this.key = new PfReferenceKey(copyConcept.key);
126 this.pdpState = copyConcept.pdpState;
127 this.healthy = copyConcept.healthy;
128 this.message = copyConcept.message;
129 this.lastUpdate = copyConcept.lastUpdate;
133 * Authorative constructor.
135 * @param authorativeConcept the authorative concept to copy from
137 public JpaPdp(@NonNull final Pdp authorativeConcept) {
138 this.fromAuthorative(authorativeConcept);
142 public Pdp toAuthorative() {
145 pdp.setInstanceId(key.getLocalName());
146 pdp.setPdpState(pdpState);
147 pdp.setHealthy(healthy);
148 pdp.setMessage(message);
149 pdp.setLastUpdate(lastUpdate.toInstant());
155 public void fromAuthorative(@NonNull final Pdp pdp) {
156 if (this.key == null || this.getKey().isNullKey()) {
157 this.setKey(new PfReferenceKey());
158 getKey().setLocalName(pdp.getInstanceId());
161 this.setPdpState(pdp.getPdpState());
162 this.setHealthy(pdp.getHealthy());
163 this.setMessage(pdp.getMessage());
165 if (pdp.getLastUpdate() == null) {
166 this.setLastUpdate(new Date());
168 this.setLastUpdate(Date.from(pdp.getLastUpdate()));
173 public List<PfKey> getKeys() {
174 return getKey().getKeys();
178 public void clean() {
181 if (message != null) {
182 message = message.trim();
187 public BeanValidationResult validate(@NonNull String fieldName) {
188 BeanValidationResult result = super.validate(fieldName);
190 validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
192 if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) {
193 addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL);
200 public int compareTo(final PfConcept otherConcept) {
201 if (otherConcept == null) {
204 if (this == otherConcept) {
207 if (getClass() != otherConcept.getClass()) {
208 return getClass().getName().compareTo(otherConcept.getClass().getName());
211 final JpaPdp other = (JpaPdp) otherConcept;
214 return new CompareToBuilder()
215 .append(this.key, other.key)
216 .append(this.pdpState, other.pdpState)
217 .append(this.healthy, other.healthy)
218 .append(this.message, other.message)
219 .append(this.lastUpdate, other.lastUpdate)