From 83df6e1df5ec20627c85af9ba2f49036dd58f328 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 4 Sep 2018 09:29:49 +0200 Subject: Refatoring due to prh workflow 1. Added specified HttpClient for DmaaPPublisher: *DmaaP Handle transfer-encoding: chunk header and reject the request if it will be set by the client. In conclusion no other reactive http client can be used for pushing something to dmaap. 2. Added sll support to A&AI rective webclient. *Behaviour of reactive A&AI HttpClient is different as in native spring have without it. 3. Added 10s fixed time in PRH for requesting DmaaP. 4. Added debug log in reactive/native http clients. 5. Fixed reactive workflow of prh. 6. Updated the version of: * spring-boot-dependencies:2.0.1.RELEASE->2.0.4.RELEASE * spring-boot-starter-reactor-netty:2.0.2.RELEASE->2.0.4.RELEASE * spring-webflux:5.0.5.RELEASE->5.0.8.RELEASE * reactor-bom:Bismuth-RELEASE->Bismuth-SR10 Change-Id: I815ffb5bdcf48d94f3b7c64040a73e98e404a5e8 Issue-ID: DCAEGEN2-743 Signed-off-by: pwielebs --- .../services/prh/service/AaiReactiveWebClient.java | 28 +++++++++-- .../producer/AaiProducerReactiveHttpClient.java | 58 +++++++++------------- 2 files changed, 46 insertions(+), 40 deletions(-) (limited to 'prh-aai-client/src/main/java') diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java index 6daf54a1..55dcb398 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AaiReactiveWebClient.java @@ -24,11 +24,18 @@ import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.RESPONSE import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.SERVICE_NAME; import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; + import java.util.Map; +import javax.net.ssl.SSLException; + import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; @@ -36,7 +43,7 @@ import reactor.core.publisher.Mono; public class AaiReactiveWebClient { - private Logger logger = LoggerFactory.getLogger(this.getClass()); + private static final Logger LOGGER = LoggerFactory.getLogger(AaiReactiveWebClient.class); private String aaiUserName; private String aaiUserPassword; @@ -60,8 +67,19 @@ public class AaiReactiveWebClient { * * @return WebClient */ - public WebClient build() { + public WebClient build() throws SSLException { + SslContext sslContext; + sslContext = SslContextBuilder + .forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .build(); + LOGGER.debug("Setting ssl context"); + return WebClient.builder() + .clientConnector(new ReactorClientHttpConnector(clientOptions -> { + clientOptions.sslContext(sslContext); + clientOptions.disablePool(); + })) .defaultHeaders(httpHeaders -> httpHeaders.setAll(aaiHeaders)) .filter(basicAuthentication(aaiUserName, aaiUserPassword)) .filter(logRequest()) @@ -72,9 +90,9 @@ public class AaiReactiveWebClient { private ExchangeFilterFunction logRequest() { return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { MDC.put(SERVICE_NAME, String.valueOf(clientRequest.url())); - logger.info("Request: {} {}", clientRequest.method(), clientRequest.url()); + LOGGER.info("Request: {} {}", clientRequest.method(), clientRequest.url()); clientRequest.headers() - .forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value))); + .forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value))); return Mono.just(clientRequest); }); } @@ -82,7 +100,7 @@ public class AaiReactiveWebClient { private ExchangeFilterFunction logResponse() { return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { MDC.put(RESPONSE_CODE, String.valueOf(clientResponse.statusCode())); - logger.info("Response Status {}", clientResponse.statusCode()); + LOGGER.info("Response Status {}", clientResponse.statusCode()); return Mono.just(clientResponse); }); } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java index be6c63e0..358a4524 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/producer/AaiProducerReactiveHttpClient.java @@ -20,35 +20,32 @@ package org.onap.dcaegen2.services.prh.service.producer; -import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID; -import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_INVOCATION_ID; -import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_ONAP_REQUEST_ID; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.UUID; import org.apache.http.client.utils.URIBuilder; import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; -import org.onap.dcaegen2.services.prh.exceptions.AaiRequestException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; -import org.springframework.http.HttpStatus; -import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.UUID; + +import static org.onap.dcaegen2.services.prh.model.CommonFunctions.createJsonBody; +import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.*; + public class AaiProducerReactiveHttpClient { + private WebClient webClient; private final String aaiHost; private final String aaiProtocol; private final Integer aaiHostPortNumber; private final String aaiBasePath; - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - private WebClient webClient; - + private final String aaiPnfPath; /** * Constructor of AaiProducerReactiveHttpClient. @@ -60,6 +57,7 @@ public class AaiProducerReactiveHttpClient { this.aaiProtocol = configuration.aaiProtocol(); this.aaiHostPortNumber = configuration.aaiPort(); this.aaiBasePath = configuration.aaiBasePath(); + this.aaiPnfPath = configuration.aaiPnfPath(); } /** @@ -68,10 +66,12 @@ public class AaiProducerReactiveHttpClient { * @param consumerDmaapModelMono - object which will be sent to AAI database * @return status code of operation */ - public Mono getAaiProducerResponse(Mono consumerDmaapModelMono) { - return consumerDmaapModelMono - .doOnNext(consumerDmaapModel -> logger.info("Sending PNF model to AAI {}", consumerDmaapModel)) - .flatMap(this::patchAaiRequest); + public Mono getAaiProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) { + try { + return patchAaiRequest(consumerDmaapModelMono); + } catch (URISyntaxException e) { + return Mono.error(e); + } } public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) { @@ -79,26 +79,14 @@ public class AaiProducerReactiveHttpClient { return this; } - private Mono patchAaiRequest(ConsumerDmaapModel dmaapModel) { - try { - return webClient.patch() + private Mono patchAaiRequest(ConsumerDmaapModel dmaapModel) throws URISyntaxException { + return + webClient.patch() .uri(getUri(dmaapModel.getSourceName())) .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)) .header(X_INVOCATION_ID, UUID.randomUUID().toString()) - .body(BodyInserters.fromObject(dmaapModel)) - .retrieve() - .onStatus( - HttpStatus::is4xxClientError, - clientResponse -> Mono - .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode())) - ) - .onStatus(HttpStatus::is5xxServerError, - clientResponse -> Mono - .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode()))) - .bodyToMono(Integer.class); - } catch (URISyntaxException e) { - return Mono.error(e); - } + .body(Mono.just(createJsonBody(dmaapModel)), String.class) + .exchange(); } URI getUri(String pnfName) throws URISyntaxException { @@ -106,7 +94,7 @@ public class AaiProducerReactiveHttpClient { .setScheme(aaiProtocol) .setHost(aaiHost) .setPort(aaiHostPortNumber) - .setPath(aaiBasePath + "/" + pnfName) + .setPath(aaiBasePath + aaiPnfPath + "/" + pnfName) .build(); } } -- cgit 1.2.3-korg