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