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 javax.annotation.PostConstruct;
 
  24 import javax.annotation.PreDestroy;
 
  25 import javax.net.ssl.SSLException;
 
  27 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
 
  28 import org.onap.bbs.event.processor.config.ConfigurationChangeObserver;
 
  29 import org.onap.bbs.event.processor.exceptions.EmptyDmaapResponseException;
 
  30 import org.onap.bbs.event.processor.model.CpeAuthenticationConsumerDmaapModel;
 
  31 import org.onap.bbs.event.processor.utilities.CpeAuthenticationDmaapConsumerJsonParser;
 
  32 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.ConsumerReactiveHttpClientFactory;
 
  33 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;
 
  34 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPReactiveWebClientFactory;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  37 import org.springframework.beans.factory.annotation.Autowired;
 
  38 import org.springframework.stereotype.Component;
 
  40 import reactor.core.publisher.Flux;
 
  41 import reactor.core.publisher.Mono;
 
  44 public class DmaapCpeAuthenticationConsumerTaskImpl
 
  45         implements DmaapCpeAuthenticationConsumerTask, ConfigurationChangeObserver {
 
  47     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapCpeAuthenticationConsumerTaskImpl.class);
 
  48     private ApplicationConfiguration configuration;
 
  49     private final CpeAuthenticationDmaapConsumerJsonParser cpeAuthenticationDmaapConsumerJsonParser;
 
  50     private final ConsumerReactiveHttpClientFactory httpClientFactory;
 
  52     private static final EmptyDmaapResponseException EMPTY_DMAAP_EXCEPTION =
 
  53             new EmptyDmaapResponseException("CPE Authentication: Got an empty response from DMaaP");
 
  55     private DMaaPConsumerReactiveHttpClient httpClient;
 
  58     public DmaapCpeAuthenticationConsumerTaskImpl(ApplicationConfiguration configuration) throws SSLException {
 
  59         this(configuration, new CpeAuthenticationDmaapConsumerJsonParser(),
 
  60                 new ConsumerReactiveHttpClientFactory(new DMaaPReactiveWebClientFactory()));
 
  63     DmaapCpeAuthenticationConsumerTaskImpl(ApplicationConfiguration configuration,
 
  64                                            CpeAuthenticationDmaapConsumerJsonParser
 
  65                                                    cpeAuthenticationDmaapConsumerJsonParser,
 
  66                                            ConsumerReactiveHttpClientFactory httpClientFactory) throws SSLException {
 
  67         this.configuration = configuration;
 
  68         this.cpeAuthenticationDmaapConsumerJsonParser = cpeAuthenticationDmaapConsumerJsonParser;
 
  69         this.httpClientFactory = httpClientFactory;
 
  71         httpClient = httpClientFactory.create(this.configuration.getDmaapCpeAuthenticationConsumerConfiguration());
 
  75     void registerForConfigChanges() {
 
  76         configuration.register(this);
 
  80     void unRegisterForConfigChanges() {
 
  81         configuration.unRegister(this);
 
  85     public synchronized void updateConfiguration() {
 
  87             LOGGER.info("DMaaP CPE authentication consumer update due to new application configuration");
 
  88             httpClient = httpClientFactory.create(this.configuration.getDmaapCpeAuthenticationConsumerConfiguration());
 
  89         } catch (SSLException e) {
 
  90             LOGGER.error("SSL error while updating HTTP Client after a config update");
 
  91             LOGGER.debug("SSL exception\n", e);
 
  96     public Flux<CpeAuthenticationConsumerDmaapModel> execute(String taskName) {
 
  97         LOGGER.debug("Executing task for CPE-Authentication with name \"{}\"", taskName);
 
  98         DMaaPConsumerReactiveHttpClient httpClient = getHttpClient();
 
  99         Mono<String> response = httpClient.getDMaaPConsumerResponse();
 
 100         return cpeAuthenticationDmaapConsumerJsonParser.extractModelFromDmaap(response)
 
 101                 .switchIfEmpty(Flux.error(EMPTY_DMAAP_EXCEPTION))
 
 103                     if (!(e instanceof EmptyDmaapResponseException)) {
 
 104                         LOGGER.error("DMaaP Consumption Exception: {}", e.getMessage());
 
 105                         LOGGER.debug("Exception\n", e);
 
 110     private synchronized DMaaPConsumerReactiveHttpClient getHttpClient() {