From ce993645564e2ba43c80fdc80a7265dc4032ceae Mon Sep 17 00:00:00 2001 From: Dan Timoney Date: Thu, 27 Feb 2020 16:27:37 -0500 Subject: Support merge JSON to SvcLogicContext Add support for merging JSON into SvcLogicContext Change-Id: If13fd6328a36ccff6393d0829319bbd99cd2a1f0 Issue-ID: CCSDK-2096 Signed-off-by: Dan Timoney --- sli/common/pom.xml | 4 ++ .../onap/ccsdk/sli/core/sli/SvcLogicContext.java | 34 +++++++++++++++ .../sli/provider/base/SvcLogicServiceBase.java | 14 ++++++ .../sli/provider/base/SvcLogicServiceImplBase.java | 26 ++++++++--- .../sliapi/springboot/RestconfApiController.java | 50 +++++----------------- .../springboot/RestconfApiControllerTest.java | 8 ++++ 6 files changed, 89 insertions(+), 47 deletions(-) diff --git a/sli/common/pom.xml b/sli/common/pom.xml index 8290a42b4..2570f33bc 100755 --- a/sli/common/pom.xml +++ b/sli/common/pom.xml @@ -61,6 +61,10 @@ org.onap.logging-analytics logging-slf4j + + com.google.code.gson + gson + junit diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java index 3681e1e11..fcd51d14b 100644 --- a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java @@ -25,6 +25,8 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Set; + +import com.google.gson.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @@ -207,6 +209,38 @@ public class SvcLogicContext { } + public void mergeJson(String pfx, String jsonString) { + JsonParser jp = new JsonParser(); + JsonElement element = jp.parse(jsonString); + + mergeJsonObject(element.getAsJsonObject(), pfx+"."); + } + + public void mergeJsonObject(JsonObject jsonObject, String pfx) { + for (Map.Entry entry : jsonObject.entrySet()) { + if (entry.getValue().isJsonObject()) { + mergeJsonObject(entry.getValue().getAsJsonObject(), pfx + entry.getKey() + "."); + } else if (entry.getValue().isJsonArray()) { + JsonArray array = entry.getValue().getAsJsonArray(); + this.setAttribute(pfx + entry.getKey() + "_length", String.valueOf(array.size())); + Integer arrayIdx = 0; + for (JsonElement element : array) { + if (element.isJsonObject()) { + mergeJsonObject(element.getAsJsonObject(), pfx + entry.getKey() + "[" + arrayIdx + "]."); + } + arrayIdx++; + } + } else { + if (entry.getValue() instanceof JsonNull) { + LOG.debug("Skipping parameter {} with null value",entry.getKey()); + + } else { + this.setAttribute(pfx + entry.getKey(), entry.getValue().getAsString()); + } + } + } + } + public String resolve(String ctxVarName) { if (ctxVarName.indexOf('[') == -1) { diff --git a/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceBase.java b/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceBase.java index 8c436fef7..3bade8132 100644 --- a/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceBase.java +++ b/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceBase.java @@ -56,6 +56,20 @@ public interface SvcLogicServiceBase { * */ Properties execute(String module, String rpc, String version, String mode, Properties parms) throws SvcLogicException; + /** + * Execute a directed graph + * + * @param module - module name + * @param rpc - rpc name + * @param version - version. If null, use active version + * @param mode - mode (sync/async) + * @param ctx - parameters, as a SvcLogicContext object + * @return final values of attributes from SvcLogicContext, as Properties + * @throws SvcLogicException + * + * + */ + SvcLogicContext execute(String module, String rpc, String version, String mode, SvcLogicContext ctx) throws SvcLogicException; SvcLogicStore getStore() throws SvcLogicException; diff --git a/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceImplBase.java b/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceImplBase.java index 80d992f28..75673f756 100644 --- a/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceImplBase.java +++ b/sli/provider-base/src/main/java/org/onap/ccsdk/sli/core/sli/provider/base/SvcLogicServiceImplBase.java @@ -142,25 +142,37 @@ public class SvcLogicServiceImplBase implements SvcLogicServiceBase { @Override public Properties execute(String module, String rpc, String version, String mode, Properties props) throws SvcLogicException { + + SvcLogicContext ctx = new SvcLogicContext(props); + + return(execute(module, rpc, version, mode, ctx).toProperties()); + } + + @Override + public SvcLogicContext execute(String module, String rpc, String version, String mode, SvcLogicContext ctx) throws SvcLogicException { SvcLogicGraph graph = store.fetch(module, rpc, version, mode); + if (ctx == null) { + ctx = new SvcLogicContext(); + } + if (graph == null) { - Properties retProps = new Properties(); - retProps.setProperty("error-code", "401"); - retProps.setProperty("error-message", + ctx.setAttribute("error-code", "401"); + ctx.setAttribute("error-message", "No service logic found for [" + module + "," + rpc + "," + version + "," + mode + "]"); - return (retProps); + return (ctx); } - SvcLogicContext ctx = new SvcLogicContext(props); + + ctx.setAttribute(CURRENT_GRAPH, graph.toString()); // To support legacy code we should not stop populating X-ECOMP-RequestID ctx.setAttribute("X-ECOMP-RequestID", MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)); execute(graph, ctx); - return (ctx.toProperties()); + return (ctx); } - @Override + @Override public SvcLogicStore getStore() throws SvcLogicException { return this.store; } diff --git a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java index 7c4717da8..2da5490ae 100644 --- a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java @@ -85,8 +85,9 @@ public class RestconfApiController implements RestconfApi { try { log.info("Calling SLI-API:healthcheck DG"); - Properties inputProps = new Properties(); - Properties respProps = svc.execute("sli", "healthcheck", null, "sync", inputProps); + SvcLogicContext ctxIn = new SvcLogicContext(); + SvcLogicContext ctxOut = svc.execute("sli", "healthcheck", null, "sync", ctxIn); + Properties respProps = ctxOut.toProperties(); resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y")); resp.setResponseCode(respProps.getProperty("error-code", "200")); @@ -110,9 +111,9 @@ public class RestconfApiController implements RestconfApi { try { log.info("Calling SLI-API:vlbcheck DG"); - Properties inputProps = new Properties(); - Properties respProps = svc.execute("sli", "vlbcheck", null, "sync", inputProps); - + SvcLogicContext ctxIn = new SvcLogicContext(); + SvcLogicContext ctxOut = svc.execute("sli", "vlbcheck", null, "sync", ctxIn); + Properties respProps = ctxOut.toProperties(); resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y")); resp.setResponseCode(respProps.getProperty("error-code", "200")); resp.setResponseMessage(respProps.getProperty("error-message", "Success")); @@ -142,7 +143,7 @@ public class RestconfApiController implements RestconfApi { @Override public ResponseEntity executeGraph(@Valid ExecuteGraphInput executeGraphInput) { - Properties parms = new Properties(); + SvcLogicContext ctxIn = new SvcLogicContext(); ResponseFields resp = new ResponseFields(); String executeGraphInputJson = null; @@ -161,8 +162,7 @@ public class RestconfApiController implements RestconfApi { JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class); JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject(); - writeResponseToCtx(passthroughObj.toString(), parms, "input"); - + ctxIn.mergeJson("input", passthroughObj.toString()); try { // Any of these can throw a nullpointer exception @@ -170,7 +170,8 @@ public class RestconfApiController implements RestconfApi { String calledRpc = executeGraphInput.getInput().getRpcName(); String modeStr = executeGraphInput.getInput().getMode(); // execute should only throw a SvcLogicException - Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms); + SvcLogicContext ctxOut = svc.execute(calledModule, calledRpc, null, modeStr, ctxIn); + Properties respProps = ctxOut.toProperties(); resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y")); resp.setResponseCode(respProps.getProperty("error-code", "200")); @@ -193,37 +194,6 @@ public class RestconfApiController implements RestconfApi { } } - public static void writeResponseToCtx(String resp, Properties ctx, String prefix) { - JsonParser jp = new JsonParser(); - JsonElement element = jp.parse(resp); - writeJsonObject(element.getAsJsonObject(), ctx, prefix + "."); - } - - public static void writeJsonObject(JsonObject obj, Properties ctx, String root) { - for (Map.Entry entry : obj.entrySet()) { - if (entry.getValue().isJsonObject()) { - writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + entry.getKey() + "."); - } else if (entry.getValue().isJsonArray()) { - JsonArray array = entry.getValue().getAsJsonArray(); - ctx.put(root + entry.getKey() + "_length", String.valueOf(array.size())); - Integer arrayIdx = 0; - for (JsonElement element : array) { - if (element.isJsonObject()) { - writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "]."); - } - arrayIdx++; - } - } else { - if (entry.getValue() instanceof JsonNull) { - log.info("Skipping parameter "+entry.getKey()+" with null value"); - - } else { - ctx.put(root + entry.getKey(), entry.getValue().getAsString()); - } - } - } - } - public static String propsToJson(Properties props, String root) { StringBuffer sbuff = new StringBuffer(); diff --git a/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiControllerTest.java b/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiControllerTest.java index 701cb3eb3..13f5939c5 100644 --- a/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiControllerTest.java +++ b/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiControllerTest.java @@ -7,6 +7,8 @@ import org.junit.runner.RunWith; import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput; import org.onap.ccsdk.sli.core.sliapi.model.ExecutegraphinputInput; import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.http.MediaType; @@ -21,6 +23,9 @@ import static org.junit.Assert.assertEquals; @WebMvcTest(RestconfApiController.class) public class RestconfApiControllerTest { + + private static final Logger log = LoggerFactory.getLogger(RestconfApiControllerTest.class); + @Autowired private MockMvc mvc; @@ -56,6 +61,7 @@ public class RestconfApiControllerTest { executeGraphInput.setInput(executeGraphData); String jsonString = mapToJson(executeGraphInput); + log.error("jsonString is {}", jsonString); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)).andReturn(); @@ -77,6 +83,8 @@ public class RestconfApiControllerTest { String jsonString = mapToJson(executeGraphInput); + log.error("jsonString is {}", jsonString); + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)).andReturn(); assertEquals(401, mvcResult.getResponse().getStatus()); -- cgit 1.2.3-korg