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.sli.core.sliapi.springboot.controllers.swagger;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonObject;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
29 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
30 import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput;
31 import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields;
32 import org.onap.ccsdk.sli.core.sliapi.model.TestResult;
33 import org.onap.ccsdk.sli.core.sliapi.model.TestResults;
34 import org.onap.ccsdk.sli.core.sliapi.springboot.controllers.data.TestResultConfig;
35 import org.onap.ccsdk.sli.core.sliapi.springboot.controllers.data.TestResultsConfigRepository;
36 import org.onap.ccsdk.sli.core.sliapi.springboot.controllers.data.TestResultsOperationalRepository;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
41 import org.springframework.boot.autoconfigure.domain.EntityScan;
42 import org.springframework.context.annotation.ComponentScan;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.stereotype.Controller;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.validation.Valid;
51 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
54 @ComponentScan(basePackages = {"org.onap.ccsdk.sli.core.sliapi.springboot.*"})
55 @EntityScan("org.onap.ccsdk.sli.core.sliapi.springboot.*")
56 public class RestconfApiController implements RestconfApi {
58 private final ObjectMapper objectMapper;
59 private final HttpServletRequest request;
62 protected SvcLogicServiceBase svc;
65 private TestResultsConfigRepository testResultsConfigRepository;
68 private TestResultsOperationalRepository testResultsOperationalRepository;
70 private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
72 @org.springframework.beans.factory.annotation.Autowired
73 public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
74 this.objectMapper = objectMapper;
75 this.request = request;
79 public ResponseEntity<ResponseFields> healthcheck() {
80 ResponseFields resp = new ResponseFields();
83 log.info("Calling SLI-API:healthcheck DG");
84 SvcLogicContext ctxIn = new SvcLogicContext();
85 SvcLogicContext ctxOut = svc.execute("sli", "healthcheck", null, "sync", ctxIn);
86 Properties respProps = ctxOut.toProperties();
88 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
89 resp.setResponseCode(respProps.getProperty("error-code", "200"));
90 resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
91 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
93 return (new ResponseEntity<>(resp, HttpStatus.OK));
94 } catch (Exception e) {
95 resp.setAckFinalIndicator("true");
96 resp.setResponseCode("500");
97 resp.setResponseMessage(e.getMessage());
98 log.error("Error calling healthcheck directed graph", e);
101 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
105 public ResponseEntity<ResponseFields> vlbcheck() {
106 ResponseFields resp = new ResponseFields();
109 log.info("Calling SLI-API:vlbcheck DG");
110 SvcLogicContext ctxIn = new SvcLogicContext();
111 SvcLogicContext ctxOut = svc.execute("sli", "vlbcheck", null, "sync", ctxIn);
112 Properties respProps = ctxOut.toProperties();
113 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
114 resp.setResponseCode(respProps.getProperty("error-code", "200"));
115 resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
116 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
118 return (new ResponseEntity<>(resp, HttpStatus.OK));
119 } catch (Exception e) {
120 resp.setAckFinalIndicator("true");
121 resp.setResponseCode("500");
122 resp.setResponseMessage(e.getMessage());
123 log.error("Error calling vlbcheck directed graph", e);
126 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
131 public Optional<ObjectMapper> getObjectMapper() {
132 return Optional.ofNullable(objectMapper);
136 public Optional<HttpServletRequest> getRequest() {
137 return Optional.ofNullable(request);
141 public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
142 SvcLogicContext ctxIn = new SvcLogicContext();
143 ResponseFields resp = new ResponseFields();
144 String executeGraphInputJson = null;
147 executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
148 log.info("Input as JSON is "+executeGraphInputJson);
149 } catch (JsonProcessingException e) {
151 resp.setAckFinalIndicator("true");
152 resp.setResponseCode("500");
153 resp.setResponseMessage(e.getMessage());
154 log.error("Cannot create JSON from input object", e);
155 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
158 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
159 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
161 ctxIn.mergeJson("input", passthroughObj.toString());
164 // Any of these can throw a nullpointer exception
165 String calledModule = executeGraphInput.getInput().getModuleName();
166 String calledRpc = executeGraphInput.getInput().getRpcName();
167 String modeStr = executeGraphInput.getInput().getMode();
168 // execute should only throw a SvcLogicException
169 SvcLogicContext ctxOut = svc.execute(calledModule, calledRpc, null, modeStr, ctxIn);
170 Properties respProps = ctxOut.toProperties();
172 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
173 resp.setResponseCode(respProps.getProperty("error-code", "200"));
174 resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
175 resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
176 return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
178 } catch (NullPointerException npe) {
179 resp.setAckFinalIndicator("true");
180 resp.setResponseCode("500");
181 resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
183 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
184 } catch (SvcLogicException e) {
185 resp.setAckFinalIndicator("true");
186 resp.setResponseCode("500");
187 resp.setResponseMessage(e.getMessage());
189 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
194 public ResponseEntity<Void> deleteTestResult(String testIdentifier) {
196 List<TestResultConfig> testResultConfigs = testResultsConfigRepository.findByTestIdentifier(testIdentifier);
198 if (testResultConfigs != null) {
199 Iterator<TestResultConfig> testResultConfigIterator = testResultConfigs.iterator();
200 while (testResultConfigIterator.hasNext()) {
201 testResultsConfigRepository.delete(testResultConfigIterator.next());
205 return (new ResponseEntity<>(HttpStatus.OK));
209 public ResponseEntity<Void> deleteTestResults() {
211 testResultsConfigRepository.deleteAll();
213 return (new ResponseEntity<>(HttpStatus.OK));
217 public ResponseEntity<TestResults> gETTestResults() {
219 TestResults results = new TestResults();
221 testResultsOperationalRepository.findAll().forEach(testResult -> {
222 TestResult item = null;
224 item = objectMapper.readValue(testResult.getResults(), TestResult.class);
225 results.addTestResultsItem(item);
226 } catch (JsonProcessingException e) {
227 log.error("Could not convert testResult", e);
232 return new ResponseEntity<>(results, HttpStatus.OK);
236 public ResponseEntity<TestResult> getTestResult(String testIdentifier) {
237 List<TestResultConfig> testResultConfigs = testResultsConfigRepository.findByTestIdentifier(testIdentifier);
239 if ((testResultConfigs == null) || (testResultConfigs.size() == 0)) {
240 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
242 TestResultConfig testResultConfig = testResultConfigs.get(0);
243 TestResult testResult = null;
245 testResult = objectMapper.readValue(testResultConfig.getResults(), TestResult.class);
246 } catch (JsonProcessingException e) {
247 log.error("Cannot convert test result", e);
248 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
252 return new ResponseEntity<>(testResult, HttpStatus.OK);
257 public ResponseEntity<TestResults> getTestResults() {
258 if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
260 log.warn("ObjectMapper or HttpServletRequest not configured in default RestconfApi interface so no example is generated");
263 TestResults results = new TestResults();
265 testResultsConfigRepository.findAll().forEach(testResult -> {
266 TestResult item = null;
268 item = objectMapper.readValue(testResult.getResults(), TestResult.class);
269 results.addTestResultsItem(item);
270 } catch (JsonProcessingException e) {
271 log.error("Could not convert testResult", e);
276 return new ResponseEntity<>(results, HttpStatus.OK);
280 public ResponseEntity<TestResult> pUTTestResult(String testIdentifier, @Valid TestResult testResult) {
281 if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
283 log.warn("ObjectMapper or HttpServletRequest not configured in default RestconfApi interface so no example is generated");
286 List<TestResultConfig> testResultConfigs = testResultsConfigRepository.findByTestIdentifier(testIdentifier);
287 Iterator<TestResultConfig> testResultIter = testResultConfigs.iterator();
288 while (testResultIter.hasNext()) {
289 testResultsConfigRepository.delete(testResultIter.next());
292 TestResultConfig testResultConfig = null;
294 testResultConfig = new TestResultConfig(testResult.getTestIdentifier(), objectMapper.writeValueAsString(testResult));
295 testResultsConfigRepository.save(testResultConfig);
296 } catch (JsonProcessingException e) {
297 log.error("Could not save test result", e);
298 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
301 return new ResponseEntity<>(testResult, HttpStatus.OK);
305 public ResponseEntity<TestResults> postTestResults(@Valid TestResults testResults) {
306 List<TestResult> resultList = testResults.getTestResults();
308 Iterator<TestResult> resultIterator = resultList.iterator();
310 while (resultIterator.hasNext()) {
311 TestResult curResult = resultIterator.next();
313 testResultsConfigRepository.save(new TestResultConfig(curResult.getTestIdentifier(), objectMapper.writeValueAsString(curResult)));
314 } catch (JsonProcessingException e) {
315 log.error("Could not save test result", e);
316 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
320 return new ResponseEntity<>(testResults, HttpStatus.OK);
324 public ResponseEntity<TestResults> putTestResults(@Valid TestResults testResults) {
325 testResultsConfigRepository.deleteAll();
327 List<TestResult> resultList = testResults.getTestResults();
329 Iterator<TestResult> resultIterator = resultList.iterator();
332 while (resultIterator.hasNext()) {
333 TestResult curResult = resultIterator.next();
335 testResultsConfigRepository.save(new TestResultConfig(curResult.getTestIdentifier(), objectMapper.writeValueAsString(curResult)));
336 } catch (JsonProcessingException e) {
337 log.error("Could not save test result", e);
338 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
342 return new ResponseEntity<>(testResults, HttpStatus.OK);
345 public static String propsToJson(Properties props, String root)
347 StringBuffer sbuff = new StringBuffer();
349 sbuff.append("{ \""+root+"\" : { ");
350 boolean needComma = false;
351 for (Map.Entry<Object, Object> prop : props.entrySet()) {
352 sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
359 sbuff.append(" } }");
361 return(sbuff.toString());