diff options
Diffstat (limited to 'rest-services/cbs-client/src/main')
-rw-r--r-- | rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClient.java | 97 | ||||
-rw-r--r-- | rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/EnvProperties.java (renamed from rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClient.java) | 27 | ||||
-rw-r--r-- | rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationClient.java | 116 | ||||
-rw-r--r-- | rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfigurationProvider.java | 90 | ||||
-rw-r--r-- | rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProvider.java | 122 | ||||
-rw-r--r-- | rest-services/cbs-client/src/main/resources/logback.xml | 38 |
6 files changed, 483 insertions, 7 deletions
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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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 <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/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/http/configuration/EnvProperties.java index 9a80eeda..2438a2b8 100644 --- 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/http/configuration/EnvProperties.java @@ -18,18 +18,31 @@ * ============LICENSE_END========================================================= */ -package org.onap.dcaegen2.services.sdk.rest.services.cbs.client; +package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration; + +import org.immutables.value.Value; /** - * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/14/18 - * @version 1.0.0 + * Immutable object which helps with construction of cloudRequestObject for specified Client. For usage take a look in + * CloudConfigurationClient.class * - * Sample Class empty class / checking that java documentation will be created by maven + * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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 */ -public class DummyClient { +@Value.Immutable(prehash = true) +public interface EnvProperties { + + @Value.Parameter + String consulHost(); - public void print() { + @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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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<JsonObject> 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<JsonObject> 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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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<JsonObject> 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<JsonObject> 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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) { + return callConsulForConfigBindingServiceEndpoint(envProperties) + .flatMap(this::callConfigBindingServiceForPrhConfiguration); + } + + @Override + public Mono<JsonObject> 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<String> 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<JsonObject> callConfigBindingServiceForPrhConfiguration(String configBindingServiceUri) { + LOGGER.info("Retrieving PRH configuration"); + return cloudHttpClient.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(); + } +} diff --git a/rest-services/cbs-client/src/main/resources/logback.xml b/rest-services/cbs-client/src/main/resources/logback.xml new file mode 100644 index 00000000..09ecdb38 --- /dev/null +++ b/rest-services/cbs-client/src/main/resources/logback.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<configuration> + <include resource="org/springframework/boot/logging/logback/defaults.xml"/> + <property name="LOG_FILE" + value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> + <property name="FILE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN}"/> + <Property name="outputFilename" value="prh-app-server_output"/> + <Property name="log-path" value="/var/log/ONAP/prh/prh-app-server"/> + <Property name="archive" value="/var/log/ONAP/prh/prh-app-server/archive"/> + <property name="maxFileSize" value="50MB"/> + <property name="maxHistory" value="30"/> + <property name="totalSizeCap" value="10GB"/> + <property name="defaultPattern" value="%nopexception%logger + |%date{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC} + |%level + |%replace(%replace(%message){'\t','\\\\t'}){'\n','\\\\n'} + |%replace(%replace(%mdc){'\t','\\\\t'}){'\n','\\\\n'} + |%replace(%replace(%rootException){'\t','\\\\t'}){'\n','\\\\n'} + |%replace(%replace(%marker){'\t','\\\\t'}){'\n','\\\\n'} + |%thread + |%n"/> + + <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="ROLLING-FILE"> + <encoder> + <pattern>${defaultPattern}</pattern> + </encoder> + <File>${log-path}/${outputFilename}.log</File> + <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> + <FileNamePattern>${archive}/${outputFilename}.%d{yyyy-MM-dd}.%i.log.zip</FileNamePattern> + <MaxFileSize>${maxFileSize}</MaxFileSize> + <MaxHistory>${maxHistory}</MaxHistory> + <TotalSizeCap>${totalSizeCap}</TotalSizeCap> + </rollingPolicy> + </appender> + <root level="ERROR"> + <appender-ref ref="ROLLING-FILE"/> + </root> +</configuration> |