1 package org.onap.ccsdk.sli.core.sliapi.springboot;
3 import java.util.HashMap;
4 import java.util.Map.Entry;
5 import java.util.Properties;
7 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
8 import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;
9 import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver;
10 import org.onap.ccsdk.sli.core.sli.provider.base.InMemorySvcLogicStore;
11 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicResolver;
12 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
13 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.boot.SpringApplication;
17 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
18 import org.springframework.stereotype.Controller;
19 import org.springframework.web.bind.annotation.RequestBody;
20 import org.springframework.web.bind.annotation.RequestMapping;
21 import org.springframework.web.bind.annotation.RequestMethod;
22 import org.springframework.web.bind.annotation.ResponseBody;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
31 @EnableAutoConfiguration
32 public class ExecuteGraphController {
33 static SvcLogicServiceBase svc;
34 private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteGraphController.class);
36 @RequestMapping(value = "/executeGraph", method = RequestMethod.POST)
38 public HashMap<String, String> executeGraph(@RequestBody String input) {
39 LOGGER.error("In request");
42 HashMap<String, String> hash = new HashMap<String, String>();
43 Properties parms = new Properties();
45 hash.put("status", "success");
46 JsonObject jsonInput = new Gson().fromJson(input, JsonObject.class);
47 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
49 writeResponseToCtx(passthroughObj.toString(), parms, "input");
51 JsonObject inputObject = jsonInput.get("graphDetails").getAsJsonObject();
53 // Any of these can throw a nullpointer exception
54 String calledModule = inputObject.get("module").getAsString();
55 String calledRpc = inputObject.get("rpc").getAsString();
56 String modeStr = inputObject.get("mode").getAsString();
57 // execute should only throw a SvcLogicException
58 Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms);
59 for (Entry<Object, Object> prop : respProps.entrySet()) {
60 hash.put((String) prop.getKey(), (String) prop.getValue());
62 } catch (NullPointerException npe) {
63 HashMap<String, String> errorHash = new HashMap<String, String>();
64 errorHash.put("error-message", "check that you populated module, rpc and or mode correctly.");
66 } catch (SvcLogicException e) {
67 HashMap<String, String> errorHash = new HashMap<String, String>();
68 errorHash.put("status", "failure");
69 errorHash.put("message", e.getMessage());
75 public static void writeResponseToCtx(String resp, Properties ctx, String prefix) {
76 JsonParser jp = new JsonParser();
77 JsonElement element = jp.parse(resp);
78 writeJsonObject(element.getAsJsonObject(), ctx, prefix + ".");
81 public static void writeJsonObject(JsonObject obj, Properties ctx, String root) {
82 for (Entry<String, JsonElement> entry : obj.entrySet()) {
83 if (entry.getValue().isJsonObject()) {
84 writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + entry.getKey() + ".");
85 } else if (entry.getValue().isJsonArray()) {
86 JsonArray array = entry.getValue().getAsJsonArray();
87 ctx.put(root + entry.getKey() + "_length", String.valueOf(array.size()));
89 for (JsonElement element : array) {
90 if (element.isJsonObject()) {
91 writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
96 ctx.put(root + entry.getKey(), entry.getValue().getAsString());
101 public static void main(String[] args) throws Exception {
102 InMemorySvcLogicStore store = new InMemorySvcLogicStore();
103 SvcLogicLoader loader = new SvcLogicLoader(System.getProperty("serviceLogicDirectory"), store);
104 loader.loadAndActivate();
105 SvcLogicResolver resolver = new HashMapResolver();
106 svc = new SvcLogicServiceImplBase(store, resolver);
107 SpringApplication.run(ExecuteGraphController.class, args);