aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLiam Fallon <liam.fallon@est.tech>2022-03-21 09:21:13 +0000
committerGerrit Code Review <gerrit@onap.org>2022-03-21 09:21:13 +0000
commitdf406ee3dc0efccc4d0f87c4b8cc5755f201ba48 (patch)
treef1cf3c8a409f45f1831e58051066e9f969f6a2c2
parent448c221502fac54c52e088caca56b9aa45797ef7 (diff)
parentd08d7fea4cf9400b9ba258768a3e2d082d849df2 (diff)
Merge "Fix metadataSet serialization for db persistence and retrieval."
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntityType.java28
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java12
2 files changed, 38 insertions, 2 deletions
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 84381fb50..14c8ca4a3 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
@@ -31,16 +31,20 @@ 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.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.validation.annotations.VerifyKey;
import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
@@ -54,6 +58,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept implements PfAuthorative<T> {
private static final long serialVersionUID = -1330661834220739393L;
+ private static final StandardCoder STANDARD_CODER = new StandardCoder();
+
@EmbeddedId
@VerifyKey
@NotNull
@@ -128,7 +134,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme
toscaEntity.setDescription(description);
}
- toscaEntity.setMetadata(PfUtils.mapMap(metadata, item -> item));
+ toscaEntity.setMetadata(PfUtils.mapMap(metadata, this::deserializeMetadataValue));
return toscaEntity;
}
@@ -158,7 +164,7 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme
description = toscaEntity.getDescription();
}
- metadata = PfUtils.mapMap(toscaEntity.getMetadata(), Object::toString);
+ metadata = PfUtils.mapMap(toscaEntity.getMetadata(), this::serializeMetadataValue);
}
@Override
@@ -219,4 +225,22 @@ public class JpaToscaEntityType<T extends ToscaEntity> extends PfConcept impleme
return ObjectUtils.compare(description, other.description);
}
+
+ 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);
+ }
+ }
+
+ 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);
+ }
+ }
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java
index feae48e1a..92e0faea6 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java
@@ -62,6 +62,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
@EqualsAndHashCode(callSuper = true)
public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties<ToscaPolicy> {
private static final long serialVersionUID = 3265174757061982805L;
+ private static final String METADATA_METADATA_SET_NAME_TAG = "metadataSetName";
+ private static final String METADATA_METADATA_SET_VERSION_TAG = "metadataSetVersion";
// Tags for metadata
private static final String METADATA_POLICY_ID_TAG = "policy-id";
@@ -143,6 +145,16 @@ public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties<ToscaPol
// Add the policy name and version fields to the metadata
getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
+
+ // Add metadataSet name and version to the metadata
+ if (getMetadata().containsKey(METADATA_METADATA_SET_NAME_TAG)
+ && getMetadata().containsKey(METADATA_METADATA_SET_VERSION_TAG)) {
+ getMetadata().put(METADATA_METADATA_SET_NAME_TAG, getMetadata().get(METADATA_METADATA_SET_NAME_TAG)
+ .replaceAll("^\"|\"$", ""));
+
+ getMetadata().put(METADATA_METADATA_SET_VERSION_TAG, getMetadata().get(METADATA_METADATA_SET_VERSION_TAG)
+ .replaceAll("^\"|\"$", ""));
+ }
}
@Override