1 package org.onap.ccsdk.sli.core.sliapi.springboot;
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.google.gson.*;
6 import org.onap.ccsdk.sli.core.sli.*;
7 import org.onap.ccsdk.sli.core.sli.provider.base.*;
8 import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput;
9 import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.HttpStatus;
13 import org.springframework.http.ResponseEntity;
14 import org.springframework.stereotype.Controller;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.validation.Valid;
18 import java.io.FileInputStream;
19 import java.io.IOException;
21 import java.util.Optional;
22 import java.util.Properties;
24 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
27 public class RestconfApiController implements RestconfApi {
29 private final ObjectMapper objectMapper;
30 private final HttpServletRequest request;
33 private static SvcLogicServiceBase svc;
34 private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
36 @org.springframework.beans.factory.annotation.Autowired
37 public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
38 this.objectMapper = objectMapper;
39 this.request = request;
41 SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() {
44 public Properties getProperties() {
45 Properties props = new Properties();
46 String propPath = "src/main/resources/svclogic.properties";
47 System.out.println(propPath);
48 try (FileInputStream fileInputStream = new FileInputStream(propPath)) {
49 props = new Properties();
50 props.load(fileInputStream);
51 } catch (final IOException e) {
52 log.error("Failed to load properties for file: {}", propPath,
53 new ConfigurationException("Failed to load properties for file: " + propPath, e));
59 SvcLogicStore store = null;
61 store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties());
62 } catch (SvcLogicException e) {
63 log.error("Cannot create SvcLogicStore", e);
67 String serviceLogicDirectory = System.getProperty("serviceLogicDirectory", "src/main/resources");
68 System.out.println("serviceLogicDirectory is " + serviceLogicDirectory);
69 SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, store);
72 loader.loadAndActivate();
73 } catch (IOException e) {
74 log.error("Cannot load directed graphs", e);
76 SvcLogicResolver resolver = new HashMapResolver();
78 svc = new SvcLogicServiceImplBase(store, resolver);
83 public ResponseEntity<ResponseFields> healthcheck() {
84 ResponseFields resp = new ResponseFields();
87 log.info("Calling SLI-API:healthcheck DG");
88 Properties inputProps = new Properties();
89 Properties respProps = svc.execute("sli", "healthcheck", null, "sync", inputProps);
91 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
92 resp.setResponseCode(respProps.getProperty("error-code", "200"));
93 resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
94 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
96 return (new ResponseEntity<>(resp, HttpStatus.OK));
97 } catch (Exception e) {
98 resp.setAckFinalIndicator("true");
99 resp.setResponseCode("500");
100 resp.setResponseMessage(e.getMessage());
101 log.error("Error calling healthcheck directed graph", e);
104 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
108 public ResponseEntity<ResponseFields> vlbcheck() {
109 ResponseFields resp = new ResponseFields();
112 log.info("Calling SLI-API:vlbcheck DG");
113 Properties inputProps = new Properties();
114 Properties respProps = svc.execute("sli", "vlbcheck", null, "sync", inputProps);
116 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
117 resp.setResponseCode(respProps.getProperty("error-code", "200"));
118 resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
119 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
121 return (new ResponseEntity<>(resp, HttpStatus.OK));
122 } catch (Exception e) {
123 resp.setAckFinalIndicator("true");
124 resp.setResponseCode("500");
125 resp.setResponseMessage(e.getMessage());
126 log.error("Error calling vlbcheck directed graph", e);
129 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
134 public Optional<ObjectMapper> getObjectMapper() {
135 return Optional.ofNullable(objectMapper);
139 public Optional<HttpServletRequest> getRequest() {
140 return Optional.ofNullable(request);
144 public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
145 Properties parms = new Properties();
146 ResponseFields resp = new ResponseFields();
147 String executeGraphInputJson = null;
150 executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
151 log.info("Input as JSON is "+executeGraphInputJson);
152 } catch (JsonProcessingException e) {
154 resp.setAckFinalIndicator("true");
155 resp.setResponseCode("500");
156 resp.setResponseMessage(e.getMessage());
157 log.error("Cannot create JSON from input object", e);
158 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
161 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
162 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
164 writeResponseToCtx(passthroughObj.toString(), parms, "input");
168 // Any of these can throw a nullpointer exception
169 String calledModule = executeGraphInput.getInput().getModuleName();
170 String calledRpc = executeGraphInput.getInput().getRpcName();
171 String modeStr = executeGraphInput.getInput().getMode();
172 // execute should only throw a SvcLogicException
173 Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms);
175 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
176 resp.setResponseCode(respProps.getProperty("error-code", "200"));
177 resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
178 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
179 return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
181 } catch (NullPointerException npe) {
182 resp.setAckFinalIndicator("true");
183 resp.setResponseCode("500");
184 resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
186 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
187 } catch (SvcLogicException e) {
188 resp.setAckFinalIndicator("true");
189 resp.setResponseCode("500");
190 resp.setResponseMessage(e.getMessage());
192 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
196 public static void writeResponseToCtx(String resp, Properties ctx, String prefix) {
197 JsonParser jp = new JsonParser();
198 JsonElement element = jp.parse(resp);
199 writeJsonObject(element.getAsJsonObject(), ctx, prefix + ".");
202 public static void writeJsonObject(JsonObject obj, Properties ctx, String root) {
203 for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
204 if (entry.getValue().isJsonObject()) {
205 writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + entry.getKey() + ".");
206 } else if (entry.getValue().isJsonArray()) {
207 JsonArray array = entry.getValue().getAsJsonArray();
208 ctx.put(root + entry.getKey() + "_length", String.valueOf(array.size()));
209 Integer arrayIdx = 0;
210 for (JsonElement element : array) {
211 if (element.isJsonObject()) {
212 writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
217 if (entry.getValue() instanceof JsonNull) {
218 log.info("Skipping parameter "+entry.getKey()+" with null value");
221 ctx.put(root + entry.getKey(), entry.getValue().getAsString());
227 public static String propsToJson(Properties props, String root)
229 StringBuffer sbuff = new StringBuffer();
231 sbuff.append("{ \""+root+"\" : { ");
232 boolean needComma = false;
233 for (Map.Entry<Object, Object> prop : props.entrySet()) {
234 sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
241 sbuff.append(" } }");
243 return(sbuff.toString());