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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.bbs.event.processor.tasks;
 
  23 import java.util.Optional;
 
  25 import javax.annotation.PostConstruct;
 
  26 import javax.annotation.PreDestroy;
 
  27 import javax.net.ssl.SSLException;
 
  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;
 
  43 import reactor.core.publisher.Mono;
 
  46 public class DmaapPublisherTaskImpl implements DmaapPublisherTask, ConfigurationChangeObserver {
 
  48     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapPublisherTaskImpl.class);
 
  49     private ApplicationConfiguration configuration;
 
  50     private final PublisherReactiveHttpClientFactory httpClientFactory;
 
  52     private DMaaPPublisherReactiveHttpClient httpClient;
 
  55     DmaapPublisherTaskImpl(ApplicationConfiguration configuration) {
 
  56         this(configuration, new PublisherReactiveHttpClientFactory(new DmaaPRestTemplateFactory(),
 
  57                 new ControlLoopJsonBodyBuilder()));
 
  60     DmaapPublisherTaskImpl(ApplicationConfiguration configuration,
 
  61                            PublisherReactiveHttpClientFactory httpClientFactory) {
 
  62         this.configuration = configuration;
 
  63         this.httpClientFactory = httpClientFactory;
 
  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);
 
  74     void registerForConfigChanges() {
 
  75         configuration.register(this);
 
  79     void unRegisterForConfigChanges() {
 
  80         configuration.unRegister(this);
 
  84     public synchronized void updateConfiguration() {
 
  85         LOGGER.info("DMaaP Publisher update due to new application configuration");
 
  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);
 
  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");
 
 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());
 
 106     private synchronized DMaaPPublisherReactiveHttpClient getHttpClient() {