c41dce5b295f4c8d3e7110218d8f7adfb91fa2d2
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dcaegen2.collectors.datafile.tasks;
22
23
24 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
25 import org.onap.dcaegen2.collectors.datafile.configuration.Config;
26 import org.onap.dcaegen2.collectors.datafile.model.FileReadyMessage;
27 import org.onap.dcaegen2.collectors.datafile.service.DmaapReactiveWebClient;
28 import org.onap.dcaegen2.collectors.datafile.service.JsonMessageParser;
29 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
30 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.web.reactive.function.client.WebClient;
34
35 import reactor.core.publisher.Flux;
36 import reactor.core.publisher.Mono;
37
38 /**
39  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
40  */
41 public class DMaaPMessageConsumerTask {
42     private static final Logger logger = LoggerFactory.getLogger(DMaaPMessageConsumerTask.class);
43
44     private Config datafileAppConfig;
45     private JsonMessageParser jsonMessageParser;
46     private DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient;
47
48     public DMaaPMessageConsumerTask(AppConfig datafileAppConfig) {
49         this.datafileAppConfig = datafileAppConfig;
50         this.jsonMessageParser = new JsonMessageParser();
51     }
52
53     protected DMaaPMessageConsumerTask(AppConfig datafileAppConfig,
54                                     DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient,
55             JsonMessageParser messageParser) {
56         this.datafileAppConfig = datafileAppConfig;
57         this.dmaaPConsumerReactiveHttpClient = dmaaPConsumerReactiveHttpClient;
58         this.jsonMessageParser = messageParser;
59     }
60
61     public Flux<FileReadyMessage> execute() {
62         dmaaPConsumerReactiveHttpClient = resolveClient();
63         logger.trace("execute called");
64         return consume((dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()));
65     }
66
67     private Flux<FileReadyMessage> consume(Mono<String> message) {
68         logger.trace("consume called with arg {}", message);
69         return jsonMessageParser.getMessagesFromJson(message);
70     }
71
72     protected DmaapConsumerConfiguration resolveConfiguration() {
73         return datafileAppConfig.getDmaapConsumerConfiguration();
74     }
75
76     protected DMaaPConsumerReactiveHttpClient resolveClient() {
77         return new DMaaPConsumerReactiveHttpClient(resolveConfiguration(), buildWebClient());
78     }
79
80     protected WebClient buildWebClient() {
81         return new DmaapReactiveWebClient().fromConfiguration(resolveConfiguration()).build();
82     }
83 }