fe7ec8b7da7388897df1416c84b65652bb80c8f7
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / tasks / DmaapTopicConsumer.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.oran.dmaapadapter.tasks;
22
23 import java.time.Duration;
24
25 import org.oran.dmaapadapter.clients.AsyncRestClient;
26 import org.oran.dmaapadapter.clients.AsyncRestClientFactory;
27 import org.oran.dmaapadapter.configuration.ApplicationConfig;
28 import org.oran.dmaapadapter.repository.InfoType;
29 import org.oran.dmaapadapter.repository.Jobs;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.http.MediaType;
33
34 import reactor.core.publisher.Flux;
35 import reactor.core.publisher.Mono;
36
37 /**
38  * The class fetches incoming requests from DMAAP and sends them further to the
39  * consumers that has a job for this InformationType.
40  */
41 public class DmaapTopicConsumer {
42     private static final Duration TIME_BETWEEN_DMAAP_RETRIES = Duration.ofSeconds(10);
43     private static final Logger logger = LoggerFactory.getLogger(DmaapTopicConsumer.class);
44
45     private final AsyncRestClient dmaapRestClient;
46     protected final ApplicationConfig applicationConfig;
47     protected final InfoType type;
48     protected final Jobs jobs;
49
50     public DmaapTopicConsumer(ApplicationConfig applicationConfig, InfoType type, Jobs jobs) {
51         AsyncRestClientFactory restclientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
52         this.dmaapRestClient = restclientFactory.createRestClientNoHttpProxy("");
53         this.applicationConfig = applicationConfig;
54         this.type = type;
55         this.jobs = jobs;
56     }
57
58     public void start() {
59         Flux.range(0, Integer.MAX_VALUE) //
60                 .flatMap(notUsed -> getFromMessageRouter(getDmaapUrl()), 1) //
61                 .flatMap(this::pushDataToConsumers) //
62                 .subscribe(//
63                         null, //
64                         throwable -> logger.error("DmaapMessageConsumer error: {}", throwable.getMessage()), //
65                         this::onComplete); //
66     }
67
68     private void onComplete() {
69         logger.warn("DmaapMessageConsumer completed {}", type.getId());
70         start();
71     }
72
73     private String getDmaapUrl() {
74         return this.applicationConfig.getDmaapBaseUrl() + type.getDmaapTopicUrl();
75     }
76
77     private Mono<String> handleDmaapErrorResponse(Throwable t) {
78         logger.debug("error from DMAAP {} {}", t.getMessage(), type.getDmaapTopicUrl());
79         return Mono.delay(TIME_BETWEEN_DMAAP_RETRIES) //
80                 .flatMap(notUsed -> Mono.empty());
81     }
82
83     private Mono<String> getFromMessageRouter(String topicUrl) {
84         logger.trace("getFromMessageRouter {}", topicUrl);
85         return dmaapRestClient.get(topicUrl) //
86                 .filter(body -> body.length() > 3) // DMAAP will return "[]" sometimes. That is thrown away.
87                 .doOnNext(message -> logger.debug("Message from DMAAP topic: {} : {}", topicUrl, message)) //
88                 .onErrorResume(this::handleDmaapErrorResponse); //
89     }
90
91     private Mono<String> handleConsumerErrorResponse(Throwable t) {
92         logger.warn("error from CONSUMER {}", t.getMessage());
93         return Mono.empty();
94     }
95
96     protected Flux<String> pushDataToConsumers(String body) {
97         logger.debug("Received data {}", body);
98         final int CONCURRENCY = 50;
99
100         // Distibute the body to all jobs for this type
101         return Flux.fromIterable(this.jobs.getJobsForType(this.type)) //
102                 .filter(job -> job.isFilterMatch(body)) //
103                 .doOnNext(job -> logger.debug("Sending to consumer {}", job.getCallbackUrl())) //
104                 .flatMap(job -> job.getConsumerRestClient().post("", body, MediaType.APPLICATION_JSON), CONCURRENCY) //
105                 .onErrorResume(this::handleConsumerErrorResponse);
106     }
107 }