1 package org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger;
 
   4 import java.util.Optional;
 
   5 import java.util.Properties;
 
   6 import javax.servlet.http.HttpServletRequest;
 
   7 import javax.validation.Valid;
 
   8 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
   9 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  10 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
 
  11 import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput;
 
  12 import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields;
 
  13 import org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger.RestconfApi;
 
  14 import org.slf4j.Logger;
 
  15 import org.slf4j.LoggerFactory;
 
  16 import org.springframework.beans.factory.annotation.Autowired;
 
  17 import org.springframework.http.HttpStatus;
 
  18 import org.springframework.http.ResponseEntity;
 
  19 import org.springframework.stereotype.Controller;
 
  20 import com.fasterxml.jackson.core.JsonProcessingException;
 
  21 import com.fasterxml.jackson.databind.ObjectMapper;
 
  22 import com.google.gson.Gson;
 
  23 import com.google.gson.JsonObject;
 
  25 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
 
  28 public class RestconfApiController implements RestconfApi {
 
  30         private final ObjectMapper objectMapper;
 
  31         private final HttpServletRequest request;
 
  34   protected SvcLogicServiceBase svc;
 
  35         private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
 
  37         @org.springframework.beans.factory.annotation.Autowired
 
  38         public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
 
  39                 this.objectMapper = objectMapper;
 
  40                 this.request = request;
 
  44         public ResponseEntity<ResponseFields> healthcheck() {
 
  45                 ResponseFields resp = new ResponseFields();
 
  48                         log.info("Calling SLI-API:healthcheck DG");
 
  49                         SvcLogicContext ctxIn = new SvcLogicContext();
 
  50                         SvcLogicContext ctxOut = svc.execute("sli", "healthcheck", null, "sync", ctxIn);
 
  51                         Properties respProps = ctxOut.toProperties();
 
  53                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
 
  54                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
 
  55                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
 
  56                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
 
  58                         return (new ResponseEntity<>(resp, HttpStatus.OK));
 
  59                 } catch (Exception e) {
 
  60                         resp.setAckFinalIndicator("true");
 
  61                         resp.setResponseCode("500");
 
  62                         resp.setResponseMessage(e.getMessage());
 
  63                         log.error("Error calling healthcheck directed graph", e);
 
  66                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
  70         public ResponseEntity<ResponseFields> vlbcheck() {
 
  71                 ResponseFields resp = new ResponseFields();
 
  74                         log.info("Calling SLI-API:vlbcheck DG");
 
  75                         SvcLogicContext ctxIn = new SvcLogicContext();
 
  76                         SvcLogicContext ctxOut = svc.execute("sli", "vlbcheck", null, "sync", ctxIn);
 
  77                         Properties respProps = ctxOut.toProperties();
 
  78                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
 
  79                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
 
  80                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
 
  81                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
 
  83                         return (new ResponseEntity<>(resp, HttpStatus.OK));
 
  84                 } catch (Exception e) {
 
  85                         resp.setAckFinalIndicator("true");
 
  86                         resp.setResponseCode("500");
 
  87                         resp.setResponseMessage(e.getMessage());
 
  88                         log.error("Error calling vlbcheck directed graph", e);
 
  91                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
  96         public Optional<ObjectMapper> getObjectMapper() {
 
  97                 return Optional.ofNullable(objectMapper);
 
 101         public Optional<HttpServletRequest> getRequest() {
 
 102                 return Optional.ofNullable(request);
 
 106         public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
 
 107                 SvcLogicContext ctxIn = new SvcLogicContext();
 
 108                 ResponseFields resp = new ResponseFields();
 
 109                 String executeGraphInputJson = null;
 
 112                          executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
 
 113                          log.info("Input as JSON is "+executeGraphInputJson);
 
 114                 } catch (JsonProcessingException e) {
 
 116                         resp.setAckFinalIndicator("true");
 
 117                         resp.setResponseCode("500");
 
 118                         resp.setResponseMessage(e.getMessage());
 
 119                         log.error("Cannot create JSON from input object", e);
 
 120                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
 123                 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
 
 124                 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
 
 126                 ctxIn.mergeJson("input", passthroughObj.toString());
 
 129                         // Any of these can throw a nullpointer exception
 
 130                         String calledModule = executeGraphInput.getInput().getModuleName();
 
 131                         String calledRpc = executeGraphInput.getInput().getRpcName();
 
 132                         String modeStr = executeGraphInput.getInput().getMode();
 
 133                         // execute should only throw a SvcLogicException
 
 134                         SvcLogicContext ctxOut = svc.execute(calledModule, calledRpc, null, modeStr, ctxIn);
 
 135                         Properties respProps = ctxOut.toProperties();
 
 137                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
 
 138                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
 
 139                         resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
 
 140                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
 
 141                         return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
 
 143                 } catch (NullPointerException npe) {
 
 144                         resp.setAckFinalIndicator("true");
 
 145                         resp.setResponseCode("500");
 
 146                         resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
 
 148                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
 149                 } catch (SvcLogicException e) {
 
 150                         resp.setAckFinalIndicator("true");
 
 151                         resp.setResponseCode("500");
 
 152                         resp.setResponseMessage(e.getMessage());
 
 154                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
 158         public static String propsToJson(Properties props, String root)
 
 160                 StringBuffer sbuff = new StringBuffer();
 
 162                 sbuff.append("{ \""+root+"\" : { ");
 
 163                 boolean needComma = false;
 
 164                 for (Map.Entry<Object, Object> prop : props.entrySet()) {
 
 165                         sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
 
 172                 sbuff.append(" } }");
 
 174                 return(sbuff.toString());