b923d61140a58fb391c290151cc61507625adc33
[validation.git] / ui / src / main / java / org / akraino / validation / ui / controller / ResultController.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License. You may obtain
6  * a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 package org.akraino.validation.ui.controller;
17
18 import java.text.DateFormat;
19 import java.text.SimpleDateFormat;
20 import java.util.List;
21 import java.util.Set;
22
23 import org.akraino.validation.ui.entity.ValidationDbTestResult;
24 import org.akraino.validation.ui.service.DbResultAdapter;
25 import org.akraino.validation.ui.service.IntegratedResultService;
26 import org.onap.portalsdk.core.controller.RestrictedBaseController;
27 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
28 import org.onap.portalsdk.core.web.support.UserUtils;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.web.bind.annotation.PathVariable;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36
37 @Controller
38 @RequestMapping("/api/v1/results")
39 public class ResultController extends RestrictedBaseController {
40
41     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ResultController.class);
42
43     @Autowired
44     IntegratedResultService resultService;
45
46     @Autowired
47     DbResultAdapter dbAdapter;
48
49     public ResultController() {
50         super();
51     }
52
53     @RequestMapping(value = { "/getblueprintnamesoflab/{lab}" }, method = RequestMethod.GET)
54     public ResponseEntity<Set<String>> getBlueprintNamesOfLab(@PathVariable("lab") String lab) {
55         try {
56             return new ResponseEntity<>(resultService.getBlueprintNamesOfLabFromDb(lab), HttpStatus.OK);
57         } catch (Exception e) {
58             LOGGER.error(EELFLoggerDelegate.errorLogger,
59                     "Error when retrieving blueprint names of a lab. " + UserUtils.getStackTrace(e));
60         }
61         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
62     }
63
64     @RequestMapping(value = { "/getblueprintversions/{name}/{lab}" }, method = RequestMethod.GET)
65     public ResponseEntity<Set<String>> getBlueprintVersions(@PathVariable("name") String name,
66             @PathVariable("lab") String lab) {
67         try {
68             return new ResponseEntity<>(resultService.getBlueprintVersionsFromDb(name, lab), HttpStatus.OK);
69         } catch (Exception e) {
70             LOGGER.error(EELFLoggerDelegate.errorLogger,
71                     "Error when retrieving blueprint versions. " + UserUtils.getStackTrace(e));
72         }
73         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
74     }
75
76     @RequestMapping(value = { "/getbysubmissionid/{id}" }, method = RequestMethod.GET)
77     public ResponseEntity<ValidationDbTestResult> getBySubmissionId(@PathVariable("id") String submissionId) {
78         try {
79             return new ResponseEntity<>(resultService.getResults(submissionId), HttpStatus.OK);
80         } catch (Exception e) {
81             LOGGER.error(EELFLoggerDelegate.errorLogger,
82                     "Error when retrieving results using submission id. " + UserUtils.getStackTrace(e));
83         }
84         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
85     }
86
87     @RequestMapping(value = { "/getmostrecent/{name}/{version}/{lab}" }, method = RequestMethod.GET)
88     public ResponseEntity<List<ValidationDbTestResult>> getMostRecent(@PathVariable("name") String name,
89             @PathVariable("version") String version, @PathVariable("lab") String lab) {
90         try {
91             return new ResponseEntity<>(dbAdapter.readResultFromDb(name, version, lab, null, null, null, null),
92                     HttpStatus.OK);
93         } catch (Exception e) {
94             LOGGER.error(EELFLoggerDelegate.errorLogger,
95                     "Error when retrieving most recent results. " + UserUtils.getStackTrace(e));
96         }
97         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
98     }
99
100     @RequestMapping(value = { "/getbytimestamp/{lab}/{name}/{version}/{timestamp}" }, method = RequestMethod.GET)
101     public ResponseEntity<ValidationDbTestResult> getByTimestamp(@PathVariable("lab") String lab,
102             @PathVariable("name") String name, @PathVariable("version") String version,
103             @PathVariable("timestamp") String timestamp) {
104         try {
105             return new ResponseEntity<>(resultService.getResult(name, version, lab, timestamp), HttpStatus.OK);
106         } catch (Exception e) {
107             LOGGER.error(EELFLoggerDelegate.errorLogger,
108                     "Error when retrieving results using timestamp data. " + UserUtils.getStackTrace(e));
109         }
110         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
111     }
112
113     @RequestMapping(value = {
114     "/getlastrun/{lab}/{name}/{version}/{allLayers}/{optional}/{outcome}" }, method = RequestMethod.GET)
115     public ResponseEntity<ValidationDbTestResult> getLastRun(@PathVariable("lab") String lab,
116             @PathVariable("name") String name, @PathVariable("version") String version,
117             @PathVariable("allLayers") Boolean allLayers, @PathVariable("optional") Boolean optional,
118             @PathVariable("outcome") boolean outcome) {
119         try {
120             return new ResponseEntity<>(
121                     resultService.getLastResultBasedOnOutcome(name, version, lab, allLayers, optional, outcome),
122                     HttpStatus.OK);
123         } catch (Exception e) {
124             LOGGER.error(EELFLoggerDelegate.errorLogger,
125                     "Error when retrieving last result. " + UserUtils.getStackTrace(e));
126         }
127         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
128     }
129
130     @RequestMapping(value = {
131     "/getlastrunoflayers/{lab}/{name}/{version}/{layers}/{optional}/{outcome}" }, method = RequestMethod.GET)
132     public ResponseEntity<ValidationDbTestResult> getLastRunOfLayers(@PathVariable("lab") String lab,
133             @PathVariable("name") String name, @PathVariable("version") String version,
134             @PathVariable("layers") List<String> layers, @PathVariable("optional") Boolean optional,
135             @PathVariable("outcome") boolean outcome) {
136         try {
137             return new ResponseEntity<>(
138                     resultService.getLastResultBasedOnOutcome(name, version, lab, layers, optional, outcome),
139                     HttpStatus.OK);
140         } catch (Exception e) {
141             LOGGER.error(EELFLoggerDelegate.errorLogger,
142                     "Error when retrieving last result. " + UserUtils.getStackTrace(e));
143         }
144         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
145     }
146
147     @RequestMapping(value = { "/getbasedondate/{lab}/{name}/{version}/{date}" }, method = RequestMethod.GET)
148     public ResponseEntity<List<ValidationDbTestResult>> getBasedOnDate(@PathVariable("lab") String lab,
149             @PathVariable("name") String name, @PathVariable("version") String version,
150             @PathVariable("date") String date) {
151         try {
152             DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
153             return new ResponseEntity<>(
154                     resultService.getBasedOnDateFromNexus(name, version, lab, dateFormat.parse(date)), HttpStatus.OK);
155         } catch (Exception e) {
156             LOGGER.error(EELFLoggerDelegate.errorLogger,
157                     "Error when retrieving results based on date. " + UserUtils.getStackTrace(e));
158         }
159         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
160     }
161
162 }