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 com.google.gson.JsonElement;
 
  25 import java.util.Optional;
 
  27 import javax.annotation.PostConstruct;
 
  28 import javax.annotation.PreDestroy;
 
  29 import javax.net.ssl.SSLException;
 
  31 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
 
  32 import org.onap.bbs.event.processor.config.ConfigurationChangeObserver;
 
  33 import org.onap.bbs.event.processor.exceptions.EmptyDmaapResponseException;
 
  34 import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
 
  35 import org.onap.bbs.event.processor.utilities.ReRegistrationDmaapConsumerJsonParser;
 
  36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.ConsumerReactiveHttpClientFactory;
 
  37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;
 
  38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPReactiveWebClientFactory;
 
  39 import org.slf4j.Logger;
 
  40 import org.slf4j.LoggerFactory;
 
  41 import org.springframework.beans.factory.annotation.Autowired;
 
  42 import org.springframework.stereotype.Component;
 
  44 import reactor.core.publisher.Flux;
 
  45 import reactor.core.publisher.Mono;
 
  48 public class DmaapReRegistrationConsumerTaskImpl implements DmaapReRegistrationConsumerTask,
 
  49         ConfigurationChangeObserver {
 
  51     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapReRegistrationConsumerTaskImpl.class);
 
  52     private ApplicationConfiguration configuration;
 
  53     private final ReRegistrationDmaapConsumerJsonParser reRegistrationDmaapConsumerJsonParser;
 
  54     private final ConsumerReactiveHttpClientFactory httpClientFactory;
 
  56     private static final EmptyDmaapResponseException EMPTY_DMAAP_EXCEPTION =
 
  57             new EmptyDmaapResponseException("PNF Re-Registration: Got an empty response from DMaaP");
 
  59     private DMaaPConsumerReactiveHttpClient httpClient;
 
  62     public DmaapReRegistrationConsumerTaskImpl(ApplicationConfiguration configuration) throws SSLException {
 
  63         this(configuration, new ReRegistrationDmaapConsumerJsonParser(),
 
  64                 new ConsumerReactiveHttpClientFactory(new DMaaPReactiveWebClientFactory()));
 
  67     DmaapReRegistrationConsumerTaskImpl(ApplicationConfiguration configuration,
 
  68                                                 ReRegistrationDmaapConsumerJsonParser reRegDmaapConsumerJsonParser,
 
  69                                                 ConsumerReactiveHttpClientFactory httpClientFactory)
 
  71         this.configuration = configuration;
 
  72         this.reRegistrationDmaapConsumerJsonParser = reRegDmaapConsumerJsonParser;
 
  73         this.httpClientFactory = httpClientFactory;
 
  75         httpClient = httpClientFactory.create(this.configuration.getDmaapReRegistrationConsumerConfiguration());
 
  79     void registerForConfigChanges() {
 
  80         configuration.register(this);
 
  84     void unRegisterForConfigChanges() {
 
  85         configuration.unRegister(this);
 
  89     public synchronized void updateConfiguration() {
 
  91             LOGGER.info("DMaaP PNF reregistration consumer update due to new application configuration");
 
  92             LOGGER.info("Creating secure context with:\n {}",
 
  93                     this.configuration.getDmaapReRegistrationConsumerConfiguration());
 
  94             httpClient = httpClientFactory.create(this.configuration.getDmaapReRegistrationConsumerConfiguration());
 
  95         } catch (SSLException e) {
 
  96             LOGGER.error("SSL error while updating HTTP Client after a config update");
 
  97             LOGGER.debug("SSL exception\n", e);
 
 102     public Flux<ReRegistrationConsumerDmaapModel> execute(String taskName) {
 
 103         LOGGER.debug("Executing task for Re-Registration with name \"{}\"", taskName);
 
 104         DMaaPConsumerReactiveHttpClient httpClient = getHttpClient();
 
 105         Mono<JsonElement> response = httpClient.getDMaaPConsumerResponse(Optional.empty());
 
 106         return reRegistrationDmaapConsumerJsonParser.extractModelFromDmaap(response)
 
 107                 .switchIfEmpty(Flux.error(EMPTY_DMAAP_EXCEPTION))
 
 109                     if (!(e instanceof EmptyDmaapResponseException)) {
 
 110                         LOGGER.error("DMaaP Consumption Exception: {}", e.getMessage());
 
 111                         LOGGER.debug("Exception\n", e);
 
 116     private synchronized DMaaPConsumerReactiveHttpClient getHttpClient() {