2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.apps.ms.sliboot.controllers;
23 import java.util.HashMap;
24 import java.util.Map.Entry;
25 import java.util.Properties;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
30 import org.springframework.boot.autoconfigure.domain.EntityScan;
31 import org.springframework.context.annotation.ComponentScan;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.web.bind.annotation.RequestBody;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.bind.annotation.ResponseBody;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonArray;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonParser;
44 @EnableAutoConfiguration
45 public class ExecuteGraphController {
47 protected SvcLogicServiceBase svc;
49 @RequestMapping(value = "/executeGraph", method = RequestMethod.POST)
51 public HashMap<String, String> executeGraph(@RequestBody String input) {
52 HashMap<String, String> hash = new HashMap<String, String>();
53 Properties parms = new Properties();
55 hash.put("status", "success");
56 JsonObject jsonInput = new Gson().fromJson(input, JsonObject.class);
57 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
59 writeResponseToCtx(passthroughObj.toString(), parms, "input");
61 JsonObject inputObject = jsonInput.get("graphDetails").getAsJsonObject();
63 // Any of these can throw a nullpointer exception
64 String calledModule = inputObject.get("module").getAsString();
65 String calledRpc = inputObject.get("rpc").getAsString();
66 String modeStr = inputObject.get("mode").getAsString();
67 // execute should only throw a SvcLogicException
68 Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms);
69 for (Entry<Object, Object> prop : respProps.entrySet()) {
70 hash.put((String) prop.getKey(), (String) prop.getValue());
72 } catch (NullPointerException npe) {
73 HashMap<String, String> errorHash = new HashMap<String, String>();
74 errorHash.put("error-message", "check that you populated module, rpc and or mode correctly.");
76 } catch (SvcLogicException e) {
77 HashMap<String, String> errorHash = new HashMap<String, String>();
78 errorHash.put("status", "failure");
79 errorHash.put("message", e.getMessage());
85 public static void writeResponseToCtx(String resp, Properties ctx, String prefix) {
86 JsonParser jp = new JsonParser();
87 JsonElement element = jp.parse(resp);
88 writeJsonObject(element.getAsJsonObject(), ctx, prefix + ".");
91 public static void writeJsonObject(JsonObject obj, Properties ctx, String root) {
92 for (Entry<String, JsonElement> entry : obj.entrySet()) {
93 if (entry.getValue().isJsonObject()) {
94 writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + entry.getKey() + ".");
95 } else if (entry.getValue().isJsonArray()) {
96 JsonArray array = entry.getValue().getAsJsonArray();
97 ctx.put(root + entry.getKey() + "_length", String.valueOf(array.size()));
99 for (JsonElement element : array) {
100 if (element.isJsonObject()) {
101 writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
106 ctx.put(root + entry.getKey(), entry.getValue().getAsString());