6c024108b031c8cc1fccd9c7b0e00e720de3e7d0
[ccsdk/sli.git] /
1 package org.onap.ccsdk.sli.core.sliapi.springboot;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Map.Entry;
6 import java.util.Properties;
7
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;
26
27
28 @Controller
29 @EnableAutoConfiguration
30 public class ExecuteGraphController {
31     static SvcLogicService svc;
32     private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteGraphController.class);
33
34     @RequestMapping(value = "/executeGraph", method = RequestMethod.POST)
35     @ResponseBody
36     public HashMap<String, String> executeGraph(@RequestBody String input) {
37         LOGGER.error("In request");
38         LOGGER.error(input);
39
40         HashMap<String, String> hash = new HashMap<String, String>();
41         Properties parms = new Properties();
42
43         hash.put("status", "success");
44         JsonObject jsonInput = new Gson().fromJson(input, JsonObject.class);
45         JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
46
47         writeResponseToCtx(passthroughObj.toString(), parms, "input");
48
49         JsonObject inputObject = jsonInput.get("graphDetails").getAsJsonObject();
50         try {
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());
59             }
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.");
63             return errorHash;
64         } catch (SvcLogicException e) {
65             HashMap<String, String> errorHash = new HashMap<String, String>();
66             errorHash.put("status", "failure");
67             errorHash.put("message", e.getMessage());
68             return errorHash;
69         }
70         return hash;
71     }
72
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 + ".");
77     }
78
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()));
86                 Integer arrayIdx = 0;
87                 for (JsonElement element : array) {
88                     if (element.isJsonObject()) {
89                         writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
90                     }
91                     arrayIdx++;
92                 }
93             } else {
94                 ctx.put(root + entry.getKey(), entry.getValue().getAsString());
95             }
96         }
97     }
98
99     public static void main(String[] args) throws Exception {
100         InMemorySvcLogicStore store = new InMemorySvcLogicStore();
101         SvcLogicLoader loader = new SvcLogicLoader(System.getProperty("serviceLogicDirectory"), store);
102
103         loader.loadAndActivate();
104         SvcLogicResolver resolver = new SvcLogicClassResolver();
105
106
107         svc = new SvcLogicServiceImpl(new SvcLogicPropertiesProviderImpl(), resolver);
108         SpringApplication.run(ExecuteGraphController.class, args);
109     }
110 }