diff options
author | Gurjeet Bedi <gurjeetb@amdocs.com> | 2018-01-07 21:07:50 -0500 |
---|---|---|
committer | Gurjeet Bedi <gurjeetb@amdocs.com> | 2018-01-07 21:10:18 -0500 |
commit | eabf0a08faa6581c88c2e4ca5be56d4630891046 (patch) | |
tree | f496c735e58fcc249a53eacaccc0456122d03a36 /src/main/java/org/onap/crud/util/CrudServiceUtil.java | |
parent | 5132ce03dac867af84eee209659238118f7ca4bc (diff) |
OXM which tracks provenance
Support for source-of-truth and last-mod-source-of-truth
Issue-ID: AAI-482
Change-Id: Idc7ec47c0561f2dad6f930d0ac0dd3fff9585b4f
Signed-off-by: Gurjeet Bedi <gurjeetb@amdocs.com>
Diffstat (limited to 'src/main/java/org/onap/crud/util/CrudServiceUtil.java')
-rw-r--r-- | src/main/java/org/onap/crud/util/CrudServiceUtil.java | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/src/main/java/org/onap/crud/util/CrudServiceUtil.java b/src/main/java/org/onap/crud/util/CrudServiceUtil.java index 88df8d1..45f2414 100644 --- a/src/main/java/org/onap/crud/util/CrudServiceUtil.java +++ b/src/main/java/org/onap/crud/util/CrudServiceUtil.java @@ -23,15 +23,27 @@ */ package org.onap.crud.util; +import org.onap.aai.db.props.AAIProperties; import org.onap.aaiutils.oxm.OxmModelLoader; import org.onap.crud.exception.CrudException; import org.onap.schema.RelationshipSchemaLoader; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +import java.util.AbstractMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response.Status; public class CrudServiceUtil { - + private static Gson gson = new Gson(); public static Object validateFieldType(String value, Class clazz) throws CrudException { try { if (clazz.isAssignableFrom(Integer.class)) { @@ -71,4 +83,46 @@ public class CrudServiceUtil { } RelationshipSchemaLoader.loadModels(); } + + public static JsonElement mergeHeaderInFoToPayload(JsonElement propertiesFromRequest, HttpHeaders headers, boolean isAdd) { + if(!headers.getRequestHeaders().containsKey("X-FromAppId")) + return propertiesFromRequest; + + String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); + Set<Map.Entry<String, JsonElement>> properties = new HashSet<Map.Entry<String, JsonElement>>(); + properties.addAll(propertiesFromRequest.getAsJsonObject().entrySet()); + + Set<String> propertyKeys = new HashSet<String>(); + for(Map.Entry<String, JsonElement> property : properties) { + propertyKeys.add(property.getKey()); + } + + if(!propertyKeys.contains(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH)) { + properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, + (JsonElement)(new JsonPrimitive(sourceOfTruth)))); + } + + if(isAdd && !propertyKeys.contains(AAIProperties.SOURCE_OF_TRUTH)) { + properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.SOURCE_OF_TRUTH, + (JsonElement)(new JsonPrimitive(sourceOfTruth)))); + } + + Object[] propArray = properties.toArray(); + StringBuilder sb = new StringBuilder(); + sb.append("{"); + boolean first=true; + for(int i=0; i<propArray.length; i++) { + + Map.Entry<String, JsonElement> entry = (Entry<String, JsonElement>) propArray[i]; + if(!first) { + sb.append(","); + } + sb.append("\"").append(entry.getKey()).append("\"").append(":").append(entry.getValue()); + first=false; + } + sb.append("}"); + + return gson.fromJson(sb.toString(), JsonElement.class); + } + } |