diff options
author | Enbo Wang <wangenbo@huawei.com> | 2020-02-21 14:57:01 +0800 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2020-02-21 20:21:32 +0000 |
commit | 3a325d8192addf27423494d8ed8b621dedcf7888 (patch) | |
tree | 46cae681522bff113a5bb8716b57c60c7f8ccedc /sliPluginUtils/provider/src/main/java | |
parent | dd60cf1b40744590285eec61aa1170ea14c1b9fb (diff) |
Support updating JSON object string for sliPluginUtils
Issue-ID: CCSDK-2111
Signed-off-by: Enbo Wang <wangenbo@huawei.com>
Change-Id: Ide6bb06a05fcbb3b88e73cc0fbec6fd46bd29033
Diffstat (limited to 'sliPluginUtils/provider/src/main/java')
-rw-r--r-- | sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java index 0d9ab217e..ce0f50800 100644 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java +++ b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java @@ -33,12 +33,15 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.onap.ccsdk.sli.core.sli.SvcLogicConstants; @@ -866,6 +869,58 @@ public class SliPluginUtils implements SvcLogicJavaPlugin { } /** + * updateJsonObjectString takes a json object string, and adds or deletes the properties of it + * @param parameters - requires source, outputPath and keys to be added or deleted. + * The key of parameter starts with "add.", e.g. "add.A", and then "A" and its value will be added + * to the JSON object. + * The key of parameter starts with "delete.", e.g. "delete.B", and then "B" will be deleted from + * the JSON object. + * @param ctx Reference to context memory + * @throws SvcLogicException if a required parameter is missing an exception is thrown + */ + public static void updateJsonObjectString(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException { + checkParameters(parameters, new String[] {"source", "outputPath"}, LOG); + try { + String source = ctx.getAttribute(parameters.get("source")); + JsonParser jp = new JsonParser(); + JsonElement element = jp.parse(source); + if (element.isJsonObject()) { + JsonObject jsonObject = element.getAsJsonObject(); + updateJsonObject(jsonObject, parameters); + + String target = jsonObject.toString(); + ctx.setAttribute(parameters.get("outputPath"), target); + } else { + throw new SvcLogicException("just update JSON object string"); + } + } catch (Exception ex) { + throw new SvcLogicException("problem with updateJsonObjectString", ex); + } + } + + protected static void updateJsonObject(JsonObject jsonObject, Map<String, String> parameters) throws SvcLogicException { + List<String> deleted_params = parameters.keySet().stream().filter(param -> param.startsWith("delete.")). + collect(Collectors.toList()); + for (String param: deleted_params) { + String[] action_key = param.split("\\.", 2); + if (action_key.length < 2) { + throw new SvcLogicException("error parameter format: " + param + ", must be \"delete.<key>\""); + } + jsonObject.remove(action_key[1]); + } + + List<String> added_params = parameters.keySet().stream().filter(param -> param.startsWith("add.")). + collect(Collectors.toList()); + for (String param: added_params) { + String[] action_key = param.split("\\.", 2); + if (action_key.length < 2) { + throw new SvcLogicException("error parameter format: " + param + ", must be \"add.<key>\""); + } + jsonObject.addProperty(action_key[1], parameters.get(param)); + } + } + + /** * getAttributeValue takes a ctx memory path as a string, gets the value stored at this path and set this value in context memory at * outputPath * @param parameters - requires source and outputPath |