diff options
Diffstat (limited to 'appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/onap/appc/dg/common/impl/JsonDgUtilImpl.java')
-rw-r--r-- | appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/onap/appc/dg/common/impl/JsonDgUtilImpl.java | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/onap/appc/dg/common/impl/JsonDgUtilImpl.java b/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/onap/appc/dg/common/impl/JsonDgUtilImpl.java index 3d43dc1cd..16488e242 100644 --- a/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/onap/appc/dg/common/impl/JsonDgUtilImpl.java +++ b/appc-dg/appc-dg-shared/appc-dg-common/src/main/java/org/onap/appc/dg/common/impl/JsonDgUtilImpl.java @@ -47,11 +47,8 @@ public class JsonDgUtilImpl implements JsonDgUtil { private static final EELFLogger logger = EELFManager.getInstance().getLogger(JsonDgUtilImpl.class); - private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() { - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - } - }; + private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL = ThreadLocal + .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); @Override public void flatAndAddToContext(Map<String, String> params, SvcLogicContext ctx) throws APPCException { @@ -69,11 +66,7 @@ public class JsonDgUtilImpl implements JsonDgUtil { } if (!StringUtils.isEmpty(payload)) { Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(payload); - if (flatMap != null && flatMap.size() > 0) { - for (Map.Entry<String, String> entry : flatMap.entrySet()) { - ctx.setAttribute(entry.getKey(), entry.getValue()); - } - } + tryUpdateContext(ctx, flatMap); } else { logger.warn("input payload param value is empty (\"\") or null"); } @@ -85,6 +78,14 @@ public class JsonDgUtilImpl implements JsonDgUtil { } } + private void tryUpdateContext(SvcLogicContext ctx, Map<String, String> flatMap) { + if (flatMap != null && flatMap.size() > 0) { + for (Map.Entry<String, String> entry : flatMap.entrySet()) { + ctx.setAttribute(entry.getKey(), entry.getValue()); + } + } + } + @Override public void generateOutputPayloadFromContext(Map<String, String> params, SvcLogicContext ctx) throws APPCException { if (logger.isTraceEnabled()) { @@ -95,30 +96,12 @@ public class JsonDgUtilImpl implements JsonDgUtil { try { Set<String> keys = ctx.getAttributeKeySet(); ObjectMapper objectMapper = new ObjectMapper(); - ObjectNode JsonNode = objectMapper.createObjectNode(); + ObjectNode jsonNode = objectMapper.createObjectNode(); for (String key : keys) { - if (key.startsWith(Constants.OUTPUT_PAYLOAD + ".")) { - String objkey = key.replaceFirst(Constants.OUTPUT_PAYLOAD + ".", ""); - if (objkey.contains("[") && objkey.contains("]")) { - ArrayNode arrayNode; - String arrayKey = objkey.substring(0, objkey.indexOf('[')); - int arrayIndex = Integer - .parseInt(objkey.substring(objkey.indexOf('[') + 1, objkey.indexOf(']'))); - if (JsonNode.has(arrayKey)) { - arrayNode = (ArrayNode) JsonNode.get(arrayKey); - arrayNode.insert(arrayIndex, ctx.getAttribute(key)); - } else { - arrayNode = objectMapper.createArrayNode(); - arrayNode.insert(arrayIndex, ctx.getAttribute(key)); - JsonNode.put(arrayKey, arrayNode); - } - } else { - JsonNode.put(objkey, ctx.getAttribute(key)); - } - } + updateJsonNode(ctx, objectMapper, jsonNode, key); } - if (JsonNode.size() > 0) { - ctx.setAttribute(Constants.OUTPUT_PAYLOAD, objectMapper.writeValueAsString(JsonNode)); + if (jsonNode.size() > 0) { + ctx.setAttribute(Constants.OUTPUT_PAYLOAD, objectMapper.writeValueAsString(jsonNode)); } } catch (Exception e) { logger.error(e.toString()); @@ -128,6 +111,28 @@ public class JsonDgUtilImpl implements JsonDgUtil { } + private void updateJsonNode(SvcLogicContext ctx, ObjectMapper objectMapper, ObjectNode jsonNode, String key) { + if (key.startsWith(Constants.OUTPUT_PAYLOAD + ".")) { + String objkey = key.replaceFirst(Constants.OUTPUT_PAYLOAD + ".", ""); + if (objkey.contains("[") && objkey.contains("]")) { + ArrayNode arrayNode; + String arrayKey = objkey.substring(0, objkey.indexOf('[')); + int arrayIndex = Integer + .parseInt(objkey.substring(objkey.indexOf('[') + 1, objkey.indexOf(']'))); + if (jsonNode.has(arrayKey)) { + arrayNode = (ArrayNode) jsonNode.get(arrayKey); + arrayNode.insert(arrayIndex, ctx.getAttribute(key)); + } else { + arrayNode = objectMapper.createArrayNode(); + arrayNode.insert(arrayIndex, ctx.getAttribute(key)); + jsonNode.put(arrayKey, arrayNode); + } + } else { + jsonNode.put(objkey, ctx.getAttribute(key)); + } + } + } + @Override public void cvaasFileNameAndFileContentToContext(Map<String, String> params, SvcLogicContext ctx) throws APPCException { |