6985396cb7c45d862bebaa1265f1c441971c587f
[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 import javax.net.ssl.SSLException;
26
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.ReRegistrationConsumerDmaapModel;
31 import org.onap.bbs.event.processor.utilities.ReRegistrationDmaapConsumerJsonParser;
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;
39
40 import reactor.core.publisher.Flux;
41 import reactor.core.publisher.Mono;
42
43 @Component
44 public class DmaapReRegistrationConsumerTaskImpl implements DmaapReRegistrationConsumerTask,
45         ConfigurationChangeObserver {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapReRegistrationConsumerTaskImpl.class);
48     private ApplicationConfiguration configuration;
49     private final ReRegistrationDmaapConsumerJsonParser reRegistrationDmaapConsumerJsonParser;
50     private final ConsumerReactiveHttpClientFactory httpClientFactory;
51
52     private static final EmptyDmaapResponseException EMPTY_DMAAP_EXCEPTION =
53             new EmptyDmaapResponseException("PNF Re-Registration: Got an empty response from DMaaP");
54
55     private DMaaPConsumerReactiveHttpClient httpClient;
56
57     @Autowired
58     public DmaapReRegistrationConsumerTaskImpl(ApplicationConfiguration configuration) throws SSLException {
59         this(configuration, new ReRegistrationDmaapConsumerJsonParser(),
60                 new ConsumerReactiveHttpClientFactory(new DMaaPReactiveWebClientFactory()));
61     }
62
63     DmaapReRegistrationConsumerTaskImpl(ApplicationConfiguration configuration,
64                                                 ReRegistrationDmaapConsumerJsonParser reRegDmaapConsumerJsonParser,
65                                                 ConsumerReactiveHttpClientFactory httpClientFactory)
66             throws SSLException {
67         this.configuration = configuration;
68         this.reRegistrationDmaapConsumerJsonParser = reRegDmaapConsumerJsonParser;
69         this.httpClientFactory = httpClientFactory;
70
71         httpClient = httpClientFactory.create(this.configuration.getDmaapReRegistrationConsumerConfiguration());
72     }
73
74     @PostConstruct
75     void registerForConfigChanges() {
76         configuration.register(this);
77     }
78
79     @PreDestroy
80     void unRegisterForConfigChanges() {
81         configuration.unRegister(this);
82     }
83
84     @Override
85     public synchronized void updateConfiguration() {
86         try {
87             LOGGER.info("DMaaP PNF reregistration consumer update due to new application configuration");
88             httpClient = httpClientFactory.create(this.configuration.getDmaapReRegistrationConsumerConfiguration());
89         } catch (SSLException e) {
90             LOGGER.error("Error while updating HTTP Client after a config update: SSL exception");
91         }
92     }
93
94     @Override
95     public Flux<ReRegistrationConsumerDmaapModel> execute(String taskName) {
96         LOGGER.debug("Executing task for Re-Registration with name \"{}\"", taskName);
97         DMaaPConsumerReactiveHttpClient httpClient = getHttpClient();
98         Mono<String> response = httpClient.getDMaaPConsumerResponse();
99         return reRegistrationDmaapConsumerJsonParser.extractModelFromDmaap(response)
100                 .switchIfEmpty(Flux.error(EMPTY_DMAAP_EXCEPTION))
101                 .doOnError(e -> {
102                     if (!(e instanceof EmptyDmaapResponseException)) {
103                         LOGGER.error("DMaaP Consumption Exception: {}", e.getMessage());
104                     }
105                 });
106     }
107
108     private synchronized DMaaPConsumerReactiveHttpClient getHttpClient() {
109         return httpClient;
110     }
111 }