From 28d8801959ffa9b12a00114c3d389a58a0359c40 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Mon, 3 Feb 2020 16:02:18 +0000 Subject: Springboot-based SLI-API Implement SLI-API RESTCONF in springboot Change-Id: I8b9b07e3b1aeb4a5adac977307c6f95c905ea038 Issue-ID: CCSDK-2096 Signed-off-by: Dan Timoney --- .../sliapi/springboot/ExecuteGraphController.java | 110 ++++++++++++++++++ .../sliapi/springboot/RestconfApiController.java | 126 +++++++++++++++++++++ .../src/main/resources/application.properties | 8 ++ .../springboot/src/main/resources/graph.versions | 1 + .../src/main/resources/sli_healthcheck.xml | 27 +++++ .../src/main/resources/svclogic.properties | 27 +++++ 6 files changed, 299 insertions(+) create mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/ExecuteGraphController.java create mode 100644 sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java create mode 100644 sliapi/springboot/src/main/resources/application.properties create mode 100644 sliapi/springboot/src/main/resources/graph.versions create mode 100644 sliapi/springboot/src/main/resources/sli_healthcheck.xml create mode 100644 sliapi/springboot/src/main/resources/svclogic.properties (limited to 'sliapi/springboot/src') 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 new file mode 100644 index 00000000..6c024108 --- /dev/null +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/ExecuteGraphController.java @@ -0,0 +1,110 @@ +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.InMemorySvcLogicStore; +import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicResolver; +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; + + +@Controller +@EnableAutoConfiguration +public class ExecuteGraphController { + static SvcLogicService 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 SvcLogicClassResolver(); + + + svc = new SvcLogicServiceImpl(new SvcLogicPropertiesProviderImpl(), 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 new file mode 100644 index 00000000..1c22da47 --- /dev/null +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/RestconfApiController.java @@ -0,0 +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 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.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; + +@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); + } + + + + + +} diff --git a/sliapi/springboot/src/main/resources/application.properties b/sliapi/springboot/src/main/resources/application.properties new file mode 100644 index 00000000..6218d11f --- /dev/null +++ b/sliapi/springboot/src/main/resources/application.properties @@ -0,0 +1,8 @@ +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.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 diff --git a/sliapi/springboot/src/main/resources/graph.versions b/sliapi/springboot/src/main/resources/graph.versions new file mode 100644 index 00000000..9c53eb2c --- /dev/null +++ b/sliapi/springboot/src/main/resources/graph.versions @@ -0,0 +1 @@ +sli healthcheck 0.7.0 sync diff --git a/sliapi/springboot/src/main/resources/sli_healthcheck.xml b/sliapi/springboot/src/main/resources/sli_healthcheck.xml new file mode 100644 index 00000000..bc8e2f70 --- /dev/null +++ b/sliapi/springboot/src/main/resources/sli_healthcheck.xml @@ -0,0 +1,27 @@ + + + + + + + + diff --git a/sliapi/springboot/src/main/resources/svclogic.properties b/sliapi/springboot/src/main/resources/svclogic.properties new file mode 100644 index 00000000..426960f7 --- /dev/null +++ b/sliapi/springboot/src/main/resources/svclogic.properties @@ -0,0 +1,27 @@ +### +# ============LICENSE_START======================================================= +# ONAP : CCSDK +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights +# reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +org.onap.ccsdk.sli.dbtype = jdbc +org.onap.ccsdk.sli.jdbc.url=jdbc:derby:memory:sdnctl;create=true +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 -- cgit 1.2.3-korg