9d888aa75d9fd5e78f2e862f70cb2568ff486978
[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.AaiClientConfiguration;
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.AaiNotFoundException;
27 import org.onap.dcaegen2.collectors.datafile.exceptions.DmaapNotFoundException;
28 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
29 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
30 import org.onap.dcaegen2.collectors.datafile.model.utils.HttpUtils;
31 import org.onap.dcaegen2.collectors.datafile.service.producer.AaiProducerReactiveHttpClient;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36 import reactor.core.publisher.Mono;
37
38 /**
39  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18
40  */
41 @Component
42 public class AaiProducerTaskImpl extends
43     AaiProducerTask {
44
45     private final Logger logger = LoggerFactory.getLogger(this.getClass());
46
47     private final Config datafileAppConfig;
48     private AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
49
50     @Autowired
51     public AaiProducerTaskImpl(AppConfig datafileAppConfig) {
52         this.datafileAppConfig = datafileAppConfig;
53     }
54
55     @Override
56     Mono<ConsumerDmaapModel> publish(Mono<ConsumerDmaapModel> consumerDmaapModel) {
57         logger.info("Sending PNF model to AAI {}", consumerDmaapModel);
58         return aaiProducerReactiveHttpClient.getAaiProducerResponse(consumerDmaapModel)
59             .flatMap(response -> {
60                 if (HttpUtils.isSuccessfulResponseCode(response)) {
61                     return consumerDmaapModel;
62                 }
63                 return Mono
64                     .error(new AaiNotFoundException("Incorrect response code for continuation of tasks workflow"));
65             });
66     }
67
68     @Override
69     AaiProducerReactiveHttpClient resolveClient() {
70         return aaiProducerReactiveHttpClient == null ? new AaiProducerReactiveHttpClient(resolveConfiguration())
71             .createAaiWebClient(buildWebClient()) : aaiProducerReactiveHttpClient;
72     }
73
74     @Override
75     protected AaiClientConfiguration resolveConfiguration() {
76         return datafileAppConfig.getAaiClientConfiguration();
77     }
78
79     @Override
80     protected Mono<ConsumerDmaapModel> execute(Mono<ConsumerDmaapModel> consumerDmaapModel) throws DatafileTaskException {
81         if (consumerDmaapModel == null) {
82             throw new DmaapNotFoundException("Invoked null object to DMaaP task");
83         }
84         aaiProducerReactiveHttpClient = resolveClient();
85         logger.trace("Method called with arg {}", consumerDmaapModel);
86         return publish(consumerDmaapModel);
87
88     }
89 }