e2cd864d707afbf51abadb46f6619f433c4fdac9
[ccsdk/sli.git] /
1 package org.onap.ccsdk.sli.core.sliapi.springboot;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Optional;
8 import java.util.Properties;
9
10 import javax.servlet.http.HttpServletRequest;
11 import javax.validation.Valid;
12
13 import com.fasterxml.jackson.core.JsonProcessingException;
14 import com.google.gson.*;
15 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
16 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
17 import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;
18 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
19 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
20 import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver;
21 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;
22 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicResolver;
23 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
24 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;
25 import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput;
26 import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.stereotype.Controller;
32
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
36
37 @Controller
38 public class RestconfApiController implements RestconfApi {
39
40         private final ObjectMapper objectMapper;
41         private final HttpServletRequest request;
42
43
44         private static SvcLogicServiceBase svc;
45         private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
46
47         @org.springframework.beans.factory.annotation.Autowired
48         public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
49                 this.objectMapper = objectMapper;
50                 this.request = request;
51
52                 SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() {
53
54                         @Override
55                         public Properties getProperties() {
56                                 Properties props = new Properties();
57                                 String propPath = "src/main/resources/svclogic.properties";
58                                 System.out.println(propPath);
59                                 try (FileInputStream fileInputStream = new FileInputStream(propPath)) {
60                                         props = new Properties();
61                                         props.load(fileInputStream);
62                                 } catch (final IOException e) {
63                                         log.error("Failed to load properties for file: {}", propPath,
64                                                         new ConfigurationException("Failed to load properties for file: " + propPath, e));
65                                 }
66                                 return props;
67                         }
68                 };
69
70                 SvcLogicStore store = null;
71                 try {
72                         store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties());
73                 } catch (SvcLogicException e) {
74                         log.error("Cannot create SvcLogicStore", e);
75                         return;
76                 }
77
78                 String serviceLogicDirectory = System.getProperty("serviceLogicDirectory");
79                 System.out.println("serviceLogicDirectory is " + serviceLogicDirectory);
80                 SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, store);
81
82                 try {
83                         loader.loadAndActivate();
84                 } catch (IOException e) {
85                         log.error("Cannot load directed graphs", e);
86                 }
87                 SvcLogicResolver resolver = new HashMapResolver();
88
89                 svc = new SvcLogicServiceImplBase(store, resolver);
90
91         }
92
93         @Override
94         public ResponseEntity<ResponseFields> healthcheck() {
95                 ResponseFields resp = new ResponseFields();
96
97                 try {
98                         log.info("Calling SLI-API:healthcheck DG");
99                         Properties inputProps = new Properties();
100                         Properties respProps = svc.execute("sli", "healthcheck", null, "sync", inputProps);
101
102                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
103                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
104                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
105                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
106
107                         return (new ResponseEntity<>(resp, HttpStatus.OK));
108                 } catch (Exception e) {
109                         resp.setAckFinalIndicator("true");
110                         resp.setResponseCode("500");
111                         resp.setResponseMessage(e.getMessage());
112                         log.error("Error calling healthcheck directed graph", e);
113
114                 }
115                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
116         }
117
118         @Override
119         public ResponseEntity<ResponseFields> vlbcheck() {
120                 ResponseFields resp = new ResponseFields();
121
122                 try {
123                         log.info("Calling SLI-API:vlbcheck DG");
124                         Properties inputProps = new Properties();
125                         Properties respProps = svc.execute("sli", "vlbcheck", null, "sync", inputProps);
126
127                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
128                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
129                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
130                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
131
132                         return (new ResponseEntity<>(resp, HttpStatus.OK));
133                 } catch (Exception e) {
134                         resp.setAckFinalIndicator("true");
135                         resp.setResponseCode("500");
136                         resp.setResponseMessage(e.getMessage());
137                         log.error("Error calling vlbcheck directed graph", e);
138
139                 }
140                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
141         }
142
143
144         @Override
145         public Optional<ObjectMapper> getObjectMapper() {
146                 return Optional.ofNullable(objectMapper);
147         }
148
149         @Override
150         public Optional<HttpServletRequest> getRequest() {
151                 return Optional.ofNullable(request);
152         }
153
154         @Override
155         public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
156                 Properties parms = new Properties();
157                 ResponseFields resp = new ResponseFields();
158                 String executeGraphInputJson = null;
159
160                 try {
161                          executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
162                          log.info("Input as JSON is "+executeGraphInputJson);
163                 } catch (JsonProcessingException e) {
164
165                         resp.setAckFinalIndicator("true");
166                         resp.setResponseCode("500");
167                         resp.setResponseMessage(e.getMessage());
168                         log.error("Cannot create JSON from input object", e);
169                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
170
171                 }
172                 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
173                 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
174
175                 writeResponseToCtx(passthroughObj.toString(), parms, "input");
176
177
178                 try {
179                         // Any of these can throw a nullpointer exception
180                         String calledModule = executeGraphInput.getInput().getModuleName();
181                         String calledRpc = executeGraphInput.getInput().getRpcName();
182                         String modeStr = executeGraphInput.getInput().getMode();
183                         // execute should only throw a SvcLogicException
184                         Properties respProps = svc.execute(calledModule, calledRpc, null, modeStr, parms);
185
186                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
187                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
188                         resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
189                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
190                         return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
191
192                 } catch (NullPointerException npe) {
193                         resp.setAckFinalIndicator("true");
194                         resp.setResponseCode("500");
195                         resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
196
197                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
198                 } catch (SvcLogicException e) {
199                         resp.setAckFinalIndicator("true");
200                         resp.setResponseCode("500");
201                         resp.setResponseMessage(e.getMessage());
202
203                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
204                 }
205         }
206
207         public static void writeResponseToCtx(String resp, Properties ctx, String prefix) {
208                 JsonParser jp = new JsonParser();
209                 JsonElement element = jp.parse(resp);
210                 writeJsonObject(element.getAsJsonObject(), ctx, prefix + ".");
211         }
212
213         public static void writeJsonObject(JsonObject obj, Properties ctx, String root) {
214                 for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
215                         if (entry.getValue().isJsonObject()) {
216                                 writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + entry.getKey() + ".");
217                         } else if (entry.getValue().isJsonArray()) {
218                                 JsonArray array = entry.getValue().getAsJsonArray();
219                                 ctx.put(root + entry.getKey() + "_length", String.valueOf(array.size()));
220                                 Integer arrayIdx = 0;
221                                 for (JsonElement element : array) {
222                                         if (element.isJsonObject()) {
223                                                 writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "].");
224                                         }
225                                         arrayIdx++;
226                                 }
227                         } else {
228                                 if (entry.getValue() instanceof JsonNull) {
229                                         log.info("Skipping parameter "+entry.getKey()+" with null value");
230
231                                 } else {
232                                         ctx.put(root + entry.getKey(), entry.getValue().getAsString());
233                                 }
234                         }
235                 }
236         }
237
238         public static String propsToJson(Properties props, String root)
239         {
240                 StringBuffer sbuff = new StringBuffer();
241
242                 sbuff.append("{ \""+root+"\" : { ");
243                 boolean needComma = false;
244                 for (Map.Entry<Object, Object> prop : props.entrySet()) {
245                         sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
246                         if (needComma) {
247                                 sbuff.append(" , ");
248                         } else {
249                                 needComma = true;
250                         }
251                 }
252                 sbuff.append(" } }");
253
254                 return(sbuff.toString());
255         }
256
257 }