da510281cb86572720d09fcd0dd9bb0b148dc21d
[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 com.google.gson.JsonElement;
24
25 import java.util.Optional;
26
27 import javax.annotation.PostConstruct;
28 import javax.annotation.PreDestroy;
29 import javax.net.ssl.SSLException;
30
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.CpeAuthenticationConsumerDmaapModel;
35 import org.onap.bbs.event.processor.utilities.CpeAuthenticationDmaapConsumerJsonParser;
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;
43
44 import reactor.core.publisher.Flux;
45 import reactor.core.publisher.Mono;
46
47 @Component
48 public class DmaapCpeAuthenticationConsumerTaskImpl
49         implements DmaapCpeAuthenticationConsumerTask, ConfigurationChangeObserver {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(DmaapCpeAuthenticationConsumerTaskImpl.class);
52     private ApplicationConfiguration configuration;
53     private final CpeAuthenticationDmaapConsumerJsonParser cpeAuthenticationDmaapConsumerJsonParser;
54     private final ConsumerReactiveHttpClientFactory httpClientFactory;
55
56     private static final EmptyDmaapResponseException EMPTY_DMAAP_EXCEPTION =
57             new EmptyDmaapResponseException("CPE Authentication: Got an empty response from DMaaP");
58
59     private DMaaPConsumerReactiveHttpClient httpClient;
60
61     @Autowired
62     public DmaapCpeAuthenticationConsumerTaskImpl(ApplicationConfiguration configuration) throws SSLException {
63         this(configuration, new CpeAuthenticationDmaapConsumerJsonParser(),
64                 new ConsumerReactiveHttpClientFactory(new DMaaPReactiveWebClientFactory()));
65     }
66
67     DmaapCpeAuthenticationConsumerTaskImpl(ApplicationConfiguration configuration,
68                                            CpeAuthenticationDmaapConsumerJsonParser
69                                                    cpeAuthenticationDmaapConsumerJsonParser,
70                                            ConsumerReactiveHttpClientFactory httpClientFactory) throws SSLException {
71         this.configuration = configuration;
72         this.cpeAuthenticationDmaapConsumerJsonParser = cpeAuthenticationDmaapConsumerJsonParser;
73         this.httpClientFactory = httpClientFactory;
74
75         httpClient = httpClientFactory.create(this.configuration.getDmaapCpeAuthenticationConsumerConfiguration());
76     }
77
78     @PostConstruct
79     void registerForConfigChanges() {
80         configuration.register(this);
81     }
82
83     @PreDestroy
84     void unRegisterForConfigChanges() {
85         configuration.unRegister(this);
86     }
87
88     @Override
89     public synchronized void updateConfiguration() {
90         try {
91             LOGGER.info("DMaaP CPE authentication consumer update due to new application configuration");
92             LOGGER.info("Creating secure context with:\n {}",
93                     this.configuration.getDmaapCpeAuthenticationConsumerConfiguration());
94             httpClient = httpClientFactory.create(this.configuration.getDmaapCpeAuthenticationConsumerConfiguration());
95         } catch (SSLException e) {
96             LOGGER.error("SSL error while updating HTTP Client after a config update");
97             LOGGER.debug("SSL exception\n", e);
98         }
99     }
100
101     @Override
102     public Flux<CpeAuthenticationConsumerDmaapModel> execute(String taskName) {
103         LOGGER.debug("Executing task for CPE-Authentication with name \"{}\"", taskName);
104         DMaaPConsumerReactiveHttpClient httpClient = getHttpClient();
105         Mono<JsonElement> response = httpClient.getDMaaPConsumerResponse(Optional.empty());
106         return cpeAuthenticationDmaapConsumerJsonParser.extractModelFromDmaap(response)
107                 .switchIfEmpty(Flux.error(EMPTY_DMAAP_EXCEPTION))
108                 .doOnError(e -> {
109                     if (!(e instanceof EmptyDmaapResponseException)) {
110                         LOGGER.error("DMaaP Consumption Exception: {}", e.getMessage());
111                         LOGGER.debug("Exception\n", e);
112                     }
113                 });
114     }
115
116     private synchronized DMaaPConsumerReactiveHttpClient getHttpClient() {
117         return httpClient;
118     }
119 }