aboutsummaryrefslogtreecommitdiffstats
path: root/prh-app-server/src/main/java
diff options
context:
space:
mode:
authorPiotr Bocheński <piotr.bochenski@nokia.com>2018-11-30 18:03:17 +0100
committerPiotr Bocheński <piotr.bochenski@nokia.com>2018-11-30 18:03:17 +0100
commit85e1d39b283ffe6c78f8d8175809f285059b92cb (patch)
treeecee952afe96a76bfdb4c9457683d3d283487094 /prh-app-server/src/main/java
parent04d00af2b844424118fe87e05c9fe80fe04ef9d2 (diff)
SDK integration: use 'cbs-client' in PRH
Change-Id: Ifd097c7d2305432f450bbcceb521cf2341f4cb6c Issue-ID: DCAEGEN2-1011 Signed-off-by: Piotr Bocheński <piotr.bochenski@nokia.com>
Diffstat (limited to 'prh-app-server/src/main/java')
-rw-r--r--prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java32
-rw-r--r--prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java21
-rw-r--r--prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java43
-rw-r--r--prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java92
-rw-r--r--prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java106
5 files changed, 25 insertions, 269 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java
index c9667ed6..c862274e 100644
--- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java
+++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java
@@ -21,36 +21,34 @@
package org.onap.dcaegen2.services.prh.configuration;
import com.google.gson.JsonObject;
-import java.util.Optional;
-import java.util.Properties;
-import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
-import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration;
-import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
-import org.onap.dcaegen2.services.prh.config.ImmutableAaiClientConfiguration;
-import org.onap.dcaegen2.services.prh.model.EnvProperties;
-import org.onap.dcaegen2.services.prh.service.PrhConfigurationProvider;
+import org.onap.dcaegen2.services.prh.config.*;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Primary;
+import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.EnableScheduling;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
+import java.util.Optional;
+import java.util.Properties;
+
/**
* @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 8/9/18
*/
@Configuration
+@ComponentScan("org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers")
@EnableConfigurationProperties
@EnableScheduling
@Primary
public class CloudConfiguration extends AppConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(CloudConfiguration.class);
- private PrhConfigurationProvider prhConfigurationProvider;
+ private CloudConfigurationClient prhConfigurationProvider;
private AaiClientConfiguration aaiClientCloudConfiguration;
private DmaapPublisherConfiguration dmaapPublisherCloudConfiguration;
@@ -61,7 +59,7 @@ public class CloudConfiguration extends AppConfig {
@Autowired
- public void setThreadPoolTaskScheduler(PrhConfigurationProvider prhConfigurationProvider) {
+ public void setThreadPoolTaskScheduler(CloudConfigurationClient prhConfigurationProvider) {
this.prhConfigurationProvider = prhConfigurationProvider;
}
@@ -72,16 +70,16 @@ public class CloudConfiguration extends AppConfig {
}
private void parsingConfigError(Throwable throwable) {
- LOGGER.warn("Error in case of processing system environment, more details below: ", throwable);
+ LOGGER.warn("Failed to process system environments", throwable);
}
private void cloudConfigError(Throwable throwable) {
- LOGGER.warn("Exception during getting configuration from CONSUL/CONFIG_BINDING_SERVICE ", throwable);
+ LOGGER.warn("Failed to gather configuration from ConfigBindingService/Consul", throwable);
}
private void parsingConfigSuccess(EnvProperties envProperties) {
- LOGGER.info("Fetching PRH configuration from ConfigBindingService/Consul");
- prhConfigurationProvider.callForPrhConfiguration(envProperties)
+ LOGGER.debug("Fetching PRH configuration from ConfigBindingService/Consul");
+ prhConfigurationProvider.callForServiceConfigurationReactive(envProperties)
.subscribe(this::parseCloudConfig, this::cloudConfigError);
}
@@ -108,4 +106,4 @@ public class CloudConfiguration extends AppConfig {
public DmaapConsumerConfiguration getDmaapConsumerConfiguration() {
return Optional.ofNullable(dmaapConsumerCloudConfiguration).orElse(super.getDmaapConsumerConfiguration());
}
-} \ No newline at end of file
+}
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java
index 114e9839..793fcc27 100644
--- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java
+++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/EnvironmentProcessor.java
@@ -20,28 +20,29 @@
package org.onap.dcaegen2.services.prh.configuration;
-import java.util.Optional;
-import java.util.Properties;
import org.onap.dcaegen2.services.prh.exceptions.EnvironmentLoaderException;
-import org.onap.dcaegen2.services.prh.model.EnvProperties;
-import org.onap.dcaegen2.services.prh.model.ImmutableEnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
+import java.util.Optional;
+import java.util.Properties;
+
/**
* @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 8/10/18
*/
-class EnvironmentProcessor {
+final class EnvironmentProcessor {
- private static final int DEFAULT_CONSUL_PORT = 8500;
private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentProcessor.class);
+ private static final int DEFAULT_CONSUL_PORT = 8500;
private EnvironmentProcessor() {
}
static Mono<EnvProperties> evaluate(Properties systemEnvironment) {
- LOGGER.info("Loading configuration from system environment variables");
+ LOGGER.debug("Loading configuration from system environment variables");
EnvProperties envProperties;
try {
envProperties = ImmutableEnvProperties.builder().consulHost(getConsulHost(systemEnvironment))
@@ -50,7 +51,7 @@ class EnvironmentProcessor {
} catch (EnvironmentLoaderException e) {
return Mono.error(e);
}
- LOGGER.info("Evaluated environment system variables {}", envProperties);
+ LOGGER.info("Evaluated system environment variables: {}", envProperties);
return Mono.just(envProperties);
}
@@ -78,9 +79,7 @@ class EnvironmentProcessor {
}
private static Integer getDefaultPortOfConsul() {
- LOGGER.warn("$CONSUL_PORT environment has not been defined");
- LOGGER.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
+ LOGGER.warn("$CONSUL_PORT environment has not been defined, using default port: {}", DEFAULT_CONSUL_PORT);
return DEFAULT_CONSUL_PORT;
}
}
-
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java
deleted file mode 100644
index c3895504..00000000
--- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/model/EnvProperties.java
+++ /dev/null
@@ -1,43 +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.model;
-
-import org.immutables.value.Value;
-
-/**
- * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 8/10/18
- */
-@Value.Immutable(prehash = true)
-public interface EnvProperties {
-
- @Value.Parameter
- String consulHost();
-
- @Value.Parameter
- Integer consulPort();
-
- @Value.Parameter
- String cbsName();
-
- @Value.Parameter
- String appName();
-
-}
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java
deleted file mode 100644
index e3a8dcf6..00000000
--- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/HttpGetClient.java
+++ /dev/null
@@ -1,92 +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.service;
-
-import com.google.gson.Gson;
-import com.google.gson.JsonSyntaxException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
-import org.springframework.web.reactive.function.client.ClientResponse;
-import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
-import org.springframework.web.reactive.function.client.WebClient;
-import reactor.core.publisher.Mono;
-
-class HttpGetClient {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(HttpGetClient.class);
-
- private final WebClient webClient;
- private final Gson gson;
-
- HttpGetClient() {
- this(WebClient.builder().filter(logRequest()).filter(logResponse()).build());
- }
-
- HttpGetClient(WebClient webClient) {
- this.webClient = webClient;
- this.gson = new Gson();
- }
-
- <T> Mono<T> callHttpGet(String url, Class<T> genericClassDeclaration) {
- return webClient
- .get()
- .uri(url)
- .retrieve()
- .onStatus(HttpStatus::is4xxClientError, response -> Mono.error(getException(response)))
- .onStatus(HttpStatus::is5xxServerError, response -> Mono.error(getException(response)))
- .bodyToMono(String.class)
- .flatMap(body -> getJsonFromRequest(body, genericClassDeclaration));
- }
-
- private RuntimeException getException(ClientResponse response) {
- return new RuntimeException(String.format("Request for cloud config failed: HTTP %d",
- response.statusCode().value()));
- }
-
- private <T> Mono<T> getJsonFromRequest(String body, Class<T> genericClassDeclaration) {
- try {
- return Mono.just(parseJson(body, genericClassDeclaration));
- } catch (JsonSyntaxException | IllegalStateException e) {
- return Mono.error(e);
- }
- }
-
- private <T> T parseJson(String body, Class<T> genericClassDeclaration) {
- return gson.fromJson(body, genericClassDeclaration);
- }
-
- private static ExchangeFilterFunction logResponse() {
- return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
- LOGGER.info("Response status {}", clientResponse.statusCode());
- return Mono.just(clientResponse);
- });
- }
-
- private static ExchangeFilterFunction logRequest() {
- return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
- LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url());
- clientRequest.headers()
- .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
- return Mono.just(clientRequest);
- });
- }
-}
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java
deleted file mode 100644
index 38b060e9..00000000
--- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java
+++ /dev/null
@@ -1,106 +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.service;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
-import org.onap.dcaegen2.services.prh.model.EnvProperties;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Service;
-import org.springframework.web.util.DefaultUriBuilderFactory;
-import reactor.core.publisher.Mono;
-
-
-/**
- * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 8/10/18
- */
-
-@Service
-public class PrhConfigurationProvider {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(PrhConfigurationProvider.class);
-
- private final HttpGetClient httpGetClient;
-
- PrhConfigurationProvider() {
- this(new HttpGetClient());
- }
-
- PrhConfigurationProvider(HttpGetClient httpGetClient) {
- this.httpGetClient = httpGetClient;
- }
-
- public Mono<JsonObject> callForPrhConfiguration(EnvProperties envProperties) {
- return callConsulForConfigBindingServiceEndpoint(envProperties)
- .flatMap(this::callConfigBindingServiceForPrhConfiguration);
- }
-
- private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) {
- LOGGER.info("Retrieving Config Binding Service endpoint from Consul");
- return httpGetClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class)
- .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName()));
-
- }
-
- private String getConsulUrl(EnvProperties envProperties) {
- return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service",
- envProperties.cbsName());
- }
-
- private Mono<JsonObject> callConfigBindingServiceForPrhConfiguration(String configBindingServiceUri) {
- LOGGER.info("Retrieving PRH configuration");
- return httpGetClient.callHttpGet(configBindingServiceUri, JsonObject.class);
- }
-
-
- private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
- return getConfigBindingObject(jsonArray)
- .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
- }
-
- private Mono<String> buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) {
- return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(),
- jsonObject.get("ServicePort").getAsInt(), "/service_component", appName));
- }
-
- private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) {
- try {
- if (jsonArray.size() > 0) {
- return Mono.just(jsonArray.get(0).getAsJsonObject());
- } else {
- throw new IllegalStateException("JSON Array was empty");
- }
- } catch (IllegalStateException e) {
- LOGGER.warn("Failed to retrieve JSON Object from array", e);
- return Mono.error(e);
- }
- }
-
- private String getUri(String host, Integer port, String... paths) {
- return new DefaultUriBuilderFactory().builder()
- .scheme("http")
- .host(host)
- .port(port)
- .path(String.join("/", paths))
- .build().toString();
- }
-}