[UI] Ignore malformed results
[validation.git] / ui / src / main / java / org / akraino / validation / ui / service / IntegratedResultService.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.service;
17
18 import java.io.IOException;
19 import java.security.KeyManagementException;
20 import java.security.NoSuchAlgorithmException;
21 import java.text.DateFormat;
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.Date;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Set;
32
33 import javax.annotation.Nonnull;
34
35 import org.akraino.validation.ui.client.nexus.NexusExecutorClient;
36 import org.akraino.validation.ui.entity.LabInfo;
37 import org.akraino.validation.ui.entity.Submission;
38 import org.akraino.validation.ui.entity.ValidationDbTestResult;
39 import org.apache.commons.httpclient.HttpException;
40 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.onap.portalsdk.core.web.support.UserUtils;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Service;
44 import org.springframework.transaction.annotation.Transactional;
45
46 import com.fasterxml.jackson.core.JsonParseException;
47 import com.fasterxml.jackson.databind.JsonMappingException;
48 import com.sun.jersey.api.client.ClientHandlerException;
49 import com.sun.jersey.api.client.UniformInterfaceException;
50
51 @Service
52 @Transactional
53 public class IntegratedResultService {
54
55     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(IntegratedResultService.class);
56
57     @Autowired
58     private DbSubmissionAdapter submissionService;
59
60     @Autowired
61     NexusExecutorClient nexusService;
62
63     @Autowired
64     DbAdapter dbAdapter;
65
66     public List<String> getLabsFromNexus()
67             throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
68             UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
69         List<String> labs = new ArrayList<String>();
70         for (String cLabSilo : nexusService.getResource(null)) {
71             for (LabInfo labInfo : dbAdapter.getLabs()) {
72                 if (labInfo.getSilo().equals(cLabSilo)) {
73                     labs.add(labInfo.getLab());
74                 }
75             }
76         }
77         return labs;
78     }
79
80     public List<String> getBlueprintNamesOfLabFromNexus(@Nonnull String lab)
81             throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
82             UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
83         LabInfo labInfo = dbAdapter.getLab(lab);
84         if (labInfo == null) {
85             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
86         }
87         List<String> rNames = new ArrayList<String>();
88         try {
89             List<String> cNames = nexusService.getResource(dbAdapter.getLab(lab).getSilo() + "/bluval_results");
90             for (String cName : cNames) {
91                 if (cName.equals("family") || cName.equals("ta") || cName.equals("job")) {
92                     continue;
93                 }
94                 rNames.add(cName);
95             }
96         } catch (HttpException ex) {
97             LOGGER.warn(EELFLoggerDelegate.auditLogger,
98                     "Error when retrieving blueprint names from nexus" + UserUtils.getStackTrace(ex));
99         }
100         return rNames;
101     }
102
103     public List<String> getBlueprintVersionsFromNexus(@Nonnull String name, @Nonnull String lab)
104             throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
105             UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
106         LabInfo labInfo = dbAdapter.getLab(lab);
107         if (labInfo == null) {
108             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
109         }
110         return nexusService.getResource(labInfo.getSilo() + "/bluval_results", name);
111     }
112
113     public List<String> getBlueprintTimeStampsFromNexus(@Nonnull String name, @Nonnull String version,
114             @Nonnull String lab) throws JsonParseException, JsonMappingException, KeyManagementException,
115     ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException, IOException, ParseException {
116         LabInfo labInfo = dbAdapter.getLab(lab);
117         if (labInfo == null) {
118             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
119         }
120         List<String> timestamps = new ArrayList<String>();
121         try {
122             timestamps = nexusService.getResource(labInfo.getSilo() + "/bluval_results", name, version);
123         } catch (HttpException ex) {
124             LOGGER.warn(EELFLoggerDelegate.auditLogger,
125                     "Error when retrieving blueprint names from nexus" + UserUtils.getStackTrace(ex));
126         }
127         return timestamps;
128     }
129
130     public List<ValidationDbTestResult> getResultsFromNexus(@Nonnull String name, @Nonnull String version,
131             @Nonnull String lab, int noTimestamps)
132                     throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
133                     UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
134         LabInfo labInfo = dbAdapter.getLab(lab);
135         if (labInfo == null) {
136             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
137         }
138         return nexusService.getResults(name, version, labInfo.getSilo(), noTimestamps);
139     }
140
141     public ValidationDbTestResult getResultFromNexus(@Nonnull String name, @Nonnull String version, @Nonnull String lab,
142             @Nonnull String timestamp) throws JsonParseException, JsonMappingException, IOException,
143     KeyManagementException, ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException,
144     NullPointerException, ParseException {
145         LabInfo labInfo = dbAdapter.getLab(lab);
146         if (labInfo == null) {
147             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
148         }
149         ValidationDbTestResult vNexusResult = nexusService.getResult(name, version, labInfo.getSilo(), timestamp);
150         if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
151             vNexusResult.setLab(labInfo);
152             return vNexusResult;
153         }
154         return null;
155     }
156
157     public ValidationDbTestResult getLastResultBasedOnOutcomeFromNexus(@Nonnull String name, @Nonnull String version,
158             @Nonnull String lab, Boolean allLayers, Boolean optional, boolean outcome)
159                     throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
160                     UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException {
161         LabInfo labInfo = dbAdapter.getLab(lab);
162         if (labInfo == null) {
163             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
164         }
165         ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
166                 allLayers, optional, outcome);
167         if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
168             vNexusResult.setLab(labInfo);
169             return vNexusResult;
170         }
171         return null;
172     }
173
174     public ValidationDbTestResult getLastResultBasedOnOutcomeFromNexus(@Nonnull String name, @Nonnull String version,
175             @Nonnull String lab, @Nonnull List<String> layers, Boolean optional, boolean outcome)
176                     throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
177                     UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException {
178         LabInfo labInfo = dbAdapter.getLab(lab);
179         if (labInfo == null) {
180             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
181         }
182         ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
183                 layers, optional, outcome);
184         if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
185             vNexusResult.setLab(labInfo);
186             return vNexusResult;
187         }
188         return null;
189     }
190
191     public List<ValidationDbTestResult> getBasedOnDateFromNexus(@Nonnull String name, @Nonnull String version,
192             @Nonnull String lab, @Nonnull Date date)
193                     throws JsonParseException, JsonMappingException, IOException, ParseException, KeyManagementException,
194                     ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException, NullPointerException {
195         LabInfo labInfo = dbAdapter.getLab(lab);
196         if (labInfo == null) {
197             throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
198         }
199         List<ValidationDbTestResult> vNexusResults = new ArrayList<ValidationDbTestResult>();
200         List<ValidationDbTestResult> vResults = nexusService.getResults(name, version, labInfo.getSilo(), date);
201         if (vResults != null && vResults.size() >= 1) {
202             for (ValidationDbTestResult vNexusResult : vResults) {
203                 if (dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
204                     vNexusResult.setLab(labInfo);
205                     vNexusResults.add(vNexusResult);
206                 }
207             }
208         }
209         return vNexusResults;
210     }
211
212     public Set<String> getBlueprintNamesOfLabFromDb(String lab) {
213         Set<String> blueprintNames = new HashSet<String>();
214         for (ValidationDbTestResult result : dbAdapter.getValidationTestResults()) {
215             if (result.getLab().getLab().equals(lab)) {
216                 blueprintNames.add(result.getBlueprintInstance().getBlueprint().getBlueprintName());
217             }
218         }
219         return blueprintNames;
220     }
221
222     public Set<String> getBlueprintVersionsFromDb(String name, String lab) {
223         Set<String> blueprintVersions = new HashSet<String>();
224         for (ValidationDbTestResult result : dbAdapter.getValidationTestResults()) {
225             if (result.getLab().getLab().equals(lab)
226                     && result.getBlueprintInstance().getBlueprint().getBlueprintName().equals(name)) {
227                 blueprintVersions.add(result.getBlueprintInstance().getVersion());
228             }
229         }
230         return blueprintVersions;
231     }
232
233     public ValidationDbTestResult getResults(@Nonnull String submissionId)
234             throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
235             UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException {
236         Submission submission = submissionService.getSubmission(submissionId);
237         ValidationDbTestResult vDbResult = dbAdapter.readResultFromDb(submissionId);
238         return vDbResult == null ? this.getResultFromNexus(
239                 submission.getValidationDbTestResult().getBlueprintInstance().getBlueprint().getBlueprintName(),
240                 submission.getValidationDbTestResult().getBlueprintInstance().getVersion(),
241                 submission.getTimeslot().getLabInfo().getLab(), submission.getValidationDbTestResult().getTimestamp())
242                 : vDbResult;
243     }
244
245     public ValidationDbTestResult getResult(@Nonnull String name, @Nonnull String version, @Nonnull String lab,
246             @Nonnull String timestamp)
247                     throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
248                     UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException {
249         LabInfo actualLabInfo = dbAdapter.getLab(lab);
250         if (actualLabInfo == null) {
251             return null;
252         }
253         ValidationDbTestResult vDbResult = dbAdapter.readResultFromDb(lab, timestamp);
254         return vDbResult == null ? this.getResultFromNexus(name, version, lab, timestamp) : vDbResult;
255     }
256
257     public ValidationDbTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version,
258             @Nonnull String lab, Boolean allLayers, Boolean optional, boolean outcome)
259                     throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
260                     UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException {
261         LabInfo actualLabInfo = dbAdapter.getLab(lab);
262         if (actualLabInfo == null) {
263             return null;
264         }
265         List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, null, allLayers,
266                 optional, outcome);
267         if (vDbResults != null) {
268             vDbResults.removeIf(entry -> entry.getDateStorage() == null);
269             DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
270             Collections.sort(vDbResults, new Comparator<ValidationDbTestResult>() {
271                 @Override
272                 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
273                     try {
274                         return dateFormat.parse(vDbResult2.getDateStorage())
275                                 .compareTo(dateFormat.parse(vDbResult1.getDateStorage()));
276                     } catch (ParseException e) {
277                         LOGGER.error(EELFLoggerDelegate.errorLogger,
278                                 "Error when parsing date. " + UserUtils.getStackTrace(e));
279                         return 0;
280                     }
281                 }
282             });
283             return vDbResults.get(0);
284         }
285         return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, allLayers, optional, outcome);
286     }
287
288     public ValidationDbTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version,
289             @Nonnull String lab, List<String> layers, Boolean optional, boolean outcome)
290                     throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
291                     UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException {
292         LabInfo actualLabInfo = dbAdapter.getLab(lab);
293         if (actualLabInfo == null) {
294             return null;
295         }
296         List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, layers, null, optional,
297                 outcome);
298         if (vDbResults != null && vDbResults.size() > 0) {
299             vDbResults.removeIf(entry -> entry.getDateStorage() == null);
300             DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
301             Collections.sort(vDbResults, new Comparator<ValidationDbTestResult>() {
302                 @Override
303                 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
304                     try {
305                         return dateFormat.parse(vDbResult2.getDateStorage())
306                                 .compareTo(dateFormat.parse(vDbResult2.getDateStorage()));
307                     } catch (ParseException e) {
308                         LOGGER.error(EELFLoggerDelegate.errorLogger,
309                                 "Error when parsing date. " + UserUtils.getStackTrace(e));
310                         return 0;
311                     }
312                 }
313             });
314             return vDbResults.get(0);
315         }
316         return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, layers, optional, outcome);
317     }
318
319 }