1 package org.onap.ccsdk.sli.core.sliapi.springboot.controllers;
 
   3 import java.util.HashMap;
 
   4 import java.util.Map.Entry;
 
   5 import java.util.Properties;
 
   6 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
   7 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
 
   8 import org.slf4j.Logger;
 
   9 import org.slf4j.LoggerFactory;
 
  10 import org.springframework.beans.factory.annotation.Autowired;
 
  11 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 
  12 import org.springframework.stereotype.Controller;
 
  13 import org.springframework.web.bind.annotation.RequestBody;
 
  14 import org.springframework.web.bind.annotation.RequestMapping;
 
  15 import org.springframework.web.bind.annotation.RequestMethod;
 
  16 import org.springframework.web.bind.annotation.ResponseBody;
 
  17 import com.google.gson.Gson;
 
  18 import com.google.gson.JsonArray;
 
  19 import com.google.gson.JsonElement;
 
  20 import com.google.gson.JsonObject;
 
  21 import com.google.gson.JsonParser;
 
  24 @EnableAutoConfiguration
 
  25 public class ExecuteGraphController {
 
  27   protected SvcLogicServiceBase svc;
 
  28         private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteGraphController.class);
 
  30         @RequestMapping(value = "/executeGraph", method = RequestMethod.POST)
 
  32         public HashMap<String, String> executeGraph(@RequestBody String input) {
 
  33                 LOGGER.error("In request");
 
  36                 HashMap<String, String> hash = new HashMap<String, String>();
 
  37                 Properties parms = new Properties();
 
  39                 hash.put("status", "success");
 
  40                 JsonObject jsonInput = new Gson().fromJson(input, JsonObject.class);
 
  41                 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
 
  43                 writeResponseToCtx(passthroughObj.toString(), parms, "input");
 
  45                 JsonObject inputObject = jsonInput.get("graphDetails").getAsJsonObject();
 
  47                         // Any of these can throw a nullpointer exception
 
  48                         String calledModule = inputObject.get("module").getAsString();
 
  49                         String calledRpc = inputObject.get("rpc").getAsString();
 
  50                         String modeStr = inputObject.get("mode").getAsString();
 
  51                         // execute should only throw a SvcLogicException
 
  52                         Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms);
 
  53                         for (Entry<Object, Object> prop : respProps.entrySet()) {
 
  54                                 hash.put((String) prop.getKey(), (String) prop.getValue());
 
  56                 } catch (NullPointerException npe) {
 
  57                         HashMap<String, String> errorHash = new HashMap<String, String>();
 
  58                         errorHash.put("error-message", "check that you populated module, rpc and or mode correctly.");
 
  60                 } catch (SvcLogicException e) {
 
  61                         HashMap<String, String> errorHash = new HashMap<String, String>();
 
  62                         errorHash.put("status", "failure");
 
  63                         errorHash.put("message", e.getMessage());
 
  69         public static void writeResponseToCtx(String resp, Properties ctx, String prefix) {
 
  70                 JsonParser jp = new JsonParser();
 
  71                 JsonElement element = jp.parse(resp);
 
  72                 writeJsonObject(element.getAsJsonObject(), ctx, prefix + ".");
 
  75         public static void writeJsonObject(JsonObject obj, Properties ctx, String root) {
 
  76                 for (Entry<String, JsonElement> entry : obj.entrySet()) {
 
  77                         if (entry.getValue().isJsonObject()) {
 
  78                                 writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + entry.getKey() + ".");
 
  79                         } else if (entry.getValue().isJsonArray()) {
 
  80                                 JsonArray array = entry.getValue().getAsJsonArray();
 
  81                                 ctx.put(root + entry.getKey() + "_length", String.valueOf(array.size()));
 
  83                                 for (JsonElement element : array) {
 
  84                                         if (element.isJsonObject()) {
 
  85                                                 writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
 
  90                                 ctx.put(root + entry.getKey(), entry.getValue().getAsString());