X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-tosca%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Ftosca%2Fsimple%2Fconcepts%2FJpaToscaEntityType.java;h=b9acde26cc47762c57877bbcc4a85618958591cd;hb=e53df8d3f8ab0464b0876bdb339fa91dc9085cd2;hp=a39515b4960ebe55b8e91aafdee8495c6a518325;hpb=69bc7db0edc751d3936b92c4bdf1ee74dfa4da57;p=policy%2Fmodels.git diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java index a39515b49..b9acde26c 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020,2022-2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,34 +21,33 @@ package org.onap.policy.models.tosca.simple.concepts; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; - import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; +import javax.persistence.Lob; import javax.persistence.MappedSuperclass; - +import javax.ws.rs.core.Response; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; - import org.apache.commons.lang3.ObjectUtils; -import org.onap.policy.common.utils.validation.Assertions; -import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfUtils; -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.tosca.authorative.concepts.ToscaEntity; /** @@ -59,23 +59,28 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; public class JpaToscaEntityType extends PfConcept implements PfAuthorative { private static final long serialVersionUID = -1330661834220739393L; + private static final StandardCoder STANDARD_CODER = new StandardCoder(); + @EmbeddedId + @VerifyKey + @NotNull private PfConceptKey key; // @formatter:off @Column @AttributeOverrides({ - @AttributeOverride(name = "name", - column = @Column(name = "derived_from_name")), - @AttributeOverride(name = "version", - column = @Column(name = "derived_from_version")) - }) + @AttributeOverride(name = "name", column = @Column(name = "derived_from_name")), + @AttributeOverride(name = "version", column = @Column(name = "derived_from_version")) + }) + @VerifyKey private PfConceptKey derivedFrom; @ElementCollection - private Map metadata; + @Lob + private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> metadata; @Column + @NotBlank private String description; private transient T toscaEntity; @@ -104,6 +109,10 @@ public class JpaToscaEntityType extends PfConcept impleme */ public JpaToscaEntityType(final JpaToscaEntityType copyConcept) { super(copyConcept); + this.key = new PfConceptKey(copyConcept.key); + this.derivedFrom = (copyConcept.derivedFrom != null ? new PfConceptKey(copyConcept.derivedFrom) : null); + this.metadata = (copyConcept.metadata != null ? new TreeMap<>(copyConcept.metadata) : null); + this.description = copyConcept.description; } /** @@ -121,22 +130,14 @@ public class JpaToscaEntityType extends PfConcept impleme toscaEntity.setVersion(getKey().getVersion()); if (derivedFrom != null) { - toscaEntity.setDerivedFrom(derivedFrom.getId()); + toscaEntity.setDerivedFrom(derivedFrom.getName()); } if (description != null) { toscaEntity.setDescription(description); } - if (metadata != null) { - Map metadataMap = new LinkedHashMap<>(); - - for (Entry entry : metadata.entrySet()) { - metadataMap.put(entry.getKey(), entry.getValue()); - } - - toscaEntity.setMetadata(metadataMap); - } + toscaEntity.setMetadata(PfUtils.mapMap(metadata, this::deserializeMetadataValue)); return toscaEntity; } @@ -153,9 +154,8 @@ public class JpaToscaEntityType extends PfConcept impleme key.setVersion(toscaEntity.getVersion()); } - if (toscaEntity.getDerivedFrom() != null) { - // CHeck if the derived from field contains a name-version ID + // Check if the derived from field contains a name-version ID if (toscaEntity.getDerivedFrom().contains(":")) { derivedFrom = new PfConceptKey(toscaEntity.getDerivedFrom()); } else { @@ -167,13 +167,7 @@ public class JpaToscaEntityType extends PfConcept impleme description = toscaEntity.getDescription(); } - if (toscaEntity.getMetadata() != null) { - metadata = new LinkedHashMap<>(); - - for (Entry metadataEntry : toscaEntity.getMetadata().entrySet()) { - metadata.put(metadataEntry.getKey(), metadataEntry.getValue()); - } - } + metadata = PfUtils.mapMap(toscaEntity.getMetadata(), this::serializeMetadataValue); } @Override @@ -202,43 +196,6 @@ public class JpaToscaEntityType extends PfConcept impleme description = (description != null ? description.trim() : null); } - @Override - public PfValidationResult validate(PfValidationResult resultIn) { - PfValidationResult result = resultIn; - - if (key.isNullKey()) { - result.addValidationMessage( - new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key")); - } - - result = key.validate(result); - - if (derivedFrom != null && derivedFrom.isNullKey()) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "derived from key is a null key")); - } - - if (metadata != null) { - for (Entry metadataEntry : metadata.entrySet()) { - if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property metadata key may not be null")); - } - if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property metadata value may not be null")); - } - } - } - - if (description != null && description.trim().length() == 0) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "property description may not be blank")); - } - - return result; - } - @Override public int compareTo(final PfConcept otherConcept) { if (otherConcept == null) { @@ -248,21 +205,23 @@ public class JpaToscaEntityType extends PfConcept impleme return 0; } if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); + return getClass().getName().compareTo(otherConcept.getClass().getName()); } @SuppressWarnings("unchecked") final JpaToscaEntityType other = (JpaToscaEntityType) otherConcept; - if (!key.equals(other.key)) { - return key.compareTo(other.key); + + int result = key.compareTo(other.key); + if (result != 0) { + return result; } - int result = ObjectUtils.compare(derivedFrom, other.derivedFrom); + result = ObjectUtils.compare(derivedFrom, other.derivedFrom); if (result != 0) { return result; } - result = PfUtils.compareObjects(metadata, other.metadata); + result = PfUtils.compareMaps(metadata, other.metadata); if (result != 0) { return result; } @@ -270,26 +229,21 @@ public class JpaToscaEntityType extends PfConcept impleme return ObjectUtils.compare(description, other.description); } - @Override - public PfConcept copyTo(@NonNull PfConcept target) { - final Object copyObject = target; - Assertions.instanceOf(copyObject, PfConcept.class); - - @SuppressWarnings("unchecked") - final JpaToscaEntityType copy = ((JpaToscaEntityType) copyObject); - copy.setKey(new PfConceptKey(key)); - copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null); - - if (metadata != null) { - final Map newMatadata = new TreeMap<>(); - for (final Entry metadataEntry : metadata.entrySet()) { - newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue()); - } - copy.setMetadata(newMatadata); + protected Object deserializeMetadataValue(String metadataValue) { + try { + return STANDARD_CODER.decode(metadataValue, Object.class); + } catch (CoderException ce) { + String errorMessage = "error decoding metadata JSON value read from database: " + metadataValue; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); } + } - copy.setDescription(description); - - return copy; + protected String serializeMetadataValue(Object metadataValue) { + try { + return STANDARD_CODER.encode(metadataValue); + } catch (CoderException ce) { + String errorMessage = "error encoding metadata JSON value for database: " + metadataValue; + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } } }