From: liamfallon Date: Sun, 5 May 2019 21:26:36 +0000 (+0000) Subject: Serializaiton of properties to DB as JSON X-Git-Tag: 2.0.0~10 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=d3394bba7c8e30073a1f7874fed4320be18545d0;p=policy%2Fmodels.git Serializaiton of properties to DB as JSON Properties should be serialized to JSON prior to writing to database and deserialized from JSON when read from database. Issue-ID: POLICY-1736 Change-Id: I5ad3fd4a87079f4557f5fcb825395f0b4bec3318 Signed-off-by: liamfallon --- diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java index 01bd83d86..0d04cb9d1 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java @@ -61,8 +61,6 @@ public class LegacyGuardPolicyMapper new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0")); GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); - GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout", - new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); GUARD_POLICY_TYPE_MAP.put("guard.blacklist", new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); } 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 e21979be3..3e049ea17 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 @@ -38,17 +38,21 @@ import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Lob; import javax.persistence.Table; +import javax.ws.rs.core.Response; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; 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; @@ -148,8 +152,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) { toscaPolicy.setTypeVersion(type.getVersion()); - } - else { + } else { toscaPolicy.setTypeVersion(null); } @@ -157,7 +160,18 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P Map propertyMap = new LinkedHashMap<>(); for (Entry entry : properties.entrySet()) { - propertyMap.put(entry.getKey(), entry.getValue()); + try { + // TODO: This is a HACK, we need to validate the properties against their + // TODO: their data type in their policy type definition in TOSCA, which means reading + // TODO: the policy type from the database and parsing the property value object correctly + // TODO: Here we are simply reading a JSON string from the database and deserializing the + // TODO: property value from JSON + propertyMap.put(entry.getKey(), new StandardCoder().decode(entry.getValue(), Object.class)); + } catch (CoderException ce) { + String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey() + + ", value=" + entry.getValue(); + throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce); + } } toscaPolicy.setProperties(propertyMap); @@ -185,7 +199,13 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P // TODO: the policy type from the database and parsing the property value object correctly // TODO: Here we are simply serializing the property value into a string and storing it // TODO: unvalidated into the database - properties.put(propertyEntry.getKey(), propertyEntry.getValue().toString()); + try { + properties.put(propertyEntry.getKey(), new StandardCoder().encode(propertyEntry.getValue())); + } catch (CoderException ce) { + String errorMessage = "error encoding property JSON value for database: key=" + + propertyEntry.getKey() + ", value=" + propertyEntry.getValue(); + 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/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index 5f50e4a43..f9e388b04 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java @@ -64,6 +64,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType { private static final long serialVersionUID = 8084846046148349401L; + public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_0_0"; public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple"; public static final String DEFAULT_VERSION = "1.0.0"; @@ -97,7 +98,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType propertyMap = new HashMap<>(); - propertyMap.put("Property", "Property Value"); + propertyMap.put("Property", "\"Property Value\""); tp.setProperties(propertyMap); assertEquals(propertyMap, tp.getProperties()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java index 4569d42ff..a2a418ef9 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java @@ -157,7 +157,7 @@ public class JpaToscaServiceTemplateTest { tst.clean(); assertEquals(tttClone0, tst); - assertFalse(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid()); + assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid()); assertTrue(tst.validate(new PfValidationResult()).isValid()); tst.setDescription(null);