aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2019-03-19 11:39:53 +0000
committerGerrit Code Review <gerrit@onap.org>2019-03-19 11:39:53 +0000
commit998fb98e470205171ba9dc37674ea9d1c31e54ed (patch)
tree84e67971c976ec925aaec28ab2d181926a2c2497 /models-tosca
parent2d4d299dc1173180aaee3240361f0b9f79648e79 (diff)
parent0dcdf4052d55ddd200750b00e942b48bc5b76b98 (diff)
Merge "Add serialization/deserialization for tosca policy"
Diffstat (limited to 'models-tosca')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPoliciesJsonAdapter.java15
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaPolicyJsonAdapter.java97
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java21
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java15
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java4
5 files changed, 129 insertions, 23 deletions
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 2b61b6861..424373c3d 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
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +30,7 @@ import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.Iterator;
-
+import java.util.Map.Entry;
import lombok.NonNull;
import org.onap.policy.models.base.PfConceptKey;
@@ -40,6 +41,7 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
* GSON type adapter for TOSCA policies.
*
* @author Liam Fallon (liam.fallon@est.tech)
+ * @author Chenfei Gao (cgao@research.att.com)
*/
public class ToscaPoliciesJsonAdapter implements JsonSerializer<ToscaPolicies>, JsonDeserializer<ToscaPolicies> {
@Override
@@ -64,9 +66,16 @@ public class ToscaPoliciesJsonAdapter implements JsonSerializer<ToscaPolicies>,
}
@Override
- public JsonElement serialize(@NonNull final ToscaPolicies policy, @NonNull final Type type,
+ public JsonElement serialize(@NonNull final ToscaPolicies policies, @NonNull final Type type,
@NonNull final JsonSerializationContext context) {
- return null;
+ JsonArray policiesJsonArray = new JsonArray();
+
+ for (ToscaPolicy policy: policies.getConceptMap().values()) {
+ JsonElement policyEntry = new ToscaPolicyJsonAdapter().serialize(policy, type, context);
+ policiesJsonArray.add(policyEntry);
+ }
+
+ 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 075445d01..aef854710 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
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,7 +29,9 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
-
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
import javax.ws.rs.core.Response;
import lombok.NonNull;
@@ -43,11 +46,18 @@ import org.slf4j.LoggerFactory;
* GSON type adapter for TOSCA policies.
*
* @author Liam Fallon (liam.fallon@est.tech)
+ * @author Chenfei Gao (cgao@research.att.com)
*/
public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, JsonDeserializer<ToscaPolicy> {
// Logger for this class
private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyJsonAdapter.class);
+ private static final String TYPE = "type";
+ private static final String DESCRIPTION = "description";
+ private static final String VERSION = "version";
+ private static final String METADATA = "metadata";
+ private static final String PROPERTIES = "properties";
+
@Override
public ToscaPolicy deserialize(@NonNull final JsonElement policyElement, @NonNull final Type type,
@NonNull final JsonDeserializationContext context) {
@@ -59,20 +69,49 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json
if (policyJsonMapObject.entrySet().size() != 1) {
String errorMessage = "a policy list entry may only contain one and only one policy";
LOGGER.debug(errorMessage);
- throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
}
- String policyName = policyJsonMapObject.entrySet().iterator().next().getKey();
- JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next().getValue().getAsJsonObject();
+ final String policyName = policyJsonMapObject.entrySet().iterator().next().getKey();
+ final JsonObject policyJsonObject = policyJsonMapObject.entrySet().iterator().next()
+ .getValue().getAsJsonObject();
- PfConceptKey policyKey = new PfConceptKey(policyName, policyJsonObject.get("version").getAsString());
+ // Set keys
+ PfConceptKey policyKey = new PfConceptKey(policyName, policyJsonObject.get(VERSION).getAsString());
PfConceptKey policyTypeKey = new PfConceptKey(
- policyJsonObject.get("type").getAsString(),
- policyJsonObject.get("version").getAsString());
+ policyJsonObject.get(TYPE).getAsString(),
+ policyJsonObject.get(VERSION).getAsString());
ToscaPolicy policy = new ToscaPolicy(policyKey, policyTypeKey);
- // TODO: Rest of parsing
+ // Set description
+ if (policyJsonObject.has(DESCRIPTION)) {
+ final String policyDescription = policyJsonObject.get(DESCRIPTION).getAsString();
+ policy.setDescription(policyDescription);
+ }
+
+ // Set metadata
+ if (policyJsonObject.has(METADATA)) {
+ 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);
+ }
+ policy.setMetadata(policyMetadataMap);
+ }
+ // Set properties
+ if (policyJsonObject.has(PROPERTIES)) {
+ final JsonObject policyPropertiesMapObject = policyJsonObject.get(PROPERTIES).getAsJsonObject();
+ Map<String, Object> propertiesMap = new HashMap<>();
+ for (Entry<String, JsonElement> entry : policyPropertiesMapObject.entrySet()) {
+ final String policyPropertiesEntryKey = entry.getKey();
+ final JsonElement policyPropertiesEntryValue = entry.getValue();
+ propertiesMap.put(policyPropertiesEntryKey, policyPropertiesEntryValue);
+ }
+ policy.setProperties(propertiesMap);
+ }
return policy;
}
@@ -80,6 +119,46 @@ public class ToscaPolicyJsonAdapter implements JsonSerializer<ToscaPolicy>, Json
public JsonElement serialize(@NonNull final ToscaPolicy policy, @NonNull final Type type,
@NonNull final JsonSerializationContext context) {
- return null;
+ JsonObject policyValJsonObject = new JsonObject();
+
+ // Add type
+ policyValJsonObject.addProperty(TYPE, policy.getType().getName());
+
+ // Add version
+ policyValJsonObject.addProperty(VERSION, policy.getType().getVersion());
+
+ // Add description
+ if (policy.getDescription() != null) {
+ policyValJsonObject.addProperty(DESCRIPTION, policy.getDescription());
+ }
+
+ // Add metadata
+ 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);
+ }
+ policyValJsonObject.add(METADATA, metadataMapObject);
+ }
+
+ // 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);
+ }
+ policyValJsonObject.add(PROPERTIES, propertiesMapObject);
+ }
+
+ JsonObject policyJsonObject = new JsonObject();
+ policyJsonObject.add(policy.getKey().getName(), policyValJsonObject);
+ return policyJsonObject;
}
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java
index 286bb7492..613b0e292 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,9 +40,14 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
* GSON type adapter for TOSCA policies.
*
* @author Liam Fallon (liam.fallon@est.tech)
+ * @author Chenfei Gao (cgao@research.att.com)
*/
public class ToscaServiceTemplateJsonAdapter
implements JsonSerializer<ToscaServiceTemplate>, JsonDeserializer<ToscaServiceTemplate> {
+
+ private static final String TOPOLOGY_TEMPLATE = "topology_template";
+ private static final String TOSCA_DEFINITIONS_VERSION = "tosca_definitions_version";
+
@Override
public ToscaServiceTemplate deserialize(@NonNull final JsonElement serviceTemplateElement, @NonNull final Type type,
@NonNull final JsonDeserializationContext context) {
@@ -53,11 +59,11 @@ public class ToscaServiceTemplateJsonAdapter
final PfConceptKey serviceTemplateKey = new PfConceptKey("IncomingServiceTemplate", "0.0.1");
final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey);
serviceTemplate
- .setToscaDefinitionsVersion(serviceTemplateJsonObject.get("tosca_definitions_version").getAsString());
+ .setToscaDefinitionsVersion(serviceTemplateJsonObject.get(TOSCA_DEFINITIONS_VERSION).getAsString());
- if (serviceTemplateJsonObject.has("topology_template")) {
+ if (serviceTemplateJsonObject.has(TOPOLOGY_TEMPLATE)) {
serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplateJsonAdapter().deserialize(
- serviceTemplateJsonObject.get("topology_template"), ToscaTopologyTemplate.class, context));
+ serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE), ToscaTopologyTemplate.class, context));
}
// Set the parent key of the topology template to be this service template
@@ -70,6 +76,13 @@ public class ToscaServiceTemplateJsonAdapter
public JsonElement serialize(@NonNull final ToscaServiceTemplate serviceTemplate, @NonNull final Type type,
@NonNull final JsonSerializationContext context) {
- return null;
+ JsonObject serviceTemplateJsonObject = new JsonObject();
+ JsonElement topologyTemplateJsonElement = new ToscaTopologyTemplateJsonAdapter()
+ .serialize(serviceTemplate.getTopologyTemplate(), type, context);
+
+ serviceTemplateJsonObject.addProperty(TOSCA_DEFINITIONS_VERSION, serviceTemplate.getToscaDefinitionsVersion());
+ serviceTemplateJsonObject.add(TOPOLOGY_TEMPLATE, topologyTemplateJsonElement);
+
+ return serviceTemplateJsonObject;
}
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java
index c76b9b027..a2974fdbd 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaTopologyTemplateJsonAdapter.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,10 +41,13 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
* GSON type adapter for TOSCA policies.
*
* @author Liam Fallon (liam.fallon@est.tech)
+ * @author Chenfei Gao (cgao@research.att.com)
*/
public class ToscaTopologyTemplateJsonAdapter
implements JsonSerializer<ToscaTopologyTemplate>, JsonDeserializer<ToscaTopologyTemplate> {
+ private static final String POLICIES = "policies";
+
@Override
public ToscaTopologyTemplate deserialize(@NonNull final JsonElement toplogyTemplateElement,
@NonNull final Type type, @NonNull final JsonDeserializationContext context) {
@@ -55,9 +59,9 @@ public class ToscaTopologyTemplateJsonAdapter
final PfReferenceKey topologyTemplateKey = new PfReferenceKey(new PfConceptKey(), "IncomingTopologyTemplate");
final ToscaTopologyTemplate topologyTemplate = new ToscaTopologyTemplate(topologyTemplateKey);
- if (topologyTemplateJsonObject.has("policies")) {
+ if (topologyTemplateJsonObject.has(POLICIES)) {
topologyTemplate.setPolicies(new ToscaPoliciesJsonAdapter()
- .deserialize(topologyTemplateJsonObject.get("policies"), ToscaPolicies.class, context));
+ .deserialize(topologyTemplateJsonObject.get(POLICIES), ToscaPolicies.class, context));
}
return topologyTemplate;
@@ -67,6 +71,11 @@ public class ToscaTopologyTemplateJsonAdapter
public JsonElement serialize(@NonNull final ToscaTopologyTemplate topologyTemplate, @NonNull final Type type,
@NonNull final JsonSerializationContext context) {
- return null;
+ JsonObject topologyTemplateJsonObject = new JsonObject();
+ JsonElement policiesJsonElement = new ToscaPoliciesJsonAdapter()
+ .serialize(topologyTemplate.getPolicies(), type, context);
+
+ topologyTemplateJsonObject.add(POLICIES, policiesJsonElement);
+ return topologyTemplateJsonObject;
}
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
index 9977f3b72..4c3cbc85a 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
@@ -68,8 +68,6 @@ public class MonitoringPolicySerializationTest {
assertEquals("onap.restart.tca:1.0.0",
serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
- assertEquals("onap.restart.tca:1.0.0",
- serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
}
@Test
@@ -88,7 +86,5 @@ public class MonitoringPolicySerializationTest {
assertEquals("onap.restart.tca:1.0.0",
serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
- assertEquals("onap.restart.tca:1.0.0",
- serviceTemplate.getTopologyTemplate().getPolicies().get("onap.restart.tca").getId());
}
}