summaryrefslogtreecommitdiffstats
path: root/sliPluginUtils
diff options
context:
space:
mode:
Diffstat (limited to 'sliPluginUtils')
-rw-r--r--sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java55
-rw-r--r--sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java37
-rw-r--r--sliPluginUtils/provider/src/test/resources/JsonObject.json5
3 files changed, 97 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
diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
index 5c222c83f..ad039d7f4 100644
--- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
+++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java
@@ -496,6 +496,43 @@ public class SliPluginUtils_StaticFunctionsTest {
}
@Test
+ public void testUpdateJsonObjectString() throws Exception {
+ String path = "src/test/resources/JsonObject.json";
+ String content = new String(Files.readAllBytes(Paths.get(path)));
+
+ SvcLogicContext ctx = new SvcLogicContext();
+ ctx.setAttribute("input", content);
+
+ Map<String, String> parametersUpdateJson = new HashMap<String, String>();
+ parametersUpdateJson.put("source", "input");
+ parametersUpdateJson.put("outputPath", "newJsonString");
+
+ // add key "ccc" and its value
+ parametersUpdateJson.put("add.ccc", "abcxyz");
+
+ // update keys and their values of "aaa" and "c.d"
+ parametersUpdateJson.put("add.aaa", "4567");
+ parametersUpdateJson.put("add.c.d", "defg");
+
+ // delete key "bbb"
+ parametersUpdateJson.put("delete.bbb", "");
+
+ SliPluginUtils.updateJsonObjectString(parametersUpdateJson, ctx);
+
+ Map<String, String> parametersJsonToCtx = new HashMap<String, String>();
+ parametersJsonToCtx.put("source", "newJsonString");
+ parametersJsonToCtx.put("outputPath", "testPath");
+ parametersJsonToCtx.put("isEscaped", "false");
+
+ SliPluginUtils.jsonStringToCtx(parametersJsonToCtx, ctx);
+
+ assertEquals("abcxyz", ctx.getAttribute("testPath.ccc"));
+ assertEquals("4567", ctx.getAttribute("testPath.aaa"));
+ assertEquals("defg", ctx.getAttribute("testPath.c.d"));
+ assertEquals(null, ctx.getAttribute("testPath.bbb"));
+ }
+
+ @Test
public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception {
String path = "src/test/resources/EmbeddedEscapedJson.json";
String content = new String(Files.readAllBytes(Paths.get(path)));
diff --git a/sliPluginUtils/provider/src/test/resources/JsonObject.json b/sliPluginUtils/provider/src/test/resources/JsonObject.json
new file mode 100644
index 000000000..0578368f8
--- /dev/null
+++ b/sliPluginUtils/provider/src/test/resources/JsonObject.json
@@ -0,0 +1,5 @@
+{
+ "aaa": "123",
+ "bbb": "xyz",
+ "c.d": "abc"
+} \ No newline at end of file