From ec7aa8b8c9fbe23f7628eb61bea93c0136cff6c5 Mon Sep 17 00:00:00 2001 From: Dan Timoney Date: Wed, 26 Feb 2020 16:07:53 -0500 Subject: Add execute-graph and vlbcheck to swagger-codegen code Add implementation for execute-graph and vlbcheck RPCs. Also, added junit tests. Change-Id: Id2a31c033d25ef12c455bfee98c5aea8439fc2f9 Issue-ID: CCSDK-2096 Signed-off-by: Dan Timoney --- .../sliapi/springboot/RestconfApiController.java | 147 +++++++++++++++++++-- .../src/main/resources/application.properties | 2 +- .../springboot/src/main/resources/graph.versions | 1 + .../springboot/src/main/resources/sli_vlbcheck.xml | 27 ++++ .../src/main/resources/svclogic.properties | 2 + 5 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 sliapi/springboot/src/main/resources/sli_vlbcheck.xml (limited to 'sliapi/springboot/src/main') 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 8d2553a9..e2cd864d 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 @@ -2,11 +2,16 @@ package org.onap.ccsdk.sli.core.sliapi.springboot; import java.io.FileInputStream; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import java.util.Properties; import javax.servlet.http.HttpServletRequest; +import javax.validation.Valid; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.gson.*; import org.onap.ccsdk.sli.core.sli.ConfigurationException; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicLoader; @@ -17,6 +22,7 @@ import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider; import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicResolver; import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase; import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase; +import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput; import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -33,6 +39,8 @@ public class RestconfApiController implements RestconfApi { private final ObjectMapper objectMapper; private final HttpServletRequest request; + + private static SvcLogicServiceBase svc; private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class); @@ -90,17 +98,11 @@ public class RestconfApiController implements RestconfApi { log.info("Calling SLI-API:healthcheck DG"); Properties inputProps = new Properties(); Properties respProps = svc.execute("sli", "healthcheck", null, "sync", inputProps); - if (respProps == null) { - log.info("DG execution returned no properties!"); - } else { - log.info("DG execution returned properties"); - for (String key : respProps.stringPropertyNames()) { - log.info("DG returned property " + key + " = " + respProps.getProperty(key)); - } - } + resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y")); resp.setResponseCode(respProps.getProperty("error-code", "200")); resp.setResponseMessage(respProps.getProperty("error-message", "Success")); + resp.setContextMemoryJson(propsToJson(respProps, "context-memory")); return (new ResponseEntity<>(resp, HttpStatus.OK)); } catch (Exception e) { @@ -113,6 +115,32 @@ public class RestconfApiController implements RestconfApi { return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); } + @Override + public ResponseEntity vlbcheck() { + ResponseFields resp = new ResponseFields(); + + try { + log.info("Calling SLI-API:vlbcheck DG"); + Properties inputProps = new Properties(); + Properties respProps = svc.execute("sli", "vlbcheck", null, "sync", inputProps); + + resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y")); + resp.setResponseCode(respProps.getProperty("error-code", "200")); + resp.setResponseMessage(respProps.getProperty("error-message", "Success")); + resp.setContextMemoryJson(propsToJson(respProps, "context-memory")); + + return (new ResponseEntity<>(resp, HttpStatus.OK)); + } catch (Exception e) { + resp.setAckFinalIndicator("true"); + resp.setResponseCode("500"); + resp.setResponseMessage(e.getMessage()); + log.error("Error calling vlbcheck directed graph", e); + + } + return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @Override public Optional getObjectMapper() { return Optional.ofNullable(objectMapper); @@ -123,4 +151,107 @@ public class RestconfApiController implements RestconfApi { return Optional.ofNullable(request); } + @Override + public ResponseEntity executeGraph(@Valid ExecuteGraphInput executeGraphInput) { + Properties parms = new Properties(); + ResponseFields resp = new ResponseFields(); + String executeGraphInputJson = null; + + try { + executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput); + log.info("Input as JSON is "+executeGraphInputJson); + } catch (JsonProcessingException e) { + + resp.setAckFinalIndicator("true"); + resp.setResponseCode("500"); + resp.setResponseMessage(e.getMessage()); + log.error("Cannot create JSON from input object", e); + return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); + + } + JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class); + JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject(); + + writeResponseToCtx(passthroughObj.toString(), parms, "input"); + + + try { + // Any of these can throw a nullpointer exception + String calledModule = executeGraphInput.getInput().getModuleName(); + 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); + + resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y")); + resp.setResponseCode(respProps.getProperty("error-code", "200")); + resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS")); + resp.setContextMemoryJson(propsToJson(respProps, "context-memory")); + return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode())))); + + } catch (NullPointerException npe) { + resp.setAckFinalIndicator("true"); + resp.setResponseCode("500"); + resp.setResponseMessage("Check that you populated module, rpc and or mode correctly."); + + return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); + } catch (SvcLogicException e) { + resp.setAckFinalIndicator("true"); + resp.setResponseCode("500"); + resp.setResponseMessage(e.getMessage()); + + return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); + } + } + + 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(); + + sbuff.append("{ \""+root+"\" : { "); + boolean needComma = false; + for (Map.Entry prop : props.entrySet()) { + sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\""); + if (needComma) { + sbuff.append(" , "); + } else { + needComma = true; + } + } + sbuff.append(" } }"); + + return(sbuff.toString()); + } + } diff --git a/sliapi/springboot/src/main/resources/application.properties b/sliapi/springboot/src/main/resources/application.properties index 6218d11f..64415bd3 100644 --- a/sliapi/springboot/src/main/resources/application.properties +++ b/sliapi/springboot/src/main/resources/application.properties @@ -5,4 +5,4 @@ spring.jackson.date-format=org.onap.ccsdk.sli.core.sliapi.springboot.RFC3339Date spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false spring.datasource.url=jdbc:derby:sdnctl;create=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect -spring.jpa.hibernate.ddl-auto=update \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update diff --git a/sliapi/springboot/src/main/resources/graph.versions b/sliapi/springboot/src/main/resources/graph.versions index 9c53eb2c..7f75be4c 100644 --- a/sliapi/springboot/src/main/resources/graph.versions +++ b/sliapi/springboot/src/main/resources/graph.versions @@ -1 +1,2 @@ sli healthcheck 0.7.0 sync +sli vlbcheck 0.7.0 sync diff --git a/sliapi/springboot/src/main/resources/sli_vlbcheck.xml b/sliapi/springboot/src/main/resources/sli_vlbcheck.xml new file mode 100644 index 00000000..820a85c1 --- /dev/null +++ b/sliapi/springboot/src/main/resources/sli_vlbcheck.xml @@ -0,0 +1,27 @@ + + + + + + + + diff --git a/sliapi/springboot/src/main/resources/svclogic.properties b/sliapi/springboot/src/main/resources/svclogic.properties index 426960f7..1d90ab9b 100644 --- a/sliapi/springboot/src/main/resources/svclogic.properties +++ b/sliapi/springboot/src/main/resources/svclogic.properties @@ -25,3 +25,5 @@ org.onap.ccsdk.sli.jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver org.onap.ccsdk.sli.jdbc.database = sdnctl org.onap.ccsdk.sli.jdbc.user = test org.onap.ccsdk.sli.jdbc.password = test + +sliapi.serviceLogicDirectory=/opt/onap/sdnc/svclogic/graphs -- cgit 1.2.3-korg