diff options
author | grabinsk <maciej.grabinski@nokia.com> | 2019-05-09 11:41:11 +0200 |
---|---|---|
committer | grabinsk <maciej.grabinski@nokia.com> | 2019-06-03 14:35:51 +0200 |
commit | 7c8f795a243d1b68c5f1595933aa564b513a23fd (patch) | |
tree | 28f57ef8d23c82332943429ce859c580ecf7080e /prh-app-server/src/main/java | |
parent | aa0e23d09507e7f675a747dd75120ed94e8c111d (diff) |
cleanups around CbsClientConfiguration resolving
Change-Id: Id3c02de034fbf396e81dab39c4442880b346e70d
Issue-ID: DCAEGEN2-1544
Signed-off-by: grabinsk <maciej.grabinski@nokia.com>
Diffstat (limited to 'prh-app-server/src/main/java')
5 files changed, 110 insertions, 98 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java index 60955eae..3445c076 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java @@ -22,7 +22,7 @@ package org.onap.dcaegen2.services.prh; import java.util.Map; import java.util.UUID; -import org.onap.dcaegen2.services.prh.configuration.ConsulConfigFileReader; + import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationClient; import org.slf4j.MDC; import org.springframework.boot.SpringApplication; @@ -65,8 +65,4 @@ public class MainApp { return new CloudConfigurationClient(); } - @Bean - ConsulConfigFileReader getConfigFileLoader(){ - return new ConsulConfigFileReader(); - } } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigFileReader.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigFileReader.java new file mode 100644 index 00000000..f481f4ca --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigFileReader.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.configuration; + +import com.google.gson.Gson; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Mono; + +@Component +public class CbsClientConfigFileReader { + + private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigFileReader.class); + + private final Resource cbsClientConfigFile; + + public CbsClientConfigFileReader(@Value("classpath:cbs_client_config.json") Resource cbsClientConfigFile) { + this.cbsClientConfigFile = cbsClientConfigFile; + } + + public Mono<CbsClientConfiguration> readConfig() { + LOGGER.debug("Loading CBS client configuration from configuration file"); + try (InputStream inputStream = cbsClientConfigFile.getInputStream()) { + CbsClientConfiguration config = new Gson().fromJson( + new InputStreamReader(inputStream, StandardCharsets.UTF_8), ImmutableCbsClientConfiguration.class); + LOGGER.info("Evaluated variables: {}", config); + return Mono.just(config); + } catch (Exception e) { + return Mono.error(new RuntimeException("Failed to load/parse CBS client configuration file", e)); + } + } + +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigurationResolver.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigurationResolver.java new file mode 100644 index 00000000..ce4cd4ed --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigurationResolver.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.configuration; + +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Mono; + +@Component +public class CbsClientConfigurationResolver { + + private static final Logger LOGGER = LoggerFactory.getLogger(CbsClientConfigurationResolver.class); + private final CbsClientConfigFileReader cbsClientConfigFileReader; + + public CbsClientConfigurationResolver(CbsClientConfigFileReader cbsClientConfigFileReader) { + this.cbsClientConfigFileReader = cbsClientConfigFileReader; + } + + Mono<CbsClientConfiguration> resolveCbsClientConfiguration() { + return Mono.fromSupplier(CbsClientConfiguration::fromEnvironment) + .doOnError(err -> LOGGER.warn("Failed resolving CBS client configuration from system environments", err)) + .onErrorResume(err -> cbsClientConfigFileReader.readConfig()); + } + +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsConfiguration.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsConfiguration.java index e262da55..1f752733 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsConfiguration.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CbsConfiguration.java @@ -35,7 +35,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; import java.util.Optional; @@ -51,27 +50,18 @@ public class CbsConfiguration implements Config { private MessageRouterSubscribeRequest messageRouterCBSSubscribeRequest; private MessageRouterPublishRequest messageRouterCBSUpdatePublishRequest; - private final ConsulConfigFileReader consulConfigFileReader; + private final CbsClientConfigurationResolver cbsClientConfigurationResolver; - public CbsConfiguration(ConsulConfigFileReader consulConfigFileReader) { - this.consulConfigFileReader = consulConfigFileReader; + public CbsConfiguration(CbsClientConfigurationResolver cbsClientConfigurationResolver) { + this.cbsClientConfigurationResolver = cbsClientConfigurationResolver; } public void runTask() { - Flux.defer(this::resolveCbsClientConfiguration) + Flux.defer(cbsClientConfigurationResolver::resolveCbsClientConfiguration) .subscribeOn(Schedulers.parallel()) .subscribe(this::parsingConfigSuccess, this::parsingConfigError); } - private Mono<CbsClientConfiguration> resolveCbsClientConfiguration() { - try { - return Mono.just(CbsClientConfiguration.fromEnvironment()); - } catch(Exception e){ - parsingConfigError(e); - return consulConfigFileReader.evaluate(); - } - } - private void parsingConfigSuccess(CbsClientConfiguration cbsClientConfiguration) { LOGGER.debug("Fetching PRH configuration from Consul"); CbsClientFactory.createCbsClient(cbsClientConfiguration) diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/ConsulConfigFileReader.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/ConsulConfigFileReader.java deleted file mode 100644 index 401db881..00000000 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/ConsulConfigFileReader.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PNF-REGISTRATION-HANDLER - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.dcaegen2.services.prh.configuration; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.Optional; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.Resource; -import reactor.core.publisher.Mono; - -@Configuration -public class ConsulConfigFileReader { - - private static final Logger LOGGER = LoggerFactory.getLogger(ConsulConfigFileReader.class); - private static final int DEFAULT_CONSUL_PORT = 8500; - private ImmutableCbsClientConfiguration jsonCbsClientConfiguration; - - @Value("classpath:consul_config.json") - private Resource consulConfig; - - public Mono<CbsClientConfiguration> evaluate() { - initFileStreamReader(); - CbsClientConfiguration cbsClientConfiguration = ImmutableCbsClientConfiguration.builder() - .consulHost(jsonCbsClientConfiguration.consulHost()) - .consulPort(Optional.ofNullable(jsonCbsClientConfiguration.consulPort()).orElse(DEFAULT_CONSUL_PORT)) - .cbsName(jsonCbsClientConfiguration.cbsName()) - .appName(jsonCbsClientConfiguration.appName()) - .build(); - LOGGER.info("Evaluated variables: {}", cbsClientConfiguration); - return Mono.just(cbsClientConfiguration); - } - - private void initFileStreamReader() { - LOGGER.debug("Loading configuration from configuration file"); - Gson gson = new Gson(); - try (InputStream inputStream = consulConfig.getInputStream()) { - JsonElement rootElement = getJsonElement(inputStream); - if (rootElement.isJsonObject()) { - jsonCbsClientConfiguration = gson.fromJson(rootElement, ImmutableCbsClientConfiguration.class); - } - } catch (IOException e) { - LOGGER.warn("Failed to load/parse file", e); - } - } - - private JsonElement getJsonElement(InputStream inputStream) { - return new JsonParser().parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); - } -} |