[RECV-94] Separate docker/robot invoking
[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.URL;
20 import java.security.KeyManagementException;
21 import java.security.NoSuchAlgorithmException;
22 import java.util.List;
23
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;
36
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;
41
42 @Service
43 public class ResultService {
44
45     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ResultService.class);
46
47     @Autowired
48     private SubmissionService submissionService;
49
50     @Autowired
51     private SiloService siloService;
52
53     @Deprecated
54     public URL getNexusResultUrl(Submission submission) throws Exception {
55
56         String url = System.getenv("JENKINS_URL");
57         String userName = System.getenv("JENKINS_USERNAME");
58         String password = System.getenv("JENKINS_USER_PASSWORD");
59
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();
66             Thread.sleep(2000);
67         }
68         String siloText = null;
69         for (LabSilo silo : siloService.getSilos()) {
70             if (silo.getLab().getLab().equals(submission.getTimeslot().getLab().getLab())) {
71                 siloText = silo.getSilo();
72             }
73         }
74         if (siloText == null) {
75             throw new Exception("Could not retrieve silo of the selected lab : "
76                     + submission.getTimeslot().getLab().getLab().toString());
77         }
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();
82         }
83         return new URL(nexusUrl);
84     }
85
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");
92             return null;
93         }
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);
98         }
99         NexusExecutorClient client = new NexusExecutorClient(nexusUrl);
100         return client.getRobotTestResults();
101     }
102
103     private boolean blueprintLayerContains(String layer) {
104         for (BlueprintLayer blueprintLayer : BlueprintLayer.values()) {
105             if (blueprintLayer.name().equals(layer)) {
106                 return true;
107             }
108         }
109         return false;
110     }
111
112 }