diff options
author | 2019-02-27 08:46:23 +0100 | |
---|---|---|
committer | 2019-02-27 10:52:15 +0100 | |
commit | bc386bcf22a1078a9e29db3faff63667a3b5c99d (patch) | |
tree | 0ea41a858b75cb14da74254c1d23de7bcfafec50 /rest-services/cbs-client/src/main | |
parent | 4b1062c01ea444f429ca7c51153c2cea5692ed80 (diff) |
Implement CBS Client
Change-Id: I6736dd6ea7598beb8542274a91fcb3967fac9c89
Issue-ID: DCAEGEN2-1233
Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
Diffstat (limited to 'rest-services/cbs-client/src/main')
3 files changed, 59 insertions, 50 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()); - }; - } - - } |