summaryrefslogtreecommitdiffstats
path: root/rest-services/cbs-client
diff options
context:
space:
mode:
Diffstat (limited to 'rest-services/cbs-client')
-rw-r--r--rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClientFactory.java2
-rw-r--r--rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java29
-rw-r--r--rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/adapters/CloudHttpClient.java78
-rw-r--r--rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java96
-rw-r--r--rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java59
-rw-r--r--rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java91
-rw-r--r--rest-services/cbs-client/src/test/resources/logback-test.xml50
-rw-r--r--rest-services/cbs-client/src/test/resources/sample_config.json3
8 files changed, 344 insertions, 64 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 c1b14343..7a463178 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
@@ -51,7 +51,7 @@ public class CbsClientFactory {
final CloudHttpClient httpClient = new CloudHttpClient();
final CbsLookup lookup = new CbsLookup(httpClient);
return lookup.lookup(env)
- .map(addr -> new CbsClientImpl(httpClient, env.appName()));
+ .map(addr -> CbsClientImpl.create(httpClient, addr, env.appName()));
});
}
}
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java
index 1df42c69..0d32320a 100644
--- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java
+++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImpl.java
@@ -20,23 +20,42 @@
package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl;
import com.google.gson.JsonObject;
+import java.net.InetSocketAddress;
+import java.net.MalformedURLException;
+import java.net.URL;
import org.jetbrains.annotations.NotNull;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.adapters.CloudHttpClient;
import reactor.core.publisher.Mono;
public class CbsClientImpl implements CbsClient {
+
private final CloudHttpClient httpClient;
- private final String serviceName;
+ private final String fetchUrl;
- public CbsClientImpl(
- CloudHttpClient httpClient, String serviceName) {
+ CbsClientImpl(CloudHttpClient httpClient, URL fetchUrl) {
this.httpClient = httpClient;
- this.serviceName = serviceName;
+ this.fetchUrl = fetchUrl.toString();
+ }
+
+ public static CbsClientImpl create(CloudHttpClient httpClient, InetSocketAddress cbsAddress, String serviceName) {
+ return new CbsClientImpl(httpClient, constructUrl(cbsAddress, serviceName));
+ }
+
+ private static URL constructUrl(InetSocketAddress cbsAddress, String serviceName) {
+ try {
+ return new URL(
+ "http",
+ cbsAddress.getHostString(),
+ cbsAddress.getPort(),
+ "/service_component/" + serviceName);
+ } catch (MalformedURLException e) {
+ throw new IllegalArgumentException("Invalid CBS URL", e);
+ }
}
@Override
public @NotNull Mono<JsonObject> get() {
- return Mono.empty();
+ return Mono.defer(() -> httpClient.callHttpGet(fetchUrl, JsonObject.class));
}
}
diff --git a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/adapters/CloudHttpClient.java b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/adapters/CloudHttpClient.java
index 264a392e..438ff667 100644
--- a/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/adapters/CloudHttpClient.java
+++ b/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/adapters/CloudHttpClient.java
@@ -22,7 +22,15 @@ package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.adapters;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
+import io.netty.handler.codec.http.HttpStatusClass;
+import io.vavr.collection.Stream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
import java.util.function.BiConsumer;
+import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
@@ -39,70 +47,52 @@ public class CloudHttpClient {
private static final Logger LOGGER = LoggerFactory.getLogger(CloudHttpClient.class);
- private final Gson gson;
+ private final Gson gson = new Gson();
private final HttpClient httpClient;
-
public CloudHttpClient() {
- this(HttpClient.create().doOnRequest(logRequest()).doOnResponse(logResponse()));
+ this(HttpClient.create()
+ .doOnRequest(CloudHttpClient::logRequest)
+ .doOnResponse(CloudHttpClient::logResponse));
}
CloudHttpClient(HttpClient httpClient) {
- this.gson = new Gson();
this.httpClient = httpClient;
}
-
- public <T> Mono<T> callHttpGet(String url, Class<T> genericClassDeclaration) {
+ public <T> Mono<T> callHttpGet(String url, Class<T> bodyClass) {
return httpClient
- .baseUrl(url)
- .doOnResponseError(doOnError())
- .get()
- .responseSingle(
- (httpClientResponse, content) -> getJsonFromRequest(content.toString(), genericClassDeclaration));
+ .get()
+ .uri(url)
+ .responseSingle((resp, content) -> HttpStatusClass.SUCCESS.contains(resp.status().code())
+ ? content.asString()
+ : Mono.error(createException(url, resp)))
+ .map(body -> parseJson(body, bodyClass));
}
- private BiConsumer<HttpClientResponse, Throwable> doOnError() {
- return (httpClientResponse, throwable) -> {
- Mono.error(getException(httpClientResponse));
- };
+ private Exception createException(String url, HttpClientResponse response) {
+ return new IOException(String.format("Request failed for URL '%s'. Response code: %s",
+ url,
+ response.status()));
}
-
- private RuntimeException getException(HttpClientResponse response) {
- return new RuntimeException(String.format("Request for cloud config failed: HTTP %d",
- response.status().code()));
+ private <T> T parseJson(String body, Class<T> bodyClass) {
+ return gson.fromJson(body, bodyClass);
}
- 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 static void logRequest(HttpClientRequest httpClientRequest, Connection connection) {
+ LOGGER.debug("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri());
+ if (LOGGER.isTraceEnabled()) {
+ final String headers = Stream.ofAll(httpClientRequest.requestHeaders())
+ .map(entry -> entry.getKey() + "=" + entry.getValue())
+ .collect(Collectors.joining("\n"));
+ LOGGER.trace(headers);
}
}
- private <T> T parseJson(String body, Class<T> genericClassDeclaration) {
- return gson.fromJson(body, genericClassDeclaration);
- }
-
-
- private static BiConsumer<HttpClientRequest, Connection> logRequest() {
- return (httpClientRequest, connection) -> {
- LOGGER.debug("Request: {} {}", httpClientRequest.method(), httpClientRequest.uri());
- httpClientRequest.requestHeaders().forEach(stringStringEntry -> {
- LOGGER.trace("{}={}", stringStringEntry.getKey(), stringStringEntry.getValue());
- });
-
- };
+ private static void logResponse(HttpClientResponse httpClientResponse, Connection connection) {
+ LOGGER.debug("Response status: {}", httpClientResponse.status());
}
- private static BiConsumer<? super HttpClientResponse, ? super Connection> logResponse() {
- return (httpClientresponse, connection) -> {
- LOGGER.debug("Response status: {}", httpClientresponse.status());
- };
- }
-
-
}
diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java
new file mode 100644
index 00000000..761cc5c1
--- /dev/null
+++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplIT.java
@@ -0,0 +1,96 @@
+/*
+ * ============LICENSE_START====================================
+ * DCAEGEN2-SERVICES-SDK
+ * =========================================================
+ * Copyright (C) 2019 Nokia. 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.impl;
+
+import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendResource;
+import static org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.DummyHttpServer.sendString;
+
+import com.google.gson.JsonObject;
+import java.time.Duration;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.EnvProperties;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.ImmutableEnvProperties;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+/**
+ * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
+ * @since February 2019
+ */
+class CbsClientImplIT {
+
+ private static final String CONSUL_RESP = "[\n"
+ + " {\n"
+ + " \"ServiceAddress\": \"HOST\",\n"
+ + " \"ServiceName\": \"the_cbs\",\n"
+ + " \"ServicePort\": PORT\n"
+ + " }\n"
+ + "]\n";
+ private static final String RES_CONFIG = "/sample_config.json";
+ private static DummyHttpServer server;
+
+ @BeforeAll
+ static void setUp() {
+ server = DummyHttpServer.start(routes ->
+ routes.get("/v1/catalog/service/the_cbs", (req, resp) -> sendString(resp, lazyConsulResponse()))
+ .get("/service_component/dcae-component", (req, resp) -> sendResource(resp, RES_CONFIG)));
+ }
+
+ @AfterAll
+ static void tearDown() {
+ server.close();
+ }
+
+ @Test
+ void testCbsClient() {
+ // given
+ final EnvProperties env = ImmutableEnvProperties.builder()
+ .appName("dcae-component")
+ .cbsName("the_cbs")
+ .consulHost(server.host())
+ .consulPort(server.port())
+ .build();
+ final Mono<CbsClient> sut = CbsClientFactory.createCbsClient(env);
+
+ // when
+ final Mono<JsonObject> result = sut.flatMap(CbsClient::get);
+
+ // then
+ StepVerifier.create(result.map(obj -> obj.get("keystore.path").getAsString()))
+ .expectNext("/var/run/security/keystore.p12")
+ .expectComplete()
+ .verify(Duration.ofSeconds(5));
+ }
+
+ private static Mono<String> lazyConsulResponse() {
+ return Mono.just(CONSUL_RESP)
+ .map(CbsClientImplIT::processConsulResponseTemplate);
+ }
+
+ private static String processConsulResponseTemplate(String resp) {
+ return resp.replaceAll("HOST", server.host())
+ .replaceAll("PORT", Integer.toString(server.port()));
+ }
+}
diff --git a/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java
new file mode 100644
index 00000000..65284c5f
--- /dev/null
+++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/CbsClientImplTest.java
@@ -0,0 +1,59 @@
+/*
+ * ============LICENSE_START====================================
+ * DCAEGEN2-SERVICES-SDK
+ * =========================================================
+ * Copyright (C) 2019 Nokia. 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.impl;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import com.google.gson.JsonObject;
+import java.net.InetSocketAddress;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.impl.adapters.CloudHttpClient;
+import reactor.core.publisher.Mono;
+
+/**
+ * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
+ * @since February 2019
+ */
+class CbsClientImplTest {
+ private final CloudHttpClient httpClient = mock(CloudHttpClient.class);
+
+ @Test
+ void shouldFetchUsingProperUrl() {
+ // given
+ InetSocketAddress cbsAddress = InetSocketAddress.createUnresolved("cbshost", 6969);
+ String serviceName = "dcaegen2-ves-collector";
+ final CbsClientImpl cut = CbsClientImpl.create(httpClient, cbsAddress, serviceName);
+ final JsonObject httpResponse = new JsonObject();
+ given(httpClient.callHttpGet(anyString(), any(Class.class))).willReturn(Mono.just(httpResponse));
+
+ // when
+ final JsonObject result = cut.get().block();
+
+ // then
+ verify(httpClient).callHttpGet("http://cbshost:6969/service_component/dcaegen2-ves-collector", JsonObject.class);
+ assertThat(result).isSameAs(httpResponse);
+ }
+} \ 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/impl/DummyHttpServer.java b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java
new file mode 100644
index 00000000..d0485f57
--- /dev/null
+++ b/rest-services/cbs-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/impl/DummyHttpServer.java
@@ -0,0 +1,91 @@
+/*
+ * ============LICENSE_START====================================
+ * DCAEGEN2-SERVICES-SDK
+ * =========================================================
+ * Copyright (C) 2019 Nokia. 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.impl;
+
+import io.vavr.CheckedFunction0;
+import io.vavr.Function0;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.function.Consumer;
+import org.reactivestreams.Publisher;
+import reactor.core.publisher.Mono;
+import reactor.netty.DisposableServer;
+import reactor.netty.http.server.HttpServer;
+import reactor.netty.http.server.HttpServerResponse;
+import reactor.netty.http.server.HttpServerRoutes;
+
+/**
+ * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
+ * @since February 2019
+ */
+public class DummyHttpServer {
+
+ private final DisposableServer server;
+
+ private DummyHttpServer(DisposableServer server) {
+ this.server = server;
+ }
+
+ public static DummyHttpServer start(Consumer<HttpServerRoutes> routes) {
+ return new DummyHttpServer(HttpServer.create()
+ .host("127.0.0.1")
+ .route(routes)
+ .bind()
+ .block());
+ }
+
+ public static Publisher<Void> sendResource(HttpServerResponse httpServerResponse, String resourcePath) {
+ return sendString(httpServerResponse, Mono.fromCallable(() -> readResource(resourcePath)));
+ }
+
+ public static Publisher<Void> sendString(HttpServerResponse httpServerResponse, Publisher<String> content) {
+ return httpServerResponse.sendString(content);
+ }
+
+ public void close() {
+ server.disposeNow();
+ }
+
+ public String host() {
+ return server.host();
+ }
+
+ public int port() {
+ return server.port();
+ }
+
+ private static String readResource(String resourcePath) {
+ try {
+ return CheckedFunction0.constant(resourcePath)
+ .andThen(DummyHttpServer.class::getResource)
+ .andThen(URL::toURI)
+ .andThen(Paths::get)
+ .andThen(Files::readAllBytes)
+ .andThen(String::new)
+ .apply();
+ } catch (Throwable throwable) {
+ throw new RuntimeException(throwable);
+ }
+ }
+}
diff --git a/rest-services/cbs-client/src/test/resources/logback-test.xml b/rest-services/cbs-client/src/test/resources/logback-test.xml
index c1f00665..8e468cfb 100644
--- a/rest-services/cbs-client/src/test/resources/logback-test.xml
+++ b/rest-services/cbs-client/src/test/resources/logback-test.xml
@@ -1,21 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- ~ ===============================LICENSE_START======================================
- ~ Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ ~ ============LICENSE_START=======================================================
+ ~ dcaegen2-collectors-veshv
~ ================================================================================
- ~ 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
+ ~ Copyright (C) 2018 NOKIA
+ ~ ================================================================================
+ ~ 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
+ ~ 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===========================================
- -->
+ ~ 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"/>
+ <property name="p_tim" value="%date{&quot;yyyy-MM-dd'T'HH:mm:ss.SSSXXX&quot;, UTC}"/>
+ <property name="p_lvl" value="%highlight(%-5level)"/>
+ <property name="p_log" value="%50.50logger"/>
+ <property name="SIMPLE_LOG_PATTERN" value="
+%nopexception
+| ${p_tim}\t
+| ${p_log}\t
+| ${p_lvl}\t
+| %msg%n"/>
+
+ <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>${SIMPLE_LOG_PATTERN}</pattern>
+ </encoder>
+ </appender>
+
+ <logger name="org.onap.dcaegen2.services.sdk" level="TRACE"/>
+
+ <root level="INFO">
+ <appender-ref ref="CONSOLE"/>
+ </root>
</configuration>
diff --git a/rest-services/cbs-client/src/test/resources/sample_config.json b/rest-services/cbs-client/src/test/resources/sample_config.json
new file mode 100644
index 00000000..a95b723f
--- /dev/null
+++ b/rest-services/cbs-client/src/test/resources/sample_config.json
@@ -0,0 +1,3 @@
+{
+ "keystore.path": "/var/run/security/keystore.p12"
+}