Merge "Restructure test cases and upgraded test environment"
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / tasks / ProducerSupervision.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment.tasks;
22
23 import org.oransc.enrichment.clients.AsyncRestClient;
24 import org.oransc.enrichment.configuration.ApplicationConfig;
25 import org.oransc.enrichment.repository.EiJobs;
26 import org.oransc.enrichment.repository.EiProducer;
27 import org.oransc.enrichment.repository.EiProducers;
28 import org.oransc.enrichment.repository.EiTypes;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.scheduling.annotation.EnableScheduling;
33 import org.springframework.scheduling.annotation.Scheduled;
34 import org.springframework.stereotype.Component;
35
36 import reactor.core.publisher.Flux;
37 import reactor.core.publisher.Mono;
38
39 /**
40  * Regularly checks the availability of the EI Producers
41  */
42 @Component
43 @EnableScheduling
44 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
45 public class ProducerSupervision {
46     private static final Logger logger = LoggerFactory.getLogger(ProducerSupervision.class);
47
48     @Autowired
49     ApplicationConfig applicationConfig;
50
51     @Autowired
52     EiProducers eiProducers;
53
54     @Autowired
55     EiJobs eiJobs;
56
57     @Autowired
58     EiTypes eiTypes;
59
60     @Scheduled(fixedRate = 1000 * 60 * 5)
61     public void checkAllProducers() {
62         logger.debug("Checking producers starting");
63         createTask().subscribe(null, null, () -> logger.debug("Checking all Producers completed"));
64     }
65
66     public Flux<EiProducer> createTask() {
67         return Flux.fromIterable(eiProducers.getAllProducers()) //
68             .flatMap(this::checkOneProducer);
69     }
70
71     private Mono<EiProducer> checkOneProducer(EiProducer producer) {
72         return restClient().get(producer.getProducerSupervisionCallbackUrl()) //
73             .onErrorResume(throwable -> {
74                 handleNonRespondingProducer(throwable, producer);
75                 return Mono.empty();
76             })//
77             .doOnNext(response -> handleRespondingProducer(response, producer))
78             .flatMap(response -> Mono.just(producer));
79     }
80
81     private void handleNonRespondingProducer(Throwable throwable, EiProducer producer) {
82         logger.warn("Unresponsive producer: {} exception: {}", producer.getId(), throwable.getMessage());
83         producer.setAliveStatus(false);
84         if (producer.isDead()) {
85             this.eiProducers.deregisterProducer(producer, this.eiTypes, this.eiJobs);
86         }
87     }
88
89     private void handleRespondingProducer(String response, EiProducer producer) {
90         logger.debug("{}", response);
91         producer.setAliveStatus(true);
92     }
93
94     private AsyncRestClient restClient() {
95         return new AsyncRestClient("", this.applicationConfig.getWebClientConfig());
96     }
97
98 }