[RECV-94] Separate docker/robot invoking
[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 org.akraino.validation.ui.conf.UiUtils;
19 import org.akraino.validation.ui.data.BlueprintLayer;
20 import org.akraino.validation.ui.data.JnksJobNotify;
21 import org.akraino.validation.ui.data.SubmissionStatus;
22 import org.akraino.validation.ui.entity.LabSilo;
23 import org.akraino.validation.ui.entity.Submission;
24 import org.akraino.validation.ui.service.utils.SubmissionHelper;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
28 import org.springframework.transaction.annotation.Transactional;
29
30 @Service
31 @Transactional
32 public class JenkinsJobNotificationService {
33
34     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(JenkinsJobNotificationService.class);
35
36     @Autowired
37     private SubmissionHelper submissionHelper;
38
39     @Autowired
40     private SubmissionService submissionService;
41
42     @Autowired
43     private SiloService siloService;
44
45     public void handle(JnksJobNotify jnksJobNotify) throws Exception {
46         String jenkinsJobName = System.getenv("JENKINS_JOB_NAME");
47         if (!jenkinsJobName.equals(jnksJobNotify.getName())) {
48             return;
49         }
50         Submission submission = submissionService.getSubmission(Integer.toString(jnksJobNotify.getSubmissionId()));
51         if (submission == null) {
52             LOGGER.debug(EELFLoggerDelegate.debugLogger, "No related submission was found");
53             return;
54         }
55         String siloText = null;
56         for (LabSilo silo : siloService.getSilos()) {
57             if (silo.getLab().getLab().equals(submission.getTimeslot().getLab().getLab())) {
58                 siloText = silo.getSilo();
59             }
60         }
61         if (siloText == null) {
62             throw new Exception("Could not retrieve silo of the selected lab : "
63                     + submission.getTimeslot().getLab().getLab().toString());
64         }
65
66         String nexusUrl = UiUtils.NEXUS_URL + "/" + siloText + "/job/" + System.getenv("JENKINS_JOB_NAME") + "/"
67                 + String.valueOf(jnksJobNotify.getbuildNumber() + "/results");
68         if (!submission.getBlueprintInstanceForValidation().getLayer().equals(BlueprintLayer.All)) {
69             nexusUrl = nexusUrl + "/" + submission.getBlueprintInstanceForValidation().getLayer().name().toLowerCase();
70         }
71         submission.setNexusResultUrl(nexusUrl);
72         LOGGER.info(EELFLoggerDelegate.applicationLogger,
73                 "Updating submission with id: " + submission.getSubmissionId());
74         submission.setSubmissionStatus(SubmissionStatus.Completed);
75         submissionHelper.saveSubmission(submission);
76     }
77
78 }