10442d43278b702c8db34aabc2b81f2fd9fe2a4e
[ccsdk/sli/core.git] /
1 package org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger;
2
3 import java.util.Map;
4 import java.util.Optional;
5 import java.util.Properties;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.validation.Valid;
8 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
9 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
10 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
11 import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput;
12 import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields;
13 import org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger.RestconfApi;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.http.HttpStatus;
18 import org.springframework.http.ResponseEntity;
19 import org.springframework.stereotype.Controller;
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import com.google.gson.Gson;
23 import com.google.gson.JsonObject;
24
25 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
26
27 @Controller
28 public class RestconfApiController implements RestconfApi {
29
30         private final ObjectMapper objectMapper;
31         private final HttpServletRequest request;
32
33   @Autowired
34   protected SvcLogicServiceBase svc;
35         private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
36
37         @org.springframework.beans.factory.annotation.Autowired
38         public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
39                 this.objectMapper = objectMapper;
40                 this.request = request;
41         }
42
43         @Override
44         public ResponseEntity<ResponseFields> healthcheck() {
45                 ResponseFields resp = new ResponseFields();
46
47                 try {
48                         log.info("Calling SLI-API:healthcheck DG");
49                         SvcLogicContext ctxIn = new SvcLogicContext();
50                         SvcLogicContext ctxOut = svc.execute("sli", "healthcheck", null, "sync", ctxIn);
51                         Properties respProps = ctxOut.toProperties();
52
53                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
54                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
55                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
56                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
57
58                         return (new ResponseEntity<>(resp, HttpStatus.OK));
59                 } catch (Exception e) {
60                         resp.setAckFinalIndicator("true");
61                         resp.setResponseCode("500");
62                         resp.setResponseMessage(e.getMessage());
63                         log.error("Error calling healthcheck directed graph", e);
64
65                 }
66                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
67         }
68
69         @Override
70         public ResponseEntity<ResponseFields> vlbcheck() {
71                 ResponseFields resp = new ResponseFields();
72
73                 try {
74                         log.info("Calling SLI-API:vlbcheck DG");
75                         SvcLogicContext ctxIn = new SvcLogicContext();
76                         SvcLogicContext ctxOut = svc.execute("sli", "vlbcheck", null, "sync", ctxIn);
77                         Properties respProps = ctxOut.toProperties();
78                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
79                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
80                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
81                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
82
83                         return (new ResponseEntity<>(resp, HttpStatus.OK));
84                 } catch (Exception e) {
85                         resp.setAckFinalIndicator("true");
86                         resp.setResponseCode("500");
87                         resp.setResponseMessage(e.getMessage());
88                         log.error("Error calling vlbcheck directed graph", e);
89
90                 }
91                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
92         }
93
94
95         @Override
96         public Optional<ObjectMapper> getObjectMapper() {
97                 return Optional.ofNullable(objectMapper);
98         }
99
100         @Override
101         public Optional<HttpServletRequest> getRequest() {
102                 return Optional.ofNullable(request);
103         }
104
105         @Override
106         public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
107                 SvcLogicContext ctxIn = new SvcLogicContext();
108                 ResponseFields resp = new ResponseFields();
109                 String executeGraphInputJson = null;
110
111                 try {
112                          executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
113                          log.info("Input as JSON is "+executeGraphInputJson);
114                 } catch (JsonProcessingException e) {
115
116                         resp.setAckFinalIndicator("true");
117                         resp.setResponseCode("500");
118                         resp.setResponseMessage(e.getMessage());
119                         log.error("Cannot create JSON from input object", e);
120                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
121
122                 }
123                 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
124                 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
125
126                 ctxIn.mergeJson("input", passthroughObj.toString());
127
128                 try {
129                         // Any of these can throw a nullpointer exception
130                         String calledModule = executeGraphInput.getInput().getModuleName();
131                         String calledRpc = executeGraphInput.getInput().getRpcName();
132                         String modeStr = executeGraphInput.getInput().getMode();
133                         // execute should only throw a SvcLogicException
134                         SvcLogicContext ctxOut = svc.execute(calledModule, calledRpc, null, modeStr, ctxIn);
135                         Properties respProps = ctxOut.toProperties();
136
137                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
138                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
139                         resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
140                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
141                         return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
142
143                 } catch (NullPointerException npe) {
144                         resp.setAckFinalIndicator("true");
145                         resp.setResponseCode("500");
146                         resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
147
148                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
149                 } catch (SvcLogicException e) {
150                         resp.setAckFinalIndicator("true");
151                         resp.setResponseCode("500");
152                         resp.setResponseMessage(e.getMessage());
153
154                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
155                 }
156         }
157
158         public static String propsToJson(Properties props, String root)
159         {
160                 StringBuffer sbuff = new StringBuffer();
161
162                 sbuff.append("{ \""+root+"\" : { ");
163                 boolean needComma = false;
164                 for (Map.Entry<Object, Object> prop : props.entrySet()) {
165                         sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
166                         if (needComma) {
167                                 sbuff.append(" , ");
168                         } else {
169                                 needComma = true;
170                         }
171                 }
172                 sbuff.append(" } }");
173
174                 return(sbuff.toString());
175         }
176
177 }