Add execute-graph and vlbcheck to swagger-codegen code
[ccsdk/sli/core.git] / sliapi / springboot / src / main / java / org / onap / ccsdk / sli / core / sliapi / springboot / RestconfApiController.java
index 8d2553a..e2cd864 100644 (file)
@@ -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<ResponseFields> 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<ObjectMapper> getObjectMapper() {
                return Optional.ofNullable(objectMapper);
@@ -123,4 +151,107 @@ public class RestconfApiController implements RestconfApi {
                return Optional.ofNullable(request);
        }
 
+       @Override
+       public ResponseEntity<ResponseFields> 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<String, JsonElement> 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<Object, Object> prop : props.entrySet()) {
+                       sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
+                       if (needComma) {
+                               sbuff.append(" , ");
+                       } else {
+                               needComma = true;
+                       }
+               }
+               sbuff.append(" } }");
+
+               return(sbuff.toString());
+       }
+
 }