UI initial implementation.
[validation.git] / ui / src / main / java / org / akraino / validation / ui / service / ResultService.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.akraino.validation.ui.service;
17
18 import java.io.IOException;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.security.KeyManagementException;
22 import java.security.NoSuchAlgorithmException;
23 import java.util.List;
24
25 import org.akraino.validation.ui.client.jenkins.JenkinsExecutorClient;
26 import org.akraino.validation.ui.client.jenkins.resources.QueueJobItem;
27 import org.akraino.validation.ui.client.jenkins.resources.QueueJobItem.Executable;
28 import org.akraino.validation.ui.client.nexus.NexusExecutorClient;
29 import org.akraino.validation.ui.client.nexus.resources.RobotTestResult;
30 import org.akraino.validation.ui.entity.Submission;
31 import org.apache.commons.httpclient.HttpException;
32 import org.apache.log4j.Logger;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35
36 import com.fasterxml.jackson.core.JsonParseException;
37 import com.fasterxml.jackson.databind.JsonMappingException;
38 import com.sun.jersey.api.client.ClientHandlerException;
39 import com.sun.jersey.api.client.UniformInterfaceException;
40
41 @Service
42 public class ResultService {
43
44     private static final Logger LOGGER = Logger.getLogger(ResultService.class);
45
46     @Autowired
47     private SubmissionService submissionService;
48
49     @Deprecated
50     public URL getNexusResultUrl(Submission submission)
51             throws MalformedURLException, KeyManagementException, HttpException, ClientHandlerException,
52             UniformInterfaceException, NoSuchAlgorithmException, InterruptedException {
53
54         String url = System.getenv("jenkins_url");
55         String userName = System.getenv("jenkins_user_name");
56         String password = System.getenv("jenkins_user_pwd");
57
58         Executable executable = null;
59         while (executable == null) {
60             JenkinsExecutorClient client;
61             client = JenkinsExecutorClient.getInstance(userName, password, url);
62             QueueJobItem queueJobItem = client.getQueueJobItem(new URL(submission.getJenkinsQueueJobItemUrl()));
63             executable = queueJobItem.getExecutable();
64             Thread.sleep(2000);
65         }
66         return new URL(System.getenv("nexus_results_url") + "/"
67                 + submission.getBlueprintInstance().getTimeslot().getLab().name().toLowerCase() + "-blu-val"
68                 + "/job/validation/" + String.valueOf(executable.getNumber()));
69     }
70
71     public List<RobotTestResult> getRobotTestResults(String submissionId)
72             throws JsonParseException, JsonMappingException, KeyManagementException, ClientHandlerException,
73             UniformInterfaceException, NoSuchAlgorithmException, IOException {
74         Submission submission = submissionService.getSubmission(submissionId);
75         if (submission == null) {
76             LOGGER.info("Requested submission does not exist");
77             return null;
78         }
79         String nexusUrl = submission.getNexusResultUrl();
80         NexusExecutorClient client = new NexusExecutorClient(nexusUrl + "/results");
81         return client.getRobotTestResults();
82     }
83
84 }