2 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.akraino.validation.ui.service;
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;
33 import javax.annotation.Nonnull;
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;
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;
53 public class IntegratedResultService {
55 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(IntegratedResultService.class);
58 private DbSubmissionAdapter submissionService;
61 NexusExecutorClient nexusService;
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());
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());
87 List<String> rNames = new ArrayList<String>();
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")) {
96 } catch (HttpException ex) {
97 LOGGER.warn(EELFLoggerDelegate.auditLogger,
98 "Error when retrieving blueprint names from nexus" + UserUtils.getStackTrace(ex));
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());
110 return nexusService.getResource(labInfo.getSilo() + "/bluval_results", name);
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());
120 List<String> timestamps = new ArrayList<String>();
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));
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());
138 return nexusService.getResults(name, version, labInfo.getSilo(), noTimestamps);
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());
149 ValidationDbTestResult vNexusResult = nexusService.getResult(name, version, labInfo.getSilo(), timestamp);
150 if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
151 vNexusResult.setLab(labInfo);
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());
165 ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
166 allLayers, optional, outcome);
167 if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
168 vNexusResult.setLab(labInfo);
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());
182 ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
183 layers, optional, outcome);
184 if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
185 vNexusResult.setLab(labInfo);
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());
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);
209 return vNexusResults;
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());
219 return blueprintNames;
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());
230 return blueprintVersions;
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())
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) {
253 ValidationDbTestResult vDbResult = dbAdapter.readResultFromDb(lab, timestamp);
254 return vDbResult == null ? this.getResultFromNexus(name, version, lab, timestamp) : vDbResult;
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) {
265 List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, null, allLayers,
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>() {
272 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
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));
283 return vDbResults.get(0);
285 return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, allLayers, optional, outcome);
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) {
296 List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, layers, null, optional,
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>() {
303 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
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));
314 return vDbResults.get(0);
316 return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, layers, optional, outcome);