X-Git-Url: https://gerrit.akraino.org/r/gitweb?p=validation.git;a=blobdiff_plain;f=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fservice%2FIntegratedResultService.java;fp=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fservice%2FIntegratedResultService.java;h=001559097b1e7181a0c30e57a383073f4ab2836a;hp=0000000000000000000000000000000000000000;hb=2eba847ebb6acb2686be08eb1cdafc1b12071e7d;hpb=f86b9715d156238532fcb0bf464bd72e9cf7ce96 diff --git a/ui/src/main/java/org/akraino/validation/ui/service/IntegratedResultService.java b/ui/src/main/java/org/akraino/validation/ui/service/IntegratedResultService.java new file mode 100644 index 0000000..0015590 --- /dev/null +++ b/ui/src/main/java/org/akraino/validation/ui/service/IntegratedResultService.java @@ -0,0 +1,336 @@ +/* + * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.akraino.validation.ui.service; + +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import javax.annotation.Nonnull; + +import org.akraino.validation.ui.client.nexus.NexusExecutorClient; +import org.akraino.validation.ui.client.nexus.resources.ValidationNexusTestResult; +import org.akraino.validation.ui.data.BlueprintLayer; +import org.akraino.validation.ui.data.Lab; +import org.akraino.validation.ui.data.SubmissionData; +import org.akraino.validation.ui.entity.LabInfo; +import org.akraino.validation.ui.entity.LabSilo; +import org.akraino.validation.ui.entity.ValidationDbTestResult; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.sun.jersey.api.client.ClientHandlerException; +import com.sun.jersey.api.client.UniformInterfaceException; + +@Service +@Transactional +public class IntegratedResultService { + + private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(IntegratedResultService.class); + + @Autowired + private DbSubmissionAdapter submissionService; + + @Autowired + private SiloService siloService; + + @Autowired + NexusExecutorClient nexusService; + + @Autowired + LabService labService; + + @Autowired + DbResultAdapter dbAdapter; + + public List getLabsFromNexus() + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException { + List labs = new ArrayList(); + for (String cLabSilo : nexusService.getResource(null)) { + for (LabSilo silo : siloService.getSilos()) { + if (silo.getSilo().equals(cLabSilo)) { + labs.add(silo.getLab().getLab()); + } + } + } + return labs; + } + + public List getBlueprintNamesOfLabFromNexus(@Nonnull Lab lab) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Could not retrieve blueprint names of lab : " + lab.toString()); + } + List blueprintNames = new ArrayList(); + List cBlueprintNames = nexusService.getResource(siloText); + for (String cBlueprintName : cBlueprintNames) { + if (!cBlueprintName.equals("job")) { + blueprintNames.add(cBlueprintName); + } + } + return blueprintNames; + } + + public List getBlueprintVersionsFromNexus(@Nonnull String name, @Nonnull Lab lab) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Could not retrieve silo of the lab : " + lab.toString()); + } + return nexusService.getResource(siloText, name); + } + + public List getBlueprintTimeStampsFromNexus(@Nonnull String name, @Nonnull String version, @Nonnull Lab lab) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, ParseException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Could not retrieve silo of the lab : " + lab.toString()); + } + return nexusService.getResource(siloText, name, version); + } + + public List getResultsFromNexus(@Nonnull String name, @Nonnull String version, + @Nonnull Lab lab, int noTimestamps) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, IllegalArgumentException, ParseException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Could not retrieve silo of the lab : " + lab.toString()); + } + return nexusService.getResults(name, version, siloText, noTimestamps); + } + + public ValidationNexusTestResult getResultFromNexus(@Nonnull String name, @Nonnull String version, @Nonnull Lab lab, + @Nonnull String timestamp) throws JsonParseException, JsonMappingException, IOException, + KeyManagementException, ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException, + NullPointerException, ParseException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Could not retrieve silo of the lab : " + lab.toString()); + } + return nexusService.getResult(name, version, siloText, timestamp); + } + + public ValidationNexusTestResult getLastResultBasedOnOutcomeFromNexus(@Nonnull String name, @Nonnull String version, + @Nonnull Lab lab, Boolean allLayers, Boolean optional, boolean outcome) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Lab does not exist: " + lab.toString()); + } + return nexusService.getLastResultBasedOnOutcome(name, version, siloText, allLayers, optional, outcome); + } + + public ValidationNexusTestResult getLastResultBasedOnOutcomeFromNexus(@Nonnull String name, @Nonnull String version, + @Nonnull Lab lab, @Nonnull List layers, Boolean optional, boolean outcome) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Lab does not exist: " + lab.toString()); + } + return nexusService.getLastResultBasedOnOutcome(name, version, siloText, layers, optional, outcome); + } + + public List getBasedOnDateFromNexus(@Nonnull String name, @Nonnull String version, + @Nonnull Lab lab, @Nonnull Date date) + throws JsonParseException, JsonMappingException, IOException, ParseException, KeyManagementException, + ClientHandlerException, UniformInterfaceException, NoSuchAlgorithmException, NullPointerException { + String siloText = null; + for (LabSilo silo : siloService.getSilos()) { + if (silo.getLab().getLab().equals(lab)) { + siloText = silo.getSilo(); + } + } + if (siloText == null) { + throw new IllegalArgumentException("Lab does not exist: " + lab.toString()); + } + return nexusService.getResults(name, version, siloText, date); + } + + public Set getLabsFromDb() { + Set labs = new HashSet(); + for (ValidationDbTestResult result : dbAdapter.getValidationTestResults()) { + labs.add(result.getLab().getLab()); + } + return labs; + } + + public Set getBlueprintNamesOfLabFromDb(Lab lab) { + Set blueprintNames = new HashSet(); + for (ValidationDbTestResult result : dbAdapter.getValidationTestResults()) { + if (result.getLab().getLab().equals(lab)) { + blueprintNames.add(result.getBlueprintName()); + } + } + return blueprintNames; + } + + public Set getBlueprintVersionsFromDb(String name, Lab lab) { + Set blueprintVersions = new HashSet(); + for (ValidationDbTestResult result : dbAdapter.getValidationTestResults()) { + if (result.getLab().getLab().equals(lab) && result.getBlueprintName().equals(name)) { + blueprintVersions.add(result.getVersion()); + } + } + return blueprintVersions; + } + + public ValidationNexusTestResult getResults(@Nonnull String submissionId) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException { + SubmissionData submissionData = submissionService.getSubmissionData(submissionId); + ValidationNexusTestResult vNexusResult = dbAdapter.readResultFromDb(submissionId); + return vNexusResult == null + ? this.getResultFromNexus(submissionData.getValidationNexusTestResult().getBlueprintName(), + submissionData.getValidationNexusTestResult().getVersion(), + submissionData.getTimeslot().getLab().getLab(), + submissionData.getValidationNexusTestResult().getTimestamp()) + : vNexusResult; + } + + public ValidationNexusTestResult getResult(@Nonnull String name, @Nonnull String version, @Nonnull Lab lab, + @Nonnull String timestamp) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, NullPointerException, IOException, ParseException { + LabInfo actualLabInfo = labService.getLab(lab); + if (actualLabInfo == null) { + return null; + } + ValidationNexusTestResult vNexusResult = dbAdapter.readResultFromDb(lab, timestamp); + return vNexusResult == null ? this.getResultFromNexus(name, version, lab, timestamp) : vNexusResult; + } + + public ValidationNexusTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version, + @Nonnull Lab lab, Boolean allLayers, Boolean optional, boolean outcome) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException { + LabInfo actualLabInfo = labService.getLab(lab); + if (actualLabInfo == null) { + return null; + } + List vNexusResults = dbAdapter.readResultFromDb(name, version, lab, null, allLayers, + optional, outcome); + if (vNexusResults != null) { + vNexusResults.removeIf(entry -> entry.getDateOfStorage() == null); + DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + Collections.sort(vNexusResults, new Comparator() { + @Override + public int compare(ValidationNexusTestResult vNexusResult1, ValidationNexusTestResult vNexusResult2) { + try { + return dateFormat.parse(vNexusResult2.getDateOfStorage()) + .compareTo(dateFormat.parse(vNexusResult1.getDateOfStorage())); + } catch (ParseException e) { + LOGGER.error(EELFLoggerDelegate.errorLogger, + "Error when parsing date. " + UserUtils.getStackTrace(e)); + return 0; + } + } + }); + return vNexusResults.get(0); + } + return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, allLayers, optional, outcome); + } + + public ValidationNexusTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version, + @Nonnull Lab lab, List layers, Boolean optional, boolean outcome) + throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException, + UniformInterfaceException, NoSuchAlgorithmException, IOException, NullPointerException, ParseException { + LabInfo actualLabInfo = labService.getLab(lab); + if (actualLabInfo == null) { + return null; + } + List vNexusResults = dbAdapter.readResultFromDb(name, version, lab, layers, null, + optional, outcome); + if (vNexusResults != null) { + vNexusResults.removeIf(entry -> entry.getDateOfStorage() == null); + DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + Collections.sort(vNexusResults, new Comparator() { + @Override + public int compare(ValidationNexusTestResult vNexusResult1, ValidationNexusTestResult vNexusResult2) { + try { + return dateFormat.parse(vNexusResult2.getDateOfStorage()) + .compareTo(dateFormat.parse(vNexusResult1.getDateOfStorage())); + } catch (ParseException e) { + LOGGER.error(EELFLoggerDelegate.errorLogger, + "Error when parsing date. " + UserUtils.getStackTrace(e)); + return 0; + } + } + }); + return vNexusResults.get(0); + } + return this.getLastResultBasedOnOutcomeFromNexus(name, version, lab, layers, optional, outcome); + } + +}