f9c205a415f023b02c908dd2721b01a40f7de76f
[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.client.nexus.resources.ValidationNexusTestResult;
24 import org.akraino.validation.ui.data.BlueprintLayer;
25 import org.akraino.validation.ui.data.Lab;
26 import org.akraino.validation.ui.service.DbResultAdapter;
27 import org.akraino.validation.ui.service.IntegratedResultService;
28 import org.onap.portalsdk.core.controller.RestrictedBaseController;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.portalsdk.core.web.support.UserUtils;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38
39 @Controller
40 @RequestMapping("/api/v1/results")
41 public class ResultController extends RestrictedBaseController {
42
43     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ResultController.class);
44
45     @Autowired
46     IntegratedResultService resultService;
47
48     @Autowired
49     DbResultAdapter dbAdapter;
50
51     public ResultController() {
52         super();
53     }
54
55     @RequestMapping(value = { "/getlabs/" }, method = RequestMethod.GET)
56     public ResponseEntity<Set<Lab>> getLabs() {
57         try {
58             return new ResponseEntity<>(resultService.getLabsFromDb(), HttpStatus.OK);
59         } catch (Exception e) {
60             LOGGER.error(EELFLoggerDelegate.errorLogger, "Error when retrieving labs. " + UserUtils.getStackTrace(e));
61         }
62         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
63     }
64
65     @RequestMapping(value = { "/getblueprintnamesoflab/{lab}" }, method = RequestMethod.GET)
66     public ResponseEntity<Set<String>> getBlueprintNamesOfLab(@PathVariable("lab") Lab lab) {
67         try {
68             return new ResponseEntity<>(resultService.getBlueprintNamesOfLabFromDb(lab), HttpStatus.OK);
69         } catch (Exception e) {
70             LOGGER.error(EELFLoggerDelegate.errorLogger,
71                     "Error when retrieving blueprint names of a lab. " + UserUtils.getStackTrace(e));
72         }
73         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
74     }
75
76     @RequestMapping(value = { "/getblueprintversions/{name}/{lab}" }, method = RequestMethod.GET)
77     public ResponseEntity<Set<String>> getBlueprintVersions(@PathVariable("name") String name,
78             @PathVariable("lab") Lab lab) {
79         try {
80             return new ResponseEntity<>(resultService.getBlueprintVersionsFromDb(name, lab), HttpStatus.OK);
81         } catch (Exception e) {
82             LOGGER.error(EELFLoggerDelegate.errorLogger,
83                     "Error when retrieving blueprint versions. " + UserUtils.getStackTrace(e));
84         }
85         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
86     }
87
88     @RequestMapping(value = { "/getbysubmissionid/{id}" }, method = RequestMethod.GET)
89     public ResponseEntity<ValidationNexusTestResult> getBySubmissionId(@PathVariable("id") String submissionId) {
90         try {
91             return new ResponseEntity<>(resultService.getResults(submissionId), HttpStatus.OK);
92         } catch (Exception e) {
93             LOGGER.error(EELFLoggerDelegate.errorLogger,
94                     "Error when retrieving results using submission id. " + UserUtils.getStackTrace(e));
95         }
96         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
97     }
98
99     @RequestMapping(value = { "/getmostrecent/{name}/{version}/{lab}" }, method = RequestMethod.GET)
100     public ResponseEntity<List<ValidationNexusTestResult>> getMostRecent(@PathVariable("name") String name,
101             @PathVariable("version") String version, @PathVariable("lab") Lab lab) {
102         try {
103             return new ResponseEntity<>(dbAdapter.readResultFromDb(name, version, lab, null, null, null, null),
104                     HttpStatus.OK);
105         } catch (Exception e) {
106             LOGGER.error(EELFLoggerDelegate.errorLogger,
107                     "Error when retrieving most recent results. " + UserUtils.getStackTrace(e));
108         }
109         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
110     }
111
112     @RequestMapping(value = { "/getbytimestamp/{lab}/{name}/{version}/{timestamp}" }, method = RequestMethod.GET)
113     public ResponseEntity<ValidationNexusTestResult> getByTimestamp(@PathVariable("lab") Lab lab,
114             @PathVariable("name") String name, @PathVariable("version") String version,
115             @PathVariable("timestamp") String timestamp) {
116         try {
117             return new ResponseEntity<>(resultService.getResult(name, version, lab, timestamp), HttpStatus.OK);
118         } catch (Exception e) {
119             LOGGER.error(EELFLoggerDelegate.errorLogger,
120                     "Error when retrieving results using timestamp data. " + UserUtils.getStackTrace(e));
121         }
122         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
123     }
124
125     @RequestMapping(value = {
126     "/getlastrun/{lab}/{name}/{version}/{allLayers}/{optional}/{outcome}" }, method = RequestMethod.GET)
127     public ResponseEntity<ValidationNexusTestResult> getLastRun(@PathVariable("lab") Lab lab,
128             @PathVariable("name") String name, @PathVariable("version") String version,
129             @PathVariable("allLayers") Boolean allLayers, @PathVariable("optional") Boolean optional,
130             @PathVariable("outcome") boolean outcome) {
131         try {
132             return new ResponseEntity<>(
133                     resultService.getLastResultBasedOnOutcome(name, version, lab, allLayers, optional, outcome),
134                     HttpStatus.OK);
135         } catch (Exception e) {
136             LOGGER.error(EELFLoggerDelegate.errorLogger,
137                     "Error when retrieving last result. " + UserUtils.getStackTrace(e));
138         }
139         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
140     }
141
142     @RequestMapping(value = {
143     "/getlastrunoflayers/{lab}/{name}/{version}/{layers}/{optional}/{outcome}" }, method = RequestMethod.GET)
144     public ResponseEntity<ValidationNexusTestResult> getLastRunOfLayers(@PathVariable("lab") Lab lab,
145             @PathVariable("name") String name, @PathVariable("version") String version,
146             @PathVariable("layers") List<BlueprintLayer> layers, @PathVariable("optional") Boolean optional,
147             @PathVariable("outcome") boolean outcome) {
148         try {
149             return new ResponseEntity<>(
150                     resultService.getLastResultBasedOnOutcome(name, version, lab, layers, optional, outcome),
151                     HttpStatus.OK);
152         } catch (Exception e) {
153             LOGGER.error(EELFLoggerDelegate.errorLogger,
154                     "Error when retrieving last result. " + UserUtils.getStackTrace(e));
155         }
156         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
157     }
158
159     @RequestMapping(value = { "/getbasedondate/{lab}/{name}/{version}/{date}" }, method = RequestMethod.GET)
160     public ResponseEntity<List<ValidationNexusTestResult>> getBasedOnDate(@PathVariable("lab") Lab lab,
161             @PathVariable("name") String name, @PathVariable("version") String version,
162             @PathVariable("date") String date) {
163         try {
164             DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
165             return new ResponseEntity<>(
166                     resultService.getBasedOnDateFromNexus(name, version, lab, dateFormat.parse(date)), HttpStatus.OK);
167         } catch (Exception e) {
168             LOGGER.error(EELFLoggerDelegate.errorLogger,
169                     "Error when retrieving results based on date. " + UserUtils.getStackTrace(e));
170         }
171         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
172     }
173
174 }