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