diff options
13 files changed, 792 insertions, 23 deletions
@@ -29,6 +29,9 @@ <junit-jupiter.version>5.3.1</junit-jupiter.version> <junit-vintage.version>5.3.1</junit-vintage.version> <junit-platform.version>1.3.1</junit-platform.version> + <immutables.version>2.7.3</immutables.version> + <spring.version>5.1.2.RELEASE</spring.version> + <slf4j.version>1.7.25</slf4j.version> </properties> <modules> @@ -75,8 +78,8 @@ <version>3.0.1</version> </plugin> <plugin> - <artifactId>maven-jar-plugin</artifactId> - <version>3.1.0</version> + <artifactId>maven-shade-plugin</artifactId> + <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> @@ -110,17 +113,27 @@ </executions> </plugin> <plugin> - <artifactId>maven-jar-plugin</artifactId> + <artifactId>maven-shade-plugin</artifactId> <configuration> - <archive> - <manifest> - <addDefaultImplementationEntries>true</addDefaultImplementationEntries> - </manifest> - <manifestEntries> - <Implementation-Build-Version>${project.version}</Implementation-Build-Version> - </manifestEntries> - </archive> + <filters> + <filter> + <artifact>*:*</artifact> + <excludes> + <exclude>META-INF/*.SF</exclude> + <exclude>META-INF/*.DSA</exclude> + <exclude>META-INF/*.RSA</exclude> + </excludes> + </filter> + </filters> </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> </plugin> </plugins> </build> @@ -143,6 +156,43 @@ <dependencyManagement> <dependencies> <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-webflux</artifactId> + <version>${spring.version}</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>${spring.version}</version> + </dependency> + <dependency> + <groupId>org.immutables</groupId> + <artifactId>value</artifactId> + <version>${immutables.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.immutables</groupId> + <artifactId>gson</artifactId> + <version>${immutables.version}</version> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <scope>provided</scope> + <version>1.2.3</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jul-to-slf4j</artifactId> + <version>${slf4j.version}</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>log4j-over-slf4j</artifactId> + <version>${slf4j.version}</version> + </dependency> + <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.23.0</version> @@ -172,6 +222,13 @@ <version>${junit-vintage.version}</version> <scope>test</scope> </dependency> + <dependency> + <groupId>io.projectreactor</groupId> + <artifactId>reactor-bom</artifactId> + <version>Californium-SR2</version> + <type>pom</type> + <scope>import</scope> + </dependency> </dependencies> </dependencyManagement> </project> diff --git a/rest-services/cbs-client/pom.xml b/rest-services/cbs-client/pom.xml index 5a03285e..ecde84ce 100644 --- a/rest-services/cbs-client/pom.xml +++ b/rest-services/cbs-client/pom.xml @@ -21,6 +21,36 @@ <dependencies> <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-webflux</artifactId> + </dependency> + <dependency> + <groupId>org.immutables</groupId> + <artifactId>value</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.immutables</groupId> + <artifactId>gson</artifactId> + </dependency> + <dependency> + <groupId>io.projectreactor</groupId> + <artifactId>reactor-core</artifactId> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jul-to-slf4j</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>log4j-over-slf4j</artifactId> + </dependency> + <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> @@ -45,6 +75,15 @@ <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>io.projectreactor</groupId> + <artifactId>reactor-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + </dependency> </dependencies> </project> 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> diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClientTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClientTest.java new file mode 100644 index 00000000..e3e7a1d3 --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/http/configuration/CloudHttpClientTest.java @@ -0,0 +1,80 @@ +/* + * ============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 static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import org.junit.jupiter.api.Test; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +/** + * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/16/18 + */ +class CloudHttpClientTest { + + private static final String SOMEURL = "http://someurl"; + private static final String DATA = "{}"; + private Gson gson = new Gson(); + private WebClient webClient = mock(WebClient.class); + private WebClient.RequestHeadersUriSpec requestBodyUriSpec = mock(WebClient.RequestBodyUriSpec.class); + private WebClient.ResponseSpec responseSpec = mock(WebClient.ResponseSpec.class); + + @Test + void shouldReturnJsonObjectOnGetCall() { + //given + mockWebClientDependantObject(); + CloudHttpClient httpGetClient = new CloudHttpClient(webClient); + when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just(DATA)); + + //when/then + StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() + .expectNext(gson.fromJson(DATA, JsonObject.class)).verifyComplete(); + } + + @Test + void shouldReturnMonoErrorOnGetCall() { + //given + mockWebClientDependantObject(); + CloudHttpClient httpGetClient = new CloudHttpClient(webClient); + when(responseSpec.bodyToMono(String.class)).thenReturn(Mono.just("some wrong data")); + + //when/then + StepVerifier.create(httpGetClient.callHttpGet(SOMEURL, JsonObject.class)).expectSubscription() + .expectError(JsonSyntaxException.class).verify(); + } + + + private void mockWebClientDependantObject() { + doReturn(requestBodyUriSpec).when(webClient).get(); + when(requestBodyUriSpec.uri(SOMEURL)).thenReturn(requestBodyUriSpec); + doReturn(responseSpec).when(requestBodyUriSpec).retrieve(); + doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); + doReturn(responseSpec).when(responseSpec).onStatus(any(), any()); + } +}
\ No newline at end of file diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClientTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfiguratonProviderWrapperTest.java index 97843426..746ad43f 100644 --- a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/DummyClientTest.java +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/CloudConfiguratonProviderWrapperTest.java @@ -16,15 +16,15 @@ * 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; +package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers; -import static org.junit.jupiter.api.Assertions.*; /** - * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/14/18 + * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/16/18 */ -class DummyClientTest { +class CloudConfiguratonProviderWrapperTest { }
\ No newline at end of file diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java new file mode 100644 index 00000000..afc5a255 --- /dev/null +++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/providers/ReactiveCloudConfigurationProviderTest.java @@ -0,0 +1,96 @@ +/* + * ============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 static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.junit.jupiter.api.Test; +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.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.ImmutableEnvProperties; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +/** + * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18 + */ +class ReactiveCloudConfigurationProviderTest { + + private static final Gson gson = new Gson(); + private static final String configBindingService = "[{\"ID\":\"9c8dd674-34ce-7049-d318-e98d93a64303\",\"Node\"" + + ":\"dcae-bootstrap\",\"Address\":\"10.42.52.82\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" + + "{\"lan\":\"10.42.52.82\",\"wan\":\"10.42.52.82\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," + + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," + + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," + + "\"CreateIndex\":14352,\"ModifyIndex\":14352},{\"ID\":\"35c6f540-a29c-1a92-23b0-1305bd8c81f5\",\"Node\":" + + "\"dev-consul-server-1\",\"Address\":\"10.42.165.51\",\"Datacenter\":\"dc1\",\"TaggedAddresses\":" + + "{\"lan\":\"10.42.165.51\",\"wan\":\"10.42.165.51\"},\"NodeMeta\":{\"consul-network-segment\":\"\"}," + + "\"ServiceID\":\"dcae-cbs1\",\"ServiceName\":\"config-binding-service\",\"ServiceTags\":[]," + + "\"ServiceAddress\":\"config-binding-service\",\"ServicePort\":10000,\"ServiceEnableTagOverride\":false," + + "\"CreateIndex\":803,\"ModifyIndex\":803}]"; + private static final JsonArray configBindingServiceJson = gson.fromJson(configBindingService, JsonArray.class); + private static final JsonArray emptyConfigBindingServiceJson = gson.fromJson("[]", JsonArray.class); + private static final String prhMockConfiguration = "{\"test\":1}"; + private static final JsonObject prhMockConfigurationJson = gson.fromJson(prhMockConfiguration, JsonObject.class); + + private EnvProperties envProperties = ImmutableEnvProperties.builder() + .appName("dcae-prh") + .cbsName("config-binding-service") + .consulHost("consul") + .consulPort(8500) + .build(); + + @Test + void shouldReturnPrhConfiguration() { + // given + CloudHttpClient webClient = mock(CloudHttpClient.class); + when( + webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) + .thenReturn(Mono.just(configBindingServiceJson)); + when(webClient.callHttpGet("http://config-binding-service:10000/service_component/dcae-prh", JsonObject.class)) + .thenReturn(Mono.just(prhMockConfigurationJson)); + + ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); + + //when/then + StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription() + .expectNext(prhMockConfigurationJson).verifyComplete(); + } + + @Test + void shouldReturnMonoErrorWhenConsuleDoesntHaveConfigBindingServiceEntry() { + // given + CloudHttpClient webClient = mock(CloudHttpClient.class); + when( + webClient.callHttpGet("http://consul:8500/v1/catalog/service/config-binding-service", JsonArray.class)) + .thenReturn(Mono.just(emptyConfigBindingServiceJson)); + + ReactiveCloudConfigurationProvider provider = new ReactiveCloudConfigurationProvider(webClient); + + //when/then + StepVerifier.create(provider.callForServiceConfigurationReactive(envProperties)).expectSubscription() + .expectError(IllegalStateException.class).verify(); + } +}
\ No newline at end of file diff --git a/rest-services/cbs-client/src/test/resources/logback-test.xml b/rest-services/cbs-client/src/test/resources/logback-test.xml new file mode 100644 index 00000000..c1f00665 --- /dev/null +++ b/rest-services/cbs-client/src/test/resources/logback-test.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ ===============================LICENSE_START====================================== + ~ Copyright © 2017 AT&T 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=========================================== + --> +<configuration> + <root level="OFF"/> +</configuration> diff --git a/rest-services/dmaap-client/pom.xml b/rest-services/dmaap-client/pom.xml index 3f1d2581..6d7ec332 100644 --- a/rest-services/dmaap-client/pom.xml +++ b/rest-services/dmaap-client/pom.xml @@ -10,7 +10,7 @@ <version>1.0.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> - + <groupId>org.onap.dcaegen2.services.sdk.rest.services</groupId> <artifactId>dmaap-client</artifactId> <version>1.0.0-SNAPSHOT</version> |