From ec06ba416837d760e0d17de953f956ab87cd4248 Mon Sep 17 00:00:00 2001 From: wasala Date: Fri, 16 Nov 2018 15:08:25 +0100 Subject: Added cbs API * one main cloudConfigurationProvider * Mixing string and object property passed to API * added maven-shade plugin Change-Id: Idcd66fd70c20c859b5b6f94524f3fdb709fdfc57 Issue-ID: DCAEGEN2-982 Signed-off-by: wasala --- .../sdk/rest/services/cbs/client/DummyClient.java | 35 ------ .../client/http/configuration/CloudHttpClient.java | 97 ++++++++++++++++ .../client/http/configuration/EnvProperties.java | 48 ++++++++ .../client/providers/CloudConfigurationClient.java | 116 ++++++++++++++++++++ .../providers/CloudConfigurationProvider.java | 90 +++++++++++++++ .../ReactiveCloudConfigurationProvider.java | 122 +++++++++++++++++++++ 6 files changed, 473 insertions(+), 35 deletions(-) delete mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java create mode 100644 rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java (limited to 'rest-services/cbs-client/src/main/java') diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java deleted file mode 100644 index 9a80eeda..00000000 --- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * DCAEGEN2-SERVICES-SDK - * ================================================================================ - * 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.sdk.rest.services.cbs.client; - -/** - * @author Przemysław Wąsala on 11/14/18 - * @version 1.0.0 - * - * Sample Class empty class / checking that java documentation will be created by maven - */ -public class DummyClient { - - public void print() { - - } - -} diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java new file mode 100644 index 00000000..f3177e4c --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java @@ -0,0 +1,97 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.cbs.client.http.configuration; + +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; + +/** + * @author Przemysław Wąsala on 11/15/18 + */ + +public class CloudHttpClient { + + private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class); + + private final Gson gson; + private final WebClient webClient; + + public CloudHttpClient() { + this(WebClient.builder().filter(logRequest()).filter(logResponse()).build()); + } + + CloudHttpClient(WebClient webClient) { + this.gson = new Gson(); + this.webClient = webClient; + } + + public Mono callHttpGet(String url, Class 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 Mono getJsonFromRequest(String body, Class genericClassDeclaration) { + try { + return Mono.just(parseJson(body, genericClassDeclaration)); + } catch (JsonSyntaxException | IllegalStateException e) { + return Mono.error(e); + } + } + + private T parseJson(String body, Class 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/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java new file mode 100644 index 00000000..2438a2b8 --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.cbs.client.http.configuration; + +import org.immutables.value.Value; + +/** + * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in + * CloudConfigurationClient.class + * + * @author Przemysław Wąsala on 11/16/18 + * @version 1.0.0 can be passed to ReactiveCloudConfigurationProvider, can be constructed out of + * org.onap.dcaegen2.services.sdk library. + * @since 1.0.0 + */ +@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/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 new file mode 100644 index 00000000..3fb8da60 --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java @@ -0,0 +1,116 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.cbs.client.providers; + +import com.google.gson.JsonObject; +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.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +/** + * Complete CloudConfiguration HTTPClient API. + * + * @author Przemysław Wąsala on 11/16/18 + * @version 1.0.0 + * @since 1.0.0 + */ +@Service +public final class CloudConfigurationClient implements CloudConfigurationProvider { + + private final CloudConfigurationProvider cloudConfigurationProvider; + + /** + * Default constructor for CloudConfigurationClient, set CloudConfigurationProvider cloudConfigurationProvider + * property by calling: {@link ReactiveCloudConfigurationProvider}. + * Calls other constructor in this class {@link #CloudConfigurationClient(CloudConfigurationProvider)}. + */ + public CloudConfigurationClient() { + this(new ReactiveCloudConfigurationProvider()); + } + + /** + * Constructor for CloudConfigurationClient, set loudConfigurationProvider cloudConfigurationProvider property + * by passing them in constructor {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider} + * implementation client. + * + * @param cloudConfigurationProvider - client provider for calling ConfigBindingService + */ + public CloudConfigurationClient( + CloudConfigurationProvider cloudConfigurationProvider) { + this.cloudConfigurationProvider = cloudConfigurationProvider; + } + + /** + * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider} + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + */ + @Override + public Mono callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, + String appName) { + return cloudConfigurationProvider.callForServiceConfigurationReactive( + ImmutableEnvProperties.builder().consulHost(consulHost) + .consulPort(consulPort).cbsName(cbsName) + .appName(appName).build()); + } + + /** + * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. + * + * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * been defined in dcaegen2 cloud environment. + */ + @Override + public Mono callForServiceConfigurationReactive(EnvProperties envProperties) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties); + } + + /** + * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + */ + @Override + public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) { + return cloudConfigurationProvider.callForServiceConfigurationReactive( + ImmutableEnvProperties.builder().consulHost(consulHost) + .consulPort(consulPort).cbsName(cbsName) + .appName(appName).build()).block(); + } + + /** + * Documentation in {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers.CloudConfigurationProvider}. + * + * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + */ + @Override + public JsonObject callForServiceConfiguration(EnvProperties envProperties) { + return cloudConfigurationProvider.callForServiceConfigurationReactive(envProperties).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 new file mode 100644 index 00000000..f8486f6f --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java @@ -0,0 +1,90 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.cbs.client.providers; + +import com.google.gson.JsonObject; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import reactor.core.publisher.Mono; + +/** + * Rest CloudConfigurationClient interface which will define all necessary methods in connection with downloading JSON + * for each component in DCAEGEN2 repository. + * + * This interface holds contract for user-defined client or our defined implementation in CBS-client module. + * + * @author Przemysław Wąsala on 11/16/18 + * @version 1.0.0 has got only one implementation of this interface: 1. ReactiveCloudConfigurationProvider.class + * @since 1.0.0 + */ +public interface CloudConfigurationProvider { + + /* callForServiceConfigurationReactive */ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param envProperties - 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 callForServiceConfigurationReactive(EnvProperties envProperties); + + /* callForServiceConfigurationReactive */ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + * @return rective configuration for specified application in dcaegen2 cloud infrastructure. + */ + Mono callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, + String appName); + + + /*callForServiceConfiguration*/ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param consulHost - Hostname/IPAddress of consul Database + * @param consulPort - Port number of consul Database + * @param cbsName - ConfigBindingService url + * @param appName - ApplicationName for each config will be returned + * @return configuration for specified application in dcaegen2 cloud infrastructure. + */ + JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName); + + /*callForServiceConfiguration*/ + + /** + * Getting configuration for appName from ConfigBindingService. + * + * @param envProperties - Object holds consulPort, consulURL, configBindingSeriveName, applicationName which have + * @return configuration for specified application in dcaegen2 cloud infrastructure. + */ + JsonObject callForServiceConfiguration(EnvProperties envProperties); + +} 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 new file mode 100644 index 00000000..88072985 --- /dev/null +++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java @@ -0,0 +1,122 @@ +/* + * ============LICENSE_START======================================================= + * DCAEGEN2-SERVICES-SDK + * ================================================================================ + * 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.sdk.rest.services.cbs.client.providers; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.CloudHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.util.DefaultUriBuilderFactory; +import reactor.core.publisher.Mono; + +/** + * @author Przemysław Wąsala on 11/15/18 + */ +final class ReactiveCloudConfigurationProvider implements CloudConfigurationProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveCloudConfigurationProvider.class); + private static final String EXCEPTION_MESSAGE = "Unsupported method call: "; + + private final CloudHttpClient cloudHttpClient; + + public ReactiveCloudConfigurationProvider() { + this(new CloudHttpClient()); + } + + ReactiveCloudConfigurationProvider( + CloudHttpClient cloudHttpClient) { + this.cloudHttpClient = cloudHttpClient; + } + + @Override + public Mono callForServiceConfigurationReactive(EnvProperties envProperties) { + return callConsulForConfigBindingServiceEndpoint(envProperties) + .flatMap(this::callConfigBindingServiceForPrhConfiguration); + } + + @Override + public Mono callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName, + String appName) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); + } + + @Override + public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); + } + + @Override + public JsonObject callForServiceConfiguration(EnvProperties envProperties) { + throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this); + } + + private Mono callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { + LOGGER.info("Retrieving Config Binding Service endpoint from Consul"); + return cloudHttpClient.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 callConfigBindingServiceForPrhConfiguration(String configBindingServiceUri) { + LOGGER.info("Retrieving PRH configuration"); + return cloudHttpClient.callHttpGet(configBindingServiceUri, JsonObject.class); + } + + + private Mono createConfigBindingServiceUrl(JsonArray jsonArray, String appName) { + return getConfigBindingObject(jsonArray) + .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName)); + } + + private Mono buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) { + return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(), + jsonObject.get("ServicePort").getAsInt(), "/service_component", appName)); + } + + private Mono 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(); + } +} -- cgit 1.2.3-korg