Merge "Refactor conformance robot test"
[validation.git] / ui / src / main / java / org / akraino / validation / ui / service / JenkinsJobNotificationService.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.util.ArrayList;
19 import java.util.List;
20
21 import org.akraino.validation.ui.dao.ValidationTestResultDAO;
22 import org.akraino.validation.ui.data.JnksJobNotify;
23 import org.akraino.validation.ui.data.SubmissionStatus;
24 import org.akraino.validation.ui.entity.LabInfo;
25 import org.akraino.validation.ui.entity.Submission;
26 import org.akraino.validation.ui.entity.ValidationDbTestResult;
27 import org.akraino.validation.ui.service.utils.SubmissionHelper;
28 import org.apache.commons.httpclient.HttpException;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.portalsdk.core.web.support.UserUtils;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33 import org.springframework.transaction.annotation.Transactional;
34
35 @Service
36 @Transactional
37 public class JenkinsJobNotificationService {
38
39     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(JenkinsJobNotificationService.class);
40
41     @Autowired
42     private SubmissionHelper submissionHelper;
43
44     @Autowired
45     private DbSubmissionAdapter submissionService;
46
47     @Autowired
48     private DbAdapter dbAdapter;
49
50     @Autowired
51     private IntegratedResultService iService;
52
53     @Autowired
54     private ValidationTestResultDAO vTestResultDAO;
55
56     public void handle(JnksJobNotify jnksJobNotify) throws Exception {
57         String jenkinsJobName = System.getenv("JENKINS_JOB_NAME");
58         if (!jenkinsJobName.equals(jnksJobNotify.getName())) {
59             return;
60         }
61         Submission submission = submissionService.getSubmission(Integer.toString(jnksJobNotify.getSubmissionId()));
62         if (submission == null) {
63             LOGGER.debug(EELFLoggerDelegate.debugLogger, "No related submission was found");
64             return;
65         }
66         LabInfo labInfo = dbAdapter.getLab(submission.getTimeslot().getLabInfo().getLab());
67         if (labInfo == null) {
68             throw new IllegalArgumentException(
69                     "Could not retrieve lab : " + submission.getTimeslot().getLabInfo().getLab().toString());
70         }
71         LOGGER.info(EELFLoggerDelegate.applicationLogger,
72                 "Updating submission with id: " + submission.getSubmissionId());
73         submission.setSubmissionStatus(SubmissionStatus.Completed);
74         submissionHelper.saveSubmission(submission);
75         ValidationDbTestResult vDbResult = vTestResultDAO.getValidationTestResult(submission);
76         try {
77             if (vDbResult != null) {
78                 // Fetch submission result from nexus
79                 ValidationDbTestResult vNexusResult = iService.getResult(
80                         vDbResult.getBlueprintInstance().getBlueprint().getBlueprintName(),
81                         vDbResult.getBlueprintInstance().getVersion(), vDbResult.getLab().getLab(),
82                         jnksJobNotify.getTimestamp());
83                 if (vNexusResult != null) {
84                     List<ValidationDbTestResult> vNexusResults = new ArrayList<ValidationDbTestResult>();
85                     vNexusResults.add(vNexusResult);
86                     dbAdapter.storeResultsInDb(vNexusResults);
87                 }
88             }
89         } catch (HttpException ex) {
90             LOGGER.error(EELFLoggerDelegate.errorLogger,
91                     "Error when retrieving Nexus results. " + UserUtils.getStackTrace(ex));
92         }
93         dbAdapter.updateTimestamp(jnksJobNotify);
94     }
95
96 }