4d435a4fa0c1e5d397e5e84a34056a2c334abd3f
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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.DmaapPublisherConfiguration;
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.exceptions.DmaapNotFoundException;
27 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
28 import org.onap.dcaegen2.collectors.datafile.service.producer.DMaaPProducerReactiveHttpClient;
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 4/13/18
37  */
38 @Component
39 public class DmaapPublisherTaskImpl extends DmaapPublisherTask {
40
41     private final Logger logger = LoggerFactory.getLogger(this.getClass());
42     private final Config datafileAppConfig;
43     private DMaaPProducerReactiveHttpClient dmaapProducerReactiveHttpClient;
44
45     @Autowired
46     public DmaapPublisherTaskImpl(AppConfig datafileAppConfig) {
47         this.datafileAppConfig = datafileAppConfig;
48     }
49
50     @Override
51     Mono<String> publish(Mono<ConsumerDmaapModel> consumerDmaapModel) {
52         logger.info("Publishing on DMaaP topic {} object {}", resolveConfiguration().dmaapTopicName(),
53             consumerDmaapModel);
54         return dmaapProducerReactiveHttpClient.getDMaaPProducerResponse(consumerDmaapModel);
55     }
56
57     @Override
58     public Mono<String> execute(Mono<ConsumerDmaapModel> consumerDmaapModel) throws DmaapNotFoundException {
59         if (consumerDmaapModel == null) {
60             throw new DmaapNotFoundException("Invoked null object to DMaaP task");
61         }
62         dmaapProducerReactiveHttpClient = resolveClient();
63         logger.trace("Method called with arg {}", consumerDmaapModel);
64         return publish(consumerDmaapModel);
65     }
66
67     @Override
68     protected DmaapPublisherConfiguration resolveConfiguration() {
69         return datafileAppConfig.getDmaapPublisherConfiguration();
70     }
71
72     @Override
73     DMaaPProducerReactiveHttpClient resolveClient() {
74         return dmaapProducerReactiveHttpClient == null
75             ? new DMaaPProducerReactiveHttpClient(resolveConfiguration()).createDMaaPWebClient(buildWebClient())
76             : dmaapProducerReactiveHttpClient;
77     }
78 }