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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.collectors.datafile.tasks;
23 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
24 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
25 import org.onap.dcaegen2.collectors.datafile.model.FileReadyMessage;
26 import org.onap.dcaegen2.collectors.datafile.service.DmaapWebClient;
27 import org.onap.dcaegen2.collectors.datafile.service.JsonMessageParser;
28 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
29 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.web.reactive.function.client.WebClient;
34 import reactor.core.publisher.Flux;
35 import reactor.core.publisher.Mono;
38 * Component used to get messages from the MessageRouter.
40 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
42 public class DMaaPMessageConsumer {
43 private static final Logger logger = LoggerFactory.getLogger(DMaaPMessageConsumer.class);
45 private final JsonMessageParser jsonMessageParser;
46 private final DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient;
48 public DMaaPMessageConsumer(AppConfig datafileAppConfig) throws DatafileTaskException {
49 this.jsonMessageParser = new JsonMessageParser();
50 this.dmaaPConsumerReactiveHttpClient = createHttpClient(datafileAppConfig);
53 protected DMaaPMessageConsumer(DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient,
54 JsonMessageParser messageParser) {
55 this.dmaaPConsumerReactiveHttpClient = dmaaPConsumerReactiveHttpClient;
56 this.jsonMessageParser = messageParser;
60 * Gets the response from the MessageRouter and turns it into a stream of fileReady messages.
62 * @return a stream of fileReady messages.
64 public Flux<FileReadyMessage> getMessageRouterResponse() {
65 logger.trace("getMessageRouterResponse called");
66 return consume((dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()));
69 private Flux<FileReadyMessage> consume(Mono<String> message) {
70 logger.trace("consume called with arg {}", message);
71 return jsonMessageParser.getMessagesFromJson(message);
74 private static DMaaPConsumerReactiveHttpClient createHttpClient(AppConfig datafileAppConfig)
75 throws DatafileTaskException {
76 DmaapConsumerConfiguration config = datafileAppConfig.getDmaapConsumerConfiguration().toDmaap();
77 WebClient client = new DmaapWebClient().fromConfiguration(config).build();
78 return new DMaaPConsumerReactiveHttpClient(config, client);