283e5ef962cffe7d803d7c27a174904b6f402ae2
[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 java.util.Optional;
24
25 import javax.annotation.PostConstruct;
26 import javax.annotation.PreDestroy;
27 import javax.net.ssl.SSLException;
28
29 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
30 import org.onap.bbs.event.processor.config.ConfigurationChangeObserver;
31 import org.onap.bbs.event.processor.exceptions.DmaapException;
32 import org.onap.bbs.event.processor.model.ControlLoopPublisherDmaapModel;
33 import org.onap.bbs.event.processor.utilities.ControlLoopJsonBodyBuilder;
34 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.DMaaPPublisherReactiveHttpClient;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.DmaaPRestTemplateFactory;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.PublisherReactiveHttpClientFactory;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 import reactor.core.publisher.Mono;
44
45 @Component
46 public class DmaapPublisherTaskImpl implements DmaapPublisherTask, ConfigurationChangeObserver {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapPublisherTaskImpl.class);
49     private ApplicationConfiguration configuration;
50     private final PublisherReactiveHttpClientFactory httpClientFactory;
51
52     private DMaaPPublisherReactiveHttpClient httpClient;
53
54     @Autowired
55     DmaapPublisherTaskImpl(ApplicationConfiguration configuration) {
56         this(configuration, new PublisherReactiveHttpClientFactory(new DmaaPRestTemplateFactory(),
57                 new ControlLoopJsonBodyBuilder()));
58     }
59
60     DmaapPublisherTaskImpl(ApplicationConfiguration configuration,
61                            PublisherReactiveHttpClientFactory httpClientFactory) {
62         this.configuration = configuration;
63         this.httpClientFactory = httpClientFactory;
64
65         try {
66             httpClient = httpClientFactory.create(this.configuration.getDmaapPublisherConfiguration());
67         } catch (SSLException e) {
68             LOGGER.error("SSL error while creating HTTP Client: {}", e.getMessage());
69             LOGGER.debug("SSL exception\n", e);
70         }
71     }
72
73     @PostConstruct
74     void registerForConfigChanges() {
75         configuration.register(this);
76     }
77
78     @PreDestroy
79     void unRegisterForConfigChanges() {
80         configuration.unRegister(this);
81     }
82
83     @Override
84     public synchronized void updateConfiguration() {
85         LOGGER.info("DMaaP Publisher update due to new application configuration");
86         try {
87             LOGGER.info("Creating secure context with:\n {}", this.configuration.getDmaapPublisherConfiguration());
88             httpClient = httpClientFactory.create(this.configuration.getDmaapPublisherConfiguration());
89         } catch (SSLException e) {
90             LOGGER.error("SSL error while updating HTTP Client after a config update: {}", e.getMessage());
91             LOGGER.debug("SSL exception\n", e);
92         }
93     }
94
95     @Override
96     public Mono<HttpResponse> execute(ControlLoopPublisherDmaapModel controlLoopPublisherDmaapModel) {
97         if (controlLoopPublisherDmaapModel == null) {
98             throw new DmaapException("Cannot invoke a DMaaP Publish task with a null message");
99         }
100         LOGGER.info("Executing task for publishing control loop message");
101         LOGGER.debug("CL message \n{}", controlLoopPublisherDmaapModel);
102         DMaaPPublisherReactiveHttpClient httpClient = getHttpClient();
103         return httpClient.getDMaaPProducerResponse(controlLoopPublisherDmaapModel, Optional.empty());
104     }
105
106     private synchronized DMaaPPublisherReactiveHttpClient getHttpClient() {
107         return httpClient;
108     }
109 }