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 SvcLogicContext ctxIn = new SvcLogicContext();
89 SvcLogicContext ctxOut = svc.execute("sli", "healthcheck", null, "sync", ctxIn);
90 Properties respProps = ctxOut.toProperties();
92 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
93 resp.setResponseCode(respProps.getProperty("error-code", "200"));
94 resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
95 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
97 return (new ResponseEntity<>(resp, HttpStatus.OK));
98 } catch (Exception e) {
99 resp.setAckFinalIndicator("true");
100 resp.setResponseCode("500");
101 resp.setResponseMessage(e.getMessage());
102 log.error("Error calling healthcheck directed graph", e);
105 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
109 public ResponseEntity<ResponseFields> vlbcheck() {
110 ResponseFields resp = new ResponseFields();
113 log.info("Calling SLI-API:vlbcheck DG");
114 SvcLogicContext ctxIn = new SvcLogicContext();
115 SvcLogicContext ctxOut = svc.execute("sli", "vlbcheck", null, "sync", ctxIn);
116 Properties respProps = ctxOut.toProperties();
117 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
118 resp.setResponseCode(respProps.getProperty("error-code", "200"));
119 resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
120 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
122 return (new ResponseEntity<>(resp, HttpStatus.OK));
123 } catch (Exception e) {
124 resp.setAckFinalIndicator("true");
125 resp.setResponseCode("500");
126 resp.setResponseMessage(e.getMessage());
127 log.error("Error calling vlbcheck directed graph", e);
130 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
135 public Optional<ObjectMapper> getObjectMapper() {
136 return Optional.ofNullable(objectMapper);
140 public Optional<HttpServletRequest> getRequest() {
141 return Optional.ofNullable(request);
145 public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
146 SvcLogicContext ctxIn = new SvcLogicContext();
147 ResponseFields resp = new ResponseFields();
148 String executeGraphInputJson = null;
151 executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
152 log.info("Input as JSON is "+executeGraphInputJson);
153 } catch (JsonProcessingException e) {
155 resp.setAckFinalIndicator("true");
156 resp.setResponseCode("500");
157 resp.setResponseMessage(e.getMessage());
158 log.error("Cannot create JSON from input object", e);
159 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
162 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
163 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
165 ctxIn.mergeJson("input", passthroughObj.toString());
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 SvcLogicContext ctxOut = svc.execute(calledModule, calledRpc, null, modeStr, ctxIn);
174 Properties respProps = ctxOut.toProperties();
176 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
177 resp.setResponseCode(respProps.getProperty("error-code", "200"));
178 resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
179 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
180 return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
182 } catch (NullPointerException npe) {
183 resp.setAckFinalIndicator("true");
184 resp.setResponseCode("500");
185 resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
187 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
188 } catch (SvcLogicException e) {
189 resp.setAckFinalIndicator("true");
190 resp.setResponseCode("500");
191 resp.setResponseMessage(e.getMessage());
193 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
197 public static String propsToJson(Properties props, String root)
199 StringBuffer sbuff = new StringBuffer();
201 sbuff.append("{ \""+root+"\" : { ");
202 boolean needComma = false;
203 for (Map.Entry<Object, Object> prop : props.entrySet()) {
204 sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
211 sbuff.append(" } }");
213 return(sbuff.toString());