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