Merge "Refactor conformance robot test"
[validation.git] / ui / src / main / java / org / akraino / validation / ui / service / DbSubmissionAdapter.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.DatagramSocket;
20 import java.net.InetAddress;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Random;
24 import java.util.concurrent.CompletableFuture;
25 import java.util.concurrent.ExecutorService;
26
27 import org.akraino.validation.ui.client.jenkins.JenkinsExecutorClient;
28 import org.akraino.validation.ui.client.jenkins.resources.Parameter;
29 import org.akraino.validation.ui.client.jenkins.resources.Parameters;
30 import org.akraino.validation.ui.conf.ExecutorServiceInitializer;
31 import org.akraino.validation.ui.dao.SubmissionDAO;
32 import org.akraino.validation.ui.data.SubmissionStatus;
33 import org.akraino.validation.ui.entity.Submission;
34 import org.akraino.validation.ui.service.utils.PrioritySupplier;
35 import org.akraino.validation.ui.service.utils.SubmissionHelper;
36 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.onap.portalsdk.core.web.support.UserUtils;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
43
44 import com.fasterxml.jackson.core.JsonParseException;
45 import com.fasterxml.jackson.databind.JsonMappingException;
46
47 @Service
48 @Transactional
49 public class DbSubmissionAdapter {
50
51     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(DbSubmissionAdapter.class);
52     private static final Object LOCK = new Object();
53
54     @Autowired
55     private SubmissionDAO submissionDAO;
56
57     @Autowired
58     private SubmissionHelper submissionHelper;
59
60     @Autowired
61     private JenkinsExecutorClient jenkinsService;
62
63     @Autowired
64     private DbAdapter dbAdapter;
65
66     public Submission saveSubmission(Submission submission)
67             throws JsonParseException, JsonMappingException, IOException {
68         synchronized (LOCK) {
69             submission.setSubmissionStatus(SubmissionStatus.Submitted);
70             submissionDAO.saveOrUpdate(submission);
71             dbAdapter.associateSubmissionWithValidationResult(submission);
72             ApplicationContext context = new AnnotationConfigApplicationContext(ExecutorServiceInitializer.class);
73             ExecutorService service = (ExecutorService) context.getBean("executorService");
74             JenkinsTriggerSubmissionJob task = new JenkinsTriggerSubmissionJob(submission);
75             CompletableFuture<Submission> completableFuture = CompletableFuture
76                     .supplyAsync(new PrioritySupplier<>(1, task::execute), service);
77             completableFuture.thenAcceptAsync(result -> this.callbackNotify(result));
78             return submission;
79         }
80     }
81
82     public List<Submission> getSubmissions() {
83         synchronized (LOCK) {
84             return submissionDAO.getSubmissions();
85         }
86     }
87
88     public Submission getSubmission(String submissionId) {
89         synchronized (LOCK) {
90             return submissionDAO.getSubmission(Integer.valueOf(submissionId));
91         }
92     }
93
94     public void deleteSubmission(Integer submissionId) {
95         synchronized (LOCK) {
96             submissionDAO.deleteSubmission(submissionId);
97         }
98     }
99
100     public void deleteAll() {
101         synchronized (LOCK) {
102             submissionDAO.deleteAll();
103         }
104     }
105
106     private void callbackNotify(Submission submission) {
107         if (submission == null) {
108             return;
109         }
110         submission.setSubmissionStatus(SubmissionStatus.Running);
111         submissionHelper.saveSubmission(submission);
112     }
113
114     private class JenkinsTriggerSubmissionJob {
115
116         private Submission submission;
117
118         public JenkinsTriggerSubmissionJob(Submission submission) {
119             this.submission = submission;
120         }
121
122         public Submission execute() {
123             try (final DatagramSocket socket = new DatagramSocket()) {
124                 String jobName = System.getenv("JENKINS_JOB_NAME");
125                 List<Parameter> listOfParameters = new ArrayList<Parameter>();
126                 Parameters parameters = new Parameters();
127                 Parameter parameter = new Parameter();
128                 parameter.setName("SUBMISSION_ID");
129                 parameter.setValue(String.valueOf(submission.getSubmissionId()));
130                 listOfParameters.add(parameter);
131                 parameter = new Parameter();
132                 parameter.setName("BLUEPRINT");
133                 parameter.setValue(submission.getValidationDbTestResult().getBlueprintInstance().getBlueprint()
134                         .getBlueprintName());
135                 listOfParameters.add(parameter);
136                 parameter = new Parameter();
137                 parameter.setName("LAYER");
138                 if (submission.getValidationDbTestResult().getAllLayers()) {
139                     parameter.setValue("all");
140                 } else {
141                     parameter.setValue(submission.getValidationDbTestResult().getBlueprintInstance()
142                             .getBlueprintLayers().iterator().next().getLayer().toLowerCase());
143                 }
144                 listOfParameters.add(parameter);
145                 parameter = new Parameter();
146                 parameter.setName("VERSION");
147                 parameter.setValue(submission.getValidationDbTestResult().getBlueprintInstance().getVersion());
148                 listOfParameters.add(parameter);
149                 parameter = new Parameter();
150                 parameter.setName("LAB");
151                 parameter.setValue(submission.getTimeslot().getLabInfo().getLab());
152                 listOfParameters.add(parameter);
153                 parameter = new Parameter();
154                 parameter.setName("OPTIONAL");
155                 parameter.setValue(String.valueOf(submission.getValidationDbTestResult().getOptional().toString()));
156                 listOfParameters.add(parameter);
157                 parameter = new Parameter();
158                 parameter.setName("UI_IP");
159                 Random random = new Random();
160                 socket.connect(InetAddress.getByName(random.nextInt(256) + "." + random.nextInt(256) + "."
161                         + random.nextInt(256) + "." + random.nextInt(256)), 10002);
162                 String localIP = socket.getLocalAddress().getHostAddress();
163                 parameter.setValue(localIP);
164                 listOfParameters.add(parameter);
165                 parameters.setParameter(listOfParameters);
166                 jenkinsService.postJobWithQueryParams(jobName, parameters).toString();
167                 return submission;
168             } catch (Exception e) {
169                 LOGGER.error(EELFLoggerDelegate.errorLogger,
170                         "Error when triggering Jenkins job. " + UserUtils.getStackTrace(e));
171                 return null;
172             }
173         }
174     }
175
176 }