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.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;
31 import javax.annotation.Nonnull;
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;
44 import com.fasterxml.jackson.core.JsonParseException;
45 import com.fasterxml.jackson.databind.JsonMappingException;
49 public class IntegratedResultService {
51 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(IntegratedResultService.class);
54 private DbSubmissionAdapter submissionService;
57 NexusExecutorClient nexusService;
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());
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());
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")) {
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());
98 return nexusService.getResource(labInfo.getSilo() + "/bluval_results", name);
101 public List<String> getBlueprintTimeStampsFromNexus(@Nonnull String name, @Nonnull String version,
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());
108 List<String> timestamps = new ArrayList<String>();
109 timestamps = nexusService.getResource(labInfo.getSilo() + "/bluval_results", name, version);
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());
120 return nexusService.getResults(name, version, labInfo.getSilo(), noTimestamps);
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());
130 ValidationDbTestResult vNexusResult = nexusService.getResult(name, version, labInfo.getSilo(), timestamp);
131 if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
132 vNexusResult.setLab(labInfo);
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());
145 ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
146 allLayers, optional, outcome);
147 if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
148 vNexusResult.setLab(labInfo);
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());
161 ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
162 layers, optional, outcome);
163 if (vNexusResult != null && dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
164 vNexusResult.setLab(labInfo);
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());
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);
187 return vNexusResults;
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());
197 return blueprintNames;
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());
208 return blueprintVersions;
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())
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) {
229 ValidationDbTestResult vDbResult = dbAdapter.readResultFromDb(lab, timestamp);
230 return vDbResult == null ? this.getResultFromNexus(name, version, lab, timestamp) : vDbResult;
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) {
240 List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, null, allLayers,
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>() {
247 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
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));
258 return vDbResults.get(0);
260 return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, allLayers, optional, outcome);
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,
267 LabInfo actualLabInfo = dbAdapter.getLab(lab);
268 if (actualLabInfo == null) {
271 List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, layers, null, optional,
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>() {
278 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
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));
289 return vDbResults.get(0);
291 return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, layers, optional, outcome);