8d45a7fd17710e30d3ea864fd2bd25b7416de20c
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
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.onap.dcaegen2.collectors.datafile.tasks;
22
23 import org.onap.dcaegen2.collectors.datafile.config.DmaapConsumerConfiguration;
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.ConsumerDmaapModel;
27 import org.onap.dcaegen2.collectors.datafile.service.DmaapConsumerJsonParser;
28 import org.onap.dcaegen2.collectors.datafile.service.consumer.DMaaPConsumerReactiveHttpClient;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33 import reactor.core.publisher.Mono;
34
35 /**
36  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
37  */
38 @Component
39 public class DmaapConsumerTaskImpl extends DmaapConsumerTask {
40
41     private final Logger logger = LoggerFactory.getLogger(this.getClass());
42     private final Config datafileAppConfig;
43     private DmaapConsumerJsonParser dmaapConsumerJsonParser;
44     private DMaaPConsumerReactiveHttpClient dmaaPConsumerReactiveHttpClient;
45
46     @Autowired
47     public DmaapConsumerTaskImpl(AppConfig datafileAppConfig) {
48         this.datafileAppConfig = datafileAppConfig;
49         this.dmaapConsumerJsonParser = new DmaapConsumerJsonParser();
50     }
51
52     DmaapConsumerTaskImpl(AppConfig datafileAppConfig, DmaapConsumerJsonParser dmaapConsumerJsonParser) {
53         this.datafileAppConfig = datafileAppConfig;
54         this.dmaapConsumerJsonParser = dmaapConsumerJsonParser;
55     }
56
57     @Override
58     Mono<ConsumerDmaapModel> consume(Mono<String> message) {
59         logger.info("Consumed model from DMaaP: {}", message);
60         return dmaapConsumerJsonParser.getJsonObject(message);
61     }
62
63     @Override
64     public Mono<ConsumerDmaapModel> execute(String object) {
65         dmaaPConsumerReactiveHttpClient = resolveClient();
66         logger.trace("Method called with arg {}", object);
67         return consume((dmaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()));
68     }
69
70     @Override
71     void initConfigs() {
72         datafileAppConfig.initFileStreamReader();
73     }
74
75     @Override
76     protected DmaapConsumerConfiguration resolveConfiguration() {
77         return datafileAppConfig.getDmaapConsumerConfiguration();
78     }
79
80     @Override
81     DMaaPConsumerReactiveHttpClient resolveClient() {
82         return dmaaPConsumerReactiveHttpClient == null
83             ? new DMaaPConsumerReactiveHttpClient(resolveConfiguration()).createDMaaPWebClient(buildWebClient())
84             : dmaaPConsumerReactiveHttpClient;
85     }
86 }