diff options
author | Filip Krzywka <filip.krzywka@nokia.com> | 2019-05-28 12:28:54 +0200 |
---|---|---|
committer | Filip Krzywka <filip.krzywka@nokia.com> | 2019-05-30 12:46:02 +0200 |
commit | da54f3e4c7e825908ef332e7913e1c4eb4fa82d1 (patch) | |
tree | d6560f5ae3af50a48fc6a364e0bec19c98ae9d5a /rest-services/cbs-client/src/main/java | |
parent | 7e893708bbdc36698a5d90502b318a5fd4f3be21 (diff) |
Omit CBS lookup in Consul
This is first iteration of changes related to switching from "CBS lookup"
mechanism to direct CBS usage.
Change-Id: I910612c689e10be5d9506643c61ae3cd8ffd0dca
Issue-ID: DCAEGEN2-1521
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
Diffstat (limited to 'rest-services/cbs-client/src/main/java')
7 files changed, 93 insertions, 105 deletions
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java index 053c60c5..821805fc 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java @@ -24,7 +24,7 @@ import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsClientImpl; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.CbsLookup; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import reactor.core.publisher.Mono; /** @@ -40,24 +40,24 @@ public class CbsClientFactory { * <p>Creates Mono which will emit instance of {@link CbsClient} when service discovery is complete.</p> * * <p> - * This method will do a lookup of Config Binding Service using Consul as service discovery mechanism and create - * client configured with found address. Created client will be published in returned Mono instance. + * This method will do a lookup of Config Binding Service and create client configured with found address. + * Created client will be published in returned Mono instance. * </p> * <p> * In case of failure during CBS resolution, returned Mono will emit error signal with possible cause. * User is expected to handle this signal and possibly retry subscription to returned Mono. * </p> * - * @param env required environment properties + * @param configuration required CBS configuration as viewed by client application * @return non-null {@link Mono} of {@link CbsClient} instance * @since 1.1.2 */ - public static @NotNull Mono<CbsClient> createCbsClient(EnvProperties env) { + public static @NotNull Mono<CbsClient> createCbsClient(CbsClientConfiguration configuration) { return Mono.defer(() -> { final RxHttpClient httpClient = RxHttpClientFactory.create(); - final CbsLookup lookup = new CbsLookup(httpClient); - return lookup.lookup(env) - .map(addr -> new CbsClientImpl(httpClient, env.appName(), addr)); + final CbsLookup lookup = new CbsLookup(); + return lookup.lookup(configuration) + .map(addr -> new CbsClientImpl(httpClient, configuration.appName(), addr)); }); } } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java index 3724338d..99ad3b20 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsRequests.java @@ -22,7 +22,6 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api; import org.jetbrains.annotations.NotNull; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsRequest; import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java index 99058772..c07ed8e4 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsLookup.java @@ -20,20 +20,13 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import java.net.InetSocketAddress; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.exceptions.ServiceLookupException; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; +import java.net.InetSocketAddress; + /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> * @since February 2019 @@ -41,56 +34,16 @@ import reactor.core.publisher.Mono; public class CbsLookup { private static final Logger LOGGER = LoggerFactory.getLogger(CbsLookup.class); - private static final String CONSUL_JSON_SERVICE_ADDRESS = "ServiceAddress"; - private static final String CONSUL_JSON_SERVICE_PORT = "ServicePort"; - private final RxHttpClient httpClient; - - public CbsLookup(RxHttpClient httpClient) { - this.httpClient = httpClient; - } - public Mono<InetSocketAddress> lookup(EnvProperties env) { - return Mono.fromCallable(() -> createConsulUrl(env)) - .doOnNext(this::logConsulRequestUrl) - .flatMap(this::fetchHttpData) - .doOnNext(this::logConsulResponse) - .flatMap(this::firstService) - .map(this::parseServiceEntry) + public Mono<InetSocketAddress> lookup(CbsClientConfiguration configuration) { + return Mono.just(createCbsAddress(configuration)) .doOnNext(this::logCbsServiceAddress); } - private String createConsulUrl(EnvProperties env) { - return String.format("http://%s:%s/v1/catalog/service/%s", env.consulHost(), env.consulPort(), env.cbsName()); - } - - private void logConsulRequestUrl(String consulUrl) { - LOGGER.debug("Calling Consul for CBS address. consulUrl={}", consulUrl); - } - - private Mono<JsonArray> fetchHttpData(String consulUrl) { - return httpClient.call( - ImmutableHttpRequest.builder() - .method(HttpMethod.GET) - .url(consulUrl) - .build()) - .doOnNext(HttpResponse::throwIfUnsuccessful) - .map(resp -> resp.bodyAsJson(JsonArray.class)); - } - - private void logConsulResponse(JsonArray consulResponse) { - LOGGER.debug("Consul response with CBS service list. Will use 1st one. response={}", consulResponse); - } - - private Mono<JsonObject> firstService(JsonArray services) { - return services.size() == 0 - ? Mono.error(new ServiceLookupException("Consul server did not return any service with given name")) - : Mono.just(services.get(0).getAsJsonObject()); - } - - private InetSocketAddress parseServiceEntry(JsonObject service) { + private InetSocketAddress createCbsAddress(CbsClientConfiguration configuration) { return InetSocketAddress.createUnresolved( - service.get(CONSUL_JSON_SERVICE_ADDRESS).getAsString(), - service.get(CONSUL_JSON_SERVICE_PORT).getAsInt()); + configuration.hostname(), + configuration.port()); } private void logCbsServiceAddress(InetSocketAddress address) { diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/EnvProperties.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java index 8a40c2c0..e3c7d2ea 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/EnvProperties.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/model/CbsClientConfiguration.java @@ -21,6 +21,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model; import org.immutables.value.Value; +import org.jetbrains.annotations.Nullable; /** * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in @@ -32,47 +33,82 @@ import org.immutables.value.Value; * @since 1.0.0 */ @Value.Immutable(prehash = true) -public interface EnvProperties { +public interface CbsClientConfiguration { /** - * Name of environment variable containing Consul host name. + * Name of environment variable containing Config Binding Service network hostname. */ - String ENV_CONSUL_HOST = "CONSUL_HOST"; + String ENV_CBS_HOSTNAME = "CONFIG_BINDING_SERVICE"; /** - * Name of environment variable containing Config Binding Service <em>service name</em> as registered in Consul - * services API. + * Name of environment variable containing Config Binding Service network port. */ - String ENV_CBS_NAME = "CONFIG_BINDING_SERVICE"; + String ENV_CBS_PORT = "CONFIG_BINDING_SERVICE_SERVICE_PORT"; /** * Name of environment variable containing current application name. */ String ENV_APP_NAME = "HOSTNAME"; - @Value.Parameter - String consulHost(); + /** + * Name of environment variable containing Consul host name. + * + * @deprecated CBS lookup in Consul service should not be needed, + * instead {@link #ENV_CBS_HOSTNAME} should be used directly. + */ + @Deprecated + String ENV_CONSUL_HOST = "CONSUL_HOST"; + + /** + * Name of environment variable containing Config Binding Service <em>service name</em> as registered in Consul + * services API. + * + * @deprecated CBS lookup in Consul service should not be needed, + * instead {@link #ENV_CBS_HOSTNAME} should be used directly. + */ + @Deprecated + String ENV_CBS_NAME = "CONFIG_BINDING_SERVICE"; @Value.Parameter - Integer consulPort(); + @Nullable + String hostname(); @Value.Parameter - String cbsName(); + @Nullable + Integer port(); @Value.Parameter String appName(); + @Value.Default + @Deprecated + default String consulHost() { + return "consul-server"; + } + + @Value.Default + @Deprecated + default Integer consulPort() { + return 8500; + } + + @Value.Default + @Deprecated + default String cbsName() { + return "config-binding-service"; + } + /** - * Creates EnvProperties from system environment variables. + * Creates CbsClientConfiguration from system environment variables. * - * @return an instance of EnvProperties + * @return an instance of CbsClientConfiguration * @throws NullPointerException when at least one of required parameters is missing */ - static EnvProperties fromEnvironment() { - return ImmutableEnvProperties.builder() + static CbsClientConfiguration fromEnvironment() { + return ImmutableCbsClientConfiguration.builder() .consulHost(System.getenv(ENV_CONSUL_HOST)) - .consulPort(8500) - .cbsName(System.getenv(ENV_CBS_NAME)) + .hostname(System.getenv(ENV_CBS_HOSTNAME)) + .port(Integer.valueOf(System.getenv(ENV_CBS_PORT))) .appName(System.getenv(ENV_APP_NAME)) .build(); } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java index 534397ca..e1dd7aff 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java @@ -22,8 +22,8 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers; import com.google.gson.JsonObject; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties; +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 reactor.core.publisher.Mono; /** @@ -70,7 +70,7 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide public Mono<JsonObject> callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, String appName) { return cloudConfigurationProvider.callForServiceConfigurationReactive( - ImmutableEnvProperties.builder().consulHost(consulHost) + ImmutableCbsClientConfiguration.builder().consulHost(consulHost) .consulPort(consulPort).cbsName(cbsName) .appName(appName).build()); } @@ -78,12 +78,12 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide /** * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have * been defined in dcaegen2 cloud environment. */ @Override - public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) { - return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties); + public Mono<JsonObject> callForServiceConfigurationReactive(CbsClientConfiguration cbsClientConfiguration) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(cbsClientConfiguration); } /** @@ -97,7 +97,7 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide @Override public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) { return cloudConfigurationProvider.callForServiceConfigurationReactive( - ImmutableEnvProperties.builder().consulHost(consulHost) + ImmutableCbsClientConfiguration.builder().consulHost(consulHost) .consulPort(consulPort).cbsName(cbsName) .appName(appName).build()).block(); } @@ -105,10 +105,10 @@ public final class CloudConfigurationClient implements CloudConfigurationProvide /** * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have */ @Override - public JsonObject callForServiceConfiguration(EnvProperties envProperties) { - return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties).block(); + public JsonObject callForServiceConfiguration(CbsClientConfiguration cbsClientConfiguration) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(cbsClientConfiguration).block(); } } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java index d82d5d31..c0dbd5f9 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java @@ -22,7 +22,7 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers; import com.google.gson.JsonObject; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import reactor.core.publisher.Mono; /** @@ -42,12 +42,12 @@ public interface CloudConfigurationProvider { /** * Getting configuration for appName from ConfigBindingService. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have * been defined in dcaegen2 cloud environment. * @return Single reactive response which @Mono which holds inside them JsonObject with configuration for specified * application Name */ - Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties); + Mono<JsonObject> callForServiceConfigurationReactive(CbsClientConfiguration cbsClientConfiguration); /* callForServiceConfigurationReactive */ @@ -82,9 +82,9 @@ public interface CloudConfigurationProvider { /** * Getting configuration for appName from ConfigBindingService. * - * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @param cbsClientConfiguration - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have * @return configuration for specified application in dcaegen2 cloud infrastructure. */ - JsonObject callForServiceConfiguration(EnvProperties envProperties); + JsonObject callForServiceConfiguration(CbsClientConfiguration cbsClientConfiguration); } diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java index dbc94802..a50edd38 100644 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java @@ -28,7 +28,7 @@ import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration; import org.onap.dcaegen2.services.sdk.rest.services.uri.URI; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,8 +53,8 @@ public final class ReactiveCloudConfigurationProvider implements CloudConfigurat } @Override - public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) { - return callConsulForConfigBindingServiceEndpoint(envProperties) + public Mono<JsonObject> callForServiceConfigurationReactive(CbsClientConfiguration configuration) { + return callConsulForConfigBindingServiceEndpoint(configuration) .flatMap(this::callConfigBindingServiceForConfiguration); } @@ -70,28 +70,28 @@ public final class ReactiveCloudConfigurationProvider implements CloudConfigurat } @Override - public JsonObject callForServiceConfiguration(EnvProperties envProperties) { + public JsonObject callForServiceConfiguration(CbsClientConfiguration configuration) { throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); } - private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { + private Mono<String> callConsulForConfigBindingServiceEndpoint(CbsClientConfiguration configuration) { LOGGER.info("Retrieving Config Binding Service endpoint from Consul"); HttpRequest httpRequest = ImmutableHttpRequest.builder() - .url(getConsulUrl(envProperties)).method(HttpMethod.GET).build(); + .url(getConsulUrl(configuration)).method(HttpMethod.GET).build(); return rxHttpClient.call(httpRequest) .map(resp -> resp.bodyAsJson(JsonArray.class)) .flatMap(jsonArray -> this.createConfigBindingServiceUrl( jsonArray, - envProperties.appName()) + configuration.appName()) ); } - private String getConsulUrl(EnvProperties envProperties) { - return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service", - envProperties.cbsName()); + private String getConsulUrl(CbsClientConfiguration configuration) { + return getUri(configuration.consulHost(), configuration.consulPort(), "/v1/catalog/service", + configuration.cbsName()); } private Mono<JsonObject> callConfigBindingServiceForConfiguration(String configBindingServiceUri) { |