From 2ca6b604042735c56797a2eebd5472a6adaf44d4 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Tue, 3 Mar 2020 21:53:18 +0000 Subject: update sliapi-springboot organize sli api spring boot project Issue-ID: CCSDK-2154 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: Ic1690b8addf0134220b49a01d04e41eaeabf7bb6 --- .../onap/ccsdk/sli/core/sliapi/springboot/App.java | 12 ++ .../sliapi/springboot/ExecuteGraphController.java | 109 ----------- .../sliapi/springboot/RestconfApiController.java | 216 --------------------- .../controllers/ExecuteGraphController.java | 96 +++++++++ .../controllers/swagger/RestconfApiController.java | 177 +++++++++++++++++ .../sliapi/springboot/core/SvcLogicFactory.java | 71 +++++++ .../src/main/resources/application.properties | 4 +- .../springboot/RestconfApiControllerTest.java | 125 ++++++------ 8 files changed, 425 insertions(+), 385 deletions(-) create mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java delete mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/ExecuteGraphController.java delete mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java create mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/ExecuteGraphController.java create mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/swagger/RestconfApiController.java create mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/core/SvcLogicFactory.java (limited to 'sliapi/springboot/src') diff --git a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java new file mode 100644 index 00000000..941c1d51 --- /dev/null +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java @@ -0,0 +1,12 @@ +package org.onap.ccsdk.sli.core.sliapi.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class App { + + public static void main(String[] args) throws Exception { + SpringApplication.run(App.class, args); + } +} 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 deleted file mode 100644 index 79499d56..00000000 --- a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/ExecuteGraphController.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.onap.ccsdk.sli.core.sliapi.springboot; - -import java.util.HashMap; -import java.util.Map.Entry; -import java.util.Properties; - -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.onap.ccsdk.sli.core.sli.SvcLogicLoader; -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; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestBody; -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 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); - - 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(); - - 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; - } - - 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 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 deleted file mode 100644 index 2da5490a..00000000 --- a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java +++ /dev/null @@ -1,216 +0,0 @@ -package org.onap.ccsdk.sli.core.sliapi.springboot; - -import com.fasterxml.jackson.core.JsonProcessingException; -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.base.*; -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; -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.FileInputStream; -import java.io.IOException; -import java.util.Map; -import java.util.Optional; -import java.util.Properties; - -@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 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", "src/main/resources"); - 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"); - 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")); - 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 healthcheck directed graph", e); - - } - return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); - } - - @Override - public ResponseEntity vlbcheck() { - ResponseFields resp = new ResponseFields(); - - try { - log.info("Calling SLI-API:vlbcheck DG"); - 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")); - 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); - } - - @Override - public Optional getRequest() { - return Optional.ofNullable(request); - } - - @Override - public ResponseEntity executeGraph(@Valid ExecuteGraphInput executeGraphInput) { - SvcLogicContext ctxIn = new SvcLogicContext(); - 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(); - - ctxIn.mergeJson("input", passthroughObj.toString()); - - 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 - 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")); - 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 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/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/ExecuteGraphController.java b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/ExecuteGraphController.java new file mode 100644 index 00000000..efdefbd6 --- /dev/null +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/ExecuteGraphController.java @@ -0,0 +1,96 @@ +package org.onap.ccsdk.sli.core.sliapi.springboot.controllers; + +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.Properties; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +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 { + @Autowired + protected 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); + + 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(); + + 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; + } + + 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()); + } + } + } + + +} diff --git a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/swagger/RestconfApiController.java b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/swagger/RestconfApiController.java new file mode 100644 index 00000000..10442d43 --- /dev/null +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/controllers/swagger/RestconfApiController.java @@ -0,0 +1,177 @@ +package org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger; + +import java.util.Map; +import java.util.Optional; +import java.util.Properties; +import javax.servlet.http.HttpServletRequest; +import javax.validation.Valid; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase; +import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput; +import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields; +import org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger.RestconfApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +@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; + + @Autowired + protected 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; + } + + @Override + public ResponseEntity healthcheck() { + ResponseFields resp = new ResponseFields(); + + try { + log.info("Calling SLI-API:healthcheck DG"); + 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")); + 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 healthcheck directed graph", e); + + } + return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @Override + public ResponseEntity vlbcheck() { + ResponseFields resp = new ResponseFields(); + + try { + log.info("Calling SLI-API:vlbcheck DG"); + 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")); + 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); + } + + @Override + public Optional getRequest() { + return Optional.ofNullable(request); + } + + @Override + public ResponseEntity executeGraph(@Valid ExecuteGraphInput executeGraphInput) { + SvcLogicContext ctxIn = new SvcLogicContext(); + 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(); + + ctxIn.mergeJson("input", passthroughObj.toString()); + + 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 + 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")); + 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 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/java/org/onap/ccsdk/sli/core/sliapi/springboot/core/SvcLogicFactory.java b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/core/SvcLogicFactory.java new file mode 100644 index 00000000..f39fc229 --- /dev/null +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/core/SvcLogicFactory.java @@ -0,0 +1,71 @@ +package org.onap.ccsdk.sli.core.sliapi.springboot.core; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; +import org.onap.ccsdk.sli.core.sli.ConfigurationException; +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.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.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SvcLogicFactory { + private static final Logger log = LoggerFactory.getLogger(SvcLogicFactory.class); + + @Bean + public SvcLogicStore getStore() throws Exception { + 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 = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties()); + return store; + } + + @Bean + public SvcLogicLoader createLoader() throws Exception { + String serviceLogicDirectory = System.getProperty("serviceLogicDirectory"); + if (serviceLogicDirectory == null) { + serviceLogicDirectory = "src/main/resources"; + } + + System.out.println("serviceLogicDirectory is " + serviceLogicDirectory); + SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, getStore()); + + try { + loader.loadAndActivate(); + } catch (IOException e) { + log.error("Cannot load directed graphs", e); + } + return loader; + } + + @Bean + public SvcLogicServiceBase createService() throws Exception { + SvcLogicResolver resolver = new HashMapResolver(); + return new SvcLogicServiceImplBase(getStore(), resolver); + } + +} diff --git a/sliapi/springboot/src/main/resources/application.properties b/sliapi/springboot/src/main/resources/application.properties index 64415bd3..851488b9 100644 --- a/sliapi/springboot/src/main/resources/application.properties +++ b/sliapi/springboot/src/main/resources/application.properties @@ -1,8 +1,10 @@ springfox.documentation.swagger.v2.path=/api-docs server.contextPath=/restconf server.port=8080 -spring.jackson.date-format=org.onap.ccsdk.sli.core.sliapi.springboot.RFC3339DateFormat +spring.jackson.date-format=org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger.RFC3339DateFormat 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 +logging.level.com.att=TRACE +logging.level.org.onap=TRACE \ No newline at end of file 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 13f5939c..e34d1187 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 @@ -1,7 +1,6 @@ package org.onap.ccsdk.sli.core.sliapi.springboot; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput; @@ -10,94 +9,102 @@ 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.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; 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; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; @RunWith(SpringRunner.class) -@WebMvcTest(RestconfApiController.class) +@SpringBootTest +@AutoConfigureMockMvc public class RestconfApiControllerTest { + private static final Logger log = LoggerFactory.getLogger(RestconfApiControllerTest.class); - private static final Logger log = LoggerFactory.getLogger(RestconfApiControllerTest.class); - - @Autowired - private MockMvc mvc; - + @Autowired + private MockMvc mvc; - @Test - public void testHealthcheck() throws Exception { - String url = "/restconf/operations/SLI-API:healthcheck"; + @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(); + MvcResult mvcResult = + mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content("")) + .andReturn(); - assertEquals(200, mvcResult.getResponse().getStatus()); - } + assertEquals(200, mvcResult.getResponse().getStatus()); + } - @Test - public void testVlbcheck() throws Exception { - String url = "/restconf/operations/SLI-API:vlbcheck"; + @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(); + MvcResult mvcResult = + mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content("")) + .andReturn(); - assertEquals(200, mvcResult.getResponse().getStatus()); - } + assertEquals(200, mvcResult.getResponse().getStatus()); + } - @Test - public void testExecuteHealthcheck() throws Exception { - String url = "/restconf/operations/SLI-API:execute-graph"; + @Test + public void testExecuteHealthcheck() throws Exception { + String url = "/restconf/operations/SLI-API:execute-graph"; - ExecuteGraphInput executeGraphInput = new ExecuteGraphInput(); - ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput(); + ExecuteGraphInput executeGraphInput = new ExecuteGraphInput(); + ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput(); - executeGraphData.setModuleName("sli"); - executeGraphData.setRpcName("healthcheck"); - executeGraphData.setMode("sync"); - executeGraphInput.setInput(executeGraphData); + executeGraphData.setModuleName("sli"); + executeGraphData.setRpcName("healthcheck"); + executeGraphData.setMode("sync"); + executeGraphInput.setInput(executeGraphData); - String jsonString = mapToJson(executeGraphInput); - log.error("jsonString is {}", jsonString); + String jsonString = mapToJson(executeGraphInput); + log.error("jsonString is {}", jsonString); - MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)).andReturn(); + MvcResult mvcResult = + mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)) + .andReturn(); - assertEquals(200, mvcResult.getResponse().getStatus()); + assertEquals(200, mvcResult.getResponse().getStatus()); - } + } - @Test - public void testExecuteMissingDg() throws Exception { - String url = "/restconf/operations/SLI-API:execute-graph"; + @Test + public void testExecuteMissingDg() throws Exception { + String url = "/restconf/operations/SLI-API:execute-graph"; - ExecuteGraphInput executeGraphInput = new ExecuteGraphInput(); - ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput(); + ExecuteGraphInput executeGraphInput = new ExecuteGraphInput(); + ExecutegraphinputInput executeGraphData = new ExecutegraphinputInput(); - executeGraphData.setModuleName("sli"); - executeGraphData.setRpcName("noSuchRPC"); - executeGraphData.setMode("sync"); - executeGraphInput.setInput(executeGraphData); + executeGraphData.setModuleName("sli"); + executeGraphData.setRpcName("noSuchRPC"); + executeGraphData.setMode("sync"); + executeGraphInput.setInput(executeGraphData); - String jsonString = mapToJson(executeGraphInput); + String jsonString = mapToJson(executeGraphInput); - log.error("jsonString is {}", jsonString); + log.error("jsonString is {}", jsonString); - MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)).andReturn(); + MvcResult mvcResult = + mvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON_VALUE).content(jsonString)) + .andReturn(); - assertEquals(401, mvcResult.getResponse().getStatus()); + assertEquals(401, mvcResult.getResponse().getStatus()); - } + } - private String mapToJson(Object obj) throws JsonProcessingException { - ObjectMapper objectMapper = new ObjectMapper(); - return objectMapper.writeValueAsString(obj); - } + 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)); - } + private ResponseFields respFromJson(String jsonString) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + return (objectMapper.readValue(jsonString, ResponseFields.class)); + } } -- cgit 1.2.3-korg