7b227211ab3161c636ea9bab03e029513ca788cd
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 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.bbs.event.processor.tasks;
22
23 import javax.annotation.PostConstruct;
24 import javax.annotation.PreDestroy;
25
26 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
27 import org.onap.bbs.event.processor.config.ConfigurationChangeObserver;
28 import org.onap.bbs.event.processor.exceptions.DmaapException;
29 import org.onap.bbs.event.processor.model.ControlLoopPublisherDmaapModel;
30 import org.onap.bbs.event.processor.utilities.ControlLoopJsonBodyBuilder;
31 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.DMaaPPublisherReactiveHttpClient;
32 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.DmaaPRestTemplateFactory;
33 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.PublisherReactiveHttpClientFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Component;
39
40 import reactor.core.publisher.Mono;
41
42 @Component
43 public class DmaapPublisherTaskImpl implements DmaapPublisherTask, ConfigurationChangeObserver {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapPublisherTaskImpl.class);
46     private ApplicationConfiguration configuration;
47     private final PublisherReactiveHttpClientFactory httpClientFactory;
48
49     private DMaaPPublisherReactiveHttpClient httpClient;
50
51     @Autowired
52     DmaapPublisherTaskImpl(ApplicationConfiguration configuration) {
53         this(configuration, new PublisherReactiveHttpClientFactory(new DmaaPRestTemplateFactory(),
54                 new ControlLoopJsonBodyBuilder()));
55     }
56
57     DmaapPublisherTaskImpl(ApplicationConfiguration configuration,
58                            PublisherReactiveHttpClientFactory httpClientFactory) {
59         this.configuration = configuration;
60         this.httpClientFactory = httpClientFactory;
61
62         httpClient = httpClientFactory.create(this.configuration.getDmaapPublisherConfiguration());
63     }
64
65     @PostConstruct
66     void registerForConfigChanges() {
67         configuration.register(this);
68     }
69
70     @PreDestroy
71     void unRegisterForConfigChanges() {
72         configuration.unRegister(this);
73     }
74
75     @Override
76     public synchronized void updateConfiguration() {
77         LOGGER.info("DMaaP Publisher update due to new application configuration");
78         httpClient = httpClientFactory.create(this.configuration.getDmaapPublisherConfiguration());
79     }
80
81     @Override
82     public Mono<ResponseEntity<String>> execute(ControlLoopPublisherDmaapModel controlLoopPublisherDmaapModel) {
83         if (controlLoopPublisherDmaapModel == null) {
84             throw new DmaapException("Cannot invoke a DMaaP Publish task with a null message");
85         }
86         LOGGER.info("Executing task for publishing control loop message \n{}", controlLoopPublisherDmaapModel);
87         DMaaPPublisherReactiveHttpClient httpClient = getHttpClient();
88         return httpClient.getDMaaPProducerResponse(controlLoopPublisherDmaapModel);
89     }
90
91     private synchronized DMaaPPublisherReactiveHttpClient getHttpClient() {
92         return httpClient;
93     }
94 }