summaryrefslogtreecommitdiffstats
path: root/models-tosca/src
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-05-08 20:59:09 +0000
committerliamfallon <liam.fallon@est.tech>2019-05-08 20:59:09 +0000
commit4a1ec1aea71bf58d371a034045defd11d9e77ce3 (patch)
tree97b80dc90ebcddc407ca67857a784496633c0514 /models-tosca/src
parentf53879588a464c727ece62f87c7625b47e6de7f1 (diff)
Add JSON encoding to legacy policy properties
Now that all properties on policies are stored as JSON, the properties on legacy policies must also be stored as JSON in the database. Issue-ID: POLICY-1753 Change-Id: Ia75a02c8d6f595ce0fb68ac1fa2f2399fbc08f6e Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java36
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java17
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java21
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java8
4 files changed, 61 insertions, 21 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java
index b46737d2b..91ff150fc 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java
@@ -28,6 +28,7 @@ import javax.ws.rs.core.Response;
import lombok.Data;
+import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper;
import org.slf4j.Logger;
@@ -55,17 +56,19 @@ public class LegacyGuardPolicyContent {
private String guardActiveEnd;
/**
- * Get contents as a map.
+ * Get contents as a property map.
*
* @return the contents as a map.
*/
public Map<String, String> getAsPropertyMap() {
final Map<String, String> propertyMap = new HashMap<>();
+ final StandardCoder coder = new StandardCoder();
+
try {
for (Field field : this.getClass().getDeclaredFields()) {
if (field.get(this) != null && field.getType().equals(String.class)) {
- propertyMap.put(field.getName(), (String)field.get(this));
+ propertyMap.put(field.getName(), coder.encode(field.get(this)));
}
}
} catch (Exception exc) {
@@ -77,4 +80,33 @@ public class LegacyGuardPolicyContent {
return propertyMap;
}
+
+ /**
+ * Set the contents from a property map.
+ *
+ * @param propertyMap the incoming property map
+ */
+ public void setContent(final Map<String, String> propertyMap) {
+ final StandardCoder coder = new StandardCoder();
+
+ try {
+ // @formatter:off
+ setActor( coder.decode(propertyMap.get("actor"), String.class));
+ setClname( coder.decode(propertyMap.get("clname"), String.class));
+ setGuardActiveEnd( coder.decode(propertyMap.get("guardActiveEnd"), String.class));
+ setGuardActiveStart(coder.decode(propertyMap.get("guardActiveStart"), String.class));
+ setLimit( coder.decode(propertyMap.get("limit"), String.class));
+ setMax( coder.decode(propertyMap.get("max"), String.class));
+ setMin( coder.decode(propertyMap.get("min"), String.class));
+ setRecipe( coder.decode(propertyMap.get("recipe"), String.class));
+ setTargets( coder.decode(propertyMap.get("targets"), String.class));
+ setTimeUnits( coder.decode(propertyMap.get("timeUnits"), String.class));
+ setTimeWindow( coder.decode(propertyMap.get("timeWindow"), String.class));
+ // @formatter:on
+ } catch (Exception exc) {
+ String errorMessage = "could not convert content to a property map";
+ LOGGER.warn(errorMessage, exc);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc);
+ }
+ }
}
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 0d04cb9d1..8fd883722 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
@@ -47,13 +47,12 @@ import org.slf4j.LoggerFactory;
*/
public class LegacyGuardPolicyMapper
implements JpaToscaServiceTemplateMapper<LegacyGuardPolicyInput, Map<String, LegacyGuardPolicyOutput>> {
+ private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
// Tag for metadata fields
private static final String POLICY_ID = "policy-id";
private static final String POLICY_VERSION = "policy-version";
- private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
-
private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
static {
@@ -141,19 +140,7 @@ public class LegacyGuardPolicyMapper
}
final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
- // @formatter:off
- content.setActor( toscaPolicy.getProperties().get("actor"));
- content.setClname( toscaPolicy.getProperties().get("clname"));
- content.setGuardActiveEnd( toscaPolicy.getProperties().get("guardActiveEnd"));
- content.setGuardActiveStart(toscaPolicy.getProperties().get("guardActiveStart"));
- content.setLimit( toscaPolicy.getProperties().get("limit"));
- content.setMax( toscaPolicy.getProperties().get("max"));
- content.setMin( toscaPolicy.getProperties().get("min"));
- content.setRecipe( toscaPolicy.getProperties().get("recipe"));
- content.setTargets( toscaPolicy.getProperties().get("targets"));
- content.setTimeUnits( toscaPolicy.getProperties().get("timeUnits"));
- content.setTimeWindow( toscaPolicy.getProperties().get("timeWindow"));
- // @formatter:on
+ content.setContent(toscaPolicy.getProperties());
final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
propertiesMap.put("content", content);
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 7caba98d8..2dddda26b 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
@@ -25,6 +25,8 @@ import java.util.Map;
import javax.ws.rs.core.Response;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
@@ -68,7 +70,14 @@ public class LegacyOperationalPolicyMapper
final Map<String, String> propertyMap = new HashMap<>();
toscaPolicy.setProperties(propertyMap);
- toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent());
+ try {
+ toscaPolicy.getProperties().put(CONTENT_PROPERTY,
+ new StandardCoder().encode(legacyOperationalPolicy.getContent()));
+ } catch (CoderException ce) {
+ String errorMessage = "encoding of property \"content\" to JSON failed";
+ LOGGER.warn(errorMessage, ce);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
+ }
final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
@@ -105,7 +114,15 @@ public class LegacyOperationalPolicyMapper
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
- final String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
+ String content = null;
+ try {
+ content = new StandardCoder().decode(toscaPolicy.getProperties().get(CONTENT_PROPERTY), String.class);
+ } catch (CoderException ce) {
+ String errorMessage = "decoding of property \"content\" from JSON failed";
+ LOGGER.warn(errorMessage, ce);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
+ }
+
if (content == null) {
String errorMessage = "property \"content\" not defined on TOSCA policy";
LOGGER.warn(errorMessage);
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 3e049ea17..eebacd1d6 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
@@ -159,6 +159,8 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
if (properties != null) {
Map<String, Object> propertyMap = new LinkedHashMap<>();
+ final StandardCoder coder = new StandardCoder();
+
for (Entry<String, String> entry : properties.entrySet()) {
try {
// TODO: This is a HACK, we need to validate the properties against their
@@ -166,7 +168,7 @@ 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 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));
+ propertyMap.put(entry.getKey(), coder.decode(entry.getValue(), Object.class));
} catch (CoderException ce) {
String errorMessage = "error decoding property JSON value read from database: key=" + entry.getKey()
+ ", value=" + entry.getValue();
@@ -193,6 +195,8 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
if (toscaPolicy.getProperties() != null) {
properties = new LinkedHashMap<>();
+ final StandardCoder coder = new StandardCoder();
+
for (Entry<String, Object> propertyEntry : toscaPolicy.getProperties().entrySet()) {
// 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
@@ -200,7 +204,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P
// TODO: Here we are simply serializing the property value into a string and storing it
// TODO: unvalidated into the database
try {
- properties.put(propertyEntry.getKey(), new StandardCoder().encode(propertyEntry.getValue()));
+ properties.put(propertyEntry.getKey(), coder.encode(propertyEntry.getValue()));
} catch (CoderException ce) {
String errorMessage = "error encoding property JSON value for database: key="
+ propertyEntry.getKey() + ", value=" + propertyEntry.getValue();