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;
20 import java.security.KeyManagementException;
21 import java.security.NoSuchAlgorithmException;
22 import java.util.List;
24 import org.akraino.validation.ui.client.jenkins.JenkinsExecutorClient;
25 import org.akraino.validation.ui.client.jenkins.resources.QueueJobItem;
26 import org.akraino.validation.ui.client.jenkins.resources.QueueJobItem.Executable;
27 import org.akraino.validation.ui.client.nexus.NexusExecutorClient;
28 import org.akraino.validation.ui.client.nexus.resources.WrapperRobotTestResult;
29 import org.akraino.validation.ui.conf.UiUtils;
30 import org.akraino.validation.ui.data.BlueprintLayer;
31 import org.akraino.validation.ui.entity.LabSilo;
32 import org.akraino.validation.ui.entity.Submission;
33 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
37 import com.fasterxml.jackson.core.JsonParseException;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.sun.jersey.api.client.ClientHandlerException;
40 import com.sun.jersey.api.client.UniformInterfaceException;
43 public class ResultService {
45 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ResultService.class);
48 private SubmissionService submissionService;
51 private SiloService siloService;
54 public URL getNexusResultUrl(Submission submission) throws Exception {
56 String url = System.getenv("JENKINS_URL");
57 String userName = System.getenv("JENKINS_USERNAME");
58 String password = System.getenv("JENKINS_USER_PASSWORD");
60 Executable executable = null;
61 while (executable == null) {
62 JenkinsExecutorClient client;
63 client = JenkinsExecutorClient.getInstance(userName, password, url);
64 QueueJobItem queueJobItem = client.getQueueJobItem(new URL(submission.getJenkinsQueueJobItemUrl()));
65 executable = queueJobItem.getExecutable();
68 String siloText = null;
69 for (LabSilo silo : siloService.getSilos()) {
70 if (silo.getLab().getLab().equals(submission.getTimeslot().getLab().getLab())) {
71 siloText = silo.getSilo();
74 if (siloText == null) {
75 throw new Exception("Could not retrieve silo of the selected lab : "
76 + submission.getTimeslot().getLab().getLab().toString());
78 String nexusUrl = UiUtils.NEXUS_URL + "/" + siloText + "/job/" + System.getenv("JENKINS_JOB_NAME") + "/"
79 + String.valueOf(executable.getNumber() + "/results");
80 if (!submission.getBlueprintInstanceForValidation().getLayer().equals(BlueprintLayer.All)) {
81 nexusUrl = nexusUrl + "/" + submission.getBlueprintInstanceForValidation().getLayer().name().toLowerCase();
83 return new URL(nexusUrl);
86 public List<WrapperRobotTestResult> getRobotTestResults(String submissionId)
87 throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
88 UniformInterfaceException, NoSuchAlgorithmException, IOException {
89 Submission submission = submissionService.getSubmission(submissionId);
90 if (submission == null) {
91 LOGGER.info(EELFLoggerDelegate.applicationLogger, "Requested submission does not exist");
94 String nexusUrl = submission.getNexusResultUrl();
95 String urlLastpart = nexusUrl.substring(nexusUrl.lastIndexOf('/') + 1);
96 if (blueprintLayerContains(urlLastpart.substring(0, 1).toUpperCase() + urlLastpart.substring(1))) {
97 nexusUrl = nexusUrl.substring(0, nexusUrl.lastIndexOf(urlLastpart) - 1);
99 NexusExecutorClient client = new NexusExecutorClient(nexusUrl);
100 return client.getRobotTestResults();
103 private boolean blueprintLayerContains(String layer) {
104 for (BlueprintLayer blueprintLayer : BlueprintLayer.values()) {
105 if (blueprintLayer.name().equals(layer)) {