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