Fix repo url retrieval
[validation.git] / ui / src / main / java / org / akraino / validation / ui / conf / ValidationNexusTestResultsGetter.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.conf;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.CompletableFuture;
21 import java.util.concurrent.ExecutorService;
22
23 import org.akraino.validation.ui.client.nexus.resources.ValidationNexusTestResult;
24 import org.akraino.validation.ui.data.Lab;
25 import org.akraino.validation.ui.service.DbResultAdapter;
26 import org.akraino.validation.ui.service.IntegratedResultService;
27 import org.akraino.validation.ui.service.utils.PrioritySupplier;
28 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
30 import org.onap.portalsdk.core.web.support.UserUtils;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.context.ApplicationListener;
34 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
35 import org.springframework.context.event.ContextRefreshedEvent;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class ValidationNexusTestResultsGetter implements ApplicationListener<ContextRefreshedEvent> {
40
41     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate
42             .getLogger(ValidationNexusTestResultsGetter.class);
43
44     @Autowired
45     IntegratedResultService integratedService;
46
47     @Autowired
48     DbResultAdapter dbAdapter;
49
50     @Override
51     public void onApplicationEvent(final ContextRefreshedEvent event) {
52         ApplicationContext context = new AnnotationConfigApplicationContext(ExecutorServiceInitializer.class);
53         ExecutorService service = (ExecutorService) context.getBean("executorService");
54         ValidationNexusTestResultsGetterExecution task = new ValidationNexusTestResultsGetterExecution();
55         CompletableFuture<List<List<ValidationNexusTestResult>>> completableFuture = CompletableFuture
56                 .supplyAsync(new PrioritySupplier<>(1, task::execute), service);
57         completableFuture.thenAcceptAsync(results -> this.callbackNotify(results));
58     }
59
60     private void callbackNotify(List<List<ValidationNexusTestResult>> results) {
61         try {
62             for (List<ValidationNexusTestResult> result : results) {
63
64                 LOGGER.debug(EELFLoggerDelegate.debugLogger,
65                         "Validation test results retrieved from nexus with size : " + result.size());
66                 dbAdapter.deleteUnreferencedEntries(result);
67                 dbAdapter.storeResultInDb(result);
68             }
69             Thread.sleep(Integer.valueOf(PortalApiProperties.getProperty("thread_sleep")));
70         } catch (Exception e) {
71             LOGGER.error(EELFLoggerDelegate.errorLogger,
72                     "Error in callback notification. " + UserUtils.getStackTrace(e));
73         }
74         // Trigger the next retrieval of results
75         ApplicationContext context = new AnnotationConfigApplicationContext(ExecutorServiceInitializer.class);
76         ExecutorService service = (ExecutorService) context.getBean("executorService");
77         ValidationNexusTestResultsGetterExecution task = new ValidationNexusTestResultsGetterExecution();
78         CompletableFuture<List<List<ValidationNexusTestResult>>> completableFuture = CompletableFuture
79                 .supplyAsync(new PrioritySupplier<>(1, task::execute), service);
80         completableFuture.thenAcceptAsync(newResults -> this.callbackNotify(newResults));
81     }
82
83     private class ValidationNexusTestResultsGetterExecution {
84
85         public ValidationNexusTestResultsGetterExecution() {
86         }
87
88         public List<List<ValidationNexusTestResult>> execute() {
89             List<List<ValidationNexusTestResult>> results = new ArrayList<List<ValidationNexusTestResult>>();
90             try {
91                 for (Lab lab : integratedService.getLabsFromNexus()) {
92                     for (String blueprintName : integratedService.getBlueprintNamesOfLabFromNexus(lab)) {
93                         for (String version : integratedService.getBlueprintVersionsFromNexus(blueprintName, lab)) {
94                             LOGGER.debug(EELFLoggerDelegate.debugLogger,
95                                     "Trying to retrieve validation test result from nexus for: blueprint name: "
96                                             + blueprintName + ", version: " + version + " and lab: " + lab.name());
97                             results.add(integratedService.getResultsFromNexus(blueprintName, version, lab,
98                                     Integer.valueOf(PortalApiProperties.getProperty("no_last_timestamps"))));
99                         }
100                     }
101                 }
102             } catch (Exception e) {
103                 LOGGER.error(EELFLoggerDelegate.errorLogger,
104                         "Error when retrieving Nexus results. " + UserUtils.getStackTrace(e));
105             }
106             return results;
107         }
108     }
109
110 }