diff options
Diffstat (limited to 'prh-app-server/src')
9 files changed, 213 insertions, 103 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)); - } -} diff --git a/prh-app-server/src/main/resources/cbs_client_config.json b/prh-app-server/src/main/resources/cbs_client_config.json new file mode 100644 index 00000000..0f196a1e --- /dev/null +++ b/prh-app-server/src/main/resources/cbs_client_config.json @@ -0,0 +1,5 @@ +{ + "hostname": "cbs", + "port": 10000, + "appName": "dcae-prh" +}
\ No newline at end of file diff --git a/prh-app-server/src/main/resources/consul_config.json b/prh-app-server/src/main/resources/consul_config.json deleted file mode 100644 index 674e90ad..00000000 --- a/prh-app-server/src/main/resources/consul_config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "consulHost": "consul", - "cbsName": "cbs", - "appName": "dcae-prh" -}
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigFileReaderTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigFileReaderTest.java new file mode 100644 index 00000000..c4cf7089 --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigFileReaderTest.java @@ -0,0 +1,46 @@ +/* + * ============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 org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import org.springframework.core.io.ClassRelativeResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; + +import static org.junit.jupiter.api.Assertions.*; + +class CbsClientConfigFileReaderTest { + + private ResourceLoader resourceLoader = new ClassRelativeResourceLoader(ConsulConfigurationParserTest.class); + + @Test + void shouldProvideDefaultCbsClientConfigurationLoadedFromTheFile() { + Resource configFile = resourceLoader.getResource("classpath:cbs_client_config.json"); + + CbsClientConfiguration configuration = new CbsClientConfigFileReader(configFile).readConfig().block(); + + assertEquals("dcae-prh", configuration.appName()); + assertEquals("cbs", configuration.hostname()); + assertEquals(Integer.valueOf(10000), configuration.port()); + } +}
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigurationResolverTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigurationResolverTest.java new file mode 100644 index 00000000..11981b50 --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/CbsClientConfigurationResolverTest.java @@ -0,0 +1,52 @@ +/* + * ============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.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; +import reactor.core.publisher.Mono; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class CbsClientConfigurationResolverTest { + + @Mock + private CbsClientConfigFileReader cbsClientConfigFileReader; + @Mock + private CbsClientConfiguration configurationFromFile; + + @Test + @DisabledIfEnvironmentVariable(named = "CONSUL_HOST", matches = ".+") + void whenCbsEnvPropertiesAreNotePresentInEnvironment_ShouldFallbackToLoadingDefaults() { + when(cbsClientConfigFileReader.readConfig()).thenReturn(Mono.just(configurationFromFile)); + CbsClientConfigurationResolver cbsClientConfigurationResolver = new CbsClientConfigurationResolver(cbsClientConfigFileReader); + + CbsClientConfiguration config = cbsClientConfigurationResolver.resolveCbsClientConfiguration().block(); + + assertSame(configurationFromFile, config); + } +}
\ No newline at end of file |