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.apps.ms.sliboot.controllers;
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.apps.ms.sliboot.swagger.ConfigApi;
28 import org.onap.ccsdk.apps.ms.sliboot.swagger.OperationalApi;
29 import org.onap.ccsdk.apps.ms.sliboot.swagger.OperationsApi;
30 import org.onap.ccsdk.apps.ms.sliboot.data.TestResultsOperationalRepository;
31 import org.onap.ccsdk.apps.ms.sliboot.swagger.model.*;
32 import org.onap.ccsdk.apps.services.RestApplicationException;
33 import org.onap.ccsdk.apps.services.RestException;
34 import org.onap.ccsdk.apps.services.RestProtocolException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
38 import org.onap.ccsdk.apps.ms.sliboot.data.TestResultConfig;
39 import org.onap.ccsdk.apps.ms.sliboot.data.TestResultsConfigRepository;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.autoconfigure.domain.EntityScan;
44 import org.springframework.context.annotation.ComponentScan;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.stereotype.Controller;
48 import org.springframework.web.bind.annotation.RestController;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.validation.Valid;
54 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
57 @ComponentScan(basePackages = {"org.onap.ccsdk.apps.ms.sliboot.*", "org.onap.ccsdk.apps.services"})
58 @EntityScan("org.onap.ccsdk.apps.ms.sliboot.*")
59 public class RestconfApiController implements ConfigApi, OperationalApi, OperationsApi {
61 private final ObjectMapper objectMapper;
62 private final HttpServletRequest request;
65 protected SvcLogicServiceBase svc;
68 private TestResultsConfigRepository testResultsConfigRepository;
71 private TestResultsOperationalRepository testResultsOperationalRepository;
73 private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
75 @org.springframework.beans.factory.annotation.Autowired
76 public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
77 this.objectMapper = objectMapper;
78 this.request = request;
83 public Optional<ObjectMapper> getObjectMapper() {
84 return Optional.ofNullable(objectMapper);
88 public Optional<HttpServletRequest> getRequest() {
89 return Optional.ofNullable(request);
92 public Optional<String> getAcceptHeader() {
93 return ConfigApi.super.getAcceptHeader();
97 public ResponseEntity<SliApiHealthcheck> operationsSLIAPIhealthcheckPost() {
99 SliApiResponseFields respFields = new SliApiResponseFields();
100 HttpStatus httpStatus = HttpStatus.OK;
103 log.info("Calling SLI-API:healthcheck DG");
104 SvcLogicContext ctxIn = new SvcLogicContext();
105 SvcLogicContext ctxOut = svc.execute("sli", "healthcheck", null, "sync", ctxIn);
106 Properties respProps = ctxOut.toProperties();
108 respFields.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
109 respFields.setResponseCode(respProps.getProperty("error-code", "200"));
110 respFields.setResponseMessage(respProps.getProperty("error-message", "Success"));
111 respFields.setContextMemoryJson(propsToJson(respProps, "context-memory"));
113 } catch (Exception e) {
114 respFields.setAckFinalIndicator("true");
115 respFields.setResponseCode("500");
116 respFields.setResponseMessage(e.getMessage());
117 httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
118 log.error("Error calling healthcheck directed graph", e);
122 SliApiHealthcheck resp = new SliApiHealthcheck();
123 resp.setOutput(respFields);
124 return (new ResponseEntity<>(resp, httpStatus));
128 public ResponseEntity<SliApiVlbcheck> operationsSLIAPIvlbcheckPost() {
130 SliApiResponseFields respFields = new SliApiResponseFields();
131 HttpStatus httpStatus = HttpStatus.OK;
134 log.info("Calling SLI-API:vlbcheck DG");
135 SvcLogicContext ctxIn = new SvcLogicContext();
136 SvcLogicContext ctxOut = svc.execute("sli", "vlbcheck", null, "sync", ctxIn);
137 Properties respProps = ctxOut.toProperties();
138 respFields.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
139 respFields.setResponseCode(respProps.getProperty("error-code", "200"));
140 respFields.setResponseMessage(respProps.getProperty("error-message", "Success"));
141 respFields.setContextMemoryJson(propsToJson(respProps, "context-memory"));
143 } catch (Exception e) {
144 respFields.setAckFinalIndicator("true");
145 respFields.setResponseCode("500");
146 respFields.setResponseMessage(e.getMessage());
147 httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
148 log.error("Error calling vlbcheck directed graph", e);
152 SliApiVlbcheck resp = new SliApiVlbcheck();
153 resp.setOutput(respFields);
154 return (new ResponseEntity<>(resp, httpStatus));
158 public ResponseEntity<SliApiExecuteGraph> operationsSLIAPIexecuteGraphPost(@Valid SliApiExecutegraphInputBodyparam executeGraphInput) {
160 SvcLogicContext ctxIn = new SvcLogicContext();
161 SliApiExecuteGraph resp = new SliApiExecuteGraph();
162 SliApiResponseFields respFields = new SliApiResponseFields();
163 String executeGraphInputJson = null;
166 executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
167 log.info("Input as JSON is "+executeGraphInputJson);
168 } catch (JsonProcessingException e) {
170 respFields.setAckFinalIndicator("true");
171 respFields.setResponseCode("500");
172 respFields.setResponseMessage(e.getMessage());
173 log.error("Cannot create JSON from input object", e);
174 resp.setOutput(respFields);
175 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
178 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
179 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
181 ctxIn.mergeJson("input", passthroughObj.toString());
184 // Any of these can throw a nullpointer exception
185 String calledModule = executeGraphInput.getInput().getModuleName();
186 String calledRpc = executeGraphInput.getInput().getRpcName();
187 String modeStr = executeGraphInput.getInput().getMode().toString();
188 // execute should only throw a SvcLogicException
189 SvcLogicContext ctxOut = svc.execute(calledModule, calledRpc, null, modeStr, ctxIn);
190 Properties respProps = ctxOut.toProperties();
192 respFields.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
193 respFields.setResponseCode(respProps.getProperty("error-code", "200"));
194 respFields.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
195 respFields.setContextMemoryJson(propsToJson(respProps, "context-memory"));
196 resp.setOutput(respFields);
197 return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(respFields.getResponseCode()))));
199 } catch (NullPointerException npe) {
200 respFields.setAckFinalIndicator("true");
201 respFields.setResponseCode("500");
202 respFields.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
204 resp.setOutput(respFields);
205 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
206 } catch (SvcLogicException e) {
207 respFields.setAckFinalIndicator("true");
208 respFields.setResponseCode("500");
209 respFields.setResponseMessage(e.getMessage());
210 resp.setOutput(respFields);
211 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
216 public ResponseEntity<Void> configSLIAPItestResultsSLIAPItestResultTestIdentifierDelete(String testIdentifier) {
218 List<TestResultConfig> testResultConfigs = testResultsConfigRepository.findByTestIdentifier(testIdentifier);
220 if (testResultConfigs != null) {
221 Iterator<TestResultConfig> testResultConfigIterator = testResultConfigs.iterator();
222 while (testResultConfigIterator.hasNext()) {
223 testResultsConfigRepository.delete(testResultConfigIterator.next());
227 return (new ResponseEntity<>(HttpStatus.OK));
231 public ResponseEntity<Void> configSLIAPItestResultsDelete() {
233 testResultsConfigRepository.deleteAll();
235 return (new ResponseEntity<>(HttpStatus.OK));
239 public ResponseEntity<SliApiTestResults> operationalSLIAPItestResultsGet() {
241 SliApiTestResults results = new SliApiTestResults();
243 testResultsOperationalRepository.findAll().forEach(testResult -> {
244 SliApiTestresultsTestResult item = null;
246 item = objectMapper.readValue(testResult.getResults(), SliApiTestresultsTestResult.class);
247 results.addTestResultItem(item);
248 } catch (JsonProcessingException e) {
249 log.error("Could not convert testResult", e);
254 return new ResponseEntity<>(results, HttpStatus.OK);
258 public ResponseEntity<SliApiTestresultsTestResult> configSLIAPItestResultsSLIAPItestResultTestIdentifierGet(String testIdentifier) {
260 List<TestResultConfig> testResultConfigs = testResultsConfigRepository.findByTestIdentifier(testIdentifier);
262 if ((testResultConfigs == null) || (testResultConfigs.size() == 0)) {
263 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
265 TestResultConfig testResultConfig = testResultConfigs.get(0);
266 SliApiTestresultsTestResult testResult = null;
268 testResult = objectMapper.readValue(testResultConfig.getResults(), SliApiTestresultsTestResult.class);
269 } catch (JsonProcessingException e) {
270 log.error("Cannot convert test result", e);
271 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
275 return new ResponseEntity<>(testResult, HttpStatus.OK);
280 public ResponseEntity<SliApiTestResults> configSLIAPItestResultsGet() throws RestException {
282 if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
284 log.warn("ObjectMapper or HttpServletRequest not configured in default RestconfApi interface so no example is generated");
287 SliApiTestResults results = new SliApiTestResults();
289 if (testResultsConfigRepository.count() == 0) {
290 throw new RestApplicationException("data-missing", "Request could not be completed because the relevant data model content does not exist", 404);
293 testResultsConfigRepository.findAll().forEach(testResult -> {
294 SliApiTestresultsTestResult item = null;
296 item = objectMapper.readValue(testResult.getResults(), SliApiTestresultsTestResult.class);
297 results.addTestResultItem(item);
298 } catch (JsonProcessingException e) {
299 log.error("Could not convert testResult", e);
304 return new ResponseEntity<>(results, HttpStatus.OK);
308 public ResponseEntity<Void> configSLIAPItestResultsSLIAPItestResultTestIdentifierPut(String testIdentifier, @Valid SliApiTestresultsTestResult testResult) {
310 if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
312 log.warn("ObjectMapper or HttpServletRequest not configured in default RestconfApi interface so no example is generated");
315 List<TestResultConfig> testResultConfigs = testResultsConfigRepository.findByTestIdentifier(testIdentifier);
316 Iterator<TestResultConfig> testResultIter = testResultConfigs.iterator();
317 while (testResultIter.hasNext()) {
318 testResultsConfigRepository.delete(testResultIter.next());
321 TestResultConfig testResultConfig = null;
323 testResultConfig = new TestResultConfig(testResult.getTestIdentifier(), objectMapper.writeValueAsString(testResult));
324 testResultsConfigRepository.save(testResultConfig);
325 } catch (JsonProcessingException e) {
326 log.error("Could not save test result", e);
327 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
330 return new ResponseEntity<>(HttpStatus.OK);
334 public ResponseEntity<Void> configSLIAPItestResultsPost(@Valid SliApiTestResults testResults) {
336 List<SliApiTestresultsTestResult> resultList = testResults.getTestResult();
338 if (resultList == null) {
339 log.error("Invalid input - no test results list");
340 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
342 Iterator<SliApiTestresultsTestResult> resultIterator = resultList.iterator();
344 while (resultIterator.hasNext()) {
345 SliApiTestresultsTestResult curResult = resultIterator.next();
347 testResultsConfigRepository.save(new TestResultConfig(curResult.getTestIdentifier(), objectMapper.writeValueAsString(curResult)));
348 } catch (JsonProcessingException e) {
349 log.error("Could not save test result", e);
350 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
354 return new ResponseEntity<>(HttpStatus.OK);
358 public ResponseEntity<Void> configSLIAPItestResultsPut(@Valid SliApiTestResults testResults) {
360 testResultsConfigRepository.deleteAll();
362 List<SliApiTestresultsTestResult> resultList = testResults.getTestResult();
364 Iterator<SliApiTestresultsTestResult> resultIterator = resultList.iterator();
367 while (resultIterator.hasNext()) {
368 SliApiTestresultsTestResult curResult = resultIterator.next();
370 testResultsConfigRepository.save(new TestResultConfig(curResult.getTestIdentifier(), objectMapper.writeValueAsString(curResult)));
371 } catch (JsonProcessingException e) {
372 log.error("Could not save test result", e);
373 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
377 return new ResponseEntity<>(HttpStatus.OK);
380 public static String propsToJson(Properties props, String root)
382 StringBuffer sbuff = new StringBuffer();
384 sbuff.append("{ \""+root+"\" : { ");
385 boolean needComma = false;
386 for (Map.Entry<Object, Object> prop : props.entrySet()) {
387 sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
394 sbuff.append(" } }");
396 return(sbuff.toString());