X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-pdp%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fpdp%2Fpersistence%2Fconcepts%2FJpaPdp.java;h=fd594a99f75580dc25ab494b76e3a59091f9e173;hb=938005505883cf7a636a8840e20e3dc8a0ad9176;hp=0a9aa5874a66f2dc48ddff61f408aadd54bab682;hpb=50bc153c11472d90aa0f2a8ca9d8afeab0efb010;p=policy%2Fmodels.git diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java index 0a9aa5874..fd594a99f 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019, 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,30 +23,30 @@ package org.onap.policy.models.pdp.persistence.concepts; +import jakarta.persistence.Column; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.Table; +import jakarta.persistence.Temporal; +import jakarta.persistence.TemporalType; +import java.io.Serial; import java.io.Serializable; +import java.util.Date; import java.util.List; - -import javax.persistence.Column; -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.Table; - import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; - -import org.apache.commons.lang3.ObjectUtils; -import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.utils.validation.Assertions; +import org.apache.commons.lang3.builder.CompareToBuilder; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfReferenceKey; -import org.onap.policy.models.base.PfValidationMessage; -import org.onap.policy.models.base.PfValidationResult; -import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.base.validation.annotations.VerifyKey; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.enums.PdpHealthStatus; import org.onap.policy.models.pdp.enums.PdpState; @@ -62,20 +62,30 @@ import org.onap.policy.models.pdp.enums.PdpState; @Data @EqualsAndHashCode(callSuper = false) public class JpaPdp extends PfConcept implements PfAuthorative, Serializable { + @Serial private static final long serialVersionUID = -357224425637789775L; @EmbeddedId + @VerifyKey + @NotNull private PfReferenceKey key; @Column + @NotNull private PdpState pdpState; @Column + @NotNull private PdpHealthStatus healthy; @Column + @NotBlank private String message; + @Column(precision = 0) + @Temporal(TemporalType.TIMESTAMP) + private Date lastUpdate; + /** * The Default Constructor creates a {@link JpaPdp} object with a null key. */ @@ -100,10 +110,11 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl * @param healthy the health state of the PDP */ public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState, - @NonNull PdpHealthStatus healthy) { + @NonNull PdpHealthStatus healthy) { this.key = key; this.pdpState = pdpState; this.healthy = healthy; + this.lastUpdate = new Date(); } /** @@ -111,8 +122,13 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl * * @param copyConcept the concept to copy from */ - public JpaPdp(final JpaPdp copyConcept) { + public JpaPdp(@NonNull final JpaPdp copyConcept) { super(copyConcept); + this.key = new PfReferenceKey(copyConcept.key); + this.pdpState = copyConcept.pdpState; + this.healthy = copyConcept.healthy; + this.message = copyConcept.message; + this.lastUpdate = copyConcept.lastUpdate; } /** @@ -120,25 +136,26 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl * * @param authorativeConcept the authorative concept to copy from */ - public JpaPdp(final Pdp authorativeConcept) { + public JpaPdp(@NonNull final Pdp authorativeConcept) { this.fromAuthorative(authorativeConcept); } @Override public Pdp toAuthorative() { - Pdp pdp = new Pdp(); + var pdp = new Pdp(); pdp.setInstanceId(key.getLocalName()); pdp.setPdpState(pdpState); pdp.setHealthy(healthy); pdp.setMessage(message); + pdp.setLastUpdate(lastUpdate.toInstant()); return pdp; } @Override - public void fromAuthorative(final Pdp pdp) { - if (this.getKey().isNullKey()) { + public void fromAuthorative(@NonNull final Pdp pdp) { + if (this.key == null || this.getKey().isNullKey()) { this.setKey(new PfReferenceKey()); getKey().setLocalName(pdp.getInstanceId()); } @@ -146,6 +163,12 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl this.setPdpState(pdp.getPdpState()); this.setHealthy(pdp.getHealthy()); this.setMessage(pdp.getMessage()); + + if (pdp.getLastUpdate() == null) { + this.setLastUpdate(new Date()); + } else { + this.setLastUpdate(Date.from(pdp.getLastUpdate())); + } } @Override @@ -163,39 +186,13 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl } @Override - public PfValidationResult validate(final PfValidationResult resultIn) { - PfValidationResult result = resultIn; + public BeanValidationResult validate(@NonNull String fieldName) { + BeanValidationResult result = super.validate(fieldName); - if (key.isNullKey()) { - result.addValidationMessage( - new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); - } - - result = key.validate(result); - - if (key.getParentConceptKey().isNullKey()) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "parent of key is a null key")); - } + validateKeyNotNull(result, "parent of key", key.getParentConceptKey()); if (PfKey.NULL_KEY_NAME.equals(key.getParentLocalName())) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "local name of parent of key is null")); - } - - if (pdpState == null) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "PDP state may not be null")); - } - - if (healthy == null) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "PDP health status may not be null")); - } - - if (StringUtils.isBlank(message)) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "message may not be blank")); + addResult(result, "local name of parent of key", key.getParentLocalName(), IS_NULL); } return result; @@ -210,37 +207,19 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl return 0; } if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); + return getClass().getName().compareTo(otherConcept.getClass().getName()); } final JpaPdp other = (JpaPdp) otherConcept; - if (!key.equals(other.key)) { - return key.compareTo(other.key); - } - - int result = ObjectUtils.compare(pdpState, other.pdpState); - if (result != 0) { - return result; - } - - result = ObjectUtils.compare(healthy, other.healthy); - if (result != 0) { - return result; - } - - return ObjectUtils.compare(message, other.message); - } - - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - Assertions.instanceOf(target, JpaPdp.class); - - final JpaPdp copy = ((JpaPdp) target); - copy.setKey(new PfReferenceKey(key)); - copy.setPdpState(pdpState); - copy.setHealthy(healthy); - copy.setMessage(message); - return copy; + // @formatter:off + return new CompareToBuilder() + .append(this.key, other.key) + .append(this.pdpState, other.pdpState) + .append(this.healthy, other.healthy) + .append(this.message, other.message) + .append(this.lastUpdate, other.lastUpdate) + .toComparison(); + // @formatter:on } }