efdefbd6f61bf9a5918c179277f5af03ecb7abd0
[ccsdk/sli.git] /
1 package org.onap.ccsdk.sli.core.sliapi.springboot.controllers;
2
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;
22
23 @Controller
24 @EnableAutoConfiguration
25 public class ExecuteGraphController {
26   @Autowired
27   protected SvcLogicServiceBase svc;
28         private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteGraphController.class);
29
30         @RequestMapping(value = "/executeGraph", method = RequestMethod.POST)
31         @ResponseBody
32         public HashMap<String, String> executeGraph(@RequestBody String input) {
33                 LOGGER.error("In request");
34                 LOGGER.error(input);
35
36                 HashMap<String, String> hash = new HashMap<String, String>();
37                 Properties parms = new Properties();
38
39                 hash.put("status", "success");
40                 JsonObject jsonInput = new Gson().fromJson(input, JsonObject.class);
41                 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
42
43                 writeResponseToCtx(passthroughObj.toString(), parms, "input");
44
45                 JsonObject inputObject = jsonInput.get("graphDetails").getAsJsonObject();
46                 try {
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());
55                         }
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.");
59                         return errorHash;
60                 } catch (SvcLogicException e) {
61                         HashMap<String, String> errorHash = new HashMap<String, String>();
62                         errorHash.put("status", "failure");
63                         errorHash.put("message", e.getMessage());
64                         return errorHash;
65                 }
66                 return hash;
67         }
68
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 + ".");
73         }
74
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()));
82                                 Integer arrayIdx = 0;
83                                 for (JsonElement element : array) {
84                                         if (element.isJsonObject()) {
85                                                 writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
86                                         }
87                                         arrayIdx++;
88                                 }
89                         } else {
90                                 ctx.put(root + entry.getKey(), entry.getValue().getAsString());
91                         }
92                 }
93         }
94
95
96 }