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.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
40 import org.onap.portalsdk.core.web.support.UserUtils;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
43 import org.springframework.transaction.annotation.Transactional;
45 import com.fasterxml.jackson.core.JsonParseException;
46 import com.fasterxml.jackson.databind.JsonMappingException;
47 import com.sun.jersey.api.client.ClientHandlerException;
48 import com.sun.jersey.api.client.UniformInterfaceException;
52 public class IntegratedResultService {
54 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(IntegratedResultService.class);
57 private DbSubmissionAdapter submissionService;
60 NexusExecutorClient nexusService;
63 LabService labService;
66 DbResultAdapter dbAdapter;
68 public List<String> getLabsFromNexus()
69 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
70 UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
71 List<String> labs = new ArrayList<String>();
72 for (String cLabSilo : nexusService.getResource(null)) {
73 for (LabInfo labInfo : labService.getLabs()) {
74 if (labInfo.getSilo().equals(cLabSilo)) {
75 labs.add(labInfo.getLab());
82 public List<String> getBlueprintNamesOfLabFromNexus(@Nonnull String lab)
83 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
84 UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
85 LabInfo labInfo = labService.getLab(lab);
86 if (labInfo == null) {
87 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
89 List<String> cNames = nexusService.getResource(labService.getLab(lab).getSilo());
90 List<String> rNames = new ArrayList<String>();
91 for (String cName : cNames) {
92 if (cName.equals("family") || cName.equals("ta") || cName.equals("job")) {
100 public List<String> getBlueprintVersionsFromNexus(@Nonnull String name, @Nonnull String lab)
101 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
102 UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
103 LabInfo labInfo = labService.getLab(lab);
104 if (labInfo == null) {
105 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
107 return nexusService.getResource(labInfo.getSilo(), name);
110 public List<String> getBlueprintTimeStampsFromNexus(@Nonnull String name, @Nonnull String version,
111 @Nonnull String lab) throws JsonParseException, JsonMappingException, KeyManagementException,
112 ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException, IOException, ParseException {
113 LabInfo labInfo = labService.getLab(lab);
114 if (labInfo == null) {
115 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
117 return nexusService.getResource(labInfo.getSilo(), name, version);
120 public List<ValidationDbTestResult> getResultsFromNexus(@Nonnull String name, @Nonnull String version,
121 @Nonnull String lab, int noTimestamps)
122 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
123 UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException {
124 LabInfo labInfo = labService.getLab(lab);
125 if (labInfo == null) {
126 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
128 return nexusService.getResults(name, version, labInfo.getSilo(), noTimestamps);
131 public ValidationDbTestResult getResultFromNexus(@Nonnull String name, @Nonnull String version, @Nonnull String lab,
132 @Nonnull String timestamp) throws JsonParseException, JsonMappingException, IOException,
133 KeyManagementException, ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException,
134 NullPointerException, ParseException {
135 LabInfo labInfo = labService.getLab(lab);
136 if (labInfo == null) {
137 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
139 ValidationDbTestResult vNexusResult = nexusService.getResult(name, version, labInfo.getSilo(), timestamp);
140 if (!dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
143 vNexusResult.setLab(labInfo);
147 public ValidationDbTestResult getLastResultBasedOnOutcomeFromNexus(@Nonnull String name, @Nonnull String version,
148 @Nonnull String lab, Boolean allLayers, Boolean optional, boolean outcome)
149 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
150 UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException {
151 LabInfo labInfo = labService.getLab(lab);
152 if (labInfo == null) {
153 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
155 ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
156 allLayers, optional, outcome);
157 if (!dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
160 vNexusResult.setLab(labInfo);
164 public ValidationDbTestResult getLastResultBasedOnOutcomeFromNexus(@Nonnull String name, @Nonnull String version,
165 @Nonnull String lab, @Nonnull List<String> layers, Boolean optional, boolean outcome)
166 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
167 UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException {
168 LabInfo labInfo = labService.getLab(lab);
169 if (labInfo == null) {
170 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
172 ValidationDbTestResult vNexusResult = nexusService.getLastResultBasedOnOutcome(name, version, labInfo.getSilo(),
173 layers, optional, outcome);
174 if (!dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
177 vNexusResult.setLab(labInfo);
181 public List<ValidationDbTestResult> getBasedOnDateFromNexus(@Nonnull String name, @Nonnull String version,
182 @Nonnull String lab, @Nonnull Date date)
183 throws JsonParseException, JsonMappingException, IOException, ParseException, KeyManagementException,
184 ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException, NullPointerException {
185 LabInfo labInfo = labService.getLab(lab);
186 if (labInfo == null) {
187 throw new IllegalArgumentException("Could not retrieve lab : " + lab.toString());
189 List<ValidationDbTestResult> vNexusResults = new ArrayList<ValidationDbTestResult>();
190 List<ValidationDbTestResult> vResults = nexusService.getResults(name, version, labInfo.getSilo(), date);
191 if (vResults != null && vResults.size() > 1) {
192 for (ValidationDbTestResult vNexusResult : vResults) {
193 if (dbAdapter.checkValidityOfNexusResult(vNexusResult)) {
194 vNexusResult.setLab(labInfo);
195 vNexusResults.add(vNexusResult);
199 return vNexusResults;
202 public Set<String> getBlueprintNamesOfLabFromDb(String lab) {
203 Set<String> blueprintNames = new HashSet<String>();
204 for (ValidationDbTestResult result : dbAdapter.getValidationTestResults()) {
205 if (result.getLab().getLab().equals(lab)) {
206 blueprintNames.add(result.getBlueprintInstance().getBlueprint().getBlueprintName());
209 return blueprintNames;
212 public Set<String> getBlueprintVersionsFromDb(String name, String lab) {
213 Set<String> blueprintVersions = new HashSet<String>();
214 for (ValidationDbTestResult result : dbAdapter.getValidationTestResults()) {
215 if (result.getLab().getLab().equals(lab)
216 && result.getBlueprintInstance().getBlueprint().getBlueprintName().equals(name)) {
217 blueprintVersions.add(result.getBlueprintInstance().getVersion());
220 return blueprintVersions;
223 public ValidationDbTestResult getResults(@Nonnull String submissionId)
224 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
225 UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException {
226 Submission submission = submissionService.getSubmission(submissionId);
227 ValidationDbTestResult vDbResult = dbAdapter.readResultFromDb(submissionId);
228 return vDbResult == null ? this.getResultFromNexus(
229 submission.getValidationDbTestResult().getBlueprintInstance().getBlueprint().getBlueprintName(),
230 submission.getValidationDbTestResult().getBlueprintInstance().getVersion(),
231 submission.getTimeslot().getLabInfo().getLab(), submission.getValidationDbTestResult().getTimestamp())
235 public ValidationDbTestResult getResult(@Nonnull String name, @Nonnull String version, @Nonnull String lab,
236 @Nonnull String timestamp)
237 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
238 UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException {
239 LabInfo actualLabInfo = labService.getLab(lab);
240 if (actualLabInfo == null) {
243 ValidationDbTestResult vDbResult = dbAdapter.readResultFromDb(lab, timestamp);
244 return vDbResult == null ? this.getResultFromNexus(name, version, lab, timestamp) : vDbResult;
247 public ValidationDbTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version,
248 @Nonnull String lab, Boolean allLayers, Boolean optional, boolean outcome)
249 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
250 UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException {
251 LabInfo actualLabInfo = labService.getLab(lab);
252 if (actualLabInfo == null) {
255 List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, null, allLayers,
257 if (vDbResults != null) {
258 vDbResults.removeIf(entry -> entry.getDateStorage() == null);
259 DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
260 Collections.sort(vDbResults, new Comparator<ValidationDbTestResult>() {
262 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
264 return dateFormat.parse(vDbResult2.getDateStorage())
265 .compareTo(dateFormat.parse(vDbResult1.getDateStorage()));
266 } catch (ParseException e) {
267 LOGGER.error(EELFLoggerDelegate.errorLogger,
268 "Error when parsing date. " + UserUtils.getStackTrace(e));
273 return vDbResults.get(0);
275 return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, allLayers, optional, outcome);
278 public ValidationDbTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version,
279 @Nonnull String lab, List<String> layers, Boolean optional, boolean outcome)
280 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
281 UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException {
282 LabInfo actualLabInfo = labService.getLab(lab);
283 if (actualLabInfo == null) {
286 List<ValidationDbTestResult> vDbResults = dbAdapter.readResultFromDb(name, version, lab, layers, null, optional,
288 if (vDbResults != null && vDbResults.size() > 0) {
289 vDbResults.removeIf(entry -> entry.getDateStorage() == null);
290 DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
291 Collections.sort(vDbResults, new Comparator<ValidationDbTestResult>() {
293 public int compare(ValidationDbTestResult vDbResult1, ValidationDbTestResult vDbResult2) {
295 return dateFormat.parse(vDbResult2.getDateStorage())
296 .compareTo(dateFormat.parse(vDbResult2.getDateStorage()));
297 } catch (ParseException e) {
298 LOGGER.error(EELFLoggerDelegate.errorLogger,
299 "Error when parsing date. " + UserUtils.getStackTrace(e));
304 return vDbResults.get(0);
306 return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, layers, optional, outcome);