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 --- .../onap/ccsdk/sli/core/sli/SvcLogicContext.java | 2 +- .../model/src/main/resources/sli-api.20161110.yaml | 43 ++--- sliapi/springboot/pom.xml | 196 +++++++++++---------- .../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 + .../springboot/RestconfApiControllerTest.java | 99 +++++++++++ 9 files changed, 387 insertions(+), 132 deletions(-) create mode 100644 sliapi/springboot/src/main/resources/sli_vlbcheck.xml create mode 100644 sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiControllerTest.java 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 549e6364..3681e1e1 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 @@ -91,7 +91,7 @@ public class SvcLogicContext { { return attributes.keySet(); } - public Boolean isSuccess() { + public Boolean isSuccess() { return status.equals(SvcLogicConstants.SUCCESS); } diff --git a/sliapi/model/src/main/resources/sli-api.20161110.yaml b/sliapi/model/src/main/resources/sli-api.20161110.yaml index db60aa1e..3420c66b 100644 --- a/sliapi/model/src/main/resources/sli-api.20161110.yaml +++ b/sliapi/model/src/main/resources/sli-api.20161110.yaml @@ -164,12 +164,6 @@ paths: produces: - application/json - application/xml -# parameters: -# - in: body -# name: healthcheckInput -# required: false -# schema: -# $ref: '#/definitions/healthcheck-input' responses: '200': description: No response was specified @@ -184,12 +178,6 @@ paths: produces: - application/json - application/xml - parameters: - - in: body - name: vlbcheckInput - required: false - schema: - $ref: '#/definitions/vlbcheck-input' responses: '200': description: No response was specified @@ -242,28 +230,23 @@ definitions: execute-graph-input: properties: - 'mode': - type: string - 'module-name': - type: string - 'rpc-name': - type: string - 'sli-parameter': - items: - $ref: '#/definitions/parameter-setting' - type: array + 'input': + type: object + properties: + 'mode': + type: string + 'module-name': + type: string + 'rpc-name': + type: string + 'sli-parameter': + items: + $ref: '#/definitions/parameter-setting' + type: array type: object - healthcheck-input: - properties: - 'dummy': - type: string - vlbcheck-input: - properties: - 'dummy': - type: string diff --git a/sliapi/springboot/pom.xml b/sliapi/springboot/pom.xml index e9d5a9d8..d7962c57 100644 --- a/sliapi/springboot/pom.xml +++ b/sliapi/springboot/pom.xml @@ -1,100 +1,112 @@ - 4.0.0 + 4.0.0 - - org.onap.ccsdk.parent - spring-boot-starter-parent - 1.5.2-SNAPSHOT - + + org.onap.ccsdk.parent + spring-boot-starter-parent + 1.5.2-SNAPSHOT + - org.onap.ccsdk.sli.core - sliapi-springboot - 0.7.1-SNAPSHOT - jar + org.onap.ccsdk.sli.core + sliapi-springboot + 0.7.1-SNAPSHOT + jar - sliapi-springboot + sliapi-springboot - - - io.swagger - swagger-annotations - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-jpa - - - io.springfox - springfox-swagger2 - 2.9.2 - - - io.springfox - springfox-swagger-ui - 2.9.2 - - - ${project.groupId} - sli-provider-base - ${project.version} - - - com.google.code.gson - gson - - - org.apache.derby - derby - runtime - - + + + io.swagger + swagger-annotations + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-data-jpa + + + io.springfox + springfox-swagger2 + 2.9.2 + - - - - io.swagger - swagger-codegen-maven-plugin - 2.3.1 - - - - generate - - - target/generated-sources - ${project.basedir}/../model/src/main/resources/sli-api.20161110.yaml - spring - org.onap.ccsdk.sli.core.sliapi.springboot - org.onap.ccsdk.sli.core.sliapi.model - org.onap.ccsdk.sli.core.sliapi.springboot - true - true - ${project.basedir}/.swagger-codegen-ignore - true - - true - 2.2.4-RELEASE - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - - - + + io.springfox + springfox-swagger-ui + 2.9.2 + + + ${project.groupId} + sli-provider-base + ${project.version} + + + com.google.code.gson + gson + + + org.apache.derby + derby + runtime + + + junit + junit + test + + + + + + + io.swagger + swagger-codegen-maven-plugin + 2.3.1 + + + + generate + + + target/generated-sources + ${project.basedir}/../model/src/main/resources/sli-api.20161110.yaml + spring + org.onap.ccsdk.sli.core.sliapi.springboot + org.onap.ccsdk.sli.core.sliapi.model + org.onap.ccsdk.sli.core.sliapi.springboot + true + true + ${project.basedir}/.swagger-codegen-ignore + true + + true + 2.2.4-RELEASE + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + 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 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 new file mode 100644 index 00000000..af55a518 --- /dev/null +++ b/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiControllerTest.java @@ -0,0 +1,99 @@ +package org.onap.ccsdk.sli.core.sliapi.springboot; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@WebMvcTest(RestconfApiController.class) +public class RestconfApiControllerTest { + + @Autowired + private MockMvc mvc; + + @MockBean + private RestconfApiController restconfApiController; + + @Test + public void testHealthcheck() throws Exception { + String url = "/restconf/operations/SLI-API:healthcheck"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content("")).andReturn(); + + assertEquals(200, mvcResult.getResponse().getStatus()); + } + + @Test + public void testVlbcheck() throws Exception { + String url = "/restconf/operations/SLI-API:vlbcheck"; + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content("")).andReturn(); + + assertEquals(200, mvcResult.getResponse().getStatus()); + } + + @Test + public void testExecuteHealthcheck() throws Exception { + String url = "/restconf/operations/SLI-API:execute-graph"; + + ExecuteGraphInput executeGraphInput = new ExecuteGraphInput(); + ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput(); + + executeGraphData.setModuleName("sli"); + executeGraphData.setRpcName("healthcheck"); + executeGraphData.setMode("sync"); + executeGraphInput.setInput(executeGraphData); + + String jsonString = mapToJson(executeGraphInput); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)).andReturn(); + + assertEquals(200, mvcResult.getResponse().getStatus()); + + } + + @Test + public void testExecuteMissingDg() throws Exception { + String url = "/restconf/operations/SLI-API:execute-graph"; + + ExecuteGraphInput executeGraphInput = new ExecuteGraphInput(); + ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput(); + + executeGraphData.setModuleName("sli"); + executeGraphData.setRpcName("noSuchRPC"); + executeGraphData.setMode("sync"); + executeGraphInput.setInput(executeGraphData); + + String jsonString = mapToJson(executeGraphInput); + + MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)).andReturn(); + + // Note: this really should return 401 (and truly does), but mockito always returns a 200. + assertEquals(200, mvcResult.getResponse().getStatus()); + + } + + private String mapToJson(Object obj) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.writeValueAsString(obj); + } + + private ResponseFields respFromJson(String jsonString) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + return(objectMapper.readValue(jsonString, ResponseFields.class)); + } +} -- cgit 1.2.3-korg