summaryrefslogtreecommitdiffstats
path: root/models-tosca/src/main/java
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-03-20 16:08:21 +0000
committerliamfallon <liam.fallon@est.tech>2019-03-20 16:08:21 +0000
commit8fc237cc606b6e9c8c7d7e7a2c811fc671a4b40e (patch)
tree1dc4369a5f94683a2140f28968a1bd32566743df /models-tosca/src/main/java
parentd614f2f3871ea6b46b0e8b0bbd8be70414710164 (diff)
Implement persistence test for policies
The unit test MonitoringPolicySerializationTest tests persistence for policies and shows how persistence works. Issue-ID: POLICY-1195 Change-Id: I933eb538238f9ccd41ce69614e0c9afcac869c29 Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java2
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaEntityType.java25
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicy.java15
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java3
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java34
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java1
6 files changed, 47 insertions, 33 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
index 781602a7e..2c0d5084d 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
@@ -54,7 +54,7 @@ public class LegacyOperationalPolicyMapper
// TODO: Check if this is the correct way to set the policy type version
toscaPolicy.setType(new PfConceptKey("SomeDerivedPolicyType", "1.0.0"));
- Map<String, Object> propertyMap = new HashMap<>();
+ Map<String, String> propertyMap = new HashMap<>();
toscaPolicy.setProperties(propertyMap);
toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent());
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaEntityType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaEntityType.java
index 7eaf58b2e..f2ae05119 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaEntityType.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaEntityType.java
@@ -20,20 +20,17 @@
package org.onap.policy.models.tosca.simple.concepts;
-import com.google.gson.annotations.SerializedName;
-
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
-import javax.persistence.CascadeType;
+import javax.persistence.AttributeOverride;
+import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
+import javax.persistence.ElementCollection;
import javax.persistence.EmbeddedId;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
-import javax.persistence.OneToMany;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -54,7 +51,6 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
* Class to represent the EntrySchema of list/map property in TOSCA definition.
*/
@MappedSuperclass
-@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = false)
public class ToscaEntityType extends PfConcept {
@@ -63,15 +59,22 @@ public class ToscaEntityType extends PfConcept {
@EmbeddedId
private PfConceptKey key;
- @SerializedName("derived_from")
- @Column(name = "derivedFrom")
+ // @formatter:off
+ @Column
+ @AttributeOverrides({
+ @AttributeOverride(name = "name",
+ column = @Column(name = "derived_from_name")),
+ @AttributeOverride(name = "version",
+ column = @Column(name = "derived_from_version"))
+ })
private PfConceptKey derivedFrom;
- @OneToMany(cascade = CascadeType.ALL)
+ @ElementCollection
private Map<String, String> metadata;
- @Column(name = "description")
+ @Column
private String description;
+ // @formatter:on
/**
* The Default Constructor creates a {@link ToscaEntityType} object with a null key.
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicy.java
index af94af08d..e08079c40 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicy.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicy.java
@@ -28,6 +28,8 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import javax.persistence.AttributeOverride;
+import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
@@ -63,14 +65,23 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
public class ToscaPolicy extends ToscaEntityType {
private static final long serialVersionUID = 3265174757061982805L;
+ // @formatter:off
@Column
+ @AttributeOverrides({
+ @AttributeOverride(name = "name",
+ column = @Column(name = "type_name")),
+ @AttributeOverride(name = "version",
+ column = @Column(name = "type_version"))
+ })
private PfConceptKey type;
@ElementCollection
- private Map<String, Object> properties;
+ @Column(length = 10000)
+ private Map<String, String> properties;
@ElementCollection
private List<PfConceptKey> targets;
+ // @formatter:on
/**
* The Default Constructor creates a {@link ToscaPolicy} object with a null key.
@@ -166,7 +177,7 @@ public class ToscaPolicy extends ToscaEntityType {
private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) {
PfValidationResult result = resultIn;
- for (Entry<String, Object> propertyEntry : properties.entrySet()) {
+ for (Entry<String, String> propertyEntry : properties.entrySet()) {
if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID,
"policy property key may not be null "));
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java
index 3f25b708f..53088d637 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java
@@ -71,8 +71,7 @@ public class ToscaPoliciesJsonAdapter implements JsonSerializer<ToscaPolicies>,
JsonArray policiesJsonArray = new JsonArray();
for (ToscaPolicy policy: policies.getConceptMap().values()) {
- JsonElement policyEntry = new ToscaPolicyJsonAdapter().serialize(policy, type, context);
- policiesJsonArray.add(policyEntry);
+ policiesJsonArray.add(new ToscaPolicyJsonAdapter().serialize(policy, type, context));
}
return policiesJsonArray;
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java
index aef854710..95b4b3bba 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java
@@ -21,6 +21,8 @@
package org.onap.policy.models.tosca.simple.serialization;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@@ -58,6 +60,8 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json
private static final String METADATA = "metadata";
private static final String PROPERTIES = "properties";
+ private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
+
@Override
public ToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type,
@NonNull final JsonDeserializationContext context) {
@@ -94,9 +98,7 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json
final JsonObject policyMetadataMapObject = policyJsonObject.get(METADATA).getAsJsonObject();
Map<String, String> policyMetadataMap = new HashMap<>();
for (Entry<String, JsonElement> entry : policyMetadataMapObject.entrySet()) {
- final String policyMetadataEntryKey = entry.getKey();
- final String policyMetadataEntryValue = entry.getValue().getAsString();
- policyMetadataMap.put(policyMetadataEntryKey, policyMetadataEntryValue);
+ policyMetadataMap.put(entry.getKey(), entry.getValue().getAsString());
}
policy.setMetadata(policyMetadataMap);
}
@@ -104,11 +106,14 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json
// Set properties
if (policyJsonObject.has(PROPERTIES)) {
final JsonObject policyPropertiesMapObject = policyJsonObject.get(PROPERTIES).getAsJsonObject();
- Map<String, Object> propertiesMap = new HashMap<>();
+ Map<String, String> propertiesMap = new HashMap<>();
for (Entry<String, JsonElement> entry : policyPropertiesMapObject.entrySet()) {
- final String policyPropertiesEntryKey = entry.getKey();
- final JsonElement policyPropertiesEntryValue = entry.getValue();
- propertiesMap.put(policyPropertiesEntryKey, policyPropertiesEntryValue);
+ // 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 serializing the property value into a string and storing it
+ // TODO: unvalidated into the database
+ propertiesMap.put(entry.getKey(), gson.toJson(entry.getValue()));
}
policy.setProperties(propertiesMap);
}
@@ -136,9 +141,7 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json
if (policy.getMetadata() != null) {
JsonObject metadataMapObject = new JsonObject();
for (Entry<String, String> entry : policy.getMetadata().entrySet()) {
- final String entryKey = entry.getKey();
- final String entryVal = entry.getValue();
- metadataMapObject.addProperty(entryKey, entryVal);
+ metadataMapObject.addProperty(entry.getKey(), entry.getValue());
}
policyValJsonObject.add(METADATA, metadataMapObject);
}
@@ -146,13 +149,10 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json
// Add properties
if (policy.getProperties() != null) {
JsonObject propertiesMapObject = new JsonObject();
- for (Entry<String, Object> entry : policy.getProperties().entrySet()) {
- final String entryKey = entry.getKey();
- JsonElement entryVal = null;
- if (entry.getValue() instanceof JsonElement) {
- entryVal = (JsonElement) entry.getValue();
- }
- propertiesMapObject.add(entryKey, entryVal);
+ for (Entry<String, String> entry : policy.getProperties().entrySet()) {
+ // TODO: This is the other direction of the HACK
+ JsonObject valueObject = gson.fromJson(entry.getValue(), JsonObject.class);
+ propertiesMapObject.add(entry.getKey(), valueObject);
}
policyValJsonObject.add(PROPERTIES, propertiesMapObject);
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java
index a386a9cc5..cf3e668b3 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateMessageBodyHandler.java
@@ -56,6 +56,7 @@ public class ToscaServiceTemplateMessageBodyHandler extends GsonMessageBodyHandl
.registerTypeAdapter(ToscaTopologyTemplate.class, new ToscaTopologyTemplateJsonAdapter())
.registerTypeAdapter(ToscaPolicies.class, new ToscaPoliciesJsonAdapter())
.registerTypeAdapter(ToscaPolicy.class, new ToscaPolicyJsonAdapter())
+ .setPrettyPrinting()
.create()
);
// @formatter:on