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;
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;
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;
44 import com.fasterxml.jackson.core.JsonParseException;
45 import com.fasterxml.jackson.databind.JsonMappingException;
49 public class DbSubmissionAdapter {
51 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(DbSubmissionAdapter.class);
52 private static final Object LOCK = new Object();
55 private SubmissionDAO submissionDAO;
58 private SubmissionHelper submissionHelper;
61 private JenkinsExecutorClient jenkinsService;
64 private DbAdapter dbAdapter;
66 public Submission saveSubmission(Submission submission)
67 throws JsonParseException, JsonMappingException, IOException {
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));
82 public List<Submission> getSubmissions() {
84 return submissionDAO.getSubmissions();
88 public Submission getSubmission(String submissionId) {
90 return submissionDAO.getSubmission(Integer.valueOf(submissionId));
94 public void deleteSubmission(Integer submissionId) {
96 submissionDAO.deleteSubmission(submissionId);
100 public void deleteAll() {
101 synchronized (LOCK) {
102 submissionDAO.deleteAll();
106 private void callbackNotify(Submission submission) {
107 if (submission == null) {
110 submission.setSubmissionStatus(SubmissionStatus.Running);
111 submissionHelper.saveSubmission(submission);
114 private class JenkinsTriggerSubmissionJob {
116 private Submission submission;
118 public JenkinsTriggerSubmissionJob(Submission submission) {
119 this.submission = submission;
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");
141 parameter.setValue(submission.getValidationDbTestResult().getBlueprintInstance()
142 .getBlueprintLayers().iterator().next().getLayer().toLowerCase());
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();
168 } catch (Exception e) {
169 LOGGER.error(EELFLoggerDelegate.errorLogger,
170 "Error when triggering Jenkins job. " + UserUtils.getStackTrace(e));