[RECV-94] Separate docker/robot invoking
[validation.git] / ui / src / main / java / org / akraino / validation / ui / service / SubmissionService.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.net.DatagramSocket;
19 import java.net.InetAddress;
20 import java.net.MalformedURLException;
21 import java.net.SocketException;
22 import java.net.UnknownHostException;
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Random;
28 import java.util.concurrent.CompletableFuture;
29
30 import org.akraino.validation.ui.client.jenkins.JenkinsExecutorClient;
31 import org.akraino.validation.ui.client.jenkins.resources.Parameter;
32 import org.akraino.validation.ui.client.jenkins.resources.Parameters;
33 import org.akraino.validation.ui.conf.UiUtils;
34 import org.akraino.validation.ui.dao.SubmissionDAO;
35 import org.akraino.validation.ui.data.SubmissionStatus;
36 import org.akraino.validation.ui.entity.Submission;
37 import org.akraino.validation.ui.service.utils.PrioritySupplier;
38 import org.akraino.validation.ui.service.utils.SubmissionHelper;
39 import org.apache.commons.httpclient.HttpException;
40 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.onap.portalsdk.core.web.support.UserUtils;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Service;
44 import org.springframework.transaction.annotation.Transactional;
45
46 import com.sun.jersey.api.client.ClientHandlerException;
47 import com.sun.jersey.api.client.UniformInterfaceException;
48
49 @Service
50 @Transactional
51 public class SubmissionService {
52
53     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(SubmissionService.class);
54
55     @Autowired
56     private SubmissionDAO submissionDAO;
57
58     @Autowired
59     private SubmissionHelper submissionHelper;
60
61     public Submission saveSubmission(Submission submission) {
62         submission.setSubmissionStatus(SubmissionStatus.Submitted);
63         submissionDAO.saveOrUpdate(submission);
64
65         JenkinsTriggerSubmissionJob task = new JenkinsTriggerSubmissionJob(submission);
66         CompletableFuture<Submission> completableFuture =
67                 CompletableFuture.supplyAsync(new PrioritySupplier<>(1, task::execute), UiUtils.executorService);
68         completableFuture.thenAcceptAsync(result -> this.callbackNotify(result));
69
70         return submission;
71     }
72
73     public List<Submission> getSubmissions() {
74         return submissionDAO.getSubmissions();
75     }
76
77     public Submission getSubmission(String submissionId) {
78         return submissionDAO.getSubmission(Integer.valueOf(submissionId));
79     }
80
81     public void deleteSubmission(Integer submissionId) {
82         submissionDAO.deleteSubmission(submissionId);
83     }
84
85     public void deleteAll() {
86         submissionDAO.deleteAll();
87     }
88
89     private void callbackNotify(Submission submission) {
90         if (submission == null) {
91             return;
92         }
93         submission.setSubmissionStatus(SubmissionStatus.Running);
94         submissionHelper.saveSubmission(submission);
95     }
96
97     private class JenkinsTriggerSubmissionJob {
98
99         private Submission submission;
100
101         public JenkinsTriggerSubmissionJob(Submission submission) {
102             this.submission = submission;
103         }
104
105         public Submission execute() {
106             String url = System.getenv("JENKINS_URL");
107             String userName = System.getenv("JENKINS_USERNAME");
108             String userPassword = System.getenv("JENKINS_USER_PASSWORD");
109             String jobName = System.getenv("JENKINS_JOB_NAME");
110             List<Parameter> listOfParameters = new ArrayList<Parameter>();
111             Parameters parameters = new Parameters();
112             Parameter parameter = new Parameter();
113             parameter.setName("SUBMISSION_ID");
114             parameter.setValue(String.valueOf(submission.getSubmissionId()));
115             listOfParameters.add(parameter);
116             parameter = new Parameter();
117             parameter.setName("BLUEPRINT");
118             parameter.setValue(
119                     submission.getBlueprintInstanceForValidation().getBlueprint().getBlueprintName().toLowerCase());
120             listOfParameters.add(parameter);
121             parameter = new Parameter();
122             parameter.setName("LAYER");
123             parameter.setValue(submission.getBlueprintInstanceForValidation().getLayer().name().toLowerCase());
124             listOfParameters.add(parameter);
125             parameter = new Parameter();
126             parameter.setName("VERSION");
127             parameter.setValue(submission.getBlueprintInstanceForValidation().getVersion().toLowerCase());
128             listOfParameters.add(parameter);
129             parameter = new Parameter();
130             parameter.setName("UI_IP");
131             Random random = new Random();
132             String localIP = null;
133             try (final DatagramSocket socket = new DatagramSocket()) {
134                 socket.connect(InetAddress.getByName(random.nextInt(256) + "." + random.nextInt(256) + "."
135                         + random.nextInt(256) + "." + random.nextInt(256)), 10002);
136                 localIP = socket.getLocalAddress().getHostAddress();
137                 parameter.setValue(localIP);
138                 listOfParameters.add(parameter);
139                 parameters.setParameter(listOfParameters);
140                 JenkinsExecutorClient client;
141                 client = JenkinsExecutorClient.getInstance(userName, userPassword, url);
142                 submission.setJnksQueueJobItemUrl(client.postJobWithQueryParams(jobName, parameters).toString());
143                 return submission;
144             } catch (SocketException | UnknownHostException | KeyManagementException | HttpException
145                     | ClientHandlerException | UniformInterfaceException | MalformedURLException
146                     | NoSuchAlgorithmException e) {
147                 LOGGER.error(EELFLoggerDelegate.errorLogger,
148                         "Error when triggering Jenkins job. " + UserUtils.getStackTrace(e));
149                 return null;
150             }
151         }
152     }
153
154 }