f239a4876001eb05b7299bc261a77d78fc4b3b4d
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / tasks / ProducerSupervision.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.clients.AsyncRestClientFactory;
25 import org.oransc.enrichment.configuration.ApplicationConfig;
26 import org.oransc.enrichment.repository.EiJobs;
27 import org.oransc.enrichment.repository.EiProducer;
28 import org.oransc.enrichment.repository.EiProducers;
29 import org.oransc.enrichment.repository.EiTypes;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.scheduling.annotation.EnableScheduling;
34 import org.springframework.scheduling.annotation.Scheduled;
35 import org.springframework.stereotype.Component;
36
37 import reactor.core.publisher.Flux;
38 import reactor.core.publisher.Mono;
39
40 /**
41  * Regularly checks the availability of the EI Producers
42  */
43 @Component
44 @EnableScheduling
45 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
46 public class ProducerSupervision {
47     private static final Logger logger = LoggerFactory.getLogger(ProducerSupervision.class);
48
49     private final EiProducers eiProducers;
50     private final EiJobs eiJobs;
51     private final EiTypes eiTypes;
52     private final AsyncRestClient restClient;
53
54     @Autowired
55     public ProducerSupervision(ApplicationConfig applicationConfig, EiProducers eiProducers, EiJobs eiJobs,
56         EiTypes eiTypes) {
57         AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
58         this.restClient = restClientFactory.createRestClient("");
59         this.eiJobs = eiJobs;
60         this.eiProducers = eiProducers;
61         this.eiTypes = eiTypes;
62     }
63
64     @Scheduled(fixedRate = 1000 * 60 * 5)
65     public void checkAllProducers() {
66         logger.debug("Checking producers starting");
67         createTask().subscribe(null, null, () -> logger.debug("Checking all Producers completed"));
68     }
69
70     public Flux<EiProducer> createTask() {
71         return Flux.fromIterable(eiProducers.getAllProducers()) //
72             .flatMap(this::checkOneProducer);
73     }
74
75     private Mono<EiProducer> checkOneProducer(EiProducer producer) {
76         return restClient.get(producer.getProducerSupervisionCallbackUrl()) //
77             .onErrorResume(throwable -> {
78                 handleNonRespondingProducer(throwable, producer);
79                 return Mono.empty();
80             })//
81             .doOnNext(response -> handleRespondingProducer(response, producer))
82             .flatMap(response -> Mono.just(producer));
83     }
84
85     private void handleNonRespondingProducer(Throwable throwable, EiProducer producer) {
86         logger.warn("Unresponsive producer: {} exception: {}", producer.getId(), throwable.getMessage());
87         producer.setAliveStatus(false);
88         if (producer.isDead()) {
89             this.eiProducers.deregisterProducer(producer, this.eiTypes, this.eiJobs);
90         }
91     }
92
93     private void handleRespondingProducer(String response, EiProducer producer) {
94         logger.debug("{}", response);
95         producer.setAliveStatus(true);
96     }
97
98 }