From 2c78899f04fd40115d3c4cc606e8380c00cdda0b Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Wed, 26 Feb 2020 18:42:16 +0000 Subject: update sliapi-springboot format source, use provider base and minor changes Issue-ID: CCSDK-2121 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I4066ce5e15b1fa298b6e3de6ed8d7d1794b12013 --- sliapi/springboot/README.md | 25 ++- sliapi/springboot/pom.xml | 184 +++++++++--------- .../sliapi/springboot/ExecuteGraphController.java | 153 ++++++++------- .../sliapi/springboot/RestconfApiController.java | 216 ++++++++++----------- 4 files changed, 298 insertions(+), 280 deletions(-) diff --git a/sliapi/springboot/README.md b/sliapi/springboot/README.md index 5737072e..3e47f341 100644 --- a/sliapi/springboot/README.md +++ b/sliapi/springboot/README.md @@ -1,10 +1,29 @@ This directory contains a demo springboot implementation of the SLI-API healthcheck method. To start this server, run: - -java -jar -DserviceLogicDirectory=src/main/resources target/sliapi-springboot-{version}.jar +mvn -DserviceLogicDirectory=src/main/resources spring-boot:run This will start a servlet on port 8080. To test to that servlet, post a blank message to that port: -curl http://127.0.0.1:8080/SLI-API:healthcheck -X POST -H "Content-Type: application/json" \ No newline at end of file +curl http://127.0.0.1:8080/restconf/operations/SLI-API:healthcheck -X POST -H "Content-Type: application/json" + +Requests can also be sent to `http://localhost:8080/executeGraph` + +The graph details need to match a graph which has been loaded and activated + +An example request +``` +{ + "graphDetails": { + "module": "prov", + "rpc": "test", + "mode": "sync" + }, + "input": { + "name": "Hello World", + "test": "one", + "mixed": "cAsE" + } +} +``` \ No newline at end of file diff --git a/sliapi/springboot/pom.xml b/sliapi/springboot/pom.xml index be6bb629..e9d5a9d8 100644 --- a/sliapi/springboot/pom.xml +++ b/sliapi/springboot/pom.xml @@ -1,100 +1,100 @@ - 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.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.springfox - springfox-swagger-ui - 2.9.2 - - - ${project.groupId} - sli-provider - ${project.version} - - - com.google.code.gson - gson - - - org.apache.derby - derby - runtime - - - - - - - io.swagger - swagger-codegen-maven-plugin - 2.3.1 - - - - generate - - - ${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.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/ExecuteGraphController.java b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/ExecuteGraphController.java index 6c024108..79499d56 100644 --- a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/ExecuteGraphController.java +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/ExecuteGraphController.java @@ -1,19 +1,16 @@ package org.onap.ccsdk.sli.core.sliapi.springboot; import java.util.HashMap; -import java.util.Map; import java.util.Map.Entry; import java.util.Properties; -import com.google.gson.*; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicLoader; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicClassResolver; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicPropertiesProviderImpl; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicServiceImpl; +import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver; import org.onap.ccsdk.sli.core.sli.provider.base.InMemorySvcLogicStore; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; @@ -24,87 +21,89 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; @Controller @EnableAutoConfiguration public class ExecuteGraphController { - static SvcLogicService svc; - private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteGraphController.class); + static SvcLogicServiceBase svc; + private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteGraphController.class); - @RequestMapping(value = "/executeGraph", method = RequestMethod.POST) - @ResponseBody - public HashMap executeGraph(@RequestBody String input) { - LOGGER.error("In request"); - LOGGER.error(input); + @RequestMapping(value = "/executeGraph", method = RequestMethod.POST) + @ResponseBody + public HashMap executeGraph(@RequestBody String input) { + LOGGER.error("In request"); + LOGGER.error(input); - HashMap hash = new HashMap(); - Properties parms = new Properties(); + HashMap hash = new HashMap(); + Properties parms = new Properties(); - hash.put("status", "success"); - JsonObject jsonInput = new Gson().fromJson(input, JsonObject.class); - JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject(); + hash.put("status", "success"); + JsonObject jsonInput = new Gson().fromJson(input, JsonObject.class); + JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject(); - writeResponseToCtx(passthroughObj.toString(), parms, "input"); + writeResponseToCtx(passthroughObj.toString(), parms, "input"); - JsonObject inputObject = jsonInput.get("graphDetails").getAsJsonObject(); - try { - // Any of these can throw a nullpointer exception - String calledModule = inputObject.get("module").getAsString(); - String calledRpc = inputObject.get("rpc").getAsString(); - String modeStr = inputObject.get("mode").getAsString(); - // execute should only throw a SvcLogicException - Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms); - for (Entry prop : respProps.entrySet()) { - hash.put((String) prop.getKey(), (String) prop.getValue()); - } - } catch (NullPointerException npe) { - HashMap errorHash = new HashMap(); - errorHash.put("error-message", "check that you populated module, rpc and or mode correctly."); - return errorHash; - } catch (SvcLogicException e) { - HashMap errorHash = new HashMap(); - errorHash.put("status", "failure"); - errorHash.put("message", e.getMessage()); - return errorHash; - } - return hash; - } + JsonObject inputObject = jsonInput.get("graphDetails").getAsJsonObject(); + try { + // Any of these can throw a nullpointer exception + String calledModule = inputObject.get("module").getAsString(); + String calledRpc = inputObject.get("rpc").getAsString(); + String modeStr = inputObject.get("mode").getAsString(); + // execute should only throw a SvcLogicException + Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms); + for (Entry prop : respProps.entrySet()) { + hash.put((String) prop.getKey(), (String) prop.getValue()); + } + } catch (NullPointerException npe) { + HashMap errorHash = new HashMap(); + errorHash.put("error-message", "check that you populated module, rpc and or mode correctly."); + return errorHash; + } catch (SvcLogicException e) { + HashMap errorHash = new HashMap(); + errorHash.put("status", "failure"); + errorHash.put("message", e.getMessage()); + return errorHash; + } + return hash; + } - 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 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 (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 { - ctx.put(root + entry.getKey(), entry.getValue().getAsString()); - } - } - } + public static void writeJsonObject(JsonObject obj, Properties ctx, String root) { + for (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 { + ctx.put(root + entry.getKey(), entry.getValue().getAsString()); + } + } + } - public static void main(String[] args) throws Exception { - InMemorySvcLogicStore store = new InMemorySvcLogicStore(); - SvcLogicLoader loader = new SvcLogicLoader(System.getProperty("serviceLogicDirectory"), store); - - loader.loadAndActivate(); - SvcLogicResolver resolver = new SvcLogicClassResolver(); - - - svc = new SvcLogicServiceImpl(new SvcLogicPropertiesProviderImpl(), resolver); - SpringApplication.run(ExecuteGraphController.class, args); - } + public static void main(String[] args) throws Exception { + InMemorySvcLogicStore store = new InMemorySvcLogicStore(); + SvcLogicLoader loader = new SvcLogicLoader(System.getProperty("serviceLogicDirectory"), store); + loader.loadAndActivate(); + SvcLogicResolver resolver = new HashMapResolver(); + svc = new SvcLogicServiceImplBase(store, resolver); + SpringApplication.run(ExecuteGraphController.class, args); + } } 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 1c22da47..8d2553a9 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 @@ -1,126 +1,126 @@ package org.onap.ccsdk.sli.core.sliapi.springboot; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.*; -import org.onap.ccsdk.sli.core.sli.*; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicClassResolver; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicPropertiesProviderImpl; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService; -import org.onap.ccsdk.sli.core.sli.provider.SvcLogicServiceImpl; -import org.onap.ccsdk.sli.core.sli.provider.base.InMemorySvcLogicStore; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Optional; +import java.util.Properties; + +import javax.servlet.http.HttpServletRequest; + +import org.onap.ccsdk.sli.core.sli.ConfigurationException; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.SvcLogicLoader; +import org.onap.ccsdk.sli.core.sli.SvcLogicStore; +import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory; +import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver; 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.sliapi.model.ExecuteGraphInput; -import org.onap.ccsdk.sli.core.sliapi.model.HealthcheckInput; +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.ResponseFields; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.boot.SpringApplication; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import javax.servlet.http.HttpServletRequest; -import javax.validation.Valid; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.Properties; + +import com.fasterxml.jackson.databind.ObjectMapper; @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00") @Controller public class RestconfApiController implements RestconfApi { - private final ObjectMapper objectMapper; - - private final HttpServletRequest request; - - private static SvcLogicService svc; - Logger log = LoggerFactory.getLogger(RestconfApiController.class); - - @org.springframework.beans.factory.annotation.Autowired - public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) { - this.objectMapper = objectMapper; - this.request = request; - - SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProviderImpl(); - SvcLogicStore store = null; - try { - store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties()); - } catch (SvcLogicException e) { - log.error("Cannot create SvcLogicStore", e); - return; - } - - String serviceLogicDirectory = System.getProperty("serviceLogicDirectory"); - System.out.println("serviceLogicDirectory is "+serviceLogicDirectory); - SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, store); - - try { - loader.loadAndActivate(); - } catch (IOException e) { - log.error("Cannot load directed graphs", e); - } - SvcLogicResolver resolver = new SvcLogicClassResolver(); - - - try { - svc = new SvcLogicServiceImpl(new SvcLogicPropertiesProviderImpl(), resolver); - } catch (SvcLogicException e) { - log.error("Cannot execute directed graph", e); - } - } - - @Override - public ResponseEntity healthcheck() { - SvcLogicContext ctx = new SvcLogicContext(); - ResponseFields resp = new ResponseFields(); - - - try { - - 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")); - - return(new ResponseEntity<>(resp, HttpStatus.OK)); - } - catch (Exception e) { - resp.setAckFinalIndicator("true"); - resp.setResponseCode("500"); - resp.setResponseMessage(e.getMessage()); - log.error("Error calling healthcheck directed graph", e); - - } - - return(new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); - - } - - @Override - public Optional getObjectMapper() { - return Optional.ofNullable(objectMapper); - } - - @Override - public Optional getRequest() { - return Optional.ofNullable(request); - } - - - - + private final ObjectMapper objectMapper; + private final HttpServletRequest request; + private static SvcLogicServiceBase svc; + private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class); + + @org.springframework.beans.factory.annotation.Autowired + public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) { + this.objectMapper = objectMapper; + this.request = request; + + SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() { + + @Override + public Properties getProperties() { + Properties props = new Properties(); + String propPath = "src/main/resources/svclogic.properties"; + System.out.println(propPath); + try (FileInputStream fileInputStream = new FileInputStream(propPath)) { + props = new Properties(); + props.load(fileInputStream); + } catch (final IOException e) { + log.error("Failed to load properties for file: {}", propPath, + new ConfigurationException("Failed to load properties for file: " + propPath, e)); + } + return props; + } + }; + + SvcLogicStore store = null; + try { + store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties()); + } catch (SvcLogicException e) { + log.error("Cannot create SvcLogicStore", e); + return; + } + + String serviceLogicDirectory = System.getProperty("serviceLogicDirectory"); + System.out.println("serviceLogicDirectory is " + serviceLogicDirectory); + SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, store); + + try { + loader.loadAndActivate(); + } catch (IOException e) { + log.error("Cannot load directed graphs", e); + } + SvcLogicResolver resolver = new HashMapResolver(); + + svc = new SvcLogicServiceImplBase(store, resolver); + + } + + @Override + public ResponseEntity healthcheck() { + ResponseFields resp = new ResponseFields(); + + try { + 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")); + + return (new ResponseEntity<>(resp, HttpStatus.OK)); + } catch (Exception e) { + resp.setAckFinalIndicator("true"); + resp.setResponseCode("500"); + resp.setResponseMessage(e.getMessage()); + log.error("Error calling healthcheck directed graph", e); + + } + return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @Override + public Optional getObjectMapper() { + return Optional.ofNullable(objectMapper); + } + + @Override + public Optional getRequest() { + return Optional.ofNullable(request); + } } -- cgit 1.2.3-korg