Serializaiton of properties to DB as JSON 81/86981/1
authorliamfallon <liam.fallon@est.tech>
Sun, 5 May 2019 21:26:36 +0000 (21:26 +0000)
committerliamfallon <liam.fallon@est.tech>
Sun, 5 May 2019 21:26:36 +0000 (21:26 +0000)
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 <liam.fallon@est.tech>
models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java

index 01bd83d..0d04cb9 100644 (file)
@@ -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"));
     }
index e21979b..3e049ea 100644 (file)
@@ -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<ToscaPolicy> 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<ToscaPolicy> implements P
             Map<String, Object> propertyMap = new LinkedHashMap<>();
 
             for (Entry<String, String> 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<ToscaPolicy> 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);
+                }
             }
         }
 
index 5f50e4a..f9e388b 100644 (file)
@@ -64,6 +64,7 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp
         implements PfAuthorative<ToscaServiceTemplate> {
     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<ToscaServiceTemp
      * @param key the key
      */
     public JpaToscaServiceTemplate(@NonNull final PfConceptKey key) {
-        this(key, "");
+        this(key, DEFAULT_TOSCA_DEFINTIONS_VERISON);
     }
 
     /**
index 571cde4..ae38ab9 100644 (file)
@@ -81,7 +81,7 @@ public class JpaToscaPolicyTest {
         JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
 
         Map<String, String> propertyMap = new HashMap<>();
-        propertyMap.put("Property", "Property Value");
+        propertyMap.put("Property", "\"Property Value\"");
         tp.setProperties(propertyMap);
         assertEquals(propertyMap, tp.getProperties());
 
index 4569d42..a2a418e 100644 (file)
@@ -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);